Sign in to follow this  
tonnot

Very confused with GL_projection / GL_modelview / camera / objetcts

Recommended Posts

[color="#000000"]I have a very big brain confusion:
How affects the :
glTranslatef
[/color][color="#000000"] glTranslated
[/color][color="#000000"] glRotatef
depending of the glMatrixMode[/color][color="#000000"] set

glMatrixMode[/color][color="#000000"](GL_MODELVIEW[/color][color="#000000"]);[/color][color="#000000"] [/color][color="#000000"]//[/color] [color="#000000"]Select[/color] [color="#000000"]The[/color] [color="#000000"]Modelview[/color] [color="#000000"]Matrix
glMatrixMode[/color][color="#000000"](GL_PROJECTION[/color][color="#000000"]);[/color][color="#000000"] [/color][color="#000000"]//[/color] [color="#000000"]Select[/color] [color="#000000"]The[/color] [color="#000000"]Modelview[/color][color="#000000"] Matrix

I'd want :
1.- Set the position of the camera focus and the ref .
What I have to use ?
[/color][color="#000000"]gluPerspective[/color][color="#000000"](), [/color][color="#000000"] glFrustum , [/color][color="#000000"] gluLookAt[/color][color="#000000"]() ?????
[/color][color="#000000"]2.- Drawing objects
2.1- Using their absolute coordinates
2.2.- Move them before paint.
[/color][color="#008000"][color="#000000"]
Now, I can view my objects if they are at 0,0,0 . Using the examples and tutorials founded on the web.
But, I'm unable to draw nothing when I want to use a world whose limits are -1000,-1000,200 1000,1000,300 (x,y,z min x,y,z max ) and simple 2D square defined by -500,500,250 500,500, 250. By moment I' want to place the camara like an airplane (on XY plane ).

Somebody can give me a little lesson ?. Thanks





[/color]
[/color]

Share this post


Link to post
Share on other sites
First of all, I strongly advise you read [url="http://glprogramming.com/red/chapter03.html"]Chapter 3 of the OpenGL Red Book[/url]. It has a very detailed explanation on GL transformations. Take specific note that there is no separate 'camera' transformation: it is accumulated in Model/View matrix. Even better, read the whole book :) It's outdated if you search for modern principles and techniques like shaders and tessellation, but the core OpenGL operation is very well documented there.

Simply put, glMatrixMode() selects the current matrix, the one that will be affected by further glTranslate/Rotate/LookAt/whatever calls. Technically, every call to any such function multiplies current matrix with a desired transformation. Therefore, you'd rarely want to call i.e. glTranslate/Rotate with GL_PROJECTION as current matrix (at least until you're being fancy with your projection transformation).

Simple way of things for your desired setup is:

[code]
// Select the projection matrix to set up camera 'focus'
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Clear out any existing transformation
// Set up camera 'focus'
gluPerspective(); // or glFrustum, or glOrtho, or glLoadMatrix, or glMultMatrix, whatever your setup for projection transformation is

// Done with the projection, everything else is model/view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Clear out any existing transformation
// Set up camera position and angle. Note the negative coordinates. See Red Book why this is so.
gluLookAt(-camera.x,-camera.y,-camera.z, where.x, where.y, where.z, camera.upX, camera.upY, camera.upZ);

// Set up objects, for each object repeat this:
glPushMatrix(); // save the 'view' matrix
glTranslatef(object.x,object.y,object.z); // 'move' your object somewhere
// Draw the object however you want, and then:
glPopMatrix(); // restore the 'view' matrix
[/code]

Take a note that when you set up camera 'focus' through gluPerspective, you should set far clipping plane distance. Setting it with a big number will decrease your Z-buffer precision. There are many techniques to draw objects in a vast world, but this is another topic. To make things simpler for yourself, at least for now while you're getting the hang of things, I'd advise you to rethink your world's dimensions, i.e. scale everything down unit-wise, e.g. 100 or 1000 times. It won't matter for viewing if it's 1, 100 or 1000 when relative dimensions are preserved (e.g. if you had one object 1000x1000 and another 500x500, it'd be exactly the same if you made the first one 10x10 and the second one 5x5, and scaled their coordinates accordingly).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this