PDA

View Full Version : ortho2d + glTranslatef == ... no translation?



vertex3f
02-04-2010, 05:48 PM
Am I doing something glaringly stupid here?

I set up an Ortho2D this way:



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho2d(0.0, 500., 0.0, 500.);



Then do my display this way:


void paint( ){

float matrix[16];

glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glBegin(GL_LINES);
for( int i = 0; i < 5; i++ ) {
cout << " ****************** " << i << " ******************** \n";
cout.flush();
glGetFloatv(GL_MODELVIEW_MATRIX, (float*)matrix);
printMatrix(matrix);
glVertex2i(0, 0);
glVertex2i(50, 50);
glTranslatef(10,10,0);
}
glEnd();
glPopMatrix();
glFinish();

}





void printMatrix(float* f) {

for (int i = 0; i < 4; i++) {
cout << " | " << f[i] << "\t" << f[i+4] << "\t"
<< f[i+8] << "\t" << f[i+12] << " |\n";
cout.flush();
}

cout << "\n\n\n";
cout.flush();
}





Gives me the following as output:


****************** 0 ********************
| 1.4013e-45 4.70198e-37 -2.76267e-05 4.70198e-37 |
| -1.59186 4.70198e-37 -1.59186 5.09673e-34 |
| -2.92047e-05 5.09673e-34 -2.74551e-05 1.4013e-45 |
| -2.97713e-05 0 3.99481e-34 -2.76267e-05 |



****************** 1 ********************
| 1.4013e-45 4.70198e-37 -2.76267e-05 4.70198e-37 |
| -1.59186 4.70198e-37 -1.59186 5.09673e-34 |
| -2.92047e-05 5.09673e-34 -2.74551e-05 1.4013e-45 |
| -2.97713e-05 0 3.99481e-34 -2.76267e-05 |



****************** 2 ********************
| 1.4013e-45 4.70198e-37 -2.76267e-05 4.70198e-37 |
| -1.59186 4.70198e-37 -1.59186 5.09673e-34 |
| -2.92047e-05 5.09673e-34 -2.74551e-05 1.4013e-45 |
| -2.97713e-05 0 3.99481e-34 -2.76267e-05 |



****************** 3 ********************
| 1.4013e-45 4.70198e-37 -2.76267e-05 4.70198e-37 |
| -1.59186 4.70198e-37 -1.59186 5.09673e-34 |
| -2.92047e-05 5.09673e-34 -2.74551e-05 1.4013e-45 |
| -2.97713e-05 0 3.99481e-34 -2.76267e-05 |



****************** 4 ********************
| 1.4013e-45 4.70198e-37 -2.76267e-05 4.70198e-37 |
| -1.59186 4.70198e-37 -1.59186 5.09673e-34 |
| -2.92047e-05 5.09673e-34 -2.74551e-05 1.4013e-45 |
| -2.97713e-05 0 3.99481e-34 -2.76267e-05 |






All I'm seeing is one line, and the printouts of the Modelview are all the same. Is it something I'm doing, or does glTransaltef not get along well with Ortho2D?

marshats
02-04-2010, 09:14 PM
Is it something I'm doing, or does glTransaltef not get along well with Ortho2D?


It is not something you are doing but where you are doing it. BTW it has nothing to do with orth2d and glTranslatef "getting along" with one another.

From glBegin/glEnd (http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml)



Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.


So your setting of the MODELVIEW matrix is being ignored. To be specific glGetFloatv and glTranslate are both being ignored since they are inside the glBegin/glEnd block of code.

I don't quite understand what the goal of you code is but the following should at least show the MV matrix changing:


void paint( ){

float matrix[16];

glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
for( int i = 0; i < 5; i++ ) {
cout << " ****************** " << i << " ******************** \n";
cout.flush();
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
printMatrix(matrix);
glBegin(GL_LINES);
glVertex2i(0, 0);
glVertex2i(50, 50);
glEnd();
glTranslatef(10,5,0); // changed to 5 so consecutive lines do not fall on one another
}
glPopMatrix();
glFinish();

}

vertex3f
02-04-2010, 09:26 PM
Well I was trying to draw a line, translate, draw a line, translate, then when that wasn't working, I started trying to print the matrix - when that didn't work I was getting confused.. I forgot completely about the issues with what you could and couldn't do within glBegin / glEnd.. bah!