C++ tutorial- draw cube


Draw a cube

In the previous page, you learnt to draw basic shapes, a line and a triangle. In this tutorial, you will learn to draw a cube as shown below. The cube has six faces: front, back, right, left, top, and bottom faces. Each face has different colors.

cube

To draw the cube, the first thing you have to do is defining arrays of vertices, and arrays of colors for all faces of the cube. Then activate the arrays by calling the glEnableClientState(GLenum array) functions. To activate the arrays of vertices, and the arrays of colors, you will use the GL_VERTEX_ARRAY, and GL_COLOR_ARRAY for the GLenum array argument respectively. After that you need to specify the arrays of vertices and colors for drawing by calling the glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer), and glColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer). For the glVertexPointer function, the size argument is the number of coordinates per vertex which must be 2, 3, or 4. The type argument specifies the data type (GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE) of a coordinate value. The stride arguemnt is the byte offset between consecutive vertices. If stride is 0, the vertices are understood to be tightly packed in the array. The *pointer argument contains the memory address of the first vertex in the array. Similarly, in the glColorPointer function, the size argument represents the number of color values used to create a color. It can be 3 or 4. The data type can be GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE. The *pointer contains the memory address of the first color in the array. After the arrays are activated, you can call the glArrayElement(GLint index) to dereference the content of vertex arrays and draw them on the screen.

Below is the complete code of the cube drawing program.

#include <cstdlib>
#include <iostream>
#include <gl/freeglut.h>

using namespace std;

void setup() {

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

}


void displayCube(){

//clear window to the black color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//define vertices arrays and colors for all faces of a cube
GLfloat verticesFront[]={-0.5,0.5,0.5,0.50,0.50,-0.5,-0.5,-0.5};
GLfloat colorsFront[]={0.0,1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0,0.0,0.0,1.0};
GLfloat verticesRight[]={0.5,-0.5,0.5,0.5,0.75,0.75,0.75,-0.25};
GLfloat colorsRight[]={1.0,1.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,1.0,1.0};
GLfloat verticesLeft[]={-0.5,0.5,-0.25,0.75,-0.25,-0.25,-0.5,-0.5};
GLfloat colorsLeft[]={0.0,1.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0};
GLfloat verticesBack[]={-0.25,0.75,0.75,0.75,0.75,-0.25,-0.25,-0.25};
GLfloat colorsBack[]={1.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,1.0,1.0,1.0};
GLfloat verticesTop[]={-0.5,0.5,-0.25,0.75,0.75,0.75,0.5,0.5};
GLfloat colorsTop[]={0.0,1.0,1.0,0.0,1.0,1.0,1.0,0.0,0.0,1.0,1.0,1.0};
GLfloat verticesBottom[]={-0.5,-0.5,-0.25,-0.25,0.75,0.75,0.5,0.5};
GLfloat colorsBottom[]={0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0};

//drawing font face
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colorsFront);
glVertexPointer(2, GL_FLOAT, 0, verticesFront);
glBegin(GL_POLYGON);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
//drawing back face
glColorPointer(3, GL_FLOAT, 0, colorsBack);
glVertexPointer(2, GL_FLOAT, 0, verticesBack);
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
//drawing right face
glColorPointer(3, GL_FLOAT, 0, colorsRight);
glVertexPointer(2, GL_FLOAT, 0, verticesRight);
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
//drawing left face
glColorPointer(3, GL_FLOAT, 0, colorsLeft);
glVertexPointer(2, GL_FLOAT, 0, verticesLeft);
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
//drawing top face
glColorPointer(3, GL_FLOAT, 0, colorsTop);
glVertexPointer(2, GL_FLOAT, 0, verticesTop);
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
//drawing bottom face
glColorPointer(3, GL_FLOAT, 0, colorsBottom);
glVertexPointer(2, GL_FLOAT, 0, verticesBottom);
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
glutSwapBuffers();

}

int main(int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,400);
glutCreateWindow("Drawing a cube");
setup();
glutDisplayFunc(displayCube);
glutMainLoop();

system("PAUSE");
return EXIT_SUCCESS;
}


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.