#include #include /* Pi konstans kozelito erteke. */ #define GL_PI 3.1415926535f #define TRUE 1 #define FALSE 0 // Rotation amounts static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; // Flags for effects static int bCull = FALSE; static int bOutline = FALSE; static int bDepth = FALSE; static int shadeModel = 1; static int displayMode = 1; /////////////////////////////////////////////////////////////////////////////// // Reset flags as appropriate in response to menu selections void ProcessMenu( int value ) { switch( value ) { case 1: displayMode = 1; break; case 2: displayMode = 2; break; case 3: displayMode = 3; break; case 4: displayMode = 4; break; case 5: displayMode = 5; break; } glutPostRedisplay(); } void RenderCone() { GLfloat x, y, angle; int i; // Clock wise wound polygons are front facing, this is reversed // because we are using triangle fans glFrontFace( GL_CW ); // Begin a triangle fan 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, 75.0f ); // Loop around in a circle and specify even points along the circle // as the vertices of the triangle fan for( i = 0, angle = 0.0f; i <= 16; i++, angle += ( GL_PI / 8.0f ) ) { // Calculate x and y position of the next vertex x = 50.0f * cos( angle ); y = 50.0f * sin( angle ); // Specify the next vertex for the triangle fan glVertex2f( x, y ); } // Done drawing fan for cone glEnd(); glFrontFace(GL_CCW); // Begin a new triangle fan to cover the bottom glBegin(GL_TRIANGLE_FAN); // Center of fan is at the origin glVertex2f( 0.0f, 0.0f ); for( i = 0, angle = 0.0f; i <= 16; i++, angle += ( GL_PI / 8.0f ) ) { // Calculate x and y position of the next vertex x = 50.0f * cos(angle); y = 50.0f * sin(angle); // Specify the next vertex for the triangle fan glVertex2f( x, y ); } // Done drawing the fan that covers the bottom glEnd(); } // Called to draw scene void RenderScene(void) { // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save matrix state and do the rotation glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); if( ( displayMode == 3 ) || ( displayMode == 4 ) ) { glColor3f( 0.7, 0.7, 0.7 ); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); RenderCone(); } glColor3f( 0.0, 0.0, 0.0 ); if( displayMode == 1 ) { glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); RenderCone(); } if( ( displayMode == 2 ) || ( displayMode == 4 ) ) { glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glCullFace( GL_BACK ); glEnable( GL_CULL_FACE ); RenderCone(); glDisable( GL_CULL_FACE ); } if( displayMode == 5 ) { glPointSize( 5.0 ); glPolygonMode( GL_FRONT_AND_BACK, GL_POINT ); RenderCone(); } // Restore transformations glPopMatrix(); // Flush drawing commands glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // White background glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); // Set color shading model to flat glShadeModel( GL_FLAT ); // Avoid flickering wire model glEnable( GL_POLYGON_OFFSET_FILL ); glPolygonOffset( 1.0, 2.0 ); } void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot -= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(xRot > 356.0f) xRot = 0.0f; if(xRot < -1.0f) xRot = 355.0f; if(yRot > 356.0f) yRot = 0.0f; if(yRot < -1.0f) yRot = 355.0f; // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { GLfloat nRange = 100.0f; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset projection matrix stack glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char* argv[]) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutCreateWindow( "Kup modellezese 2" ); // Create the Menu glutCreateMenu( ProcessMenu ); glutAddMenuEntry( "Drotvazas megjelenites", 1 ); glutAddMenuEntry( "Drotvazas csak elolapokkal", 2 ); glutAddMenuEntry( "Kitoltott megjelenites", 3 ); glutAddMenuEntry( "Mindketto egyszerre", 4 ); glutAddMenuEntry( "Csak csúcspontok", 5 ); glutAttachMenu( GLUT_RIGHT_BUTTON ); glutReshapeFunc( ChangeSize ); glutSpecialFunc( SpecialKeys ); glutDisplayFunc( RenderScene ); SetupRC(); glutMainLoop(); return 0; }