OpenGL: Modeling void DrawPyramid(){ glBegin(GL_TRIANGLES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.1, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.1, 0.0, 0.0); glVertex3f(0.0, 0.1, 0.0); glVertex3f(0.0, 0.0, 0.1); glEnd() ; }
Modeling: Cone void DrawCone(int res){ GLfloat d = 2*PI/res ; GLfloat a = 0; glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0, 0.0, 0.0); for (int i = 0 ; i < res ; i++ ){ glVertex3f(cos(a), sin(a), 0.0); a += d ; } glEnd() ; a = 0.0 ; glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0, 0.0, 1.0); for (int i = 0 ; i < res ; i++ ){ glVertex3f(cos(a), sin(a), 0.0); a += d ; } glEnd() ; }
Modeling: cylinder void DrawCylinder (int res){ GLfloat a, d = 2*PI/res ; glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0, 0.0, 0.0); for (int i = 0, a = 0 ; i < res ; i++,a += d ) glVertex3f(cos(a), sin(a), 0.0); glEnd() ; glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0, 0.0, 1.0); for ( i = 0, a = 0 ; i < res ; i++, a += d ) glVertex3f(cos(a), sin(a), 1.0); glEnd() ; glBegin(GL_TRIANGLE_STRIP); for ( i = 0, a = 0 ; i < res; i++, a += d) { glVertex3f(cos(a), sin(a), 0.0); glVertex3f(cos(a), sin(a), 1.0); } glEnd() ; }
Scene // Set Up glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity() ; glOrtho(-100,100,-100,100,-100,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Draw Scene glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Draw Cylinder glPushMatrix() ; glRotate(90, 1,0,0); glTranslate(0,0,10); glScale(10,10,10) DrawCylinder(50); glPopMatrix() ;
Scene // Draw Cube glPushMatrix() ; glTranslate(10,0,2); glScale(5,10,5) DrawCube(10); glPopMatrix() ; // Draw Cones glPushMatrix() ; glRotate(90, 0,1,0); glRotate(45, 0,0,1); glPushMatrix(); glTranslate(5,0,5); glScale(5,5,5) DrawCone(10); glPopMatrix() ; glTranslate(5,10,5); glScale(10,10,10); DrawCone(10); glPopMatrix() ;