iDevGames Forums
OpenGL gluLookAt() not doing anything? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL gluLookAt() not doing anything? (/thread-2660.html)



OpenGL gluLookAt() not doing anything? - ene777 - Apr 25, 2008 04:59 PM

Ok I am brand new to openGL so I Know I am doing something wrong here! Basically I have 3 spheres drawn, one is out of the screen, this is exactly what I want. But I want the user to be able to change camera position so his looking from negative 10 on the x-axis, this will allow him to see the 3rd sphere... however i try doing this when the user hits #2 with a key event, and nothing happens. I have a feeling I am not using matrices correctly, here is my code... the asterisk (*) is where the problem is
Thank you for any help you can give!!!!
Code:
#include<iostream>
#include<GL/glut.h>

using namespace std;



void handleKeypress(unsigned char key, int x, int y)
{
switch(key)
{
case 'q':
    exit(0);
case 27:
    exit(0);
case 'a':;
    //start animation
    break;
case '1':;
    break;

//HERE IS WHERE THE PROBLEM IS!
case '2':
    {
//*************this does nothing!*************
//change camera to view 2
glMatrixMode(GL_MODELVIEW);
gluLookAt(-10.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//***************************************
    }
    
}
}

void initRendering()
{
    glEnable(GL_DEPTH_TEST); //make 3d work when something is in front of it
}

void drawScene()
{
    //clear the screen from last draw
    glClear(GL_COLOR_BUFFER_BIT);
  
  // draw something
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 600 / 600, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);


    glPushMatrix();
    glTranslatef(0.7, 0.0, 0.0);
    glColor3f(1,1,1);//Change the object color to white
    glutSolidSphere(.1, 600, 600);
    glPopMatrix();

    glColor3f(0,0,1);//Change the object color to blue
    glutSolidSphere(.4, 600, 600);

    glPushMatrix();
    glTranslatef(40.0, 0.0, -5.0);
    glColor3f(1,1,0);//Change the object color to yellow
    glutSolidSphere(5.0, 600, 600);
    glPopMatrix();


    glutSwapBuffers();
}    




void main(int argc, char** argv)
{
//init GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE |GLUT_RGB |GLUT_DEPTH);


    glutInitWindowSize(600, 600); //set window size
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Project 3");
    //initRendering();

    //set handler functions for drawing, keypresses, and window resizes
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
//    glutReshapeFunc(handleResize);

// define the color we use to clearscreen
    glClearColor(0.0,0.0,0.0,0.0);
glViewport(0,0,600,600);

    glutMainLoop(); //start the main loop
}



OpenGL gluLookAt() not doing anything? - ThemsAllTook - May 1, 2008 08:50 AM

I see two problems here:
  • You'll need to call glutPostRedisplay at the end of handleKeypress so that your displayFunc will get called again. Otherwise, GLUT has no way to know that your scene needs to be redrawn.
  • Although you're performing a transformation in handleKeypress, drawScene clears that out by calling glLoadIdentity() on the modelview matrix. You'll need to add some smarts so that the appropriate gluLookAt call (only one!) happens after glLoadIdentity().
Good luck!