http://liuxiaoxia2125.blog.163.com/blog/static/1375023412010023114810890/
这两个都是glMatrixMode()函数的参数,那就先说说glMatrixMode吧~,这个函数其实就是对接下来要做什么进行一下声明,也就是在要做下一步之前告诉计算机我要对“什么”进行操作了,这个“什么”在glMatrixMode的“()”里的选项(参数)有,GL_PROJECTION,GL_MODELVIEW和GL_TEXTURE;
如果参数是GL_PROJECTION,这个是投影的意思,就是要对投影相关进行操作,也就是把物体投影到一个平面上,就像我们照相一样,把3维物体投到2维的平面上。这样,接下来的语句可以是跟透视相关的函数,比如glFrustum()或gluPerspective();
如果参数是GL_MODELVIEW,这个是对模型视景的操作,接下来的语句描绘一个以模型为基础的适应,这样来设置参数,接下来用到的就是像gluLookAt()这样的函数;
若是GL_TEXTURE,就是对纹理相关进行操作;
顺便说下,OpenGL里面的操作,很多是基于对矩阵的操作的,比如位移,旋转,缩放,所以,这里其实说的规范一点就是glMatrixMode是用来指定哪一个矩阵是当前矩阵,而它的参数代表要操作的目标,GL_PROJECTION是对投影矩阵操作,GL_MODELVIEW是对模型视景矩阵操作,GL_TEXTURE是对纹理矩阵进行随后的操作。
http://www.storkyak.com/2005/11/opengl-glprojection-and-glmodelview.html
Cameras are not the right way to think about OpenGL
Sigh, the tales of an old programmer. I'm teaching myself 3d graphics. My bag of 2d tricks has been used up, 3d has been around for almost 10 years in the mainstream, and it's time to move on.
To learn, I set out to make a little 3d representation of the mandelbrot set. I thought I could just whip that baby out in no time. But, as it turned out, I can't. I had to overcome some basic hurdles with Opengl.
A few things went through my head.
A lot of OpenGL tutorials talk about cameras, and yet, OpenGL doesn't have cameras. Instead, you get a bevy of matrixes and glMatrixModes. Computer science is full of horrific analogies, and I really think that "camera" metaphor people use to describe OpenGL is one of them. I ask myself, in my best Battlestar Galactica parlance, "what the frak going on!!!!"
As I was smashing my keyboard with my fist, causing my shepherd to hide under the bed, two things dawned on me. First is, thank God Logitech keyboards are indestructible. Second, all these matrixes do is move pixels around. There's nothing magic going on. The camera is a lie.
OpenGL is about a set of vertices, or points, in a 3d space. You draw them into 3d coordinates using any of various primitives, such as TRIANGLE_FAN, TRIANGLE_LIST, etc. OpenGL marches through each of these points and "transforms" them into a set of coordinates that look three-dish on screen.
Thus, when you specify various transformations, you are telling OpenGL how to move your points around. With that perspective, you can understand GL_PROJECTION and GL_MODELVIEW this way:
GL_PROJECTION is a matrix transformation that is applied to every point that comes after it, always. GL_MODELVIEW is a matrix transformation that is applied to every point in a particular model. There's a hierarchy of transformations, with GL_PROJECTION at the top, and a set of GL_MODEL branches. You set the matrix transformation with glMatrixMode. By default, the matrix mode is GL_MODELVIEW, which assumes everything you draw will be in one 3d space.
Let's talk about GL_PROJECTION first.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1.0, 1.0);
It's all about matrix math. I basically use the glOrtho to apply some perspective to what I'm doing. Everything in 3d graphics is about matrix math.
Once you set the matrix mode, each subsequent operation is applied to that particular matrix mode and below it. They almost should have called it matrix levels and not matrix modes. I load identity first to give it a place to start because most of the gl matrix commands work by multiplying themselves against whatever the matrix was for that mode before. For example:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1.0, 1.0);
glTranslate( 100, 100, 100 );
glRotateF( 45, 1, 0, 0 );
Really means: GL_PROJECTION_MATRIX = IDENTITY * ORTHOGRAPHIC_MATRIX * TRANSLATION_MATRIX * ROTATION_MATRIX.
The order of these calls seems extremely important.
So what's MODEL_VIEW all about? GL_PROJECTION transformations are always applied, so, we can use it to define a camera, set the perspective, among other things. What model view lets us to do is set up different measuring systems for vertices of different things. I select model view by calling
glMatrixMode(GL_MODELVIEW);
Then I can apply stuff to the model:
glLoadIdentity();
glTranslate( modelx, modely, modelz );
To move it in space somewhere, for example.
GL_MODELVIEW is about having different objects being pushed into a "world space". The biggest reason is that I can draw each object using coordinates based around 0, merely specifying the translations or rotations or scaling based on how I want my model to go.
To really appreciate this, let's consider a simple example. I want to draw a world that has two things in it, a car and a battleship. To do this, I might:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1.0, 1.0);
glTranslate( camerax, cameray, cameraz );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslate( carx, cary, carz );
// draw car here, by specifying various gl vertices
glLoadIdentity();
glTranslate( battleshipx, battleshipy, battleshipz );
// draw battleship here, by specifying various gl vertices
In that example, each car vertice I specify is being altered as follows:
car_vertice_matrix = projection_ortho_matrix * projection_translation * car_translation_matrix
and, for the battleship
battleship_vertice_matrix = projection_ortho_matrix * projection_translation * battleship_translation_matrix
So, to actually draw the stuff, OpenGl takes the projection matrix multiplied by the current model matrix and uses that to translate each specific vertice within that model. Wow, 3d graphics is not hard. Hell I should go write my own OpenGL. You know, if there wasn't the issue of hardware accelaration, it would be tempting. Well, I still got a ways to go.
I'll have my little multithreaded 3d demo up shortly. There's actually a bug in my thread pool that I will be posting a fix for as well.
I remember reading somewhere in a 3d graphics book that the glProjection matrix should not be used to fake a camera. I think that was an evil thing to remember. But, wow, it just seems like it would be such a good spot to do it. What I have to try is something like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1.0, 1.0);
glTranslate( camerax, cameray, cameraz );
glRotateF( cameraanglex, 1, 0, 0 );
In that case, the glTranslate would be where the camera is, and glRotateF, where it is facing.
Talking about cameras
Now, I know I said that the camera is a lie. It is, because its pretty obvious that we're really moving the entire world to get our picture, and not some "camera".