I’ve used glDrawElements (in OpenGL 3.1) before without problems. Now, however, it doesn’t want to draw anything, and I can’t figure out why. My render code looks like this:
void renderFunc(){
frameCount++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
baseShader.bind();
testProp.render();
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
And the Prop class’s render function is:
void Prop::render(){
glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->iboId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vert), (GLvoid*)0);
glEnableVertexAttribArray(0);
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_BYTE, (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
}
The Prop’s initialization function is:
void Prop::init(){
Vert baseVert1;
Vert baseVert2;
Vert baseVert3;
baseVert1.pos = glm::vec3(1.0f, 0.0f, -4.0f);
baseVert2.pos = glm::vec3(0.0f, 1.0f, -4.0f);
baseVert3.pos = glm::vec3(-1.0f, 0.0f, -4.0f);
Vert vertices[3] = {baseVert1, baseVert2, baseVert3};
GLubyte indices[3] = {0, 1, 2};
glGenBuffers(1, &this->vboId);
glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vert)*3, vertices, GL_STATIC_DRAW);
glGenBuffers(1, &this->iboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*3, indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
And my vertex and fragment shaders:
//Vertex shader:
#version 130
in vec3 in_Position;
//in vec4 in_Color;
out vec4 ex_Color;
out float ex_Time;
uniform float time;
uniform mat4 PMatrix;
uniform mat4 VMatrix;
uniform mat4 MMatrix;
void main(void)
{
ex_Time = time;
//ex_Color = in_Color;
ex_Color = vec4(0.0f, 1.0f, 0.0f, 1.0f);
gl_Position = (PMatrix * VMatrix * MMatrix) * vec4(in_Position, 1.0f);
}
// Fragment shader:
#version 130
in vec4 ex_Color;
in float ex_Time;
out vec4 out_Color;
void main(void)
{
ex_Time = 0.0f;
out_Color = ex_Color;
}
I also create and bind a vertex array object early after the context’s initialization.
Now, the weird thing is that with glDrawElements, I get a blank screen, while with glDrawArrays I get a white (not a green) triangle which doesn’t seem to respond to the shaders. I’ve tried a whole bunch of stuff, from using glEnableClientState calls to moving the VAO binding inside the prop’s render function. I don’t really know what I’m doing; I can’t find anything wrong. Am I missing something blatantly obvious?