#### Archived

This topic is now archived and is closed to further replies.

# Damn those coordinate systems !

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

## Recommended Posts

Ok, now I''m totaly new to this OpenGL stuff so this thing I just bumped into is REALLY frustrating (almost makes me want to give it up and pick up something else, more platform-specific, like Direct3D or something...) But enough about that...here''s my problem: I''m trying to make a program that lets the user move the camera around a 3D scene. I have the following code doing the 3D scene initialization: glMatrixMode( GL_PERSPECTIVE ); glLoadIdentity(); gluPerspective( 65.0, // FOV (double)wwidth/(double)wheight, // window aspect ratio 1.0, // near Z clipping plane 100.0 // far Z clipping plane ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( cam_x, cam_y, cam_z, // camera position view_x, view_y, view_z, // view point 0.0, 1.0, 0.0 // up vector ); As you might have already guesed, cam_x, cam_y and cam_z are the coordinates of the camera (initially set to 0.0, 2.0, 0.0 respectivly) and view_x, view_y, view_z are the coordinates of the view point. I also have two angle, alpha and beta, which describe the orientation of the (camera, view-point) vector in relation to the camera as follows: - alpha is the angle between the vector and the x axis - beta is the angle between the vector and the xOz plane To compute the coordinates of the view point in relation to the camera position I use the following: float xzlength = view_l * cos( f_AngleToRadians(beta) ); float xlength = xzlength * cos( f_AngleToRadians(alpha) ); float ylength = view_l * sin( f_AngleToRadians(beta) ); float zlength = xzlength * sin( f_AngleToRadians(alpha) ); view_x = cam_x + xlength; view_y = cam_y + ylength; view_z = cam_z + zlength; Evidently, f_AngleToRadians does exactly what the name implies... Anyway, my problem is that when I call ALL these in a function which does the drawing of the scene and try to rotate the camera to the left or to the right NOTHING HAPPENS ! The way I see the problem, to do the turning all you need to do is modify the alpha angle and then by simply using the formulae bove the camera should face the new direction. I might have completaly misunderstood OpenGLs coordinate system, but the way I see it it should look like this: - the x asix pointed with the positive direction to the right of the viewport - the y axis pointed with the positive direction up (relative to the viewport) - the z axis pointed with the positive direction away on the viewport (perpendicular to the screen, pointing away from the user, into the screen that is) Could someone please explain how the OpenGL coordinate system is pointed and why the code above doesn''t work ?

##### Share on other sites
Without seeing more of the update code, my first guess would be are you calling gluLookAt each time you update? While I''ve never used gluLookAt (and so can''t really say I know exactly what I''m talking about), would really need to see a bit more code before anyone can say anything definite. It may be a good idea to read up on Matrices (and eventually Quaternions) if you''re serious about doing a proper camera. Perhaps a bit daunting to begin with, but well worth it in the end.

##### Share on other sites

Apologies if this is a dum question .. but you are re-calling gluLookAt(), aren''t you? And redrawing the scene?

Just an initial thought

Oh, and try printing the values of view_ to stdout (the console) or something, so that you can verify you''re changing them the way you expect.

##### Share on other sites
As a matter of fact, I do call gluLookAt each time I redraw the scene. Here is the entire drawing function:

int wwidth, wheight;
double t;

// get the current world time
t = glfwGetTime();

// get window size and set viewport
glfwGetWindowSize( &wwidth, &wheight );
wheight = ((wheight < 1) ? 1 : wheight);
glViewport( 0, 0, wwidth, wheight );

// clear the screen to black
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// set up the environment - first the projection matrix
glMatrixMode( GL_PROJECTION );
gluPerspective( 65.0, (double)wwidth/(double)wheight, 1.0, 1000.0 );
// now the modelview matrix. This is where we compute the view point in regards to the
// current camera position
float xzlength = view_l * cos( f_AngleToRadians(beta) );
float xlength = xzlength * cos( f_AngleToRadians(alpha) );
float ylength = view_l * sin( f_AngleToRadians(beta) );
float zlength = xzlength * sin( f_AngleToRadians(alpha) );
view_x = cam_x + xlength;
view_y = cam_y + ylength;
view_z = cam_z + zlength;
glMatrixMode( GL_MODELVIEW );
gluLookAt(
cam_x, cam_y, cam_z, // camera position
view_x, view_y, view_z, // view point
0.0, 1.0, 0.0 // set the y axis as being up
);

glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );

// some drawing code...
// attempt to draw a sort of room
// first the floor
f_DrawBox( 0.0, -0.5, 55.0, 100.0, 1.0, 100.0 );
// left wall
f_DrawBox( -5.0, 10.0, 20.0, 1.0, 20.0, 20.0 );

All functions beginning with glfw are part of the GLFW OpenGL Application Framework (which is really cool and easy to learn). f_DrawBox creates a box centered in a specified point in space (it actualy creates the 6 quads that make up the box).

The way I see the problem, since I always recalculate the position of the view point in relation to the current camera position there should be no problem in the code above...but aparently OpenGL doesn't think so too .
Could you now please tell my why nothing happens when I modify the angles ?

BTW, thanks for replying. You are right, I could use some more info on 3D math and I'm now setting off to find juicy chucks of data to asimilate :-D

PS: keep in mind that I am a COMPLETALLY NEW to OpenGL programming. I would really apreciate it if you'd explain to me WHY calling gluLookAt each time I redraw the scene is bad. Thanks again for your answers

[edited by - gargoylle_ltk on November 21, 2002 2:57:02 PM]

##### Share on other sites
i'm not seeing a call to:
SwapBuffers(hDC);

are you making that on each iteration of the draw loop. if not then you are never updating what gets sent to the monitor.

also, i assume that view_x, etc are of type float or double, and we're not dealing with integer truncation issues?

also, please put your code in [ source ] and [ /source ] tags (without the spaces in those tags that i writ). it makes it easier to read

[EDIT: oh and calling gluLookAt(...) is _good_ and _necessary_ to call on each draw loop. you misread the above posts]

-me

[edited by - Palidine on November 21, 2002 2:58:58 PM]

##### Share on other sites
Sorry if I''m misundertanding something, but I didn''t read your
post really thoroughly ...

But this thing, I think isn''t correct:

- the z axis pointed with the positive direction away on the viewport (perpendicular to the screen, pointing away from the user, into the screen that is)

If I''m understanding you right,
than you''re saying the z axis is pointing into the screen with
the positive direction.

But if I''m remembering it right, it is pointing into the screen
with the negative direction, I think you should check that out,
cause I don''t have the possibility at the moment.

##### Share on other sites
palidine:
my app is based on the GLFW framework. This framework contains functions for initalizing OpenGL, creating a front and back buffer, creating a window to render to, handling keyboard and mouse input and much more. my application's structure is as follows:
- there is a callback function that handles all keyboard input (and which modifies the angles, coordinates etc.)
- there is the drawing function which is called in the main loop
- the WinMain function contains the following drawing loop:

do {
Draw(); // update the scene
glfwSwapBuffers(); // swap the buffers and update keyb/mouse state
running = ....; // deterimie wethe the user pressed the ESC key...
} while ( running );

I do get an initial display of the floor, but then noting happens when I press press the arrow keys. Input does work (I can terminate the application by pressing the ESC key) and I have also implemented a sort of log (I write the values of the variables into a file) and they DO get modified when keys are pressed...still, nothing happens...

[edited by - gargoylle_ltk on November 21, 2002 3:17:08 PM]

##### Share on other sites
Beast Master:
I did some checking, and the z axis does point INTO the screen. I also observed that the x axis is pointing to the left, and not the the right as I expected...rather strange, I must say...

##### Share on other sites
ok to make some thing clear

+x is to the right
+z is out of the screen
+y is upwards

the reason why you think it points into the opponent direction is because you have to translate into the camera into
-x-y-z direction to get to the coordinate your want to

with the projection translation example page 1xy

##### Share on other sites
i guess at this point you should sanity check your input system. yeah i did read the part where you say it''s working. but try this just to be absolutely sure:

in whatever method you look for keystates and then move the object accordinly, print out the keyboard state at that exact moment. you may, hopefully, find that at that moment in your game loop there are no keys marked as down, ever. i say hopefully b/c that would be a solution in that you have found the problem

-me

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009239
• ### Who's Online (See full list)

There are no registered users currently online

×