Reputation: 95
I have interleaved [position, colour, normal] data in an array that I then pass to the vertex shader. The vertex shader only seems to pick up the first two values [position and colour]. What is preventing it from getting the normal data as well?
data store
struct c_vertex{
GLfloat x;
GLfloat z;
GLfloat y;
GLfloat r;
GLfloat g;
GLfloat b;
GLfloat nx;
GLfloat nz;
GLfloat ny;
};
array
c_vertex vertex_store[max_bins][max_bins];
having assigned values to the elements of the array [which works correctly as I'll demonstrate later] the draw routine:
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glGenBuffers(1,&VertexVBoid);
glBindBuffer (GL_ARRAY_BUFFER, VertexVBoid);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertex_store),&(vertex_store[0][0].x),GL_STATIC_DRAW);
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_order), vertex_order, GL_STATIC_DRAW);
glBindBuffer (GL_ARRAY_BUFFER, VertexVBoid);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(0));
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);
glEnableVertexAttribArray (2);
glBindVertexArray (vao);
glDrawElements(GL_TRIANGLES, (max_bins-1)*(max_bins-1)*2*3, GL_UNSIGNED_INT, (void *) 0);
glDisableVertexAttribArray (0);
glDisableVertexAttribArray (1);
glDisableVertexAttribArray (2);
};
Vertex shader
attribute vec3 kposition;
attribute vec3 kcolor;
attribute vec3 knormal;
uniform mat4 MVP;
varying vec4 kolor;
void main()
{
kolor = vec4(knormal,1.0);
gl_Position = MVP*vec4(kposition,1.0);
}
fragment shader:
varying vec4 kolor;
void main(){
gl_FragColor=kolor;
}
the normal values are the same as the colour values
if I swap the offsets in
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
to
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
then I get the normal data being used as the colour data in the shaders
if i have the offsets as
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
if I change the shader from
attribute vec3 kposition;
attribute vec3 kcolor;
attribute vec3 knormal;
to
attribute vec3 kposition;
attribute vec3 knormal;
attribute vec3 kcolor;
knormal and kcolor have the same values - the colour attribute.
What is the error in my code?
Upvotes: 1
Views: 521
Reputation: 6821
There are no guarantees about the mapping between the attributes in the shader and attribute index. Use glGetAttribLocation
to query the shader for each attribute.
GLint positionIndex = glGetAttribLocation(myProgramID, "kposition");
glVertexAttribPointer (positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray (positionIndex);
GLint colorIndex = glGetAttribLocation(myProgramID, "kcolor");
glVertexAttribPointer (colorIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glEnableVertexAttribArray (colorIndex);
GLint normalIndex = glGetAttribLocation(myProgramID, "knormal");
glVertexAttribPointer (normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
glEnableVertexAttribArray (normalIndex);
Upvotes: 3