Code:
#
# This example code will show how to draw a cube rotate and zoom it
# Was created by Jabka Atu and is released under GPL
#
#include <iostream>
#include <GL/glut.h>
using namespace std;
typedef unsigned char uchar;
static float fTranslatex = 0 ,fTranslatey = 0 ,fTranslatez = 0 ;
static float angle = 0;
static float fScale = 1;
static float rotqube ;
void disp();
void idle();
void drawexis();
void keyb(unsigned char key, int x, int y);
void skeyb(int key, int x, int y);
int drawcube();
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("Cube ... ");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(disp);
glutIdleFunc(idle);
glutKeyboardFunc(keyb);
glutSpecialFunc(skeyb);
glutMainLoop();
return 0;
}
void disp()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotatef(angle,1.0,1.0,0.0);
glTranslatef(fTranslatex, fTranslatey, fTranslatez);
glScalef(fScale, fScale, fScale);
drawcube();
glutSwapBuffers();
}
int drawcube()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(rotqube,0.0f,1.0f,0.0f); // Rotate The cube around the Y axis
glRotatef(rotqube,1.0f,1.0f,1.0f);
glEnable(GL_DEPTH_TEST); // Hide the bottom lines
glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f,0.3f,0.0f); // Color Blue
glVertex3f( 0.3f, 0.3f,-0.3f); // Top Right Of The Quad (Top)
glVertex3f(-0.3f, 0.3f,-0.3f); // Top Left Of The Quad (Top)
glVertex3f(-0.3f, 0.3f, 0.3f); // Bottom Left Of The Quad (Top)
glVertex3f( 0.3f, 0.3f, 0.3f); // Bottom Right Of The Quad (Top)
glColor3f(0.3f,0.5f,0.0f); // Color Orange
glVertex3f( 0.3f,-0.3f, 0.3f); // Top Right Of The Quad (Bottom)
glVertex3f(-0.3f,-0.3f, 0.3f); // Top Left Of The Quad (Bottom)
glVertex3f(-0.3f,-0.3f,-0.3f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 0.3f,-0.3f,-0.3f); // Bottom Right Of The Quad (Bottom)
glColor3f(0.3f,0.0f,0.0f); // Color Red
glVertex3f( 0.3f, 0.3f, 0.3f); // Top Right Of The Quad (Front)
glVertex3f(-0.3f, 0.3f, 0.3f); // Top Left Of The Quad (Front)
glVertex3f(-0.3f,-0.3f, 0.3f); // Bottom Left Of The Quad (Front)
glVertex3f( 0.3f,-0.3f, 0.3f); // Bottom Right Of The Quad (Front)
glColor3f(0.3f,0.3f,0.0f); // Color Yellow
glVertex3f( 0.3f,-0.3f,-0.3f); // Top Right Of The Quad (Back)
glVertex3f(-0.3f,-0.3f,-0.3f); // Top Left Of The Quad (Back)
glVertex3f(-0.3f, 0.3f,-0.3f); // Bottom Left Of The Quad (Back)
glVertex3f( 0.3f, 0.3f,-0.3f); // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,0.3f); // Color Blue
glVertex3f(-0.3f, 0.3f, 0.3f); // Top Right Of The Quad (Left)
glVertex3f(-0.3f, 0.3f,-0.3f); // Top Left Of The Quad (Left)
glVertex3f(-0.3f,-0.3f,-0.3f); // Bottom Left Of The Quad (Left)
glVertex3f(-0.3f,-0.3f, 0.3f); // Bottom Right Of The Quad (Left)
glColor3f(0.3f,0.0f,0.3f); // Color Violet
glVertex3f( 0.3f, 0.3f,-0.3f); // Top Right Of The Quad (Right)
glVertex3f( 0.3f, 0.3f, 0.3f); // Top Left Of The Quad (Right)
glVertex3f( 0.3f,-0.3f, 0.3f); // Bottom Left Of The Quad (Right)
glVertex3f( 0.3f,-0.3f,-0.3f); // Bottom Right Of The Quad (Right)
glEnd(); // End Drawing The Cube
return 1;
}
void idle()
{
}
void skeyb(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_LEFT :fTranslatex -= 0.1;glutPostRedisplay(); break;
case GLUT_KEY_RIGHT :fTranslatex += 0.1;glutPostRedisplay(); break;
case GLUT_KEY_UP :fTranslatey += 0.1;glutPostRedisplay(); break;
case GLUT_KEY_DOWN :fTranslatey -= 0.1;glutPostRedisplay(); break;
}
}
void keyb(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':angle+=1;glutPostRedisplay();break;
case 's':angle-=1;glutPostRedisplay();break;
case 'd':fScale*=1.1;glutPostRedisplay();break;
case 'f':fScale*=0.9;glutPostRedisplay();break;
case 'q':exit(0);break;
}
}