PDA

View Full Version : Simple Code. Please Debug.



FuzzyLogic
11-12-2001, 10:20 PM
Here is my code. My goal is to display a triangle and a square and have them both rotating. When I run the code, the triangle is displayed but the square is not and neither of them are rotating. Please help!

#include <gl\glut.h>

GLfloat rtri;
GLfloat rquad;

void Initialize()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glShadeModel(GL_SMOOTH);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

return;
}

void Reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

return;
}

void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(-1.5f, 0.0f, -6.0f);

glRotatef(rtri, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);

glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

glLoadIdentity();
glTranslatef(3.0f, 0.0f, 0.0f);

glRotatef(rquad, 1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();

glFlush();

rtri += 0.2f;
rquad -= 0.15f;

return;
}

void Keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27: exit(0);
break;
}

return;
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("");

glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);

Initialize();

glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutDisplayFunc(Display);

glutMainLoop();

glutSetCursor(GLUT_CURSOR_LEFT_ARROW);

return 0;
}

dorbie
11-13-2001, 12:00 AM
Ther is no translate along -z when you draw the quad after you load identity, it will be too close to the eye, actually it's off to the side because you translate in x. You need a viewing transform before you draw everything to move the eye back, along the z axis preferably.

As for it rotating, this should work but there is nothing telling the app to redraw, you can force a redraw by resizing the window or causing an expose event, does it rotate when you do this?

To force this to happen regularly you need an idle func which posts a redraw event. You should also do your animation updates there instead of the draw code, i.e. update the rotation values in the idle function.

Call this in main:

glutIdleFunc(idle);

and write this:

void
idle(void)
{
/* animate rotations */
rtri += 0.2;

/* force redraw */

glutPostRedisplay();
}

You should set the idle function to NULL if the window get's covered or iconified to halt the animation.

Here is a page with a good glut example:
http://www.opengl.org/developers/code/mjktips/particles/