#include #include #include #include // Define a constant for the value of PI #define GL_PI 3.1415f static int object = 0; static int type = 0; static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; GLdouble sphere_radius = 10.0; GLint sphere_slices = 20; GLint sphere_stacks = 20; GLdouble cube_size = 20.0; GLdouble cone_base = 10.0; GLdouble cone_height = 20.0; GLint cone_slices = 20; GLint cone_stacks = 20; GLdouble torus_inner_radius = 4.0; GLdouble torus_outer_radius = 10.0; GLint torus_nsides = 20; GLint torus_rings = 20; GLdouble teapot_size = 10.0; char code_string[256]; void BitmapText(GLfloat x, GLfloat y, char *string) { int len, i; glRasterPos2f(x, y); len = (int) strlen (string); for (i = 0; i < len; i++){ glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, string[i]); } } void drawGLUTObject(int which, int how) { if (how) { switch(which) { case 0: glutSolidSphere(sphere_radius, sphere_slices, sphere_stacks); break; case 1: glutSolidCube(cube_size); break; case 2: glutSolidCone(cone_base, cone_height, cone_slices, cone_stacks); break; case 3: glutSolidTorus(torus_inner_radius, torus_outer_radius, torus_nsides, torus_rings); break; case 4: glutSolidDodecahedron(); break; case 5: glutSolidOctahedron(); break; case 6: glutSolidTetrahedron(); break; case 7: glutSolidIcosahedron(); break; case 8: glutSolidTeapot(teapot_size); break; } } else { switch(which) { case 0: glutWireSphere(sphere_radius, sphere_slices, sphere_stacks); break; case 1: glutWireCube(cube_size); break; case 2: glutWireCone(cone_base, cone_height, cone_slices, cone_stacks); break; case 3: glutWireTorus(torus_inner_radius, torus_outer_radius, torus_nsides, torus_rings); break; case 4: glutWireDodecahedron(); break; case 5: glutWireOctahedron(); break; case 6: glutWireTetrahedron(); break; case 7: glutWireIcosahedron(); break; case 8: glutWireTeapot(teapot_size); break; } } } void drawGLUTObjectText(int which, int how) { glColor3f(1.0, 1.0, 1.0); if (how) { switch(which) { case 0: sprintf(code_string, "glutSolidSphere(%.1lf, %d, %d);", sphere_radius, sphere_slices, sphere_stacks); break; case 1: sprintf(code_string, "glutSolidCube(%.1lf);", cube_size); break; case 2: sprintf(code_string, "glutSolidCone(%.1lf, %.1lf, %d, %d);", cone_base, cone_height, cone_slices, cone_stacks); break; case 3: sprintf(code_string, "glutSolidTorus(%.1lf, %.1lf, %d, %d);", torus_inner_radius, torus_outer_radius, torus_nsides, torus_rings); break; case 4: sprintf(code_string, "glutSolidDodecahedron();"); break; case 5: sprintf(code_string, "glutSolidOctahedron();"); break; case 6: sprintf(code_string, "glutSolidTetrahedron();"); break; case 7: sprintf(code_string, "glutSolidIcosahedron();"); break; case 8: sprintf(code_string, "glutSolidTeapot(%.1lf);", teapot_size); break; } BitmapText(-20.0f, -20.0f, code_string); } else { switch(which) { case 0: sprintf(code_string, "glutWireSphere(%.1lf, %d, %d);", sphere_radius, sphere_slices, sphere_stacks); break; case 1: sprintf(code_string, "glutWireCube(%.1lf);", cube_size); break; case 2: sprintf(code_string, "glutWireCone(%.1lf, %.1lf, %d, %d);", cone_base, cone_height, cone_slices, cone_stacks); break; case 3: sprintf(code_string, "glutWireTorus(%.1lf, %.1lf, %d, %d);", torus_inner_radius, torus_outer_radius, torus_nsides, torus_rings); break; case 4: sprintf(code_string, "glutWireDodecahedron();"); break; case 5: sprintf(code_string, "glutWireOctahedron();"); break; case 6: sprintf(code_string, "glutWireTetrahedron();"); break; case 7: sprintf(code_string, "glutWireIcosahedron();"); break; case 8: sprintf(code_string, "glutWireTeapot(%.1lf);", teapot_size); break; } BitmapText(-20.0f, -20.0f, code_string); } } // Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0f, 0.0f, -50.0f); glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINES); glVertex3f(-30.0, 0.0, 0.0); glVertex3f(30.0, 0.0, 0.0); glVertex3f(0.0, -30.0, 0.0); glVertex3f(0.0, 30.0, 0.0); glVertex3f(0.0, 0.0, -30.0); glVertex3f(0.0, 0.0, 30.0); glEnd(); if (object%9==4 || object%9==6) glScalef(10.0f, 10.0f, 10.0f); if (object%9==5 || object%9==7) glScalef(15.0f, 15.0f, 15.0f); glColor3f(0.2, 0.8, 0.7); drawGLUTObject(object%9, type%2); glPopMatrix(); drawGLUTObjectText(object % 9, type % 2 ); glPopMatrix(); // Flush drawing commands glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); } void SpecialKeys(int key, int x, int y) { int which; int state; which = object % 9; state=glutGetModifiers(); if (!(state & GLUT_ACTIVE_SHIFT)) { if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(key == GLUT_KEY_UP) xRot -= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 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; } else { if(key == GLUT_KEY_LEFT) object -= 1; if(key == GLUT_KEY_RIGHT) object += 1; if(key == GLUT_KEY_UP) type += 1; if(key == GLUT_KEY_DOWN) type -= 1; } if(object < 0) object = 0; if(type < 0) type = 0; if((key == GLUT_KEY_F1) && (state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); sphere_radius -= 1.0; break; case 1: //glutSolidCube(20.0); cube_size -= 1.0; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_base -= 1.0; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_inner_radius -= 1.0; break; case 8: //glutSolidTeapot(10.0); teapot_size -= 1.0; break; } } if((key == GLUT_KEY_F1) && !(state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); sphere_radius += 1.0; break; case 1: //glutSolidCube(20.0); cube_size += 1.0; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_base += 1.0; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_inner_radius += 1.0; break; case 8: //glutSolidTeapot(10.0); teapot_size += 1.0; break; } } if((key == GLUT_KEY_F2) && (state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); if(sphere_slices > 1) sphere_slices --; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_height -= 1.0; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_outer_radius -= 1.0; break; } } if((key == GLUT_KEY_F2) && !(state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); sphere_slices ++; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_height += 1.0; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_outer_radius += 1.0; break; } } if((key == GLUT_KEY_F3) && (state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); if(sphere_stacks > 1) sphere_stacks --; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); if(cone_slices > 1) cone_slices --; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); if(torus_nsides > 1) torus_nsides --; break; } } if((key == GLUT_KEY_F3) && !(state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 0: //glutSolidSphere(10.0, 20, 20); sphere_stacks ++; break; case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_slices ++; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_nsides ++; break; } } if((key == GLUT_KEY_F4) && (state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 2: //glutSolidCone(10.0, 20.0, 20, 20); if(cone_stacks > 1) cone_stacks --; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); if(torus_rings > 1) torus_rings --; break; } } if((key == GLUT_KEY_F4) && !(state & GLUT_ACTIVE_SHIFT)) { switch(which) { case 2: //glutSolidCone(10.0, 20.0, 20, 20); cone_stacks ++; break; case 3: //glutSolidTorus(4.0, 10.0, 20, 20); torus_rings ++; break; } } // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { GLfloat fAspect; GLfloat zNear = 1.0f; GLfloat zFar = 400.0f; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Determine the aspect ratio fAspect=(GLfloat)w/(GLfloat)h; // Reset projection matrix stack glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Establish clipping volume (field of view angle, aspect ratio, distance from the viewer to the near clipping plane, distance from the viewer to the far clipping plane)*/ gluPerspective(45.0f, fAspect, zNear, zFar); // 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("GLUT magasabbszintu objektumok"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }