Jump to content
  • Advertisement
Sign in to follow this  
ks554

OpenGL OpenGL camera problems

This topic is 3579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]).

Share this post


Link to post
Share on other sites
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!



#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

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

GLint Xsize = 400;
GLint Ysize = 400;
GLint window_name; //name of our window


GLvoid 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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!