我創建默認的VAO和一個VBO,並綁定它們。 我加載模型數據結構的陣列vertex_data_tglVertexAttribPointer的怪異行爲
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又名(常量GLvoid *)offsetof(vertex_data_t,正常),但是當我這樣做我的蘇珊被打破:
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;
}
vertex_data_t的定義是:https://raw.github.com/jacekmigacz/cobj/master /wavefront.h](https://raw.github.com/jacekmigacz/cobj/master/wavefront.h) – jacekmigacz 2012-02-10 12:05:09
你還介意着色器嗎?你應該在某個時候使用'glBindAttribLocation'和'glGetAttribLocation',你可以發佈嗎? – tibur 2012-02-10 12:55:36
附加的頂點着色器代碼。所有片段着色器正在做的是gl_FragColor = varying_color; – jacekmigacz 2012-02-10 13:01:00