Computer Graphics Lab1: Make Sure
Computer Graphics Lab1: Make Sure
Before beginning the lab, let's make sure we have the necessary Software installed. For all our labs, we
will use Microsoft Visual C++ compiler to compile our codes. C++ is not the only programming
language you can do OpenGL Programming with. In fact, you can also use C, C#, Java, Visual Basic,
and others.
After selecting the programming language, we choose a compiler. For C++, we can use MS Visual C+
+, Code::Blocks, Delphi, Dev C++, and others. For our labs, we will use MSV C++.
To do OpenGL, we need to have some very important files installed in our system.
Make sure
1. MVC++ is installed in your system
2. The files opengl32.dll, glu32.dll, glut32.dll are present in system32 folder
3. The files gl.h, glu.h, glut.h are present in the GL folder of MVC++'s INCLUDE directory
4. The files glu32.lib and glut32.lib are present in the LIB directory of MVC++
If you are running Windows 98/Me/NT/2000/XP/2003/Vista, the OpenGL library has already been
installed on your system. The standard Windows OpenGL32.dll library alone will not provide you
with hardware acceleration for OpenGL. In order to get hardware acceleration, you will need to
install the latest drivers for your graphics card. The second reason to install new drivers is to have the
latest version of GL on your system (the max version supported by your GPU).
Without drivers, you will default to a software version of OpenGL 1.1 (on Win98, ME, and 2000), a
Direct3D wrapper that supports OpenGL 1.1 (WinXP), or a Direct3D wrapper that supports OpenGL
1.1 (Windows Vista and Windows 7). None of these options are particularly fast, so installing drivers is
always a good idea.:[src: OpenGL wiki-- http://www.opengl.org/wiki/Getting_started#FAQ]
For now, we are going to stick with the OpengGL version provided by the OS at hand, OpenGL1.1.
If these files are not present in the folders mentioned, you can download them from moodle (OpenGL
Related Libraries …) and put them in their appropriate folders.
• With OpenGL, you must build up your desired model from a small set of geometric primitives -
points, lines, and polygons.
• A sophisticated library that provides these features (complicated shapes) could certainly be built
on top of OpenGL. The OpenGL Utility Library (GLU) provides many of the modeling
features, such as quadric surfaces. [The glu32.lib and glu.h are from this Library]
• Such features as opening windows, detecting input from the keyboard and mouse are provided
by the OpenGL Utitlity Toolkit, GLUT. [The glut32.lib and glut32.h are from this Toolkit]
Because you can do so many things with the OpenGL graphics system, an OpenGL program can be
complicated. However, the basic structure of a useful program can be simple: Its tasks are to initialize
certain states that control how OpenGL renders and to specify objects to be rendered.
#include <gl/glut.h>
void display() {
glDisable(GL_DEPTH_TEST);
#include <gl/glut.h>
glutInit(&argc, argv);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(“Window Title”);
glutDisplayFunc(display);
glutMainLoop();
• glutInitWindowPosition(int x, int y) specifies the screen location for the upper-left corner of
your window.
• glutInitWindowSize(int width, int height) specifies the size, in pixels, of your window.
• int glutCreateWindow(char *string) creates a window with the title the same as the
parameter passed. It returns a unique identifier for the new window. Be warned: until
glutMainLoop() is called, the window is not yet displayed.
• If your program changes the contents of the window, sometimes you will have to call
glutPostRedisplay(), which gives glutMainLoop() a nudge to call the registered display
callback at its next opportunity. [more on this next time]
• Finally, we have glutMainLoop(). This function has to be executed for the window to be
displayed. Event processing begins, and the registered display callback is triggered. Once
the loop is entered, it is never left.
• display() is the function that we will implement to draw the graphics objects. The display()
function above displays a white square on the window we created earlier.
void display() {
glDisable(GL_DEPTH_TEST);
• glClearColor() establishes what color the window will be cleared to, and glClear() actually
clears the window. Once the clearing color is set, the window is cleared to that color whenever
glClear() is called. This clearing color can be changed with another call to glClearColor().
• glColor3f() command establishes what color to use for drawing objects —in this case, the color
is white. All objects drawn after this point use this color, until it’s changed with another call to
set the color.
• The next calls, which are bracketed by glBegin() and glEnd(), define the object to be drawn—
in this example, a polygon with four vertices. The polygon’s “corners” are defined by the
glVertex2f() commands. As you might be able to guess from the arguments, which are (x, y)
coordinates . In this case, the window is just like a normal coordinate from (-1 to 1)
• glFlush() ensures that the drawing commands are actually executed, rather than stored in a
buffer awaiting additional OpenGL commands.
• Points:
glBegin(GL_POINTS);
• Lines:
glBegin(GL_LINES);
• Triangles
glBegin(GL_TRIANGLES);
• Polygons
glBegin(GL_POLYGON);
• Now, draw the graphical primitives, and objects from them, changing the colors.