Open GL
Open GL
What is OpenGL
OpenGL is a software interface to graphics hardware.
Graphics rendering API
Rendering?–converting geometric or
mathematical object descriptions into frame
buffer values.
high-quality color images composed of geometric
and image primitives
window system independent
operating system independent
This interface consists of 150 distinct commands they are
used to specify the object and operations needed to produce
interactive 2D & 3D graphics application.
Currently there are 3 major graphics APIs currently
being used right now.
Function output
Application calls Graphics I/O
Programs system Devices
Data input
OpenGL has seven group of functions
1. Primitive functions
2. Attribute functions
3. Viewing functions
4. Transformation functions
5. Input functions
6. Control functions
7. Query functions
1. Primitive functions- Define the low-level object entities such as
points, line segments ,polygons, pixels, text and various types of
curves and surface.Eg: : glBegin,glVertex
2. Attribute functions- Govern the way that a primitive appears on
the display (color, pattern, filling, typeface, text styles
etc.,)Eg:glColor3f()
3. Viewing functions- API allows us to clip out objects that are too
close or too far away (synthetic camera position and degree of
orientation)
4. Transformation functions- API should provides the user with a set
of transformations functions that allows to carry out
transformation of objects such as rotation, scaling and translation.
5. Input functions- An API must provide input functions to deal with
different input devices (key boards, mouse, light pen, tablets etc.,)
6. Control functions- These functions enable us to communicate with
the window system, to initialize our programs, and to deal with
any errors that take place during the execution of our programs.
7. Query functions- A good API must provide information through a
set of query functions to write device-independent programs , to
use various camera parameters and values in the frame buffer.
TRANSFORMATION FUNCTIONS
(The OpenGL Interface)
In OpenGL all graphic functions are stored in three Libraries
1. GL (OpenGL in windows)- The functions in this library have names
that begin with letters gl and are stored in library GL
2. GLU (OpenGL Utility Library)- This library uses GL functions and
contains code for generating objects and simplifying views.
Function GLU library begin with “glu". They are used for 1. Setting
up matrices for viewing transformation 2. Rendering surfaces 3.
performing polygon tessellation.
3. GLUT(OpenGL Utility Toolkit)- Used to interface with the window
system and to get input from external devices.
GLX, Xlib and Xtk are used by x-windows
GLU
OpenGL Frame
GL
Application Buffer
Program Xlib, Xtk
GLUT
6
GLX
Abstractions
GLUT
Windowing toolkit (key board, mouse handler, window
events)
GLU
• Viewing –perspective/orthographic
• Image scaling, polygon tessellation
• Sphere, cylinders, quadratic surfaces
GL
GL_LINES
GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP
GL_POINTS
GL_TRIANGLES
GL_QUADS
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
8
GL_POINTS each vertex is displayed as one
pixel
GL_LINES Takes successive pair of
vertices(lines are disconnected)
GL_LINE_STRIP Successive vertices are
connected
GL_LINE_LOOP polyline are closed.
POLYGON
Polygon is an object that has
1.Border that can be describe by line
loop.
2.Defined interiors.
3 properties of a polygon
Simple, Convex, Flat
If no two edges of a polygon cross each other it’s a
simple polygon.
convex non-convex
Data types supported in OpenGL
Suffix Data Type Typical Corresponding C- OpenGL Type
Language Type Definition
b 8-bit integer signed char GLbyte
glVertex3fv( v )
Advantages
Disadvantages
25
Orthographic Viewing
In the default orthographic view,
points are projected forward
along the z axis onto the
plane z=0
Coordinate Systems
The units in points are determined by the application and
are called
– object (or model) coordinates model view transform
– world coordinates
Viewing specifications usually are also in object coordinates
transformed through
-- eye (or camera) coordinates
--clip coordinates projection transform
--normalized device coordinates
--window (or screen) coordinates
• OpenGL also uses some internal representations that usually are not visible
to the application but are important in the shades
Coordinate Systems and Transformations
Steps in Forming an Image
specify geometry (world coordinates)
specify camera (camera coordinates)
project (window coordinates)
map to viewport (screen coordinates)
Each step uses transformations
Everytransformation is equivalent to a change
in coordinate systems (frames)
Camera Analogy and Transformations
Projection transformations
adjust the lens of the camera
Viewing transformations
tripod–define
position and orientation of the viewing
volume in the world
Modeling transformations
moving the model
Viewport transformations
enlarge or reduce the physical photograph
OpenGL Geometric Transformations
glMatrixMode(GL_MODELVIEW);
Matrix modes
OpenGL pipeline architecture depend on multiplying or
concatenating a number of transformation matrices to
achieve the desired image or primitive.
The values of these matrices are part of the state of
the system
In OpenGL Model-view and Projection are two
important matrices used.
Initially these matrices are identity matrices.
The set commands used for two-dimensional viewing
are
32
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
Functions:
glFunction: glBegin, glClear, glVertex, …
Constants:
GL_CONSTANT: GL_2D, GL_LINE
Datatypes:
GLtype: GLbyte, GLint, GLfloat
H&B 3-5:64-72
Example
glClearColor(1.0,1.0,1.0,0.0);// Background color
glMatrixMode(GL_PROJECTION); // Set transformation
glLoadIdentity;
gluOrtho2D(0, 200, 0, 150);
// Set color
Note:
• Objects are drawn in the current local axis-frame;
• With transformations this frame can be changed.
View Ports
•Do not have use the entire window for the
image: glViewport(x,y,w,h)
•Values in pixels (screen coordinates)
void glViewport(Glint x, Glint y, Glsizei w, Glsizei h)
(x,y)-Lower left corner of view port, w:h is the aspect ratio
48
Camera Analogy
camera
tripod model
Transformations in OpenGL
Modeling
Viewing
orient camera
projection
Animation
Map to screen
Polygon and Recursion (Gasket Problem)
1. Draw a triangle
2. Draw line segments connecting the midpoints of the sides of the triangle
3. Divide the triangle into four triangles
4. Middle triangle contains no points
5. Repeat step 2 to 4 subdivided triangles
Eg: Program for Sierpinski gasket that uses polygons and not require the use of a random number
generator.
Three –Dimensional Gasket
3D pictures are not restricted to a plane
Basic: Triangle in 2D=Tetrahedron in 3D
Tetrahedron is convex
The midpoint of a line segment between a vertex and any point inside a tetrahedron is
also inside the tetrahedron
Eg: 3D-Program for Sierpinski gasket that uses points and a random number generator .
51
Use of polygons in three dimensions
Eg: 3D- Program for Sierpinski gasket that uses polygons (tetrahedron) and not
require the use of a random number generator.
52
3D Gasket
53