0

デフォルトの VAO と 1 つの VBO を作成し、それらをバインドします。モデル データを構造体 vertex_data_t の配列にロードしています

    glBufferData(GL_ARRAY_BUFFER, nvertices * sizeof(vertex_data_t), vertices, GL_STATIC_DRAW);

次に、描画関数で次のことを行います。

    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_data_t), (const GLvoid *)offsetof(vertex_data_t, position));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, sizeof(vertex_data_t), (const GLvoid *)offsetof(vertex_data_t, position));
    glBindVertexArray(0);
    glDrawArrays(GL_TRIANGLES, 0, nvertices);

私は元気になってきました、日陰のスザンヌ:

http://i.stack.imgur.com/uRjpv.png

しかし、これは間違っています!通常の属性の glVertexAttribPointer の最後の引数は 12 aka (const GLvoid *)offsetof(vertex_data_t, normal)である必要がありますが、そうするとスザンヌが壊れます:

http://i.stack.imgur.com/zBjTS.png

それはどのように可能ですか?シェーダーは法線へのオフセットをどのように認識しますか?

頂点シェーダー:

     attribute vec3 vertex;
     attribute vec3 normal;

     uniform vec4 ambient_color;
     uniform vec4 diffuse_color;
     uniform vec3 light_position;
     uniform mat3 normal_matrix;
     uniform mat4 model_view_matrix;
     uniform mat4 model_view_projection_matrix;

     varying vec4 varying_color;

     void main(void) {
              vec4 vertex4 = vec4(vertex.x, vertex.y, vertex.z, 1.0);
              vec3 eye_normal = normal_matrix * normal;
              vec4 position4 = model_view_matrix * vertex4;
              vec3 position3 = position4.xyz / position4.w;
              vec3 light_direction = normalize(light_position - position3);
              float diffuse = max(0.0, dot(eye_normal, light_direction));
              varying_color.rgb = diffuse * diffuse_color.rgb;
              varying_color.a = diffuse_color.a;
              varying_color += ambient_color;
              gl_Position = model_view_projection_matrix * vertex4;
    }
4

1 に答える 1

1

次のようなものを見逃していると思います:

glBindAttribLocation(progId, 0, "vertex");
glBindAttribLocation(progId, 1, "normal");
于 2012-02-10T13:10:01.420 に答える