GL_MODELVIEW and GL_PROJECTION

15,045

Solution 1

Quote from opengl.org forum:

The projection matrix is used to create your viewing volume. Imagine a scene in the real world. You don't really see everything around you, only what your eyes allow you to see. If you're a fish for example you see things a bit broader. So when we say that we set up the projection matrix we mean that we set up what we want to see from the scene that we create. I mean you can draw objects anywhere in your world. If they are not inside the view volume you won't see anything. When you create the view volume imagine that you create 6 clipping planes that define your field of view.

As for the modelview matrix, it is used to make various transformations to the models (objects) in your world. Like this you only have to define your object once and then translate it or rotate it or scale it.

You would use the projection matrix before drawing the objects in your scene to set the view volume. Then you draw your object and change the modelview matrix accordingly. Of course you can change your matrix midway of drawing your models if for example you want to draw a scene and then draw some text (which with some methods you can work easier in orthographic projection) then change back to modelview matrix.

As for the name modelview it has to do with the duality of modeling and viewing transformations. If you draw the camera 5 units back, or move the object 5 units forwards it is essentially the same.

Solution 2

First of all, I suggest that you try to abandon the fixed-function pipeline (glTranslate etc) since it's been deprecated for like 10 years now. Look here for a more modern tutorial if you're interested.

As for your problem, you can imagine the meaning of the two matrices like this: The projection matrix essentially captures properties intrinsic to the camera itself, like how its field of view is shaped.

On the other hand, the modelview matrix is composed of two parts, the model matrix and the view matrix. The model part is for transforming from object space (relative to an object itself) to world space. Then, the view part translates from there to the eye space, in which the camera sits at the origin and points down the (negative?) z axis. Together, the modelview matrix essentially states how objects are to be positioned relative to the camera.

For further information, this resource gives a detailed description of graphics transformations in the context of OpenGL.

[Jan, 2017] Edit: Pages from the first link seem to be unable to access these days, so there is another link to the same content from their archive.

Share:
15,045
dhein
Author by

dhein

SOreadytohelp As a side note: I'm using this "About me" as reminder of important stuff for my self. So don't blame me if you wasted your time clicking through the links and didn't find anything of value for your self ;) Jokes: http://i.imgur.com/clOImMm.jpg http://stackoverflow.com/a/1604477/2003898 https://xkcd.com/327/ Your momma's so fat, that when she sat on a binary tree she turned it into a sorted linked-list in O(1). Knowledge: http://www.terrybisson.com/page6/page6.html https://i.stack.imgur.com/cMZI0.gif Tools: http://rpg.stackexchange.com/questions/41866/free-basic-rules-only-adventure-to-showcase-dd-5e https://store.makewonder.com/ http://anydice.com/ http://www.ipvoid.com Not reviewed: confirmed lee? https://www.youtube.com/watch?v=LkCNJRfSZBU

Updated on June 04, 2022

Comments

  • dhein
    dhein almost 2 years

    My code Currently looks like this :

    glViewport (0, 0, this->w(), this->h());
    
    glMatrixMode(GL_PROJECTION);
    
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    //glTranslated (m_fXmovement, 0.0, m_fZmovement - 5);
    //glRotated (m_fYangleView, 1.0, 0.0, 0.0);
    //glRotated (m_fXangleView, 0.0, 1.0, 0.0);
    
    ///// Model View \\\\\
    
    glMatrixMode(GL_MODELVIEW);
    
    glTranslated (m_fXmovement, 0.0, m_fZmovement - 5 );
    glRotated (m_fYangleView, 1.0, 0.0, 0.0);
    glRotated (m_fXangleView, 0.0, 1.0, 0.0);
    
    DrawWaveFrontObject (m_pDataObjectMedia);
    
    glPushMatrix();
        glTranslated (0.0, -3.0, 0.0);  
        DrawArea();
    glPopMatrix();  
    
    DrawClickAnimation();
    
    glLoadIdentity();
    

    First I had the movement part in GL_PROJECTION and all was running fine until I was working with fog.... It felt like the Camera isn't moving, it felt more like an additional camera pointing to that camera....

    Then I accidentally copied the movement parts to the GL_MODELVIEW and the fog was acting as I wanted it to act..... all was fine accepting the click animation wasn't in relation to the area anymore, now the animation moved with my ego perspective.... and I don't really get it what kind of drawing I have to put in which of these two VIEW's. Could anyone give me examples or explanations according to my code or a hint what I could improve in my styl?

    • GraphicsMuncher
      GraphicsMuncher over 10 years
      Ahoy matey! Please, please, please use modern OpenGL! The fixed function pipeline is old, deprecated, slow, and not cool anymore. Use OpenGL 4.0+! Shaders and VAOs are all much better in every way. You may have to wear sunglasses the first time you use it.
    • dhein
      dhein over 10 years
      @GraphicsMuncher But owning a really old Pc and earning almost no money in my apprentice ship made it impossible to me to work with this elements to that time. So don't get me wrong. But advising to choose another technique as work around is never a a real answer of "how to solve a problem".
    • GraphicsMuncher
      GraphicsMuncher over 10 years
      It wasn't an answer to you question, it was a comment. The point is that learning old OpenGL only wastes your time on learning obsolete code and makes more work for you when you have to unlearn it. Sorry about your old PC, though. Most people do old OGL because they found an out of date tutorial online.
  • dhein
    dhein over 10 years
    "Of course you can change your matrix midway of drawing" Ofcourse? I didn't knew when I asked, and exactly that is what would had helping me out of my dilemma I guess. So jsut that I got you right, it is possible to do something like glMatrixMode(GL_PROJECTION); /*some stuff*/ glMatrixMode(GL_MODELVIEW); /*some stuff*/ glMatrixMode(GL_PROJECTION); /*some stuff*/ glMatrixMode(GL_MODELVIEW); /*some stuff*/ For some purpose?
  • gnomed
    gnomed almost 9 years
    This is an inefficient answer.
  • Charlie
    Charlie about 6 years
    [translate - rotate - scale] will put your object in the world. View matrix will put it in frame, and project will warp to give a feeling of depth.