קוד:
#include <GL/glut.h>
GLfloat vertices[][3] = {{2.0,2.0,-2.},{-2.,2.,-2.},{2.,2.,2.},
{-2.,2.,2},{2.,-2.,-2},{-2.,-2.,-2.},{2.,-2.,2.},{-2.,-2.,2},{0.5,4.,-0.5},{-0.5,4.,-0.5},
{-0.5,4.,0.5},{0.5,4.,0.5},{-0.5,2.,-0.5},{0.5,2.,-0.5},{-0.5,2.,0.5},{0.5,2.,0.5},{0.,-5.,0.}};
int direction = 1;
void polygon(int a, int b, int c , int d)
{
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}
void make_cube()
{
glColor3f(0.,0.,1.);
polygon(0,1,3,2);
glColor3f(0.,1.,1.);
polygon(1,3,7,5);
glColor3f(1.,0.,1.);
polygon(5,7,6,4);
glColor3f(0.,0.,1.);
polygon(2,6,4,0);
glColor3f(1.,1.,0.);
polygon(1,5,4,0);
glColor3f(1.,0.,0.);
polygon(3,7,6,2);
glColor3f(0.,0.,1.);
polygon(8,9,10,11);
glColor3f(0.,0.,1.);
polygon(9,10,14,12);
glColor3f(0.,0.,1.);
polygon(12,14,15,13);
glColor3f(0.,0.,1.);
polygon(8,11,15,13);
glColor3f(0.,0.,1.);
polygon(8,9,12,13);
glColor3f(0.,0.,1.);
polygon(11,10,14,15);
glColor3f(1.,0.,1.);
polygon(4,5,16,16);
glColor3f(1.,1.,0.);
polygon(5,7,16,16);
glColor3f(0.,1.,1.);
polygon(7,6,16,16);
glColor3f(1.,1.,1.);
polygon(6,4,16,16);
}
void disp_cube(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
make_cube();
glutSwapBuffers (); // Swapping buffers
glFlush();
}
void rotate(void)
{
// rotating the cube while Idle
glRotatef(2.*direction, 0., 1., 0.);
disp_cube();
}
void direct(int button, int state, int x, int y)
{
// changing the direction of the rotation according to
// the mouse button have been pressed
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
direction = -1;
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
direction = 1;
}
void initial(void)
{
// initialization
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-8., 8., -8., 8., -8., 8.);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
// We include new option: GLUT_DOUBLE for double buffering
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(20, 20);
glutInitWindowSize(400, 400);
glutCreateWindow("Dradle-Ohad Israeli");
glutDisplayFunc(disp_cube); // display callback function
glutIdleFunc(rotate); // idle callback function
glutMouseFunc(direct); // mouse callback function
initial();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}