#include #include #include #include #include using namespace std; GLfloat Eye_X, Eye_Y, Eye_Z; int Draw_State; void DisplayString(void * font, string s) { int i; for(i=0; i < s.size(); i++) { glutStrokeCharacter(font, s[i]); } return; } void Cube() { glBegin(GL_QUADS); // front face glVertex3f(-0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); // Back face glVertex3f(-0.5, -0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); // left face glVertex3f(-0.5, -0.5, -0.5); glVertex3f(-0.5, -0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, -0.5); // right face glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(0.5, 0.5, 0.5); glVertex3f(0.5, 0.5, -0.5); // top face glVertex3f(-0.5, 0.5, -0.5); glVertex3f(0.5, 0.5, -0.5); glVertex3f(0.5, 0.5, 0.5); glVertex3f(-0.5, 0.5, 0.5); // bottom face glVertex3f(-0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, -0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glEnd(); } void DrawChar(void * font, char c, GLfloat xs, GLfloat ys, GLfloat zs, GLfloat xr, GLfloat yr, GLfloat zr) { GLfloat width, height, big, scale; width = glutStrokeWidth(font,c); big = width; height = glutStrokeHeight(font); if (height > big) { big = height; } scale = 0.7 / big; glPushMatrix(); glTranslatef(xs,ys,zs); glRotatef(xr, 1, 0, 0); glRotatef(yr, 0, 1, 0); glRotatef(zr, 0, 0, 1); glScalef(scale, scale, scale); glTranslatef(-width/2.0,-height/2.0,0); glutStrokeCharacter(font, c); glPopMatrix(); return; } void Die() { void * fontID = GLUT_STROKE_ROMAN; Cube(); if (Draw_State > 1) { // draw the numbers: DrawChar(GLUT_STROKE_ROMAN,'1', 0.0, 0.0, 0.5, 0, 0, 0); DrawChar(GLUT_STROKE_ROMAN,'2', 0.5, 0.0, 0.0, 0, 90, 0); DrawChar(GLUT_STROKE_ROMAN,'3', 0.0, 0.5, 0.0,-90, 0, 0); DrawChar(GLUT_STROKE_ROMAN,'4', 0.0,-0.5, 0.0, 90, 0, 0); DrawChar(GLUT_STROKE_ROMAN,'5', -0.5, 0.0, 0.0, 0, -90, 0); DrawChar(GLUT_STROKE_ROMAN,'6', 0.0, 0.0, -0.5, 0, 180, 0); } } void Axis() { glColor3f(0,0,0) ; glBegin(GL_LINES) ; glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(0,0,0); glVertex3f(0,1,0); glVertex3f(0,0,0); glVertex3f(0,0,1); glEnd(); DrawChar(GLUT_STROKE_ROMAN, 'x',1,0,0, 0,0,0); DrawChar(GLUT_STROKE_ROMAN, 'y',-0.25,1,0, 0,0,0); DrawChar(GLUT_STROKE_ROMAN, 'z',0,0,1, 0,0,0); return; } void display() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); if (Draw_State > 4) { gluLookAt(Eye_X,Eye_Y,Eye_Z, 2.5, 1.0 , -3.5, 0, 1, 0); } else { gluLookAt(Eye_X,Eye_Y,Eye_Z,0,0,0,0,1,0); } if (Draw_State < 5) { Axis(); } glColor3f(1,0,0); if (Draw_State > 0) { if (Draw_State > 2) { glPushMatrix(); if (Draw_State > 3) { glTranslatef(3, 0.5, -4); } glRotatef(-90,1,0,0); } Die(); if (Draw_State > 2) { glPopMatrix(); } } glColor3f(0,1,1); if (Draw_State > 4) { glPushMatrix(); glTranslatef(2, 0.5, -3); glRotatef(180,0,0,1); glRotatef(45,0,1,0); Die(); glPopMatrix(); } glFlush(); return; } void reset() { Eye_X = 0; Eye_Y = 0; Eye_Z = 2.2; Draw_State = 0; return; } void setup() { reset(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glClearColor(1,1,1,0); return; } void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.5,1.5,-1.5,1.5,1,10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return; } void keypress (unsigned char key, int x, int y) { GLfloat cameraMove = 0.25; switch(key) { case '+': Draw_State ++; break; case '-': Draw_State--; break; case '8': Eye_Y += cameraMove; break; case '2': Eye_Y -= cameraMove; break; case '6': Eye_X += cameraMove; break; case '4': Eye_X -= cameraMove; break; case '1': Eye_Z += cameraMove; break; case '3': Eye_Z -= cameraMove; break; case 'r': reset(); break; case 'q': exit(0); } glutPostRedisplay(); return; } int main(int argc, char * argv[]) { reset(); glutInit(&argc, argv); glutInitContextVersion(3, 0); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutCreateWindow("A Demo"); glutDisplayFunc(display); glutReshapeFunc(resize); glutKeyboardFunc(keypress); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }