Carousel in OpenGL

Source Code in C


1: #include <windows.h> // – Windows API and system calls

 2: #include <stdio.h>     // - Just for some ASCII messages
 3: #include <gl/gl.h>     // - The OpenGL API 
 4: #include <gl/glaux.h>  // - A windows library extension API 
 5: #include <gl/glut.h>   // - An interface and windows 
 6:                        // management library
 7: #include "visuals.h"   // Header file for our OpenGL functions 
 8: ////////////////// State Variables //////////////////////// 
 9: float Time=0.0;
 10: /////////////// Main Program /////////////////////////// 
 11: void main(int argc, char* argv[])
 12: {
 13:   // initialize GLUT library state
 14:   glutInit(&argc, argv);
 15:   // Set up the display using the GLUT functions to 
 16:   // get rid of the window setup details:
 17:   // - Use true RGB colour mode ( and transparency )
 18:   // - Enable double buffering for faster window update
 19:   // - Allocate a Depth-Buffer in the system memory or 
 20:   // in the video memory if 3D acceleration available 
 21:   glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
 22:   // Define the main window size and initial position 
 23:   // ( upper left corner, boundaries included )
 24:   glutInitWindowSize(600,600);
 25:   glutInitWindowPosition(50,50);
 26:   // Create and label the main window
 27:   glutCreateWindow("CAROUSEL-XALBANTZIS KOSTAS");
 28:   // Configure various properties of the OpenGL rendering context
 29:   Setup();
 30:   // Callbacks for the GL and GLUT events: 
 31:   // The rendering function 
 32:   glutDisplayFunc(Render);
 33:   glutReshapeFunc(Resize);
 34:   glutIdleFunc(Idle);
 35:   //Enter main event handling loop
 36:   glutMainLoop();
 37: }  
 38: 
 39:   
 40: #include <windows.h>   // - Windows API and system calls
 41: #include <stdio.h>     // - Just for some ASCII messages
 42: #include <gl/gl.h>     // - The OpenGL API 
 43: #include <gl/glaux.h>  // - A windows library extension API
 44: #include <gl/glut.h>   // - An interface and windows 
 45:                        // management library
 46: #include "visuals.h"   // Header file for our OpenGL functions 
 47: #include <math.h> 
 48: void Render()
 49: {
 50:     //Η ΣΥΝΑΡΤΗΣΗ Render ΔΗΜΙΟΥΡΓΕΙ ΤΟ CAROUSEL TO OPOIO APOTELEITAI APO ΕΝΑΝ ΚΩΝΟ,ΕΝΑΝ ΚΕΝΤΡΙΚΟ ΒΑΣΙΚΟ ΚΥΛΙΝΔΡΟ
 51:     //4 ΠΕΡΙΦΕΡΕΙΑΚΟΥΣ ΚΥΛΙΝΔΡΟΥΣ ΚΑΙ "ΑΛΟΓΑΚΙΑ" ΠΟΥ ΠΕΡΙΣΤΡΕΦΟΝΤΑΙ ΓΥΡΩ ΑΠΟ ΤΟΝ ΚΕΝΤΡΙΚΟ ΚΑΙ ΕΝΑΝ ΚΥΛΙΝΔΡΟ ΒΑΣΗ.
 52:     //ΔΗΜΙΟΥΡΓΟΥΜΕ ΤΑ ΠΑΡΑΠΑΝΩ ΣΧΗΜΑΤΑ ΕΝΑ-ΕΝΑ ΚΑΙ ΜΕΤΑΤΟΠΙΖΟΥΜΕ ΚΑΙ ΠΕΡΙΣΤΡΕΦΟΥΜΕ ΤΟ ΚΑΘΕΝΑ ΚΑΤΑΛΛΗΛΑ ΣΤΗΝ
 53:     //ΣΩΣΤΗ ΘΕΣΗ. 
 54:     int i;
 55:     float r;
 56:     extern float Time;
 57:     glClear(GL_COLOR_BUFFER_BIT);    //The glClear function clears buffers to preset values.
 58:     glClear(GL_DEPTH_BUFFER_BIT);    //GL_COLOR_BUFFER_BIT The buffers currently enabled for color writing. 
 59:                                     //GL_DEPTH_BUFFER_BIT The depth buffer. 
 60:     glMatrixMode(GL_MODELVIEW);        //The glMatrixMode function specifies which matrix is the current matrix.
 61:                                     //GL_MODELVIEW Applies subsequent matrix operations to the modelview matrix stack. 
 62:     glLoadIdentity();                //The glLoadIdentity function replaces the current matrix with the identity matrix.
 63:     glRotatef(10,1.0,0,0);            //The glRotatef function multiply the current matrix by a rotation matrix.
 64:     glRotatef(60*Time,0,1.0,0);
 65:     glFrontFace(GL_CCW);            //The glFrontFace function defines front- and back-facing polygons. 
 66:     glColorMaterial(GL_FRONT,GL_AMBIENT);    //The glColorMaterial function causes a material color to track the current color.
 67:     kilindros(40,7);                        //Δημιουργία κυλίνδρου
 68:     glLoadIdentity();
 69:     glRotatef(10,1.0,0,0);
 70:     glRotatef(60*Time,0,1.0,0);
 71:     glTranslatef(0,20,0);                    //The glTranslatef function multiply the current matrix by a translation matrix.
 72:     glRotatef(-90,360,0,0);
 73:     //glColor3f(rand()/10000, rand()/10000, rand()/10000);
 74:     glColor3f(0.1,0.8,0.1);                    //This function set the current color
 75:     glutSolidCone(40,15,35,35);                //This function create a Cone
 76:     r=30;
 77:     for(i=0;i<4;i++)                        //ΕΠΑΝΑΛΗΨΗ ΓΙΑ ΤΗΝ ΔΗΜΙΟΥΡΓΙΑ ΤΩΝ 4 ΠΕΡΙΦΕΡΕΙΑΚΩΝ ΚΥΛΙΝΔΡΩΝ ΚΑΙ "ΑΛΟΓΩΝ"
 78:     {                                        //ΚΑΙ ΤΟΠΟΘΕΤΗΣΗ ΑΥΤΩΝ ΣΕ ΣΤΑΘΕΡΗ ΑΠΟΣΤΑΣΗ ΑΠΟ ΤΟΝ ΚΕΝΤΡΙΚΟ ΚΥΛΙΝΔΡΟ ΚΑΘΩΣ
 79:                                             //ΚΑΙ ΠΕΡΙΣΤΡΟΦΗ ΑΥΤΩΝ ΓΥΡΩ ΑΠΟ ΤΟ ΚΕΝΤΡΟ ΜΕ ΗΜΙΤΟΝΟΕΙΔΗ ΤΡΟΠΟ.
 80:         glLoadIdentity();
 81:         glFrontFace(GL_CCW);
 82:         glRotatef(10,1.0,0,0);
 83:         glRotatef(60*Time,0,1.0,0);
 84:         glTranslatef(r*cos(2*i*3.14159/4),0,r*sin(2*i*3.14159/4));
 85:         kilindros(40,2);
 86:         glTranslatef(0,-3+9*sin(i*3.14159*0.5+3*Time*3.14159),0);
 87:         glFrontFace(GL_CW);
 88:         //glColor3f(rand()/10000, rand()/10000, rand()/10000);
 89:         glColor3f(0.2,0.1,0.6);
 90:         //glutSolidTeapot(5); //This function create a Teapot
 91:         glutSolidTorus(5,0,30,25);        //This function create a Torus 
 92:     }
 93:     glLoadIdentity();                    //ΔΗΜΙΟΥΡΓΙΑ ΤΟΥ ΚΥΛΙΝΔΡΟΥ ΒΑΣΗΣ
 94:     glRotatef(10,1.0,0,0);
 95:     glRotatef(60*Time,0,1.0,0);
 96:     glTranslatef(0,-20,0);
 97:     glFrontFace(GL_CCW);
 98:     kilindros(5,40);
 99:     glutSwapBuffers(); 
 100: }
 101: //----------------------------------------------------------- 
 102: void kilindros(float h,float r)
 103: { 
 104:     glBegin(GL_TRIANGLES);     //The glBegin and glEnd functions delimit the vertices of a primitive or a group of like primitives.
 105:     int i,seg=28;
 106:     float step=2*3.1415936/seg;
 107:     for (i=0; i<seg; i++)  { 
 108:       glColor3f(0.1,0.1,0.7);
 109:       //glColor3f(rand()/10000, rand()/10000, rand()/10000);
 110:       glNormal3f(0, -1, 0);                                    //This function set the current normal vector.
 111:       glVertex3f(r*cos(i*step), -h/2, r*sin(i*step));        //This function specify a vertex.
 112:       glVertex3f(r*cos((i+1)*step), -h/2, r*sin((i+1)*step));
 113:       glVertex3f(0.0, -h/2, 0.0); 
 114:       glColor3f(0.1,0.8,0.1);
 115:       //glColor3f(rand()/10000, rand()/10000, rand()/10000);
 116:       glNormal3f(0, 1, 0);
 117:       glVertex3f(r*cos((i+1)*step), h/2, r*sin((i+1)*step));
 118:       glVertex3f(r*cos(i*step), h/2, r*sin(i*step));
 119:       glVertex3f(0.0, h/2, 0.0); 
 120:       glColor3f(0.8,0.1,0.1);
 121:       //glColor3f(rand()/10000, rand()/10000, rand()/10000);
 122:       glNormal3f(cos(i*step), 0, sin(i*step));
 123:       glVertex3f(r*cos(i*step), h/2, r*sin(i*step));
 124:       glNormal3f(cos((i+1)*step), 0, sin((i+1)*step));
 125:       glVertex3f(r*cos((i+1)*step), h/2, r*sin((i+1)*step));
 126:       glNormal3f(cos(i*step), 0, sin(i*step));
 127:       glVertex3f(r*cos(i*step), -h/2, r*sin(i*step)); 
 128:       glNormal3f(cos((i+1)*step), 0, sin((i+1)*step));
 129:       glVertex3f(r*cos((i+1)*step), -h/2, r*sin((i+1)*step));
 130:       glNormal3f(cos(i*step), 0, sin(i*step));
 131:       glVertex3f(r*cos(i*step), -h/2, r*sin(i*step));
 132:       glNormal3f(cos((i+1)*step), 0, sin((i+1)*step));
 133:       glVertex3f(r*cos((i+1)*step), h/2, r*sin((i+1)*step)); 
 134:   }
 135:     glEnd(); 
 136: }
 137: //----------------------------------------------------------- 
 138: void Resize(int w, int h)
 139: { 
 140:   // define the visible area of the window ( in pixels )
 141:   if (h==0) h=1;
 142:   glViewport(0,0,w,h);
 143:   // Setup viewing volume 
 144:   glMatrixMode(GL_PROJECTION);
 145:   glLoadIdentity();
 146:   // L R B T N F
 147:   glOrtho (-50.0f, 50.0f, -50.0f, 50.0f,100.0f,-100.0f); 
 148: }
 149: //-------------------------------------------------------------- 
 150: void Setup()
 151: { 
 152:    glShadeModel( GL_FLAT ); //The glShadeModel function selects flat or smooth shading. 
 153:    //Set up light source
 154:    GLfloat ambientLight[] = { 0.2, 0.2, 0.2, 1.0 };
 155:    GLfloat diffuseLight[] = { 0.8, 0.8, 0.8, 1.0 };
 156:    GLfloat lightPos[] = { -20.0, 20.0, 150.0, 1.0 };
 157:    glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
 158:    glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
 159:    glLightfv( GL_LIGHT0, GL_POSITION,lightPos ); 
 160:     glEnable(GL_LIGHTING);
 161:     glEnable(GL_LIGHT0);
 162:     glShadeModel(GL_SMOOTH);
 163:    // polygon rendering mode and material properties
 164:    glPolygonMode(GL_FRONT,GL_FILL);
 165:     glEnable(GL_COLOR_MATERIAL);
 166:     glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
 167:   // Black background
 168:   glClearColor(0.0f,0.0f,0.0f,1.0f);
 169:     glEnable(GL_DEPTH_TEST);
 170:     glDepthFunc(GL_GREATER);
 171:     glClearDepth(0.0); 
 172:     glEnable(GL_CULL_FACE);
 173: } 
 174: //-------------------------------------------------------------------------- 
 175: void Idle()
 176: {
 177:     extern float Time; 
 178:     Time=Time+0.01; if (Time==1.0) Time=0.0;
 179:     glutPostRedisplay();
 180: }
 181: //-------- Functions -------------------------------- 
 182: void Render();
 183: // The function responsible for drawing everything in the 
 184: // OpenGL context associated to a window. 
 185: void Resize(int w, int h);
 186: // Handle the window size changes and define the world coordinate 
 187: // system and projection type 
 188: void Setup();
 189: // Set up the OpenGL state machine and create a light source 
 190: void Idle();
 191: //Handles Animation 
 192: void kilindros(float h,float r);
 193: //Δημιουργία κυλίνδρου 

 

Comments

2 responses to “Carousel in OpenGL”

  1. elpida Avatar
    elpida

    Geia sas
    exume na kanume mia paromia aksisi k etrexa ton kodika sas g na to do apiotelesma k m zita auta ta dio ta files…prepi na ta dimiourgisume emis???glaux.h, visuals.h