Jump to content
  • Advertisement
Sign in to follow this  
CosmoKramer

OpenGL model + view = modelview?

This topic is 4998 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

Ok I read that the opengl modelview matrix is actually composed of 2 matrices, the model and the view. Im not quite sure what the differences are, what either does, or why they were thrown into one matrix, but the way I understand it, a modelling transformation is equal to the opposite viewing transformation. Is this right? For example, if you translate the model 10 units to the right, its the same thing as if you had translated the viewer 10 units to the left. What I dont understand is how to use this to my advantage when both matrices are thrown into one. For example, say I want to rotate the model around its own axis (ie make a box spin in place), then I can call glRotate() right? Ok so what if I want to make the box spin with respect to the viewer? In other words, it should spin around my virtual head instead of spinning in place. It seems to me I might have to access the 'view' part of the modelview matrix, but since its really just one matrix, how do I do this? I find it hard to keep all this striaght in my head, and havnt found a good explanation of this stuff yet. A lot of times, when i want something to happen, I just call one of the glTranslate, glRotate, or glScale functions, and it will do the opposite of what I thought. So I just change the sign, or do the reverse transformation, and everythign works out. So I get the desired effect, but I dont really understand whats going on, and I would like to. Please help! Thanks.

Share this post


Link to post
Share on other sites
Advertisement
ok.
the modelview as you said, is the model+view matrix.
the model matrix moves the objects and the view the world itself (or the view).

if you just call glrotate or translate you will be affecting the the entire world (you could say that you are affecting the view), so if you only want to rotate one model (or object) you have to get the matrix into a stack by calling
glPushMatrix();
//do some transformation with the object
// draw the object
glPopMatrix();

that way you wont be affecting the world matrix, only the model.

also, you have to be conscient (does that word exist?) that is not the same calling glTranslate and then glRotate than glRotate and then glTranslate.
also, opengl uses colum vectors and the z + axis is like going inside the screen (most math books are z- going inside)

Share this post


Link to post
Share on other sites
It's a bit pedantic, but modelview matrix is actually view*model, not model+view. Basically, a model matrix can transform your object into world space, and from there a view matrix transforms it into camera-relative space. These two steps can be combined into a single matrix transformation, so it's more efficient, since it's only matrix*vertex for each vertex, rather than matrix*(matrix*vertex).

Share this post


Link to post
Share on other sites
A vertex V exists in 'object space.' If I had a cube model, I might structure it so that the center of the cube was at (0, 0, 0) in object space, and the vertices were at (+/-1, +/-1, +/-1).

A function f(V) : [model]*V exists which transforms V from object space into world space. World space is the space in which vertices from different objects can be correct relative to each other; so if my cube centered at (5, 5, 5) in world space, f(V) will transform my vertices from (+/- 1, +/- 1, +/- 1) to (5 +/- 1, 5 +/- 1, 5 +/- 1).

Another function g(V) : [view]*V exists which transforms V from world space into camera space, so that they're correct relative to the camera (which is centered at the origin, looking along the z axis).

If you apply both of these in turn - take a vertex V in object space, transform it into world space, and then into camera space, it looks like this: g(f(V)). When you expand the functions, you get g(f(V)) : g([model]*V) : [view]*[model]*V as NoahAdler said.

You don't have to keep your model and view matrices together. You can just keep them as seperate float[16] variables, then glLoadMatrix(xViewMatrix); glMultMatrix(xWorldMatrix); if you want. Provided you set up your view matrix first (through glRotate/glTransform/glWhatever) calls and your model matrix second, they'll be combined OK.

Share this post


Link to post
Share on other sites
To explain it more simple, you can think of the view matrix as the inverse of the object look matrix.

Let's say you have 3 vectors: Eye,View,Up

This describes where you are at (Eye), what you are looking at (View) and the Up vector (normally 0,1,0)

You compute a matrix from this. Than you take the inverse of this matrix and it computes the view matrix. This is your 'camera'.

This is how gluLookAt() works.

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!