Professional Documents
Culture Documents
Topic: OpenGL
Name Id
Shariq Fareed BB-27214
Open GL:
Open GL stands for “Open Graphics Library”. A software API consisting of around several
hundred functions that allow you to talk to your graphics hardware. It is cross-platform and the
most commonly used in professional graphics applications.
By using OpenGL, a developer can use the same code to render graphics on a Mac, PC, or mobile
device. Nearly all modern operating systems and hardware devices support OpenGL, making it an
easy choice for graphics development. Additionally, many video cards and integrated GPUs are
optimized for OpenGL, allowing them to process OpenGL commands more efficiently than other
graphics libraries.
OpenGL comes in two varieties. The first is Microsoft OpenGL, which is often included in
Windows or the installation software of a graphics card. The second is Cosmo OpenGL, which is
designed for computer systems that don't have accelerated graphics card.
OpenGL serves two main purposes:
1. Hide complexities of interfacing with different 3D accelerators by presenting a single,
uniform interface.
2. Smooth over differing capabilities of hardware platforms by requiring support of the full
OpenGL feature set for all implementations (using software emulation if necessary).
Today, OpenGL is managed by the non-profit technology consortium Khronos Group.
Example of OpenGL
Triangle
Introductory program; just a static picture of a colored triangle.
Shows how to use GLUT.
Has minimal structure: only main() and a display callback.
Uses only the default viewing parameters (in fact, it never mentions viewing at all). This
is an orthographic view volume with bounds of -1..1 in all three dimensions.
Draws only using glColor and glVertex within glBegin and glEnd in the display callback.
Uses only the GL_POLYGON drawing mode.
Illustrates glClear and glFlush
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
OpenGL Libraries
gl: OpenGL Core library (rendering, drawing vertices,…..)
glu: OpenGL Utility library (transforming ,rotating, mathematical operations over
GPU,….)
glut: OpenGL Utility toolkit library (multiplatform window system interface)
wgl,cgl,glx: Advance window system interface for OS (windows, mac,Lunix,…..)
Basics OF OpenGL
OpenGL Utility Library (GLU)
The GLU is included with OpenGL and built using low-level OpenGL commands. It contains
routines for setting up viewing and projection matrices, polygonal tesselation and surface
rendering. When using the GLU library you will need to #include <GL/glu.h> in your program.
GLU routines begin with the prefix glu.
OpenGL Extension to the X Window System (GLX)
OpenGL does not include any routines for handling windowing operations so the GLX was
developed to fill this gap with regard to the X Window System. Using GLX, you can enable your
OpenGL programs to draw into a window under the X Window System. GLX routines begin with
the prefix glx.
OpenGL Utility Toolkit (GLUT)
The GLUT is a window system-independent toolkit written to hide the complexities of differing
window system APIs. It is much easier to use and more portable but far less featured than the GLX
library. Functions performed include window definition, window control, and monitoring of
keyboard and mouse input. GLUT also has limited support for creating pop-up menus. If you are
going to use the GLUT library you will need use #include <GL/glut.h> instead of #include
<GL/gl.h> in your program (Note that glut.h includes gl.h, glu.h, and glx.h automatically). GLUT
routines begin with the prefix glut.
To get you started here is an example C Makefile for a typical GLUT based OpenGL program:
CC = gcc
INCLUDE = -I/usr/X11R6/include
OPTIM = -O2 -g
CFLAGS = ${INCLUDE}
LIBS = -L/usr/X11R6/lib
.c:
$(CC) $(CFLAGS) ${OPTIM} $< ${LIBS} ${GLIBS} -o $@
To compile your C application using this Makefile you would do the following:
OpenGL Functions
Window Manager Setup Using glut
glutInit(&argc, argv) - initializes GLUT, processes any command-line arguments for X
functions. Call this before anything else.
glutInitDisplayMode( unsigned int mode ) - sets the basic display modes. For more than one
choice, OR the constants together. Usual mode constants are:
GLUT_SINGLE - single buffering
GLUT_DOUBLE - double buffering
GLUT_RGB - set to full color mode
GLUT_RGBA - same as RGB
GLUT_INDEX - set color index mode
GLUT_DEPTH - set up a depth buffer
glutInitWindowSize( int width, int height ) - sets size of screen window in pixels
glutInitWindowPosition( int x, int y ) - sets initial window position on the screen for upper left
corner
glutCreateWindow( char* title ) creates a top-level window labeled with title.
glutDisplayFunc( void (*func)(void) ) - sets the callback function, provided by your program, that
GLUT will call when a window must be redisplayed (e.g. on resize). Simply pass the name of a
void function that takes no parameters.
glutMainLoop( ) - start the event loop; never returns.
Window Manager Input Handling Using glut
glutReshapeFunc( void (*func)(int width, int height) ) - registers the callback function
that GLUT calls on window reshape events.
glutKeyboardFunc( void (*func)(unsigned char key, int x, int y) - registers the callback
function that GLUT calls on a keyboard event. The parameter key is the ASCII
value of the key pressed. Parameters x and y indicate the mouse position at the
time of the key press.
glutMouseFunc( void (*func)(int button, int state, int x, int y) ) - registers the callback
function that GLUT calls on a mouse event. The parameter button may be
GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or
GLUT_RIGHT_BUTTON (a two button mouse may not generate the middle
value). The parameter state may be GLUT_UP or GLUT_DOWN indicating
button release or press, respectively. Parameters x and y indicate the mouse
position.
glutMotionFunc( void (*func)(int x, int y) - registers the callback for a mouse move
event while a button is pressed.
glutIdleFunc( void (*func)(void)) - registers the callback for idle time (no events)
glutPostRedisplay( void ) - "nudges" the main loop to call display( ); sometimes useful
after keyboard or mouse input changes some display parameters.
glFlush( ) - flush the graphics output buffer.
glFinsh( ) - waits here for flush to complete.
glOrtho(double left, double right, double bottom, double top, double near, double far )
- sets the projection matrix to orthographic (parallel), with the near clipping plane
defined by (left, bottom, -near) and (right, top, -near) and the far clipping plane
defined by (left, bottom, -far) and (right, top, -far).
gluOrtho2D(double left, double right, double bottom, double top) - sets the 2-
dimensional clipping rectangle as (left, bottom) and (right, top).
gluPerspective(double fieldofview, double aspectratio, double near, double far) -
defines a perspective projection with the given field of view, aspect ration, and
distance to the near and far clipping planes.
gluFrustum(double left, double right, double bottom, double top, double near, double
far) - defines a perspective projection with the near clipping plane as (left,
bottom, -near) and (right, top, -near) and the far clipping plane at distance far
from the eye point.
glClearColor( float red, float green, float blue, float alpha ) - set the clear color to (red,
green, blue) with value alpha.
glClear( GLbitfield mask ) - clear the buffer indicated by mask using the current clear
color. Values for mask are:
GL_COLOR_BUFFER_BIT - color buffer
GL_DEPTH_BUFFER_BIT - depth buffer
GL_ACCUM_BUFFER_BIT - accumulation buffer
GL_STENCIL_BUFFER_BIT - stencil buffer
glColor3f( float red, float green, float blue ) - set the current drawing color to (red,
green, blue).
Lighting