Thursday, November 22, 2007

PRACTICAL 5


#include "glut.h"

GLfloat angle= 0.0f;
GLfloat width= 40.0f;
GLfloat height= 30.0f;
int x=0;int y=0;

void MyDisplay()
{
glEnable(GL_DEPTH);
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); //glLoadIdentity();
glPopMatrix();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(400,0,0);
glVertex3f(0,0,0);
glVertex3f(0,400,0);
glVertex3f(0,0,0);
glVertex3f(0,0,400);
glEnd();

glPushMatrix();
glTranslatef(::x,y,0);

glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(0,20,0);
glVertex3f(20,20,0);
glVertex3f(20,0,0);
glEnd();

glColor3f(0,0,1);
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(0,0,20);
glVertex3f(0,20,20);
glVertex3f(0,20,0);
glEnd();

glColor3f(0,1,0);
glBegin(GL_QUADS);
glVertex3f(0,20,0);
glVertex3f(0,20,20);
glVertex3f(20,20,20);
glVertex3f(20,20,0);
glEnd();

glColor3f(1,1,0);
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(0,0,20);
glVertex3f(20,0,20);
glVertex3f(20,0,0);
glEnd();

glColor3f(0,1,1);
glBegin(GL_QUADS);
glVertex3f(20,0,0);
glVertex3f(20,0,20);
glVertex3f(20,20,20);
glVertex3f(20,20,0);
glEnd();

glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(0,0,20);
glVertex3f(0,20,20);
glVertex3f(20,20,20);
glVertex3f(20,0,20);
glEnd();

glutSwapBuffers();
} // Called by GLUT library when the window has changed size

void Rescale(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h);
}

void SetupRC()
{
GLfloat final = width / height;
gluPerspective(90, final , 0, 1000);
gluLookAt(90,90,90,0,1,0,0,1,0);
glFlush();
glPushMatrix();
}

void Keyboard(unsigned char key, int x, int y)
{
switch(key) //check which key is pressed
{
case 'a': ::x++; //add 1 to global x.
break;
case 'd': ::x--; //minus 1 to global x.
break;
case 'w': ::y++;
break;
case 's': ::y--;
break;
}

glutPostRedisplay();
}

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("My Practical 5");
glutDisplayFunc(MyDisplay);
glutReshapeFunc(Rescale);
glutKeyboardFunc(Keyboard);
SetupRC();
glutMainLoop();
}

No comments: