©Zachary Wartell, UNCC9/28/ :30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights Reserved
©Zachary Wartell, UNCC9/28/ :30 AM 2 Graphics APIs Generally these API’s support both 2D and 3D graphics: ●GKS (Graphics Kernal System) ●PHIGS (Programmer Hierarchical Interactive Graphics Standard) ●PHIGS+ ●GL (Graphics Library) by SGI ●OpenGL ●DirectDraw, Direct3D by Microsoft
©Zachary Wartell, UNCC9/28/ :30 AM 3 OpenGL ●OpenGL – hardware independent, only specifies graphics output low level, no GUI operations (events, windows, menu’s) ●OpenGL Utility (GLU) – higher level graphics primitives (spheres, curves, curved surfaces) ●OpenGL Utility Toolkit (GLUT) – primitive window setup & event handling
©Zachary Wartell, UNCC9/28/ :30 AM 4 OpenGL Block Diagram input commands –vertex coordinates and vertex attributes –pixel blocks –state changes (affects how input data is rendered) output commands –read current state, read framebuffer, etc. commands
©Zachary Wartell, UNCC9/28/ :30 AM 5 OpenGL Basic Syntax (C Language Binding) ●Functions: glXXXX – glBegin, glEnd, glPolygonMode ●Constants: GL_XXXX – GL_2D, GL_RGB, GL_CCW ●Types: GLxxx – GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean
©Zachary Wartell, UNCC9/28/ :30 AM 6 OpenGL Geometric Output Primitives ●Types: GL_POINTS, GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, GL_POLYGON, GL_QUADS, GL_QUAD_STRIP, GL_TRIANGLE, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN ●Basic Syntax: glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd(); glVertex(2|3|4)(s|i|d|f)[v]
©Zachary Wartell, UNCC9/28/ :30 AM 7 OpenGL: A State Machine OpenGL state remains in effect until it is changed Examples: current - color, viewing/projection transforms, line/polygon fill pattern, light positions, etc, etc, etc.. Each state variable has default value and can be queried (glGetIntegerv, glGetFloatv, etc.) and enabled/disable collections of state can also be saved (glPushAttrib,glPopAttrib) commands
©Zachary Wartell, UNCC9/28/ :30 AM 8 Display Lists immediate vs retained mode display list can collect geometry and attributes to be stored for later use; allows optimizations display list are write-only; no other structure within objects (OpenInventor, Java3D, VRML, etc.) commands
©Zachary Wartell, UNCC9/28/ :30 AM 9 Evaluators (3D stuff) a set of functions that generates vertex coordinates lying on various types of curves surfaces uses polynomial mapping. Can produce surface normals, texture coordinates, colors, vertex coordinates given a set of control points commands
©Zachary Wartell, UNCC9/28/ :30 AM 10 Per-Vertex Operations vertices are transformed by 4x4 matrices (model&view, projection) in preparation for primitive assembly texture coordinates can be directly specified or generated and also transformed lighting calculations are also performed with transformed vertex and normals based on lighting attributes commands
©Zachary Wartell, UNCC9/28/ :30 AM 11 Primitive Assembly points, lines, polygon edges are clipped and transformed to window coordinates resulting primitives are then passed to rasterization stage commands
©Zachary Wartell, UNCC9/28/ :30 AM 12 Points & Lines v0 v1 v2 v3 glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd(); v0 v1 v2 v3 v4 v5 GL_POINTS GL_LINES GL_LINE_STRIP v0 v1 v2 v3 v4 GL_LINE_LOOP
©Zachary Wartell, UNCC9/28/ :30 AM 13 Polygons glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd(); GL_POLYGON v0 v1 v2 v3 v4 v0 v1 v2 v3 v4 v5 v6 v7 GL_QUADS v0 v1 v2 v3 v4 v5 v6 v7 GL_QUAD_STRIP
©Zachary Wartell, UNCC9/28/ :30 AM 14 ©Zachary Wartell - 1/26/2005 Polygons (2) glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd(); v0 v1 v2 v3 v4 v5 GL_TRIANGLES GL_TRIANGLE_STRIP v0 v1 v2 v3 v4 v5 GL_TRIANGLE_FAN v0 v1 v2 v3 v4 v5 N vertices N/3 Triangles N+2 vertices N Triangles N+2 vertices N Triangles
©Zachary Wartell, UNCC9/28/ :30 AM 15 Why glBegin/glEnd? ● trying to define a separate function for each possible combinations of vertex data too cumbersome: glBegin (PrimitiveConstant); glColor3f (c1,c1,c1) glTexCoord2f(t1,t2) glVertex3f (x1,y1,z1) glColor3i (c1,c2,c3) glNormal3f (n1,n2,n3) glVertex3f (x2,y2,z2) …. glEnd(); -vertex uses last specified color/normal/texCoord. Allows application to avoid redundant specification, but every app. may want different combinations -Also lots of combinations of (s|i|f|d)[v] especially when combined with all possible combinations of glColor ×glTexCoord × glNormal × glVertex
©Zachary Wartell, UNCC9/28/ :30 AM 16 Why glBegin/glEnd? ● allow maximum possible parallelism in host CPU and Graphics Processor glBegin (PrimitiveConstant); compute x1,y1,z1 coordinate glVertex3f (x1,y1,z1) compute x2,y2,z2 coordinate glVertex3f (x2,y2,z2) …. glEnd(); CPU to GP communication and GP processing of x1,y1,z1 can potentially occur while CPU does this
©Zachary Wartell, UNCC9/28/ :30 AM 17 Vertex Arrays ● downside of glBegin/glEnd is lots of function calls ●OpenGL Vertex Arrays -drastically reduce function call overhead -support most common combinations of vertex data -allow sharing of vertex coordinates for adjacent primitives glEnableClientState (GL_VERTEX_ARRAY); glVertexPointer ( 3 /* N-coordinates */, GL_FLOAT /* coordinate type */, 0 /* stride */, vertex_coordinates); glDrawElements (GL_QUADS /* primitive type */, 24, /* number of vertices */ GL_UNSIGNED_BYTE, /* type of index value */ vertex_indices ); -void* parameter, actual type determined by 3 rd parameter
©Zachary Wartell, UNCC9/28/ :30 AM 18 Pixel Operations pixels packed or unpacked into RGB(A) components; also may be scaled, biased or transformed results are clamped and sent to texture memory OR rasterization pixel data also can be read from framebuffer commands
©Zachary Wartell, UNCC9/28/ :30 AM 19 Pixel-Array Primitives glBitmap (width, height, x0, y0, xOffset, yOffset, bit_array) ● width, height - size of bit map to draw (in last specified color) ●x0, y0 – defines window coordinate of lower-left corner of bit_array ● xOffset, yOffset – added to current raster position (see glRasterPos2i); useful for text output ● bit_array – bits encode in unsigned byte array
©Zachary Wartell, UNCC9/28/ :30 AM 20 glBitmap Figure 3-61: Hearn & Baker
©Zachary Wartell, UNCC9/28/ :30 AM 21 glDrawPixels glDrawPixels (width, height, dataFormat, dataType, pixel_array) ● width, height - size of pixel array map to draw ●dataFormat – GL_RGB,GL_RED, GL_RGBA, GL_DEPTH_COMPONENT, etc. ●dataType – GL_UNSIGNED_BYTE, GL_BYTE, etc. ●pixel_array – memory encoding data -data write to current raster position (glRasterPos)
©Zachary Wartell, UNCC9/28/ :30 AM 22 OpenGL Raster Operations ●raster operations, bitblt’s (bit-block transfers), pixblt’s ● glReadPixels (xmin, ymin, width, height, dataFormat, dataType, pixel_array) -reads pixel data from framebuffer to main memory -xmin, ymin, width, height – rectangle in framebuffer to read -dataFormat – GL_RGB,GL_RED, GL_RGBA, GL_DEPTH_COMPONENT, etc. -dataType – GL_UNSIGNED_BYTE, GL_BYTE, etc. -pixel_array – memory encoding data ●glCopyPixels – from glReadBuffer to glDrawBuffer
©Zachary Wartell, UNCC9/28/ :30 AM 23 Texture Assembly Textures (images + lots of texture rendering options) can be bound to texture objects; then you can switch between texture objects Multiple textures layers commands
©Zachary Wartell, UNCC9/28/ :30 AM 24 Rasterization scan convert coordinate primitives (lines, polygons, etc.) or pixel data into fragments fragment is pixel color + depth value + stencil + accumulation commands
©Zachary Wartell, UNCC9/28/ :30 AM 25 Fragment Operations sequence of operations that may discard fragment blending, dithering, logic combination (and/or/xor) commands
©Zachary Wartell, UNCC9/28/ :30 AM 26 Revisions Revision 1.1: 1.typos 2.Added copyright 3.formatted master Slide 1.Revision added GL pipeline slides