Click to See Complete Forum and Search --> : Why won't my graphics animate?


MrDoomMaster
June 20th, 2004, 07:26 PM
Hey guys. My objective here is to create a dot that will bounce off of the X axis in a 3D plane. The dot will slowly move closer to the X axis, and once it intersects, it will "bounce" in the other direction.

For some reason, the dot I make will not move. It only moves when I resize the window. Can someone help me out?

Here is my code:


//--Header-Files----------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
//------------------------------------


//--Function-Prototypes---------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void cbDraw();
//------------------------------------


//--Global-Variables------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int WorldWin = 30;
GLfloat X = 5; //X position of DOT
GLfloat Y = 30; //Y position of DOT
GLfloat Z = 0; //Z position of DOT
bool Down = true; //Determines dot direction. TRUE = Downward, FALSE = Upward.
//------------------------------------


int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("3D World");
glutDisplayFunc(cbDraw);

//Set Window Properties
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-WorldWin, WorldWin, -WorldWin, WorldWin, 0, 200);
glViewport(0, 0, 400, 400);
gluLookAt(30, 30, 30, 0, 0, 0, 0, 1, 0);

glutMainLoop();
return 0; }



/*****************************\
| Function Bodies |
\*****************************/

void cbDraw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3i(0, 0, 30);
glVertex3i(0, 0, 0);
glVertex3i(30, 0, 0);
glVertex3i(30, 0, 30);
glEnd();

glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3i(0, 0, 0);
glVertex3i(0, 30, 0);
glVertex3i(30, 30, 0);
glVertex3i(30, 0, 0);
glEnd();

glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3i(0, 0, 0);
glVertex3i(0, 30, 0);
glVertex3i(0, 30, 30);
glVertex3i(0, 0, 30);
glEnd();

glColor3f(0.0, 1.0, 0.0);
if(Down == true) {
glBegin(GL_POINTS);
glVertex3f(X, Y, Z);
glEnd();

X += 0.01;
Y -= 0.1;

if(Y <= 0.0)
Down = false; }
else {
glBegin(GL_POINTS);
glVertex3f(X, Y, Z);
glEnd();

X += 0.01;
Y += 0.1;

if(Y > 30)
return; }

glutSwapBuffers(); }

Marc G
June 21st, 2004, 03:54 AM
The problem is because your cbDraw is only called when you resize your window. So if you want to keep redrawing your window, you somehow have to keep calling your cbDraw.

The easiest way to solve this is to add the following line in your main function:

glutIdleFunc(cbDraw);