How to make a cube in OpenGL

Author: Gregory Harris
Date Of Creation: 12 August 2021
Update Date: 1 July 2024
Anonim
OpenGL Tutorial 23 - Drawing A Cube
Video: OpenGL Tutorial 23 - Drawing A Cube

Content

OpenGL is a 3D programming tool that allows you to create complex 3D images from simple shapes. In this article, you will learn how to draw with his help a simple cube that can be rotated in three dimensions!

Steps

Part 1 of 3: Initial Installation

  1. 1 Install OpenGL. Start with a tutorial on how to install OpenGL on your computer. If you already have OpenGL and a C compiler, you can skip this step and move on to the next.
  2. 2 Create a document. Create a new file in your favorite code editor and save it as mycube.c
  3. 3 Add #includes. Here are the basic #include directives you'll need. It is important to remember that the directives are different for different operating systems, and therefore you need to choose everything so that the program is universal and can be run on any system.

      // Includes #include stdio.h> #include stdarg.h> #include math.h> #define GL_GLEXT_PROTOTYPES #ifdef __APPLE__ #include GLUT / glut.h> #else #include GL / glut.h> #endif

  4. 4 Add functional prototypes and globals. The next step is to declare functional prototypes.

      // Functional prototypes void display (); void specialKeys (); // Global variables double rotate_y = 0; double rotate_x = 0;

  5. 5 Define the main () function.

      int main (int argc, char * argv []) // Initialize GLUT and process custom parameters glutInit (& argc, argv); // Request A Window With Double Buffering, Z-Buffering And True Color GlutInitDisplayMode (GLUT_DOUBLE

Part 2 of 3: The display () Function

  1. 1 Understand the purpose of the display () function. All the work on rendering the cube will fall on the fragile lines of this function. The general idea is that you draw six separate cube faces and place them in their respective positions.
    • For each face, you will define four corners, and OpenGL will connect them with lines and fill them with your chosen color. How to do this will be explained below.
  2. 2 Add glClear () function. First of all, when working with this function, we need clear color and z-buffer... Without this, the old one will be visible under the new picture, and the objects drawn by the program will be positioned incorrectly.

      void display () // Clear The Screen And Z Buffer glClear (GL_COLOR_BUFFER_BIT

    • Pay attention to the last two lines. These are the functions glFlush (); and glutSwapBuffers ();, giving the effect of double buffering, which was described above.

Part 3 of 3: Program Interactivity

  1. 1 Add the specialKeys () function. In principle, everything is almost ready, but the cube is only drawn and not rotated. To do this, you need to create the specialKeys () function, which will allow you to rotate the cube by pressing the arrow keys!
    • It is for this function that the global variables rotate_x and rotate_y were declared. When you press the left and right arrow keys, the rotate_y value will increase or decrease by five degrees. The value of rotate_x will change in the same way, but this time by pressing the up and down arrow keys.
    • void specialKeys (int key, int x, int y) {// Right arrow - increase the rotation by 5 degrees if (key == GLUT_KEY_RIGHT) rotate_y + = 5; // Left Arrow - Decrease Rotation By 5 Degrees else if (key == GLUT_KEY_LEFT) rotate_y - = 5; else if (key == GLUT_KEY_UP) rotate_x + = 5; else if (key == GLUT_KEY_DOWN) rotate_x - = 5; // Request Screen Refresh glutPostRedisplay (); }

  2. 2 Add glRotate (). The last thing we will do is add a line that will allow us to rotate the object. Return to function display () and before the description of the FRONT side add:

      // Reset Transforms glLoadIdentity (); // Rotate When The User Changes The values ​​rotate_x and rotate_y glRotatef (rotate_x, 1.0, 0.0, 0.0); glRotatef (rotate_y, 0.0, 1.0, 0.0); // Multicolor side - FRONT ....

    • Please note that the syntax glRotatef ()which is similar to the syntax of glColor3f () and glVertex3f (), but always requires four parameters. The first is the rotation angle in degrees. The next three are the axes along which the rotation takes place, in the order x, y, z. For now, we need to rotate the cube along two axes, x and y.
    • All transformations that we define in the program require similar lines. Basically, we represent the rotation of an object along the x-axis as a change in the value of rotate_x, and rotation along the y-axis as a change in the value of rotate_y. However, OpenGL will bundle everything into one transformation matrix. Each time you call display, you will create a transformation matrix, and glLoadIdentity () will allow you to start with a new matrix each time.
    • Other transformation functions you may have used are glTranslatef () and glScalef (). They are similar to glRotatef (), except that they only require three parameters: the x, y, and z values ​​to resize and scale the object.
    • For everything to be displayed correctly when all three transformations are applied to one object, you need to set transformations in the appropriate order, namely glTranslate, glRotate, glScale - and never otherwise. OpenGL transforms the object by reading the program from bottom to top. To understand this better, imagine how the 1x1x1 cube would look after all the transformations if OpenGL applied them in the order shown (top to bottom), and then think about how OpenGL would process the cube by reading the instructions from bottom to top.
  3. 3 Add the following commands to scale the cube twice in the x and y directions, to rotate the cube 180 degrees in the y-axis, and to move the cube 0.1 in the x-axis. Make sure that all relevant commands, including the previously given glRotate () commands, are in the correct order. If you are afraid to make a mistake, see the final version of the program at the end of the article.

      // More Transformations glTranslatef (0.1, 0.0, 0.0); glRotatef (180, 0.0, 1.0, 0.0); glScalef (2.0, 2.0, 0.0);

  4. 4 Compile and run the code. Let's say you're using gcc as your compiler, so enter the following commands into your terminal:

      On Linux: gcc cube.c -o cube -lglut -lGL ./ mycube On Mac: gcc -o foo foo.c -framework GLUT -framework OpenGL ./ mycube On Windows: gcc -Wall -ofoo foo.c -lglut32cu - lglu32 -lopengl32 ./ mycube

  5. 5 Check the final code. Here is the final code created by the author of the article, which does not translate the comments.

      // // File: mycube.c // Author: Matt Daisley // Created: 4/25/2012 // Project: Source code for Make a Cube in OpenGL // Description: Creates an OpenGL window and draws a 3D cube / / That the user can rotate using the arrow keys // // Controls: Left Arrow - Rotate Left // Right Arrow - Rotate Right // Up Arrow - Rotate Up // Down Arrow - Rotate Down // ------ -------------------------------------------------- - // Includes // ------------------------------------------- --------------- #include stdio.h> #include stdarg.h> #include math.h> #define GL_GLEXT_PROTOTYPES #ifdef __APPLE__ #include GLUT / glut.h> #else # include GL / glut.h> #endif // --------------------------------------- ------------------- // Function Prototypes // ------------------------- --------------------------------- void display (); void specialKeys (); // ------------------------------------------------ ---------- // Global Variables // ---------------------------------- ------------------------ double rotate_y = 0; double rotate_x = 0; // ------------------------------------------------ ---------- // display () Callback function // ------------------------------- --------------------------- void display () // Clear screen and Z-buffer glClear (GL_COLOR_BUFFER_BIT // ------ -------------------------------------------------- - // specialKeys () Callback Function // --------------------------------------- ------------------- void specialKeys (int key, int x, int y) {// Right arrow - increase rotation by 5 degree if (key == GLUT_KEY_RIGHT) rotate_y + = 5; // Left arrow - decrease rotation by 5 degree else if (key == GLUT_KEY_LEFT) rotate_y - = 5; else if (key == GLUT_KEY_UP) rotate_x + = 5; else if (key == GLUT_KEY_DOWN) rotate_x - = 5; // Request display update glutPostRedisplay ();} // ----------------------------------- ----------------------- // main () function // ------------------- --------------------------------------- int main (int argc, char * argv [ ]) GLUT_RGB