PDA

View Full Version : OpenGL camera problem



ks554
01-25-2009, 12:32 PM
Hi all,

Wondering if u can help me out:

I am trying to get the camera to move to a particular location in my scene, but the problem is that it only works if I use gluLookAt with the projection matrix and NOT the modelview matrix.


Isnt this supposed to be bad?

how can i get it to work with modelview matrix.

code shown below...problem is encountered in the normal key method when typing in key "f" as nothing happens!

[CODE]#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

#define ESCAPE 27 //use escape key to exit program

GLint Xsize = 400;
GLint Ysize = 400;
GLint window_name; //name of our window
GLfloat xRot = 10.0f;
GLfloat yRot = 0.0f;
GLfloat zRot1 = -100.0f;
GLfloat yRot1 = 15.0f;
GLfloat Elect, Elect1, Elect2 = 0.0f;
GLfloat xdirection, ydirection;

GLvoid InitWindow(GLfloat Width, GLfloat Height)
{

glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-100.0, 100.0, -100.0 / (Width / Height), 100.0 / (Width / Height), -1.0, 10.0);
gluPerspective(45.0,Width/Height,0.0, 200.0); //push object away ffrom origin
//gluLookAt(9.0f, 0.0f, -100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);

}


GLvoid ResizeScene(GLint Width, GLint Height)
{
if (Height == 0) Height = 1; //prevent division by zero
if (Width == 0) Width = 1;
InitWindow(Width, Height); //reset perspective projection
}



GLvoid Camera()
{
//glMatrixMode(GL_PROJECTION);
//gluPerspective(45.0, 1.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(9.0f, 0.0f, 100.0f, 0.0f, 0.0f, 100.0f, 0.0f, 0.0f, 1.0f);

}





void NormalKey(GLubyte key, GLint x, GLint y)
{
switch( key)
{

case ESCAPE:
printf("Escape pressed, exiting..\n");
glutDestroyWindow(window_name);
exit(0);
break;


case 102: //letter f
Camera();
break;


default:
break;
}
}


int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(Xsize, Ysize);
glutInitWindowPosition(0,0);
window_name = glutCreateWindow("Model of an ATOM");

//Initialise window
InitWindow(Xsize, Ysize);

//Callback functions (Resize & Draw screen)
glutReshapeFunc(ResizeScene);
glutIdleFunc(DrawScene);

//Callback function when a key is pressed
glutKeyboardFunc(NormalKey);

glutMainLoop();
return 1;

}

[/B][/QUOTE]

dletozeun
01-25-2009, 12:47 PM
first, you don't set 0 for the near plane distance, otherwise you will see awful artifacts.
Second, it is not the full code, isn't it? You set DrawScene as the idle function that looks weird, you should at least set it as the display function with glut.
Finally read about the nature of modelview and projection matrices in the gl spec ("Coordinates transformation" paragraph). You will see that gluLookAt is related to the projection matrix, it just change the view. Anyway, what you are doing with gluLookAt in your code looks correct... just check you have something to watch at the location specified in gluLookAt.

ks554
01-25-2009, 01:13 PM
problem solved.