Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys
Introduction to OpenGL OpenGL is a graphics API –Software library –Layer between programmer and graphics hardware (and software) OpenGL can fit in many places –Between application and graphics system –Between higher level API and graphics system
Programmer’s View Application Graphics PackageApplication OpenGL Application Programming Interface Hardware and software Output Device Input Device
Why OpenGL? Fast Simple Window system independent Supports some high-end graphics features Geometric and pixel processing Standard, available on many platforms
OpenGL As a Renderer Renders simple geometric primitives –Points, lines, polygons Renders images and bitmaps Separate pipelines for geometry and pixel processing, linked through texture mapping The output of the renderer depends on state –Current color, light sources, current material, current texture, current normal, etc
OpenGL Buffers OpenGL supports a variety of buffers that can be used for rendering –Color buffers (front, back, left, right) –Depth buffer (used to store z information) –Accumulation buffer –Stencil buffer Window system interactions must be handled outside of OpenGL –GLUT –Motif –GLX –Tcl/TK –…
OpenGL Primitives Points Lines Polygon Triangle Quad Quad Strip Triangle StripTriangle Fan
GLUT 3D Primitives Cube Sphere Teapot And others…
Simple OpenGL Program #include void display(void) { glClearColor( 0, 0, 0, 0 ); glClear( GL_COLOR_BUFFER_BIT ); glColor3f( 1, 1, 1 ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( -1, 1, -1, 1, -1, 1 ); glBegin( GL_POLYGON ); glVertex2f( -.5, -.5 ); glVertex2f( -.5,.5 ); glVertex2f(.5,.5 ); glVertex2f(.5, -.5 ); glEnd(); glFlush(); }
Window System Glue int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize( 250, 250 ); glutCreateWindow( “We love COMS 4160” ); glutDisplayFunc( display ); glutMainLoop(); }
OpenGL Rendering Pipeline Geometry Primitive Operations Pixel Operations Scan Conversion Texture Memory Fragment Operations Framebuffer Vertices Images
Viewing consists of two parts –Object positioning: model view transformation matrix –View projection: projection transformation matrix OpenGL supports both perspective and orthographic viewing transformations OpenGL’s camera is always at the origin, pointing in the –z direction Transformations move objects relative to the camera Viewing in OpenGL
Outline Introduction (Chapter 1) Geometry, Drawing (Chapter 2) Transformations, Viewing (Chapter 3) Color, Types (Chapter 4) Lighting (Chapter 5) Putting it all together… Textures (Chapter 9) Display Lists, Vertex Arrays, etc. (Chapter 7) Advanced Topics, Extensions Rest of red book should be read…
Introduction (Chapter 1) Examples (color plates) Code sample (page 6) OpenGL as a state machine OpenGL Rendering pipeline (page 11) –Geometry: Points, lines, polygons, GLUT primitives –Pixels: Images, Bitmaps –Texture Mapping –Depends on state (color, material, texture, etc.) Animation, Double Buffering –Buffers: Color (front, back); Depth (z); Accumulation; Stencil
Drawing (Chapter 2) glClearColor(red, green, blue, alpha) [0,0,0,0] glClear (GL_COLOR_BUFFER_BIT) –Other buffers… glColor3f (r,g,b) Draw (assembly line: Transform, clip, shade, …) –Client/Server glFlush() (forces client to send network packet) –Doesn’t wait for completion; only begins execution –Previous commands execute in finite time glFinish() (waits for ack that drawing complete) –Synchronization, sparing use
Window System Interaction Not part of OpenGL Toolkits (GLUT) available Callback functions for events –Keyboard, Mouse, etc. –Open, initialize, resize window –Similar to other systems (X, Java, etc.) Menus, Animation etc. Typical main function (pp 100)
Geometry Points (GL_POINTS) –Stored in Homogeneous coordinates Line segments (GL_LINES) Polygons –Simple, convex (take your chances with concave) –Tessellate, GLU for complex shapes –Rectangles: glRect Special cases (strips, loops, triangles, fans, quads) –Pages 44, 45 More complex primitives (GLUT): Sphere, teapot, cube,…
Specifying Geometry glBegin(GL_POLYGON) ; // Page 43 –glVertex2f –glColor, glIndex, glNormal, glTexCoord, … (pp 47) –glMaterial, glArrayElement, glEvalCoord, … (pp 48) –Other GL commands invalid between begin and end Can write normal C code… glEnd() ; Examples: pp 48,49