• Advertisement
Sign in to follow this  

Problems with glLoadIdentity()

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

Hey guys!, im new too OpenGl, im trying something really simple is to draw 2 figures, the problems comes when i want to draw the second one, i wrote a glLoadIdentity(), to go back to the previous matrix and make a traslation and draw the square(like its explained in the tutorials), but that doenst happen, why??i also tried to do a function to capture the mouseclick, it works cause while debugging in went through the function but it didnt draw anything, here´s the code, if u can help me ill appreciate it It also doenst paint any color, only boring grey!! #include <windows.h> #include <GL/glut.h> #include <stdio.h> // Para cada objeto que vamos a representar //necesitamos una Matrix, que va a //va a reprensetar los puntos x,y,z que ocupa float ang = 0.0f; int max = 64; float m[16]; void mouse(int btn, int state, int x, int y){ if((btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)){ glBegin(GL_TRIANGLES); glColor3f(2.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); } } void draw(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //Carga la matriz identidad gluLookAt(0.0f, 0.0f, 4.0f, //eye 0.0f, 0.0f, 0.0f, //center 0.0f, 1.0f, 0.0f);//up //glLoadIdentity(); //Carga la matriz identidad glTranslatef(-1.5f,0.0f,0.0f); glRotatef(ang,1.0f,0.0f,0.0f); // Rotate The glColor3f(8.0f,0.0f,0.0f); glBegin(GL_TRIANGLES); glColor3f(0.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); // Top glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glEnd(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //porque no funciona??? glTranslatef(1.5f,0.0f,0.0f); // Move Right 3 Units glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); glFlush(); // Asegura que los comandos anteriores se vana ejecutar ang+=0.2f; } void display(void) { int i; GLenum err; glClear(GL_ACCUM_BUFFER_BIT); //Vacia ese buffer, porque es posible que tenga residuos for (i=0; i<max; i++) { draw(); glAccum(GL_ACCUM, 1.f/max); } glAccum(GL_RETURN, 1.f); glutSwapBuffers(); } void reshape(int w, int h) { glMatrixMode(GL_PROJECTION); //Con esta función se elige en que matriz vamos a realizar los cambios glLoadIdentity(); gluPerspective(55.0f, (float)w/(float)h, 0.3f, 5.0f); glViewport(0,0,w,h); glMatrixMode(GL_MODELVIEW); } void idle(void) { glutPostRedisplay(); } void init(void) { glReadBuffer( GL_BACK); glDrawBuffer( GL_BACK); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearAccum(0.0f, 0.0f, 0.0f, 1.0f); glClearDepth(1.0f); } int main(int argc, char *argv[]) { // inizializzo la libreria glutInit(&argc, argv); // definisco le proprieta della finestra e del display glutInitWindowPosition(16, 16); glutInitWindowSize(512, 512); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_ACCUM); // creo la fienstra glutCreateWindow("OpenGL Test"); // registro le callback glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); init(); glutMouseFunc (mouse); //estoy probando con el ratón // loop infinito glutMainLoop(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
glLoadIdentity doesn't undo the last matrix transformation, it undos all previously made transformations, which in your case includes the call to gluLookAt. What you want is glPushMatrix and glPopMatrix. They are used to save and restore transformations.

Share this post


Link to post
Share on other sites
HEy thank very much, its working can u tell me somthing about the mouse function, why it doenst work??

Thanks

Share this post


Link to post
Share on other sites
you don't want to use glLoadIdentity() here, it is glPushMatrix() and glPopMatrix() you want.

here's part of your example, using glPushMatrix() and glPopMatrix()

//glLoadIdentity(); //Carga la matriz identidad

glPushMatrix(); // Stores the current matrix on the stack


glTranslatef(-1.5f,0.0f,0.0f);
glRotatef(ang,1.0f,0.0f,0.0f); // Rotate The glColor3f(8.0f,0.0f,0.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
glMatrixMode(GL_MODELVIEW);


glPopMatrix(); // Restores the previous matrix from the stack

//glLoadIdentity(); //porque no funciona???

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement