PDA

View Full Version : Transformations not working, newbie problem.



doorman
01-29-2011, 09:56 PM
Hi, I just started using OpenGL, and for some reason I can't get any of my transformations to work. The problem is probably something really simple I overlooked.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cstdlib>


void DrawWorld()

{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
float eye[] = {1,0,0};
float center[] = {0,0,0};
float up[] = {0,0,1};
gluLookAt(eye[0],eye[1],eye[2],center[0],center[1],center[2],up[0],up[1], up[2]);

glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_LINES);

glVertex3f(0,0,0);
glPushMatrix();
glTranslatef(0,0,1); // DOESN'T DO ANYTHING
glVertex3f(0,1,0);
glPopMatrix();
glEnd();
glutSwapBuffers();

}

void Idle()

{
glutPostRedisplay();
}

void keyhit(unsigned char key, int x, int y)

{
switch(key) {
case 'q': // This will quit the program
exit(0);
break;
}
}

int main(int argc, char **argv)

{
glutInit( &amp;argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "window" );
glutDisplayFunc( DrawWorld );
glutIdleFunc(Idle);
glutKeyboardFunc(keyhit);
glEnable(GL_DEPTH_TEST);
glutMainLoop();

return 0;

}

The line "glTranslatef(0,0,1);" doesn't seem to do anything. If I comment it out, the image doesn't change at all. The image remains a horizontal line from (0,0,0) to (0,1,0). But "glTranslatef(0,0,1);" should make it display a diagonal line from (0,0,0) to (0,1,1) right?

I've also tried glRotatef, nothing happens either, none of my transformations seem to be doing anything. Again, this is probably a very simple problem to fix, but I couldn't find any answers anywhere else.

Alfonse Reinheart
01-29-2011, 10:06 PM
The line "glTranslatef(0,0,1);" doesn't seem to do anything.

Of course it doesn't. You're not allowed to call those functions between glBegin and glEnd. The only functions you can call between those two are vertex attribute functions (glVertex, glColor, etc).

doorman
01-29-2011, 10:07 PM
I see, thank you!

MaxH
01-30-2011, 02:17 PM
There are different ways to solve your problem.
Start by taking the Push, Pop, and Translate out of the block of code between Begin and End.
Put the Push before Begin, and the Pop after End.
1) Try changing the coordinates of the 2nd Vertex to (0,1,1).
2) Or, put a glRotate after glPush and before glBegin.




glBegin(GL_LINES);
glVertex3f(0,0,0);
glPushMatrix();
glTranslatef(0,0,1);
glVertex3f(0,1,0);
glPopMatrix();
glEnd();