problem with glDrawElements and vbo's

Started by
1 comment, last by bradbobak 11 years, 2 months ago

Hi. I'm having a problem getting glDrawElements working right with a vbo and index buffer.

Heres the render code: (error checking omitted for brevity).


// ... load shaders and program
 
GLfloat verts[] = ( 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0 };
GLushort idx[] = ( 0, 1, 2 };
 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
GLuint v1, v2;
glGenBuffers(1, &v1);
glBindBuffer(GL_ARRAY_BUFFER, v1);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, verts, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, 0);
glEnableVertexAttribArray(1);
 
glGenBuffers(1, &v2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, v2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 3, idx, GL_STATIC_DRAW);
 
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
 
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 
glDisableVertexAttribArray(1);
glDeleteBuffers(1, &v1);
glDeleteBuffers(1, &v2);

Vertex shader:


#version 330
 
layout (location = 1) in vec3 position;
 
uniform mat4 from_identity = mat4(1.0); // identity for now, this will change
 
void main()
{
  gl_Position = from_identity * vec4(position, 0);
}

Frag shader:


#version 330
 
void main()
{
  gl_FragColor = vec4(0.5, 1, 0, 0.5);
}

This renders nothing.

If I change the vertex shader to "gl_Position = gl_Vertex;" and draw the triangle with the old stuff (glBegin(GL_TRIANGLES); ...; glEnd();) it draws fine.

Any suggestions?

Advertisement

For positions, w should be 1 instead of 0.

Derp

I've found the problem. I was using glX rather than glut, and wasn't creating an opengl 3 context (was just using the old-style context) with glX.

This topic is closed to new replies.

Advertisement