HOME
OPENGL
CONTACT
 If you would like to be
 informed if there is
 new tutorial, please
 submit your e-mail to
 us. Thank you.
 
 
4. FIRST OPENGL PROGRAM

1.

Create a new source file. File --> New... --> Files Tab --> C++ Source File --> Name the file as "main" at File_name.

2.

Here is the  first program. Copy it into "main.cpp" file (only the program).

3.

Click Build --> Compile main.cpp --> Build <project name>.exe --> Execute <project name>.exe.

4.

Now if there is no error generated, you should be able to see this:

5.

Now, we will have a code walkthrough on what is happening inside the program. 

6.

Line 1 to 5 is the comment. Use it to write the programmer's name, program's name and date.
 //===============================================//
 // Title : First OpenGL Program
 // Programmer : V R E M P I R E
 // Date : Thursday, July 12, 2001
 //===============================================//
7. Line 8 is used to call the GLUT library. When glut.h is called, it will automatically call gl.h and glu.h.
 #include <gl/glut.h>
8. Now we go inside main function at line 37. At line 39, glutInit is used to initialize GLUT library inside our window system so they can talk with each other.
 glutInit(&argc, argv);
9. At line 40, glutInitDisplayMode is used to set the initial display mode.
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
10. At line 41, glutInitWindowSize is used to set the initial window size based on pixel unit.
 glutInitWindowSize(400, 400);
11. At line 42, glutInitWindowPosition is used to set the initial window position based on X and Y value of our window system.
 glutInitWindowPosition(200, 200);
12. At line 43, glutCreateWindow is used to create a top level window. The value inside the function will be used as the title on the top-left of the window.
 glutCreateWindow("First OpenGL Program");
13. At line 44, Init() function is called. At line 24 in the Init() function, the glClearColor command is called. glClearColor will specify clear values for the color buffers. This will give the color of the window. The first parameter is for Red,  second is for Green and the third one is for Blue. The value of each of them is between 0.0 and 1.0.
 glClearColor(0.0, 0.0, 0.0, 0.0);
14. At line 45, glutDisplayFunc is used to set the display callback for the current window. Here, it will call the function Display() at line 10.
 glutDisplayFunc(Display);
15. At line 12, glClear is used to clear buffers to preset values.
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
16. At line 13, glLoadIdentity is used to replace the current matrix with the identity matrix.
 glLoadIdentity();
17. At line 14, gluLookAt is used to define a viewing transformation. The first three parameters is the x, y and z coordinate of our eye in the 3-dimensional world. The next three parameters is the x, y and z coordinate of the Center Of Interest. The next three parameters is the up vector.
 gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
18. At line 15, glColor3f is used to set a new three-valued RGB color. First value is Red, second is Green and the third one is Blue.
 glColor3f(0.0, 1.0, 0.0);
19. At line 17, glutSolidCube is used to render a solid cube.
 glutSolidCube(1.0);
20. At line 19, glutSwapBuffers is used to swaps the buffers of the current window if double buffered. This function will automatically called glFlush to clear back the buffers.
 glutSwapBuffers();
21. At line 46, glutReshapeFunc is used to set the reshape callback for the current window. Here, it will call the function Resize() at line 27.
 glutReshapeFunc(Resize);
22. At line 29, glViewport is used to specify the affine transformation of x and y from normalized devices coordinates to window coordinates.
 glViewport(0, 0, width, height);
23. At line 30, glMatrixMode is used to specify which matrix is the current matrix. GL_PROJECTION is used to tell the matrix that currently it is for projection.
 glMatrixMode(GL_PROJECTION);
24. At line 31, glLoadIdentity is used to refresh the matrix.
 glLoadIdentity();
25. At line 32, gluPerspective is used to set up a perspective projection matrix. It specifies a viewing frustum into the world coordinate system. First parameter is the degrees for our eye to see in y-axis. The second parameter is the aspect ratio. The third parameter is distance from our eye to nearest z-plane. The fourth parameter is the distance from our eye to the farthest z-plane.
 gluPerspective(60.0, width/height, 0.1, 1000.0);
26. At line 33, glMatrixMode is used to specify which matrix is the current matrix. GL_MODELVIEW is used to tell the matrix that currently it is used to display model.
 glMatrixMode(GL_MODELVIEW);
27. At line 47, glutMainLoop is used to enters the GLUT event processing loop.
 glutMainLoop();
28. I think that's all for now for the beginning. Let's move to the next chapter on how to create our own polygon, instead of using GLUT default polygon.

PREVIOUS   INDEX   NEXT
Copyright © 2005 VREMPIRE LAIR - All rights reserved.