OpenGL camera problems
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 with the modelview matrix.
Isn't this supposed to be bad?
If so, why?
[Edited by - ks554 on January 25, 2009 3:53:23 PM]
The model view matrix is supposed to convert from model space to view space and the projection matrix from view space to normalized window coordinates space. I guess you could combine both to a single matrix and it will work fine. Anyway, it should work using the modelview matrix. Maybe you mess it up later in code?
[Edited by - CableGuy on January 26, 2009 6:43:02 AM]
[Edited by - CableGuy on January 26, 2009 6:43:02 AM]
Quote:Original post by ks554
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 with the modelview matrix.
Isn't this supposed to be bad?
If so, why?
Actually, Cameras do not exist in OpenGL.
http://www.opengl.org/resources/faq/technical/viewing.htm#view0010
Quote:Original post by ks554
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 with the modelview matrix.
Isn't this supposed to be bad?
If so, why?
Yes, I'd consider it bad, since the projection matrix defines how objects are projected from eye space to screen space (i.e. onto the screen). If you mess up the projection matrix, you'll most certainly run into problems sooner or later (e.g. with unprojection, lighting etc.).
The modelview matrix is a composed of the model and view matrices which basically transform coordinates from object local space to eye space ("camera local"), skipping the transformation to and from world space (model matrix: object local -> world, view: world -> eye).
If gluLookAt doesn't work the way you expect it, show us some code. Maybe we can find the error. Ah, and don#t forget to use the source tags (see the faq if you don't know what I mean [smile]).
Code is shown below...main problem is when letter 'd' is pressed (shown right at the bottom of the page), the modelview matrix is invoked and it should change the view to a certain location...but this does not work!
[Edited by - ks554 on January 26, 2009 4:54:17 PM]
#include <stdio.h>#include <stdlib.h>#include <GL/glut.h>#define ESCAPE 27 //use escape key to exit programGLint Xsize = 400;GLint Ysize = 400;GLint window_name; //name of our windowGLvoid InitWindow(GLfloat Width, GLfloat Height){ glViewport(0, 0, Width, Height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,Width/Height,0.0, 200.0); 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 DrawScene(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -100.0f); glColor3ub(255, 0, 0); glutSolidSphere(3.0f, 15, 15); glutSwapBuffers();}void NormalKey(GLubyte key, GLint x, GLint y){ switch( key) { case ESCAPE: printf("Escape pressed, exiting..\n"); glutDestroyWindow(window_name); exit(0); break; case 'd': glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(10.0f, 0.0f, -100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f, 1.0f); break; default: break; }}int main(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(Xsize, Ysize); glutInitWindowPosition(0,0); window_name = glutCreateWindow("Title"); //Initialise window InitWindow(Xsize, Ysize); //Callback functions (Resize & Draw screen) glutReshapeFunc(ResizeScene); glutDisplayFunc(DrawScene); //glutIdleFunc(DrawScene); //Callback function when a key is pressed glutKeyboardFunc(NormalKey); glutMainLoop(); return 1; }
[Edited by - ks554 on January 26, 2009 4:54:17 PM]
GLvoid DrawScene(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ...}
You are resetting the modelview matrix to the identity matrix at the beginning of each frame.
As said, in OpenGL both the world and view matrix are stored together, therefore after applying transformation to your objects you have to restore the modelview matrix to the original camera matrix. Something like this:
GLvoid DrawScene(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(...) // Set the "view" part glPushMatrix(); // Store the current matrix. glTranslate(..); // Apply the "world" part. RenderSometinhg(); glPopMatrix(); // Restore.}
Hope this helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement