Title: Computer Graphics Conceptual Model
1Computer GraphicsConceptual Model
API
Output Devices
Application Program
Application Model
Graphics System
Input Devices
Function Calls or Protocol
Data
2What Is OpenGL?
- Graphics rendering API
- high-quality color images composed of geometric
and image primitives - window system independent
- operating system independent
- close to hardware
- leads to discussions of algorithms and
implementation
3OpenGL and the Windowing System
- OpenGL is concerned only with rendering
- Window system independent
- No input functions
- OpenGL must interact with underlying OS and
windowing system - Need minimal interface which may be system
dependent - Done through additional libraries AGL, GLX, WGL
4GLU and GLUT
- GLU (OpenGL Utility Library)
- part of OpenGL
- NURBS, tessellators, quadric shapes, etc.
- GLUT (OpenGL Utility Toolkit)
- portable windowing API
- not officially part of OpenGL
5OpenGL and Related APIs
application program
OpenGL Motif widget or similar
GLUT
GLX, AGLor WGL
GLU
GL
X, Win32, Mac O/S
software and/or hardware
6OpenGL as a Renderer
- Geometric primitives
- points, lines and polygons
- Image Primitives
- images and bitmaps
- separate pipeline for images and geometry
- linked through texture mapping
- Rendering depends on state
- colors, materials, light sources, etc.
7Components of a Graphics API
- Primitive functions
- What to draw (points, polygons, pixels, text)
- Primitive attributes
- How to draw it (color, pattern, style)
- Synthetic camera
- Viewing functions (position, orientation, lens)
- Transformation functions
- Rotate, scale, translate objects (models)
- Input functions
- Handle interactivity (keyboards, mouse)
- Control functions
- Communicate with window system
- Initialization, error handling
8Point and Line Segment Primitives
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
9Polygon Primitives
GL_POINTS
GL_POLYGON
P2
P1
P3
P0
P4
P7
P5
P6
GL_TRIANGLES
GL_QUADS
10Attributes for lines and polygons
11Filled Objects
12Different Attributes for Polygon
13Example of Stroke text
14Example of Raster text
15Display of Stoke text
16Display of Raster text
17OpenGL Primitive Syntax
glBegin ( type ) glVertex ( . . .
) . . . . glVertex ( . . .
) glEnd ( )
18Polygons
OpenGL only supports rendering for simple, convex
and flat polygon
1. Closed 2. Has an interior
Simple Well defined interior
Complex
Simple No pair of edges of a polygon cross each
other
19Polygons Defining the Interior
Simple Well defined interior
Complex
Exercise Design a test to determine whether a 2D
polygon is SIMPLE. Extend your test for 3D
polygon.
20Polygons Convexity
Convex
Non-Convex
P1
P2
Definition extensible to 3D.
21Setting Color Attribute in OpenGLRGB Mode
void glColor3b s i d f ub ud ui(TYPE r, TYPE
g, TYPE b) glColor3f(0.0, 0.0, 0.0)
/black/ glColor3f(1.0, 0.0, 0.0)
/red/ glColor3f(0.0, 1.0, 0.0)
/green/ glColor3f(0.0, 0.0, 1.0)
/blue/ glColor3f(1.0, 1.0, 0.0)
/yellow/ glColor3f(0.0, 1.0, 1.0)
/cyan/ glColor3f(1.0, 0.0, 1.0)
/magenta/ glColor3f(1.0, 1.0, 1.0) /white/
22Specifying a ColorColor Index Mode
void glIndexs i f d(TYPE c) OpenGL does not
provide intrinsic method of entering a colormap.
This is left to the underlying windowing system.
23Other Simple OpenGL Attributes
- glClearColor(1.0, 1.0, 1.0, 0.0)
- Sets background color to white
- Fourth argument is transparency 0.0 is opaque
- Sets a state variable
- glPointSize(2.0)
- Sets point size to be 2 pixels wide
- Note that this is not a device-independent
attribute - Sets a state variable
- glLinewidth (2.0)
- glLineStipple(Glint factor, Glushort pattern)
24Synthetic Camera
25Synthetic Camera Projection Geometry
Y
X
x,y,z
d
Z
COP
(0,0,-L)
x x ( d/(z L) ) y y( d/(z L) ) z d
- L
26Clipping
27Orthographic Projection
28Orthographic Projection
void glOrtho(Gldouble left, Gldouble right,
Gldouble bottom, Gldouble top, Gldouble near,
Gldouble far) void glOrtho2D(Gldouble left,
Gldouble right Gldouble bottom, Gldouble top)
29Transformation Matrices in OpenGL
Matrix Mode
3D Model Vertices
2D
3D
Vertices
Modelview
Projection
30Setting Viewing Matrix in GLA Simple Case
glMatrixMode(GL_PROJECTION) Sets the switch so
that loaded matrix goes into the projection
stack. glLoadIdentity() Pushes an identity
matrix onto the stack glOrtho2D(GLdouble left,
Gldouble right, Gldouble bottom,
Gldouble top) Sets the current view to an
orthographic projection with view volume bounded
by x left, x right, y bottom, y top, z
-1.0 and z 1.0.
31Viewport Transformation
MyWindow
w
Clipping Window
h
x
y
void glViewport(Glint x, GLint y, GLsizei w,
Glsizei h)
Default viewport corresponds to entire window
drawable area.
32GL Library Organization (under X Windows)
GLU
GL
OpenGl application program
Frame buffer
GLUT
Xlib, Xtk
GLX
33GLUT Basics
- Application Structure
- Configure and open window
- Initialize OpenGL state
- Register input callback functions
- render
- resize
- input keyboard, mouse, etc.
- Enter event processing loop
34Simple GLUT Window Management Functions
glutInit(int argc, char argv) Initializes a
window session. glutCreate Window(char
name) Creates a window with title
name. glutInitDisplayMode(GLUT_SINGLEGLUT_RGB)
Sets the display mode to single buffered and RGB
color. glutInitWindowSize (GLsizei h, GLsizei
w) Sets initial window size to h x
w. glutInitWindowPosition(x,y) Sets initial
window position to (x, y).
35Sample Program
void main( int argc, char argv ) int mode
GLUT_RGBGLUT_DOUBLE glutInitDisplayMode(
mode ) glutCreateWindow( argv0 ) init()
glutDisplayFunc( display ) glutReshapeFunc(
resize ) glutKeyboardFunc( key )
glutIdleFunc( idle ) glutMainLoop()
36OpenGL Initialization
- Set up whatever state youre going to use
- void init( void )
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 )
- glClearDepth( 1.0 )
- glEnable( GL_LIGHT0 )
- glEnable( GL_LIGHTING )
- glEnable( GL_DEPTH_TEST )
37GLUT Callback Functions
- Routine to call when something happens
- window resize or redraw
- user input
- animation
- Register callbacks with GLUT
- glutDisplayFunc( display )
- glutIdleFunc( idle )
- glutKeyboardFunc( keyboard )
38Rendering Callback
- Do all of your drawing here
- glutDisplayFunc( display )
- void display( void )
-
- glClear( GL_COLOR_BUFFER_BIT )
- glBegin( GL_TRIANGLE_STRIP )
- glVertex3fv( v0 )
- glVertex3fv( v1 )
- glVertex3fv( v2 )
- glVertex3fv( v3 )
- glEnd()
- glutSwapBuffers()
-
39Idle Callbacks
- Use for animation and continuous update
- glutIdleFunc( idle )
- void idle( void )
-
- t dt
- glutPostRedisplay()
-
40Form of Simplestglut/OpenGL program
include ltglut.hgt / glut.h includes gl.h and
glu.h / void init (void) / Here the
initialization of the drawing context. / void
display (void) /This function contains all of
the draw/redraw commands
41Form of Simplestglut/OpenGL program (slide 2)
void reshape (int w, int h) / What to do
whenever the window is resized. Usually includes
resetting the viewport / int main (int argc,
char argv) / create window / init
() glutDisplayFunc(display) / register display
/ glutReshapeFunc(reshape) / register reshape
/ glutMainLoop() return 0
42Sierpinski Gasket Source Code Slide 1
include ltGL/glut.hgt void myinit(void) /
attributes / glClearColor(1.0, 1.0, 1.0,
0.0) / white background / glColor3f(1.0,
0.0, 0.0) / draw in red / / set up viewing
/ / 500 x 500 window with origin lower left
/ glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D(0.0, 500.0, 0.0,
500.0) glMatrixMode(GL_MODELVIEW)
43Sierpinski Gasket Source Code Slide 2
void display( void ) / define a point data
type / typedef GLfloat point22
point2 vertices30.0,0.0,250.0,500.0,500.0
,0.0 / A triangle / int i, j, k
long random() / standard random number
generator / point2 p 100.0,100.0 / An
srbitrary initial point /
glClear(GL_COLOR_BUFFER_BIT) /clear the window
/
44Sierpinski Gasket Source Code Slide 3
/ computes and plots 5000 new points /
for( k0 klt5000 k) jrandom()3
/ pick a vertex at random / / Compute
point halfway between vertex and old point /
p0 (p0verticesj0)/2.0
p1 (p1verticesj1)/2.0 / plot
new point / glBegin(GL_POINTS)
glVertex2fv(p) glEnd()
glFlush() / clear buffers /
45Sierpinski Gasket Source Code Slide 4
void main(int argc, char argv) / Standard
GLUT initialization / glutInit(argc,argv)
glutInitDisplayMode (GLUT_SINGLE
GLUT_RGB) / default, not needed /
glutInitWindowSize(500,500) / 500 x
500 pixel window / glutInitWindowPosition(0,
0) / place window top left on display
/ glutCreateWindow("Sierpinski Gasket")
/ window title / glutDisplayFunc(disp
lay) / display callback invoked when
window opened / myinit() / set
attributes / glutMainLoop() / enter
event loop /