PDA

View Full Version : glDrawElements inside if statement



fiodis
06-13-2012, 11:12 AM
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.

mark ds
06-13-2012, 11:18 AM
Post the code you're using to test for key presses. It sounds like that's the likely culprit.

EDIT - further to the above, it seems like you're setting a flag on key down, but not clearing it on key up.

fiodis
06-13-2012, 11:28 AM
Here it is:

private void openGLControl_KeyDown(object sender, KeyEventArgs e)
{
OpenGL gl = openGLControl.OpenGL;

if (e.KeyCode == Keys.V)
{
if (renderPoints == false)
{
renderPoints = true;
openGLControl.Invalidate();
}
else if (renderPoints == true)
{
renderPoints = false;
openGLControl.Invalidate();
}
}
if (e.KeyCode == Keys.G)
{
if (renderGrid == false)
{
renderGrid = true;
openGLControl.Invalidate();
}
else if (renderGrid == true)
{
renderGrid = false;
openGLControl.Invalidate();
}
}
if (e.KeyCode == Keys.T)
{
if (renderTris == false)
{
renderTris = true;
openGLControl.Invalidate();
}
if (renderTris == true)
{
renderTris = false;
openGLControl.Invalidate();
}
}
}
As mentioned, it works fine with points and lines (renderGrid), but not with tris. openGLControl.Invalidate() is something from SharpGL that's kind of like reDraw; it tells the OpenGL window that something's changed and to redraw everything to take the changes into account.

Dan Bartlett
06-13-2012, 12:07 PM
You've missed an "else" statement, so it is always executing the second part of code when T is pressed.

fiodis
06-13-2012, 01:42 PM
:doh:
I can't believe I didn't notice that. Thanks, Dan. I guess this wasn't a case of OpenGL being quirky as much as me being an idiot.