-
OpenGL general question
Hi, I am learning OpenGL and I am trying to make an object I created cross a 2D grid. Using glTranslate I have figured out how to move the entire image, but how would I say move my object ( Which is a GL quad)?
Code:
for(int i=-0; i<10;i+=10)
{
glBegin(GL_QUADS);
glVertex2f(-5.0f,i);
glVertex2f(i,5.0f);
glVertex2f(5.0f,i);
glVertex2f(i,-5.0f);
glEnd();
}
for(int i=-500; i<500; i+=30)
{
glBegin(GL_LINES);
glVertex2f(600.0f,i); //much simpler than all of that .........
glVertex2f(-600.0f,i);
glVertex2f(i,600.0f);
glVertex2f(i,-600.0f);
glEnd();
}
drawFont("Test Blue");
Mod Edit: Added code tags.
-
Try:
Code:
glPushMatrix();
glTranslate(...);
// Render your triangle here
glPopMatrix();
That ought to work...
-
Fundamental to the possibility of motion is an "event loop" -- that is, a loop that continuously redraws the screen/window. OpenGL does not do that by itself.
However, if you are using glut or SDL you probably are using an event loop if you have been following some kind of tutorial. If you are not sure, ask.
With glut, you have your scene drawing function registered like this, then you initialize the main event loop:
Code:
glutDisplayFunc(drawScene);
[...]
glutMainLoop();
You must also include this:
Code:
glutPostRedisplay();
at the end of drawScene() (or whatever you called it).
Then you just use the technique EVOEx mentioned. As is, your quad is centered on the origin. So, working with that example:
Code:
static float square[3] = {0.0f};
glTranslatef(square[0],square[1],square[2]);
glBegin(GL_QUADS);
for(int i=-0; i<10;i+=10) {
glVertex2f(-5.0f,i);
glVertex2f(i,5.0f);
glVertex2f(5.0f,i);
glVertex2f(i,-5.0f);
}
glEnd();
square[0]++;
square[1]++;
This will cause the square to move diagonally right. Using the z-axis in 2D effectively scales the shape (as if it were receeding), I think...
Note the scene drawing routine registered by glut does not accept parameters, so you must use either "static" variables or globals. Also noticed I put the for loop inside glEnd/Begin, which is a little more efficient.