Following code is working. but few things bothering me.
- when I run the code, gluLookAt command has no effect.
- 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;
}