517543243-Appendix
517543243-Appendix
517543243-Appendix
Structure Page No
1.1 Introduction 111
Objectives
1.2 A Brief Overview 112
1.3 How to Install OpenGL? 114
1.4 Your First Program Using OpenGL 115
1.5 OpenGL Functions, Constants and Data Types 119
1.6 OpenGL Transformation Functions 122
1.7 Animation 123
1.8 Mouse and Keyboard Functions 125
1.1 INTRODUCTION
OpenGL stands for Open Graphics Library. By using it, you can create
interactive applications that render high-quality color images composed of 3D
geometric objects and images. Here the term 'Open' indicates open standards,
which means that many companies are able to contribute to the development of
OpenGL.
Objectives
After reading this supplement, you should be able to
• outline the basics of OpenGL;
• draw 2D objects with specified fill colors;
111
Computer Graphics • use OpenGL transformation function for 2D and 3D geometric
transformations;
• use simple animation to objects to be displayed in the scene;
• write interactive programs using mouse and keyboard functions of
OpenGL.
You will explore many more features of OpenGL later in this appendix.
Finally we conclude this section by telling you something about the rendering
pipeline of OpenGL. OpenGL Pipeline has a series of processing stages in
order (see Fig.1). Two graphical information, vertex-based data and pixel-
based data, are processed through the pipeline, combined together and then
written into the frame buffer. Notice that OpenGL can send the processed data
back to your application.
Fig. 1.
Display List: Because of its structure, OpenGL require lots of procedure calls
to render a complex object. To improve efficiency, OpenGL allows you to
generate an object, called a display list, into which OpenGL commands are
stored in an efficient internal format, which can be called back again in the
future.
Pixel Transfer Operations: After the pixels from client's memory are
unpacked (read), scaling, bias, mapping and clamping are performed on the
data. These operations are called Pixel Operations. The transferred data are
either stored in texture memory or rasterized directly to fragments.
Primitive Assembly: After vertex operation, the primitives (point, line, and
polygon) are transformed once again by projection matrix. These primitives are
clipped against viewing volume clipping planes, changing from eye
coordinates to clip coordinates. After that, perspective division by w occurs and
viewport transform is applied in order to map 3D scene to window space
coordinates. Last thing to do in Primitive Assembly is culling test if culling is
enabled.
OpenGL is a state machine. You put it into various states (or modes) that
remain in effect until you change them. For example, the current color is a state
variable. Suppose you set the current color to white. Every object is then
drawn with white color until you set the current color to something else. There
are many such state variables that OpenGL maintains. Another example that
you might be using in your codes quite often would be point size. You can take
it to be one pixel thick or it can be thicker with more pixels.
http://www.opengl.org/resources/libraries/glut.html
or,
http://www.xmission.com/~nate/glut.html
If your system has Visual Studio 2005 , then you may try this.
1. Copy the glut.h header file in C:\Program Files\Microsoft Visual Studio
8\VC\PlatformSDK\Include location
2. Copy the glut.lib file in C:\Program Files\Microsoft Visual Studio
8\VC\PlatformSDK\Lib location
3. Copy the glut32.lib file in C:\Program Files\Microsoft Visual Studio
8\VC\PlatformSDK\Lib location
114 4. Copy the glut32.dll file in c:\windows\system32
OpenGL Installation on Linux Appendix: OpenGL -
A Quick Start
Headers compatible with OpenGL are available from the Mesa3D project. If
your distribution does not contain development files for the Mesa3D project,
you may take help of Mesa3D that comes equipped with the usual installation
procedure.
./configure
make
make install
However, take care of conflicting OpenGL libraries. It may happen that Mesa's
software implementation overrides your distribution's libraries or manually
installed libraries.
Finally, the following url is useful for setting up OpenGL on any platform.
http://www.polytech.unice.fr/~buffa/cours/synthese_image/DOCS/Tutoriaux/N
ehe/lessons.htm
Now choose the type of window that you need for your application and
initialize it.
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
This helps in initializing the graphics display mode (of the GLUT library)
to use a window with RGB buffers for color and with a single buffer for
display. Other options are also available. For example, you may use
115
Computer Graphics GLUT_DOUBLE for using a double buffer. In animation programs, it is
advisable to use double buffer. For complete information on various
options available, you may log on to
http://glprogramming.com/red/appendedixd.html or refer to the book [1],
mentioned at the end of this Study Guide that is also called Red Book.
specifies the window position at (x,y) pixel location. This means, the
window is x pixels to the right and y pixels down the top left corner of
the display screen.
glutInitWindowSize (w, h);
specifies the window size to be h units high and w units wide. You may
choose height and width as per your program's specifications and object
size.
glutCreateWindow(“A Rectangle”);
• State Initialization: Initialize any OpenGL state that you don’t want to
change in every frame of your program. This might include many states
such as background color, positions of light sources, texture maps etc.
For example, you would need to specify the RGB component of
background color to be used when clearing the color buffer using the
following function.
glClear(GL_COLOR_BUFFER_BIT);
The most important callback function is the following which renders your
scene.
glutDisplayFunc(display);
Here display() is the function you will write to create your objects.
The program Blank.c displays nothing, except the blank window. This
is because your display function does not involve any display function.
Use of
glFlush() ;
glutMainLoop();
//Blank.c
#include <gl\glut.h>
/* Includes the OpenGL core header file. This file is required
by all OpenGL applications. */
//Main Loop
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//No window size or position chosen. Program will use default
values.
glutCreateWindow("A Blank Window");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} //Output will be a blank window
Let us now modify our program to draw a square in the window. This time set
the background color to black. Color of the solid square by default will be
white. Note that there is a very little change in your display function and in
initialization.
//Square.c
#include <gl\glut.h>
117
Computer Graphics void init(void)
{
//Reset the coordinate system
glClearColor(0.0f, 0.0f, 0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 500.0f, 0.0f, 500.0f, 1.0f, -1.0f);
}
//Main Loop
void main(void)
{
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("A Rectangle");
init();
glutDisplayFunc(display);
glutMainLoop();
}
Note: You might have noticed that in the program we did not use some
functions and still the program could be executed. For example, we have not
used any arguments in the main function main( ). We have also not used
glutInit( ) here. The idea is to tell you that the program runs without these
functions and arguments also. However, for more efficient use of memory and
for a flawless performance of your program, it is advisable to make use of these
functions and arguments.
For plotting points you can use the following code and add it to your display
function.
glBegin(GL_POINTS);
glVertex3f(100.0f, 150.0f, 150.0f);
glVertex3f(200.0f, 150.0f, 200.0f);
glEnd() ;
118
There are a number of primitives for object modelling and rendering the scene. Appendix: OpenGL -
A Quick Start
You are advised to visit the official website of OpenGL to see the reference
manual for more information on drawing and other primitives.
glutReshapeFunc(reshape);
Naming Conventions
Function names in the basic OpenGL library begin with the letters gl, and each
component word within a name has its first letter capitalized. For example-
glClear(), glBegin(). Fig. 2 for the glVertex*( ) function on the next page may
help you understand better the general naming conventions in gl and glut
libraries.
119
Computer Graphics Root command
gl Library
glVertex3fv( . . . )
Fig. 2.
There are certain functions which require arguments that are constant
specifying a parameter name, a value for a parameter, a particular mode, etc.
All such constants begin with the letters GL, component words in the constant
are written in capital letters, and the underscore " _ " is used as a separator
between the words. For example, we have used GL_RGB and
GL_COLOR_BUFFER_BIT in our earlier program.
For making the program platform independent and for fast processing of
various routines, OpenGL has defined its own data types which are as given in
Table-1 below.
Table-1
OpenGL colors are typically defined using RGB (red, green, blue) components
120 and a special A (or alpha) component. There are various ways in which you
could interpret A depending on the context (especially with reference to Appendix: OpenGL -
A Quick Start
transparency and color blending). Usually A = 1 when no special effects are
desired. We have used the function glutInitDisplayMode(unsigned int
mode) for setting up display mode. The mode is the logical-OR denoted by "x
| y" with various choices of x and y from the following:
Finally a brief discussion about the projection matrices. Fig. 3 gives you an
idea about the pipeline of transformations that you need to apply to render a 3D
scene. Remember that OpenGL is a 3D graphics standard and treats every
object as a 3D object. If the object is 2D, you can simply define that as a 3D
object sitting in the plane z = 0.
Fig. 3.
glMatrixMode(GL_PROJECTION);
//Specifies that the current matrix is prjection
matrix
glLoadIdentity();
//replace the current matrix with the identity
matrix
glOrtho(0.0f, 500.0f, 0.0f, 500.0f, 1.0f, -1.0f);
/* Sets / modifies the clipping volume extents
for orthographic projection. Parameters specify
left, right, bottom, top, near, far extents in
the order */
For more details, you are advised to refer to Unit 5, Secs. 5.3-5.5 of this Study
Guide.
You can use some of the following output primitives of OpenGL to practice
more on OpenGL programming.
121
Computer Graphics GL_POINTS: set of points
will rotate the object about z-axis by an angle of 45 degrees around the origin.
Similarly, you can use OpenGL scaling function glScale*( ) for 2D
transformations. Following example of display function will help you
understand how rotation and scaling of a square have been performed with
respect to a pivot point (0.5, 0.5)
To see the effect, replace the display function of one of your previous c-
program code with the above and you will see the following output on the
display window.
1.7 ANIMATION
OpenGL provides good support for animation design. Here we give a simple
example code showing the animation for two triangles. The executed code
displays falling triangles.
#include <gl/glut.h>
#include <math.h>
123
Computer Graphics //Draw a filled triangle with current color
glBegin(GL_TRIANGLES);
glVertex2f(xx,yy);
glVertex2f(xx+50,yy);
glVertex2f(xx+25,yy+25);
glEnd();
// Change color
glColor3f(0.0f,1.0f,0.0f);
// Draw another triangle 25 units away in x direction
// and 25 units down in y direction
glBegin(GL_TRIANGLES);
glVertex2f(xx+50,yy-10);
glVertex2f(xx+100,yy-10);
glVertex2f(xx+75,yy+15);
glEnd();
//Flush drawing commands and swap
glutSwapBuffers();
}
void myInit(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void main(void)
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Rotate");
124 glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape); Appendix: OpenGL -
glutTimerFunc(100,TimerFunction,1); A Quick Start
myInit();
glutMainLoop();
}
A few words about the functions that we have used in making this animation.
void glutPostRedisplay(void) ;
This function tells GLUT that the window needs to be repainted with current
changes. So if you need to re-draw the scene with changes, call this function.
void glutSwapBuffers(void) ;
This function flushes the OpenGL commands and swaps the buffer, in case you
have given the option of double buffer ( GLUT_DOUBLE ) in your code. If
double buffer is not used, then also OpenGL commands are flushed.
This function registers a callback function func that should be called after m
milliseconds have elapsed. The integer value is the user specified value that is
passed to the callback function. Most of the OpenGL implementations provide
double-buffering. When one is being displayed, the other is getting prepared
for display. When the drawing of a frame is complete, the two buffers are
swapped, so that one that was being viewed now becomes available for
drawing, and vice versa.
glutMouseFunc(mouseFunc);
This function allows you to link a mouse button with a
function that is invoked when a mouse button is pressed or
released. Here mouseFunc is the name of the function that is
invoked on mouse event.
125
Computer Graphics
glutKeyboardFunc(keyFunc);
This function specifies a function that is to be executed when a
particular key character is selected. This function can also
return the current mouse position in window coordinates.
glutMotionFunc(motionFunc);
This function specifies a function that is to be executed when a
mouse moves within the window while one or more buttons
are pressed.
You can try the following piece of code to understand how these functions
work.
The above function is required to be registered in your main program using the
following command.
glutMouseFunc(mouseFunc);
glutKeyboardFunc(keyFunc);
1. Dave Shreiner, Mason Woo, Jakie Neider and Tom Davis, OpenGL
Programming Guide: The Official Guide to Learning OpenGL, Fifth
Edition, Addison Wesley, 2006.
127