glitchyme
glitchyme

Reputation: 287

OpenGL can't send attribute data to shader

I'm able to render a VBO with vertices but when I try adding in colours (using shader attributes) nothing shows up. What am I doing wrong here? I can find a lot of online examples showing this working for interleaved VBO's, but I'd rather use this method (serialized array) instead. Any ideas?

Buffer Generation & Allocation

glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );

glBufferData( GL_ARRAY_BUFFER, ( vertices.size() + colors.size() ) * sizeof(float), NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertices[0]), vertices.data() );
glBufferSubData( GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), colors.size() * sizeof(colors[0]), colors.data() );

/* HERE if I replace the lower block with this top portion, things show up fine
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, (void*)0);
*/
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, (void*)(vertices.size() * sizeof(GLfloat)) );

glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, faces.size() * sizeof(GLushort), faces.data(), GL_STATIC_DRAW );

Drawing here

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glUseProgram( gl );
glBindVertexArray( vao );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
glDrawElements( GL_TRIANGLE_FAN, faces.size(), GL_UNSIGNED_SHORT, (void*)0 );

glutSwapBuffers();
glutPostRedisplay();

vert shader

#version 130
layout (location = 0 ) in vec3 in_Position;
layout (location = 1 ) in vec3 in_Color;
out vec3 out_Color;
void main()
{
    out_Color = in_Color;
    gl_Position = vec4(in_Position,1.0);
}

frag shader

#version 130
in vec3 out_Color;
void main() {
    gl_FragColor = vec4(out_Color, 1.0);
}

Upvotes: 0

Views: 1166

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

You are using the version tag for GLSL 1.30 (OpenGL 3.0), which doesn't support explicit attrib locations, and neither do you explicitly enable the corresponding extension. So maybe your shader doesn't even compile (or it does but the location syntax is just ignored). This would explain it working with good old glVertexPointer, since if the shader doesn't compile, it will just use the fixed-function pipeline.

So first check if there are any results from glGetError and if your shaders compile and link successfully (and if they don't, what their info logs say). But since you have OpenGL 3.0, it's likely you have OpenGL 3.3 anyway, so just change the version tag to #version 330, which supports explicit attrib locations.

Upvotes: 1

Related Questions