Hey all,
I’ve written this simple piece of code that involves a cube rotating about the origin. From what I understand, I’ve set up my camera behind the cube and have the camera looking in the direction of the cube:
#include <gl/glut.h> // GLUT header file
#include <gl/gl.h> // OpenGL header file
#include <math.h>
float _angle = 0.0;
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.5, 0.5, -5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
gluPerspective(45.0,1.0,1.0,15.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the background
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //set current matrix to identity
glPushMatrix();
glRotatef(_angle, 0.0, 0.0, 1.0); //Rotation: theta = 30
glBegin(GL_LINE_LOOP);
glVertex3f( 0., 0., 0.); glVertex3f( 0., 1., 0.);
glVertex3f( 1., 1., 0.); glVertex3f( 1., 0., 0.);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 0., 0., 1.); glVertex3f( 1., 0., 1.);
glVertex3f( 1., 1., 1.); glVertex3f( 0., 1., 1.);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0., 0., 0.); glVertex3f( 0., 0., 1.);
glVertex3f( 0., 1., 0.); glVertex3f( 0., 1., 1.);
glVertex3f( 1., 0., 0.); glVertex3f( 1., 0., 1.);
glVertex3f( 1., 1., 0.); glVertex3f( 1., 1., 1.);
glEnd();
glPopMatrix();
glFlush();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay(); //causes the window to be redrawn
glutTimerFunc(25, update, 0);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("Q1");
init();
glutDisplayFunc(display);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
}
Now, if I omit the gluLookAt line and replace it with glTranslatef(0.0,0.0,-5.0);, then I can see the cube so I know the problem is with gluLookAt. I’ve been playing around with it but I just can’t figure it out. Why can’t I see anything the way I have it set up right now?
Thanks,
Canadian0469