You are on page 1of 5

Computer graphics lab1

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.

About OpenGL and GLUT


• Contains rendering (drawing) commands but is designed to be independent of any window
system or operating system. For this reason, it does not contain any commands for opening
windows or reading events from the keyboard or mouse. But these are very important features
of most graphics applications.

• OpenGL doesn't provide high-level commands for describing models of three-dimensional


objects. Such commands might allow you to specify relatively complicated shapes such as
automobiles, parts of the body, airplanes, or molecules.

• 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]

• All OpengGL commands start with the letters gl


• All GLUT commands start with the letters glut

Creating a C or C++ file in MVC++ for use with OpenGL


• Launch Microsoft Visual C++
• Create a new Project. In the project list, choose Win32 console application. Give the project
name and then click next and ok
• Create a new source file. In the source file, choose either C or C++ and provide a file name

Writing your first OpenGL code

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);

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


glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glBegin(GL_POLYGON);// Each set of 4 vertices form a Square


glColor3f(1.0f, 1.0f, 1.0f); // white
glVertex2f(-0.5f, 0.5f); // x, y
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(-0.5f, -0.5f);
glEnd();
glFlush(); // Render now
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(“Window Title”);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

#include <gl/glut.h>

• From the previous step, you have an empty C or C++ file


• For all OpenGL applications, you need to include the OpenGL header files. These are
#include <GL/gl.h>
#include <GL/glu.h>
• We also need to include the GLUT header file, glut.h
#inlcude <GL/glut.h>
• But, as you can see from the file glut.h, it already includes glu.h and gl.h. So, we don't need to
explicitly include these two files if we include glut.h.
• You may also need to include some C or C++ libraries when needed.

glutInit(&argc, argv);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(“Window Title”);
glutDisplayFunc(display);
glutMainLoop();

• GLUT initialization – Window Mangement


• glutInit(int *argc, char **argv) initializes GLUT and processes any command line
arguments

• 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.

• The Display Callback


• glutDisplayFunc(void (*func)(void)) is the first and most important event callback function
you will see. Whenever GLUT determines that the contents of the window need to be
redisplayed, the callback function registered by glutDisplayFunc(), which is display(), in our
sample code, is executed. Therefore, you should put all the routines you need to redraw
the scene in the display callback function.

• 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.

• The registered Display() function

• 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(0.0f, 0.0f, 0.0f, 1.0f);


glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glColor3f(1.0f, 1.0f, 1.0f); // white


glBegin(GL_POLYGON);// Each set of 4 vertices form a Square
glVertex2f(-0.5f, 0.5f); // x, y
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(-0.5f, -0.5f);
glEnd();

glFlush(); // Render now


}

• glDisable(GL_DEPTH_TEST): OpenGL is designed for 3D graphics. Now, we will work only


with 2D graphics, so, we need to disable the z-axis buffer.

• 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.

Now run the code

To draw other graphical objects, use the following:

• 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.

You might also like