Open GL
Open GL
Application Graphics
Hardware
Program Library (API)
OpenGL
objects image
(triangles) (pixels)
viewer
Physical reality (sort of)
for (each photon)
for (each triangle) lights
(photons)
for (each pixel)
draw;
objects image
(triangles) (pixels)
Ray tracing
for (each pixel)
for (each triangle) lights
(photons)
for (each light)
draw;
objects image
(triangles) (pixels)
Physical reality (sort of)
for (each light)
for (each triangle)
lights
for (each pixel)
draw;
objects image
(triangles) (pixels)
Traditional graphics pipeline (OpenGL)
for (each triangle)
for (each light)
lights
for (each pixel)
draw;
objects image
(triangles) (pixels)
Modern graphics pipeline (OpenGL 2.1)
for (each triangle)
for (each pixel)
lights
for (each light)
draw;
objects image
(triangles) (pixels)
What is OpenGL?
• With OpenGL, you must build your desired model from a small set
of geometric primitives
o Points, lines, and polygons.
What OpenGL doesn’t do?
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
Geometry Path It is a group of OpenGL
commands that have been
stored (compiled) for later
Vertex Vertex Primitive execution.
Data Operation Assembly
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
Geometry Path Each vertex and normal coordinates are
transformed by GL_MODELVIEW matrix
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
• Primitives are transformed eye
Geometry Path coordinates to clip coordinates.
• Viewport transform is applied in
order to map 3D scene to window
Vertex Primitive space coordinates.
Vertex
• Last thing to do in Primitive
Data Operation Assembly
Assembly is culling test if culling is
enabled.
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
• After reading the pixels, the data
Geometry Path are performed scaling, bias,
mapping and clamping.
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
Geometry Path Texture images are loaded into
texture memory to be applied onto
geometric objects.
Vertex Vertex Primitive
Data Operation Assembly
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
• It is the conversion of both
Geometry Path geometric and pixel data into
fragment.
Pixel
Pixel
Transfer
Data
Operation
Image Path
OpenGL Rendering Pipeline
Scissor Test ⇒ Alpha Test ⇒
Geometry Path Stencil Test ⇒ Depth Test.
Pixel
Pixel
Transfer
Data
Operation
Image Path
The OpenGL State Machine
Lines:
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();
glBegin(GL_POINTS);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();
Transformations
[x y z w] Model View
Matrix
Eye
Projection
coordinates Matrix
Clip Perspective
Coordinates Division
Viewport
Normalized Transformatio Window
Device n
Coordinate
Coordinate
Transformations
Transformations
Local
Coordinate
System
World
Model Coordinate
Matrix System
Camera
View Coordinate
Matrix System
Projection
Orthographic Perspective
OpenGL Example
…
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();/*clear the matrix*/
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0);
/* modeling transformation */
glScalef (1.0, 2.0, 1.0);
glutWireCube (1.0);
glFlush ();
…
OpenGL Example
gluLookAt
(0.0, 0.0, 5.0, // Camera position
0.0, 0.0, 0.0, // Lens aim at
0.0, 1.0, 0.0);// Camera orientation
OpenGL Example
…
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();/*clear the matrix*/
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0);
/* modeling transformation */
glScalef (1.0, 2.0, 1.0);
glutWireCube (1.0);
glFlush ();
…
Modeling Transformations
• set_color(RED);
• draw_item(A);
• draw_item(B);
• set_color(GREEN);
• set_color(BLUE);
• draw_item(C);
glColor3f
void display() {
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glColor3f(0.5f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
glEnd();
glutSwapBuffers();
}
glColor3f (contd.)
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.5f, 0.0f, 1.0f);
glVertex2f(-0.75, 0.75);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.75, -0.75);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.75, -0.75);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(0.75, 0.75);
glEnd();
glutSwapBuffers();
}
Output
Keyboard & Mouse handling
o void keyPressed
(unsigned char key, int x, int y) {}
o freeglut3-dev
o mesa-common-dev
• Command
o sudo apt-get install freeglut3 freeglut3-dev mesa-common-dev
Windows
• Runtime libraries:
o C:\Windows\System32\{opengl,glu}32.dll
• On 64-bit Windows:
o C:\Windows\SysWOW64\{opengl,glu}32.dll
• Header files:
o C:\Program Files\Microsoft SDKs\Windows\v7.1A\Include\gl\
{GL,GLU}.h
• Linker library:
o C:\Program Files\Microsoft SDKs\Windows\v7.1A\Lib\OpenGL32.Lib
Windows
• Runtime library:
• Header file:
o C:\Program Files\Microsoft Visual Studio *\VC\include\GL\
glut.h
• Linker library:
o C:\Program Files\Microsoft Visual Studio *\VC\lib\glut32.lib
Windows (in VC not there)
• Runtime library:
o C:\Windows\system\glut32.dll
• Header file:
o C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\GL\
glut.h
• Linker library:
o C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\glut32.lib
Linux
• You will have to use the -lglut linker option with gcc/g++ to
compile a program with glut library.
• For example,
o Name of the program to be compiled
cube.c
o Command
gcc -o cube cube.c -lglut -lGLU
Example
Start Visual C++ and create a new empty project of type Win32 Console
Application