You are on page 1of 7

Assignment

Topic: OpenGL

Instructor: Dr Shahid Karim

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

// A simple introductory program; its main window contains a static


picture
// of a triangle, whose three vertices are red, green and blue. The
program
// illustrates viewing with default viewing parameters only.

#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

// Clears the current window and draws a triangle.


void display() {

// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);

// Drawing is done by specifying a sequence of vertices. The way these


// vertices are connected (or not connected) depends on the argument to
// glBegin. GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();

// Flush drawing command buffer to make drawing happen as soon as


possible.
glFlush();
}

// Initializes GLUT, the display mode, and main window; registers


callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-
buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Position window at (80,80)-(480,380) and give it a title.


glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");

// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);

// Tell GLUT to start reading and processing events. This function


// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}

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

GLIBS = -lGL -lGLU -lglut -lm

.c:
$(CC) $(CFLAGS) ${OPTIM} $< ${LIBS} ${GLIBS} -o $@

To compile your C application using this Makefile you would do the following:

scc1% make Mywork.c

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.

Projection and Viewport Transformations

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.

Color and Clearing the Screen

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

glLights{if}{v}( light#, param, vector ) - specify a light# (from GL_LIGHT0, … ,


GL_LIGHT7) with some parameter and value(s). Choose from
GL_AMBIENT, GL_DIFFUSE, or GL_SPECULAR to set light colors;
GL_POSITION (vector indicates either within-scene position if vector[3] is
non-zero, or directional light if vector[3] is 0), GL_SPOT_DIRECTION,
GL_SPOT_EXPONENT, and GL_SPOT_CUTOFF (an angle) for a spotlight,
or values for distance attenuation using GL_CONSTANT_ATTENUATION,
GL_LINEAR_ATTENUATION, and GL_QUADRATIC_ATTENUATION.
glLightModelf{v}( param, vector or value) - set the light model from
GL_LIGHT_MODEL_AMBIENT, GL_LIGHT_MODEL_LOCAL_VIEWER,
GL_LIGHT_MODEL_TWO_SIDE. The latter two use the values GL_TRUE
or GL_FALSE.
glEnable( GL_LIGHTING ) - to enable the lighting system
glEnable( GL_LIGHT0 ) - or GL_LIGHT1, etc., to enable specific lights

You might also like