PDA

View Full Version : gluLootAt not working in this code..



kawaikx15
04-19-2014, 04:50 AM
Following code is working. but few things bothering me.
1. when I run the code, gluLookAt command has no effect.
2. z axis into the screen seems to be the positive.

please help to solve these two dilemmas


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

double rotate_y=0;
double rotate_x=0;

void reshape(int w, int h)
{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5,5,-5,5,-5,5);

glLoadIdentity();
gluLookAt(0.2,0,-2,0,-0.5,0,0,1,0);
}

void display(void)
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );


glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex3f(0.5,-0.5,-1);
glVertex3f(0.5,0.5,-1);
glVertex3f(-0.5,0.5,-1);
glVertex3f(-0.5,-0.5,-1);
glEnd();

glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex3f(0.5,-0.5,-0.8);
glVertex3f(0.5,0.5,-0.8);
glVertex3f(-0.5,0.5,-0.8);
glVertex3f(-0.5,-0.5,-0.8);
glEnd();

glColor3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(0.5,-0.5,-0.2);
glVertex3f(0.5,0.5,-0.2);
glVertex3f(-0.5,0.5,-0.2);
glVertex3f(-0.5,-0.5,-0.2);
glEnd();

glutSwapBuffers();


}

void specialKeys( int key, int x, int y ) {

// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;

// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;

else if (key == GLUT_KEY_UP)
rotate_x += 5;

else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;

// Request display update
glutPostRedisplay();

}

int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow ("animating triangles");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(specialKeys);
glutMainLoop ();
return 0;
}

reto.koradi
04-19-2014, 05:33 AM
On problem 1, you're missing the call to set the matrix mode back to MODELVIEW after setting up the projection. Try to add that line in your reshape() function:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5,5,-5,5,-5,5);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.2,0,-2,0,-0.5,0,0,1,0);


On problem 2, your parameters to gluLookAt() specify that you're looking from (0.2, 0.0, -2.0) towards (0.0, -0.5, 0.0). Which means that you're looking primarily in the positive z direction (plus somewhat in the negative x direction), resulting in your positive z axis pointing into the screen.

Aleksandar
04-19-2014, 05:52 AM
Of course, since it is called for the projection-matrix stack and after that annihilated by calling glLoadIdentity!
After setting the projection, you should revert to Model-View matrix stack. gluLookAt() should be called in display function, right after glLoadIdentity.

kawaikx15
04-25-2014, 07:19 PM
Thanks guys!

what I noticed is that glutlookAt and glrotate doesn't work together...

Aleksandar
04-26-2014, 06:19 AM
what I noticed is that glutlookAt and glrotate doesn't work together...
Wrong conclusion! gluLookAt() is a combination of rotate/translate operations.
Just use it as first operation (right after glLoadIdentity) in order to have a predictable result.