I’m using glDrawElements to draw a flat quad made of lots of triangles. This works fine, but I want to be able to turn the quad on or off, so I put it inside an if statement that draws it if renderTris == true, then I write an event handler for the KeyDown event so I can set renderTris on or off by pressing the T key.
That’s when it gets wierd. Outside the if statement, it works fine. If renderTris is initially false, though, I can’t get the quad to show up no matter how many times I press the T key. If renderTris is initially true, I can press T once to turn the quad off, but I can’t turn it back on again. I’m certain it’s not a problem with the if statement or the event handler, since the same exact code works when I draw the quad with GL_POINTS or GL_LINES rather than GL_TRIANGLES. And it must be registering the key press, since if renderTris is initially true, pressing T to turn the quad off works; I just can’t turn it on again.
My conclusion is that it’s some quirk of OpenGL or glDrawElements or GL_TRIANGLES that I haven’t come across before. Does anyone know what quirk might be causing this? I haven’t found much to explain it with a Google search, though admittedly that might be because I have no idea what to search for.
Here’s the if statement in question:
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBufferObjectIds[0]);
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));
if (renderTris == true)
{
gl.Color(0.2f, 0.8f, 0.2f);
gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBufferTriIds[0]);
gl.DrawElements(OpenGL.GL_TRIANGLES, 486, OpenGL.GL_UNSIGNED_INT, new IntPtr(0));
gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, 0);
}
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
It’s C# so I apologize if it looks a little wierd.