Results 1 to 4 of 4

Thread: Can't see cube using gluLookAt

  1. #1
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    11

    Can't see cube using gluLookAt

    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:

    Code :
    #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(&amp;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

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Apr 2004
    Posts
    990

    Re: Can't see cube using gluLookAt

    There are two problems with your code.

    1. Matrices that define how you look at something typically belong to the modelview matrix and not the projection matrix.

    2. Most matrix calls perform a post-multiplication. This means that the matrix you call last is executed first on the data. So as it is now you're performing a projection followed by a transform instead of vice versa.

  3. #3
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    11

    Re: Can't see cube using gluLookAt

    Thanks again -NiCo-!

    #2 fixed my problem right away.

    As for #1, if I do

    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.5, 0.5, -5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,1.0,1.0,15.0);

    instead, it doesn't work anymore... Is this what you were suggesting? I'm confused by this first point you made because so far in all OpenGL code I've seen (in my class anyway) all calls to glPerspective and gluLookAt followed the glMatrixMode(GL_PROJECTION); command...

    What aren't I understanding?

    Thanks again,

    Canadian0469

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Apr 2004
    Posts
    990

    Re: Can't see cube using gluLookAt

    That's probably because you're resetting the modelview matrix to the identity matrix in the display call. Note that, even for gluLookAt, you need to call glLoadIdentity first if you want to avoid accumulating postmultiplications.

Similar Threads

  1. Cube Map without using OpenGL's Cube Mapping
    By someoney in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-10-2010, 01:05 PM
  2. display a cube inside another bigger cube
    By jyoung77 in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 05-08-2003, 06:12 PM
  3. Wire Cube, and Cube and GUI
    By tony_ee81 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 09-20-2002, 12:58 PM
  4. Cube,Cube and a Cylinder
    By tony_ee81 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-03-2002, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean