How to Make a Cube in OpenGL

OpenGL is a powerful 3D programming tool used to draw complex three-dimensional scenes from simple primitives. This article will teach you how to draw a simple cube that you can spin to view in three dimensions!

For this project you will need a code editor and some knowledge of C programming.

Setting Up

Install OpenGL To begin follow these steps to installing OpenGL on your system.

If you already have OpenGL, as well as a compatible C compiler installed, you can skip this step and go to the next.

Create the document.

Create a new file in your favorite code editor and save it as mycube.c

Add #includes.

These are the basic includes you will need for your program. It is important to realize that there are actually different includes required for the different operating systems. Be sure to include all of these to ensure your program is versatile and can run for any user.

Add function prototypes and global variables.

Your next step is to declare some function prototypes.

Set up the main() function.

  • This statement sets up your environment. A big thing to remember when writing OpenGL programs is that you must ask for everything. This requires you to have a greater understanding of how your program works and what you need to include to get the functionality that you want. In this line, you will set up the display with double buffering, RGB color, and a Z-buffer.
  • Double buffering is a technique used in graphics programs to eliminate a problem that arises due to how images are drawn to the screen. Each time you redraw the scene, the display must first be erased then the new information will be drawn. Without double buffering you will observe a flickering effect as the screen is erased and redrawn repeatedly.
  • This problem is fixed by adding a second buffer to draw to. With this method, an image is drawn to the first buffer and that buffer is shown to you. The next frame will be drawn to the second buffer and when that’s done, the two buffers will switch places. You will immediately see the second buffer, but, hidden from us, the first buffer is being erased and redrawn with the third frame which will be swapped in when finished.
  • You also want to enable the RGB color system in your window.
  • Z-buffering is how you get the 3D effects that you want. OpenGL uses a three dimensional coordinate system with x, y and z axes. To give the effect that an object is closer to you its position on the z axis is increased, however, to make it appear further away its position on the z axis is decreased.

Create the window.

The next step is to within which you will draw the cube. In this tutorial, the window is called “Awesome Cube”.

Enable depth test.

OpenGL is a strict language in that it does not assume any special features are enabled. For your program to properly display in 3-dimensions using the Z-buffer that you looked at earlier, you need to . As you continue to explore OpenGL, you will discover many features that you will need to enable including lighting, textures, cull-facing and much more.

Add callback functions.

Here are the callback functions you wrote the prototypes for earlier. Each time through the main loop, these functions will be called. The display function redraws the scene based on any changes to variables that were made since the previous call. The specialKeys function allows us to interact with the program.

Start the MainLoop.

This will recall the main function until you close the program to allow for animations and user interaction.

Writing the display() Function

Understand the purpose of this function.

All the work of drawing your cube will be done in this function. The general idea behind your cube is to draw all six sides individually and place them in the appropriate position.

  • Conceptually, each side is going to be drawn by defining the four corners and letting OpenGL connect the lines and fill it in with a color that you define. Below are the steps to doing this.

Add glClear().

The first step you need to take in this function is to . Without these steps, the old drawings may still be visible under the new drawings and objects drawn would not be in the correct location on the screen.

Add glBegin() and glEnd().

OpenGL defines objects as combinations of different polygons. Using the command, you effectively put down a pencil that will draw a shape. To lift up the pencil and begin a new shape, you must use the command. In this tutorial you will be using GL_POLYGON to draw each side of the cube but it is possible to use other parameter options such as GL_LINE, GL_QUAD, or GL_TRIANGLE to create other shapes.

  • Here you will be starting with the front of your cube. Later you will add color to all 6 sides.

Add glVertex3f().

Once you have stated that you want to begin your polygon, you need to of the object. glVertex has multiple forms depending on what you want to do with your object.

  • The first is how many dimensions you are working in. The 3 above in glVertex3f says you are drawing in 3 dimensions. It is also possible to work in 2 or 4 dimensions. The f above in glVertex3f says you are working with floating point numbers. You can also use shorts, integers or doubles.
  • Notice that these points are defined in a counter-clockwise manner. This is not very important at the moment but when you begin to work with lighting, textures, and cull-facing, this will become incredibly important so get in the habit of defining your points counter-clockwise now.
  • Add add the vertices between the glBegin() and glEnd() lines.

Add glColor3f().

glColor works in a similar manner to glVertex. You can define points as shorts, integers, doubles, or floats. Each color has a value from 0 to 1. All 0’s makes the point black and all 1’s will make the point white. The 3 in glColor3f() refers to the RGB color system with no alpha channel. The alpha of a color defines its transparency. To change the alpha level, use glColor4f() with the last parameter being a value of 0 to 1 for opaque to transparent.

  • When you call glColor3f() every vertex drawn from that point on will be of that color. Therefore, if you want all four vertices to be red, just set the color once anytime before the glVertex3f() commands and all vertices will be red.
  • The Front side defined below shows how to define a new color for each vertex. When you do this, you can see an interesting property of the OpenGL colors. Since each vertex of the polygon has its own color, OpenGL will automatically blend the colors! The next step will show how to assign four vertices with the same color.

Handle the other sides.

Work out what the location of each vertex will be for the other five sides of the cube but for simplicity, these have been computed for you and are included in the below.

  • We also want to add in two last lines of code for this function. These are glFlush(); and glutSwapBuffers(); which give us the double-buffering effect you learned about earlier.

Building Interactivity

Add specialKeys().

You are almost done but at the moment, you can draw a cube but have no way of rotating it. To do this, you will function to allow us to press the arrow keys and rotate the cube!

  • This function is why you declared the global variables rotate_x and rotate_y. When you press the right and left arrow keys, rotate_y will be incremented or decremented by 5 degrees. Similarly, when you press the up and down arrow keys, rotate_x will change accordingly.

Add glRotate().

Your last statement is to add the statement that will rotate your object. Go back to the display() function and before the FRONT side, add these lines:

  • First notice that the syntax of glRotatef() is similar to that of glColor3f() and glVertex3f() but always requires 4 parameters. The first parameter is the degree of rotation to be applied. The next three parameters define which axis to rotate about with the first being the x axis, second being the y axis, and third being the z axis. Right now you only need to rotate about the x and y-axis.
  • All transformations that you write in your program need lines similar to this. Conceptually, you can think of this as rotating your object about the x axis by the amount defined by rotate_x and then rotating around the y axis by rotate_y. However, OpenGL combines all these statements into one matrix transformation. Each time you call the display function, you build a transformation matrix and glLoadIdentity() assures that you will start out with a new matrix in each pass.
  • The other transformation functions you could apply are glTranslatef() and glScalef(). These functions are similar to glRotatef() with the exception the they only take 3 parameters, the x, y, and z amounts to translate or scale the object.
  • In order to get the correct effect when applying all three transformations to one object, you need to apply them in the correct order. Always write them in the order glTranslate, glRotate, then glScale. OpenGL essentially applies the transformations in a bottom up manner. To understand this try to imagine what a simple 1x1x1 cube would look like with the transformations if OpenGL applied them from top to bottom and if OpenGL applied them from bottom to top.

Add the following commands to scale the cube by 2 along the x-axis, 2 along the y-axis, rotate the cube by 180 degrees about the y-axis, and translate the cube by 0.1 along the x-axis.

Make sure to arrange these as well as the previous glRotate() commands in the correct order as described above. (If you are unsure, this is done this in the final code at the end of the tutorial.)

Compile and run your code.

Assuming you are using gcc as your compiler, run these commands from your terminal to compile and test your program.

Check your complete code.

It should be like this:

Leave a Comment