Switch to full style
C++ openGL code examples
Post a reply

Drawing Octahedron using openGL

Sun Feb 19, 2012 10:23 pm

Drawing Octahedron using OpenGL/GLU library and also with animated rotation . glutSolidOctahedron() function is used.
Code:


#include <GL\glut.h>

GLfloat xRotated, yRotated, zRotated;
// Octahedron
void displayOctahedron(void)
{

    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();
    // traslate the draw by z = -4.0
    // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
    glTranslatef(0.0,0.0,-4.5);
    // Red color used to draw.
    glColor3f(0.8, 0.2, 0.1); 
    
// changing in transformation matrix.
    // rotation about X axis
    glRotatef(xRotated,1.0,0.0,0.0);
    // rotation about Y axis
    glRotatef(yRotated,0.0,1.0,0.0);
    // rotation about Z axis
    glRotatef(zRotated,0.0,0.0,1.0);
    // scaling transfomation 
    glScalef(1.0,1.0,1.0);
    // built-in (glut library) function , draw you a Octahedron.
    glutSolidOctahedron();
    // Flush buffers to screen
     
    glFlush
();        
    
// sawp buffers called because we are using double buffering 
   // glutSwapBuffers();
}

void reshapeOctahedron(int x, int y)
{
    if (== 0 || x == 0) return;  //Nothing is visible then, so return
    //Set a new projection matrix
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity
();
    //Angle of view:40 degrees
    //Near clipping plane distance: 0.5
    //Far clipping plane distance: 20.0
     
    gluPerspective
(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
 
    glViewport
(0,0,x,y);  //Use the whole window for rendering
}

void idleOctahedron(void)
{
 
     yRotated 
+= 0.03;
     
    displayOctahedron
();
}


int main (int argc, char **argv)
{
    //Initialize GLUT
    glutInit(&argc, argv);
    //double buffering used to avoid flickering problem in animation
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
    
// window size
    glutInitWindowSize(400,350);
    // create the window 
    glutCreateWindow("Octahedron Rotating Animation");
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    xRotated = yRotated = zRotated = 30.0;
     xRotated=33;
     yRotated=40;
    glClearColor(0.0,0.0,0.0,0.0);
    //Assign  the function used in events
    glutDisplayFunc(displayOctahedron);
   glutReshapeFunc(reshapeOctahedron);
    glutIdleFunc(idleOctahedron);
    //Let start glut loop
    glutMainLoop();
    return 0;
}
 



Attachments
Octahedron.png
Octahedron.png (4.84 KiB) Viewed 12447 times

Post a reply
  Related Posts  to : Drawing Octahedron using openGL
 Drawing Torus using OpenGL     -  
 Drawing teapot using OpenGL.     -  
 Drawing cone using openGL     -  
 openGL in java     -  
 OpenGl simple example     -  
 Build Calculator in OpenGL     -  
 openGL shadow mapping example     -  
 Draw 3d cube using openGL     -  
 Draw a solid sphere using openGL     -  
 3d OpenGL game with source code     -  

Topic Tags

C++ Graphics