PDA

View Full Version : Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);



openglfirst
05-01-2012, 10:19 PM
What's the difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);



#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);

int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPT H );
glutInitWindowSize(600,400);
glutCreateWindow("Opengl Test");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void display() {
float x,y,z;
int i;
x=0;
y=-0.8;
z=0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,0);
glBegin(GL_POINTS);
for(i=0;i<98;i++) {
glVertex3f(x,y,z);
x=x+0.01;
}
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int mousePositionX, int mousePositionY) {
switch ( key ) {
case KEY_ESCAPE:
exit ( 0 );
break;
default:
break;
}
}




#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);

int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPT H );
glutInitWindowSize(600,400);
glutCreateWindow("Opengl Test");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void display() {
float x,y,z;
int i;
x=0;
y=-0.8;
z=0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,0);
glBegin(GL_POINTS);
for(i=0;i<98;i++) {
glVertex3f(x,y,z);
x=x+0.01;
}
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int mousePositionX, int mousePositionY) {
switch ( key ) {
case KEY_ESCAPE:
exit ( 0 );
break;
default:
break;
}
}


For both codes, i get same result.
Can anyone show the difference of these glMatrixMode(GL_MODELVIEW); and glMatrixMode(GL_PROJECTION); changing this code?

WIld Sage
05-01-2012, 10:58 PM
As far as I know, the only difference between the two matrix modes are the order in which they're multiplied (which does make a difference). Other than that, there should be no difference between the two.

openglfirst
05-01-2012, 11:06 PM
As far as I know, the only difference between the two matrix modes are the order in which they're multiplied (which does make a difference). Other than that, there should be no difference between the two.
How can i multiply these two matrices using opengl and get the resulted matrix?

WIld Sage
05-01-2012, 11:35 PM
Not sure, sorry. I try to use core profile code as much as possible so I use GLM (http://glm.g-truc.net/) to handle my matrices, as well as a lot of really useful 3d math functions.

thokra
05-02-2012, 01:22 AM
The fixed-function pipeline maintains multiple matrix stacks. Among them are the ModelView and the Projection stack. The difference between glMatrixMode(GL_MODELVIEW) and glMatrixMode(GL_PROJECTION) is simply that you're operating on either the ModelView or the Projection stack, depending on which of the above function calls was made. In general, it is NOT advisable to manipulate the projection matrix stack if you don't intend to really change the projection matrix. Most of the the time you'll want to modify the modelview stack.

I can't tell why you get the same results in your example as I didn't look at the math involved. Still, it should be merely coincidence and you CANNOT rely that!

Edit: BTW, if you say you're using core profile code as much as possible, you might have a certain misunderstanding what "core profile" means - what it definitely doesn't involve is using ANY fixed functionality like the matrix stacks and matrix manipulation functions.

menzel
05-02-2012, 01:55 AM
In the fixed function pipeline (note: outdated) all your vertices are projected like this:

P_transformed = M_projection * M_modelview * P;

M_projection is set by matrix manipulating calls in matrixmode GL_PROJECTION (e.g. LoadIdentity, transform, etc.), M_modelview in matrixmode GL_MODELVIEW. Default is the identity matrix. That's why you don't see a difference now. You would see errors in your lighting when you use the matices wrong.

Are you sure, you really want to learn the fixed function pipeline?