【问题标题】:Using a matrix as vertex attribute in OpenGL3 Core Profile在 OpenGL3 Core Profile 中使用矩阵作为顶点属性
【发布时间】:2013-06-27 23:44:55
【问题描述】:

我在 OSX 上使用 OpenGL3.2 Core Profile。 我想做实例绘图(glDrawArraysInstanced),我为每个实例传递一个矩阵。

我的顶点着色器构建得很好:

#version 150
in mediump vec4 position;
in mediump mat4 trf;
in lowp vec4 rgb;
out lowp vec4 colour;
uniform highp mat4 modelcamviewprojmat;
void main()
{
    mediump vec4 tpos = trf * position;
    gl_Position = modelcamviewprojmat * tpos;
    colour = rgb;
}

“trf”的绑定很好:

glBindAttribLocation(program, ATTRIB_TRF, "trf" );

但是我怎样才能传递我的数据呢? glVertexAttribPointer 不能传递大于 4 个浮点数的值。 所以这个调用失败了:

glVertexAttribPointer( ATTRIB_TRF,   16, GL_FLOAT, 0, 16 * sizeof(float), ... );

我怀疑我需要用 4 次调用 glVertexAttribPointer 来替换它,每次调用 4 个浮点数。 但是我可以为“索引”(第一个参数)使用什么值?我是否需要改用 4 个向量属性,并在 GLSL 顶点着色器中组合这四个向量?如果是这样,什么样的 GLSL 代码可以做到这一点?或者我可以使用 BindAttribLocation 的返回值并对所有行使用 val+0、val+1、val+2 和 val+3?

【问题讨论】:

    标签: glsl vbo opengl-3


    【解决方案1】:

    根据this page 和我当前在游戏中实现的硬件实例化,正确的做法是mat4 属性占用4 个属性位置。你绑定的那个和下面的三个。

    int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
    int pos1 = pos + 0; 
    int pos2 = pos + 1; 
    int pos3 = pos + 2; 
    int pos4 = pos + 3; 
    glEnableVertexAttribArray(pos1);
    glEnableVertexAttribArray(pos2);
    glEnableVertexAttribArray(pos3);
    glEnableVertexAttribArray(pos4);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices);
    glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
    glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
    glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
    glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
    glVertexAttribDivisor(pos1, 1);
    glVertexAttribDivisor(pos2, 1);
    glVertexAttribDivisor(pos3, 1);
    glVertexAttribDivisor(pos4, 1);
    

    【讨论】:

    • 我一直在寻找解决问题的方法,只是缺少对 glVertexAttribDivisor 的调用。非常感谢!
    • 不工作了,我在 glEnableVertexAttribArray(pos3); 上得到分段错误;
    • 虽然在mat4中仍然可以工作,但M改为vec4 M0;在 vec4 M1 中;在 vec4 M2 中;在 vec4 M3 中;和 M = mat4(M0,M1,M2,M3);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多