// OPEN_GL_test.cpp : Defines the entry point for the console application. // // OPEN_GL_test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #define GL_PI 3.1415f GLfloat xRot = 0.45f; GLfloat yRot = 0.35f; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth= glIsEnabled(GL_DEPTH_TEST); GLboolean bOutline=(GLboolean)true; GLenum shademode = GL_FLAT; // Bright white light – full intensity RGB values GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat gray[] = { 0.75f, 0.75f, 0.75f, 1.0f }; GLfloat ex=0.0f; GLfloat ey=0.0f; GLfloat ez=-120.0f; GLfloat delta= 0.01f; GLfloat deltaR= 0.01f; /////////////////////////////////////////////////////////// void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Set drawing color to green glColor3f(0.0f, 1.0f, 0.0f); // Set color shading model to flat glShadeModel(shademode); // Clockwise-wound polygons are front facing; this is reversed // because we are using triangle fans glFrontFace(GL_CW); // Enable lighting glEnable(GL_LIGHTING); // Set light model to use ambient light specified by ambientLight[] glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight); // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Front material ambient and diffuse colors track glColor glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); } // Called to draw scene void RenderScene(void) { // Reset coordinate system glLoadIdentity(); gluLookAt (ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Turn culling on if flag is set if(bCull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); // Enable depth testing if flag is set if(bDepth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); // Draw the back side as a wireframe only, if flag is set if(bOutline)glPolygonMode(GL_BACK,GL_LINE); else glPolygonMode(GL_BACK,GL_FILL); // Save matrix state and do the rotation glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); // Begin a triangle fan (far part of plane body) glBegin(GL_TRIANGLE_FAN); // Pinnacle of cone is shared vertex for fan, moved up z-axis // to produce a cone instead of a circle glVertex3f(0.0f, 0.0f, 25.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(5.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.0f, 3.0f); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-5.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(5.0f, 0.0f); glEnd(); glBegin(GL_TRIANGLE_FAN); // Begin a new triangle fan (close part of plane body) glVertex3f(0.0f, 0.0f, -6.0f); glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(-5.0f, 0.0f); glColor3f(1.0f, 0.0f, 1.0f); glVertex2f(0.0f, 3.0f); glColor3f(0.0f, 1.0f, 1.0f); glVertex2f(5.0f, 0.0f); glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(-5.0f, 0.0f); glEnd(); // Done drawing the fan (close part of plane body) glBegin(GL_TRIANGLES); // horiz tail glColor3f(0.5f, 1.0f, 1.0f); glVertex3f(-8.0f, 0.0f, 30.0); // V0 glVertex3f( 8.0f, 0.0f, 30.0); // V1 glVertex3f( 0.0f, 0.0f, 23.0); // V2 // vertic tail glColor3f(1.0f, 0.5f, 1.0f); glVertex3f( 0.0f, 0.0f, 30.0); // V0 glVertex3f( 0.0f, 8.0f, 30.0); // V1 glVertex3f( 0.0f, 0.0f, 23.0); // V2 //wing glColor3f(0.5f, 1.0f, 1.0f); glVertex3f(-20.0f, 1.0f, 10.0); // V0 glVertex3f( 20.0f, 1.0f, 10.0); // V1 glVertex3f( 0.0f, 1.0f, 5.0); // V2 glEnd(); // Restore transformations glPopMatrix(); // Flush drawing commands glutSwapBuffers(); } // Called by GLUT library when the window has changed size void ChangeSize(GLsizei w, GLsizei h) { // Set Viewport to window dimensions glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glOrtho (-100.0, 100.0, -100, 100, -270.0, 270.0); //glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); gluPerspective(60.0, 1.0, 1.5, 200.0); glMatrixMode (GL_MODELVIEW); } void SpecialKeys(int key, int x, int y) {GLfloat dx, dy, dz; if(key == GLUT_KEY_UP) {//increase distance from camera to origin ex*=(1.0f+deltaR); ey*=(1.0f+deltaR); ez*=(1.0f+deltaR); } if(key == GLUT_KEY_DOWN) {//reduce distance from camera to origin (close up) ex*=(1.0f-deltaR); ey*=(1.0f-deltaR); ez*=(1.0f-deltaR);} if(key == GLUT_KEY_LEFT) //Rotate camera around origin in Oxz plane {dx=-ez; dz= ex; GLfloat s=sqrtf(ex*ex+ey*ey+ez*ez); ex+=delta*dx; ez+=delta*dz; GLfloat s1=sqrtf(ex*ex+ey*ey+ez*ez)/s; ex/=s1; ey/=s1; ey/=s1; } if(key == GLUT_KEY_RIGHT) //Rotate camera around origin in Oxz plane {dx=-ez; dz= ex; GLfloat s=sqrtf(ex*ex+ey*ey+ez*ez); ex-=delta*dx; ez-=delta*dz; GLfloat s1=sqrtf(ex*ex+ey*ey+ez*ez)/s; ex/=s1; ey/=s1; ey/=s1;} if(key == GLUT_KEY_F5)//Rotate camera around origin in Oyz plane {dy=-ez; dz= ey; GLfloat s=sqrtf(ex*ex+ey*ey+ez*ez); ey-=delta*dy; ez-=delta*dz; GLfloat s1=sqrtf(ex*ex+ey*ey+ez*ez)/s; ex/=s1; ey/=s1; ey/=s1;} if(key == GLUT_KEY_F6)//Rotate camera around origin in Oyz plane {dy=-ez; dz= ey; GLfloat s=sqrtf(ex*ex+ey*ey+ez*ez); ey+=delta*dy; ez+=delta*dz; GLfloat s1=sqrtf(ex*ex+ey*ey+ez*ez)/s; ex/=s1; ey/=s1; ey/=s1;} if(key == GLUT_KEY_F1) bCull=!bCull; if(key == GLUT_KEY_F2)bDepth=!bDepth; if(key == GLUT_KEY_F3)bOutline=!bOutline; if(key == GLUT_KEY_F4) { if(shademode == GL_FLAT){shademode =GL_SMOOTH;} else {if(shademode == GL_SMOOTH){shademode =GL_FLAT;}}; glShadeModel(shademode); } // Refresh the Window glutPostRedisplay(); // Refresh the Window glutPostRedisplay(); } /////////////////////////////////////////////////////////// int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (200, 200); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutSpecialFunc (SpecialKeys); SetupRC(); glutMainLoop(); return 0; }