كود HTML:
/**********************************
Computer Graphics Lab
30/03/2010
Zaed Murad
unijust@unijust.com
***********************************/
#include <cmath>
#include <gl\glut.h>
int pts[5][3] = { {0,0,0},{4,0,0},{4,0,4},{0,0,4},{2,4,2}};
double r = 3;
double theta = 0.5;
int a = 2;
int b = 2;
double cx = a + r * cos(theta);
double cz = b + r * sin(theta);
double cy = 3.0;
void triangle(int v1,int v2,int v3)
{
glBegin(GL_TRIANGLES);
glVertex3iv(pts[v1]);
glVertex3iv(pts[v2]);
glVertex3iv(pts[v3]);
glEnd();
}
void quad(int v1,int v2,int v3,int v4)
{
glBegin(GL_QUADS);
glVertex3iv(pts[v1]);
glVertex3iv(pts[v2]);
glVertex3iv(pts[v3]);
glVertex3iv(pts[v4]);
glEnd();
}
void drawCube()
{
glColor3f(1,1,1);
quad(0,1,2,3);
}
void drawTriangle()
{
glColor3f(0,0,1);
triangle(0,1,4);
glColor3f(0,1,1);
triangle(3,2,4);
glColor3f(1,0,1);
triangle(2,1,4);
glColor3f(0,1,0);
triangle(0,3,4);
}
void init()
{
glLoadIdentity();
gluLookAt(cx,cy,cz,2,0,2,0,1,0); // y is the up vector
glMatrixMode(GL_PROJECTION);
glOrtho(-7,7,-7,7,-7,7);
}
void drawAxis()
{
glBegin(GL_LINES);
/******** X ********/
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(5,0,0);
/******** Y ********/
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(0,5,0);
/******** Z ********/
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,5);
glEnd();
}
void display()
{
glClearColor(0.5,0.5,0.5,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
drawAxis();
drawCube();
drawTriangle();
glFlush();
}
void key(unsigned char c,int x,int y)
{
switch(c)
{
case 'y':
case 'Y':
theta -=0.0174444;
if(theta < 0)
theta = 2 * (22/7.0);
break;
case 'x':
case 'X':
theta +=0.0174444;
if(theta > 2*(22/7))
theta = 0;
break;
}
cx = a + r * cos(theta);
cz = b + r * sin(theta);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cx,cy,cz,2,0,2,0,1,0); // y is the up vector
display();
}
void main(int c,char** v)
{
glutInit(&c,v);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("3D Project");
init();
glEnable(GL_DEPTH_TEST);
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutMainLoop();
}