# OpenGL general projection/camera help

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

## Recommended Posts

I am fresh out of beginners programming (game maker) and I need some help with opengl's projection system. What I need is a camera that has movement and direction to it, not a camera that sits and has the world move around it. What type of projection should I use gluPerspective or the other one. What would I have to use to get a simple movement system setup. Would it have anything to do with gluLookAt? I am I bit lost and kind find many answers on my own. Some help would go a long way. Edit: Ok currently I am using gluPerspective(45,width/height,1,100000). Thats about all I could figure out, well that and how to get the world to move around it using glTranslate. Now I need to leave how to keep the world in one set location and move the camera around. Does anyone have any idea how this can be done? [Edited by - snowfell on May 10, 2008 11:36:34 AM]

##### Share on other sites
when you load the projection matrix you set the camera for projection view, thats what gluPerspective() and glFrustum() are for, they setup the andle of view and respective ratio and also the near and far camera planes.

now to setup the camera position, that is calculated on the modelview matrix. and you can use gluLookAt() for that as you said.

a simple example would be:
void render(){  glMatrixMode( GL_MODELVIEW ); //load the modelview matrix mode calculations  glLoadIdentity(); //load the identity  //set camera position  glTranslated( 0.0, 0.0, 100.0 ); //camera will be at 0, 0, -100 world coordinates, as glulookat rotates its position  gluLookAt( 0.0, 0.0, 0.0, 0.0, 0.0, -5.0, 0.0, 1.0, 0.0 ); //set it on the new origin and looking 5 unites farther into the screen (also sets the up view vector)  //render your scene  glPushMtarix(); //save current matrix into the stack  (...)  glPopMatrix();}

my comments might not be entirely correct (im also pretty new to opengl) but it can help a bit.

##### Share on other sites
Actually, if I recall correctly, standard setting of the camera position and orientation (via glTranslate and the like) can be done with the projection matrix - in fact, if I'm not much mistaken, that sounds like what you're asking for.

I'm not sure about gluLookAt; I don't think that I've used it in ages, personally, but I do have a vague memory that that call, specifically, might be intended to be used with the modelview matrix selected.

However, why not move the world? It amounts to the same thing, I believe - it just operates the other way around (that is, instead of translating to {-1, -2, 4}, you'd translate to {1, 2, -4}). Unless you're doing something unusual, then I don't think that it should introduce any difficult problems.

##### Share on other sites
What I do is I make a class that holds 3 vectors; Position, Rotation and View Direction. I then have 4 functions for movement and 4 for updating, For movement: Left, right, forward and backward then for Updating the vectors mentioned earlier and another to update them all.

Left, right, forward and backward change the position vector and then the position updating functions calls glTranslatef with Position's values.

I have the mouse edit the rotation then the rotation updating function calls glRotatef with Rotation's values.

##### Share on other sites
Well I understand that projection draws everything on a hor and vert axis (y being vert, x and z being hor). Now what I don't understand is how to get motion and direction out of the camera. So far I have been able to make a weak 3d engine using glut. I have been able to render a simple floor and and put colored markers at each corner.

So this is the bare bone engine I wiped together:

void projection(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,width/height,1.0f,10000.0f);
gluLookAt(cord_x,cord_y+5,cord_z,0.0,0.0,dir,0.0,zdir,0.0);
}

void display()
{
float room_width=255;
float room_height=255;

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glBegin(GL_POLYGON);
glColor3d(.25,.25,.25);
glVertex3f(0,0,0); glVertex3f(room_width,0,0);
glVertex3f(room_width,0,room_height); glVertex3f(0,0,room_height);
glEnd();

//drawing...
}

Is their anything I can change to include a motion system? Or are there any improvement I could add to what I have done so far.

##### Share on other sites
If you have the view direction you can multiply that by a distance and add/subtract it to the current position to move in that direction.

If you don't have the view direction here's how to work it out:
float Cos_x, PI = 3.141592654;	Vector A;	A.x = cos((Rotation_y + 90.0f) * (PI/180));	A.z = -sin((Rotation_y + 90.0f) * (PI/180));	Cos_x = cos(Rotation_x * (PI/180));	ViewDir.x = A.x * Cos_x;	ViewDir.z = A.z * Cos_x;	ViewDir.y = sin(Rotation_x * (PI/180));

##### Share on other sites
Quote:
 Original post by snowfellvoid projection(int width, int height){...glMatrixMode(GL_PROJECTION);...gluLookAt(cord_x,cord_y+5,cord_z,0.0,0.0,dir,0.0,zdir,0.0);}

You should not put the view matrix into the projection matrix. Instead, it should be put into model-view matrix.

##### Share on other sites
Quote:
 Original post by MrPickleIf you have the view direction you can multiply that by a distance and add/subtract it to the current position to move in that direction.If you don't have the view direction here's how to work it out:*** Source Snippet Removed ***

Ok so where might I put this and its variables? I have seen codes very similar to this in my game making past. Another rather important question I have is where do most of the projection codes go, and why (In their own function or what)?

##### Share on other sites
In OpenGL, you have three predefined matrices, i.e. model-view matrix, projection matrix and texture matrix.

Model-view matrix, as suggested by its name, is to handle model and viewing. Just for the naming reason, you should handle the viewing using model-view matrix. Besides, there are good some reasons behind why model and viewing get together. It is because both are usually translation and rotation only. Also, it is easier to make specular lighting correct... ...

http://www.sjbaker.org/steve/omniv/projection_abuse.html

##### Share on other sites
Ok I was able to read through that page but it didn't really explain projection order well. I was just wondering what a effective 3d projection system might look like (as in where gluLookAt and other important codes should be place and how to be used). I have a simple engine going and so far I have a general outline to this, but I need some structure to improve it. If it would help I could post what I have done so far, but only if it could help get some answers.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013601
×