Reputation: 600
I have the following code
int i = 0;
void display(void)
{
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(20.0f, 20.0f, -40.f);
glRotatef(180.f, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(pts[tri[i]], (pts[ptsLength/2 + tri[i]] - maximum) * -1, 0);
glVertex3f(pts[tri[i+1]], (pts[ptsLength/2 + tri[i+1]] - maximum) * -1, 0);
glVertex3f(pts[tri[i+2]], (pts[ptsLength/2 + tri[i+2]] - maximum) * -1, 0);
glEnd();
i+=3;
glPopMatrix();
glutSwapBuffers();
}
How can I make this method pause and wait for a keystroke before rendering the next triangle?
Upvotes: 1
Views: 539
Reputation: 47583
Is this the glutIdleFunc
, or the one called when you glutPostRedisplay
?
In either case, you need to first setup a glutKeyboardFunc
, in which you detect whether a certain key is pressed or not.
In the case you update the screen only on certain events:
... inside the keyboard function
if (key_pressed == THE_KEY_YOU_WANT)
glutPostRedisplay();
In the case you are using display
as the idle function, you can do:
global:
bool go_next = false;
... inside the keyboard function
if (key_pressed == THE_KEY_YOU_WANT)
go_next = true;
... inside display:
void display(void)
{
if (go_next)
{
go_next = false;
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
... etc
glPopMatrix();
}
glutSwapBuffers();
}
Side note: It is best to keep the logic and rendering separate. So perhaps a code like this is best:
int triangles_to_draw = 0;
... inside the keyboard function
if (key_pressed == THE_KEY_YOU_WANT)
process_event_next_triangle();
void process_event_next_triangle()
{
if (triangles_to_draw < maximum)
++triangles_to_draw;
}
void display(void)
{
go_next = false;
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glTranslatef(20.0f, 20.0f, -40.f);
glRotatef(180.f, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3*triangles_to_draw; i += 3)
{
glVertex3f(pts[tri[i]], (pts[ptsLength/2 + tri[i]] - maximum) * -1, 0);
glVertex3f(pts[tri[i+1]], (pts[ptsLength/2 + tri[i+1]] - maximum) * -1, 0);
glVertex3f(pts[tri[i+2]], (pts[ptsLength/2 + tri[i+2]] - maximum) * -1, 0);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
Note that this way, you can make the process_event_next_triangle
as complicated as you want without affecting the display
function. This makes life much easier when you have many keys doing different stuff.
Upvotes: 2
Reputation: 10125
when processing glut keyboard event add code:
if (key == DESIRED_KEY_CODE)
i+=3
and remove that i+=3
in the display()
func
but note that this way we will have only one triangle at a time... if you want to show several triangles you have to have some counter of visible triangles and display them in a loop.
Upvotes: 1