1.09k likes | 1.39k Views
CSC8820 Advanced Graphics Algorithms. Lecture 3 and 4 OpenGL Overview, Geometry Objects, User Interaction. OpenGL Files. OpenGL files comes with MS Windows and/or Visual Studio installation Header files gl.h, glu.h Glut.h (needs to be downloaded) Static libraries
E N D
CSC8820 Advanced Graphics Algorithms Lecture 3 and 4 OpenGL Overview, Geometry Objects, User Interaction
OpenGL Files • OpenGL files comes with MS Windows and/or Visual Studio installation • Header files • gl.h, glu.h • Glut.h (needs to be downloaded) • Static libraries • Opengl32.lib, glu32.lib • glut32.lib (needs to be downloaded) • DLLs • C:\windows\system32 • Opengl32.dll, glu32.dll • glut32.dll (needs to be downloaded)
How to compile OpenGL/GLUT programs? • http://www.cs.uiowa.edu/~cwyman/classes/fall04-22C151/howto/winGLUT.html • http://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/FOURTH_EDITION/vc
Sample OpenGL programs • Examples from “OpenGL Programming Guide” • http://www.opengl.org/resources/code/samples/redbook/ • OpenGL code samples: http://www.opengl.org/code/ • Simple code samples: http://www.opengl.org/resources/code/samples/simple/
A Simple OpenGL Program (1) #include <GL/glut.h> void display(void) { /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd();
A Simple OpenGL Program (2) /* start processing buffered OpenGL routines */ glFlush (); } void init (void) { /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); /* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); }
A Simple OpenGL Program (3) /* Declare initial window size, position, and display mode (single buffer and RGBA). Open window with "hello" in its title bar. Call initialization routines. Register callback function to display graphics. Enter main loop and process events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc(display); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
What is graphics rendering pipeline? • A process of generating a 2D image, given a virtual camera (eye), 3D objects, light sources, textures, etc. • The 2D image is a frame of animation. • A process for converting coordinates from its local 3D coordinate system to the final 2D window coordinate system. • Both OpenGL and Direct3D implement part of the rendering pipeline.
Graphics Rendering Pipeline Stages • The rendering pipeline is normally divided into 3 conceptual stages • Application • Geometry • Rasterizer • The output of one stage is the input of next stage, thus the name “pipeline” • Each stage also contains sub-stages Application Geometry Rasterizer
OpenGL pipeline (overview) Application Rasterizer Geometry
Why use pipeline architecture? • It’s more efficient • Think automobile assembly line • Explore data parallelism • Data parallelism vs. instruction parallelism • The pipeline architecture is the fundamental architecture for both 3D graphics software and hardware • Think GPU as an image assembly line
Application Stage Overview • 3D objects are created using modeling tools • 3D Studio Max, Maya, etc. • 3D objects are positioned in 3D world and organized in a scene graph • 3D objects can be moved by transformation • Transformation are performed on vertices using different types of transformation matrices • 3D transformations are specified in this stage but carried out in the Geometry stage
Application Stage Overview • Other tasks in the application stage: • Handling user input • level of details • occlusion culling • The result: the 3D geometry objects to be fed to the next stage • Points, lines, polygons, etc. • 3D geometry objects are eventually broken down to vertices, represented by (x, y, z) coordinates
Geometry Stage Overview • Responsible for the majority of per-polygon or per vertex operations • Contains several sub-stages • Model Transformation • View Transformation (often combined with model transformation) • Lighting • Projection • Clipping • Screen Mapping
Coordinate Systems • Multiple Cartesian coordinate systems are used at different stages of the pipeline • 3D Model Coordinates (model space) • Each model is in its own coordinate system with origin in some point on the model • 3D World Coordinates (world space) • Unified world coordinate system, with only one origin • 3D Eye Coordinates (view space) • Camera (eye) is the origin and look straight down Z-axis • 2D Screen Coordinates (screen space) • 3D coordinates are converted to 2D screen coordinates for display
3D Graphics Pipeline Data Flow • Objects in the 3D scene are sequentially transformed through different coordinate systems when proceeding the 3D pipeline.
Model Transform • Transform the vertices and normals of 3D objects from model space to world space • After the transform, all objects exit in the same coordinate system • The camera (eye) has a location in world coordinate system and a direction
View Transform • The camera and all the 3D objects are transformed with the view transform • The purpose is to place the camera at the origin and make it look in the direction of Z-axis. • Often combined with model transform – model-view transform
Projection • Transform the view volume into a unit cube with its extreme points at (-1, -1, -1) and (1, 1, 1). • The purpose is to simplify clipping. • Two projection methods: • Orthographic projection • When realism is not a concern. • Perspective projection • When realism counts.
Lighting • A lighting equation is used to calculate a color at each vertex of the 3D object that is to be affected by lighting. • The lighting equations often have little to do with how lights behave in the real world. • Per-vertex lighting vs. per-pixel lighting
Shading • Each 3D object surface is divided into many triangles. • The colors at the vertices of a triangle are interpolated over the triangle. • Three shading methods: • Flat shading (operate per triangle) • Gouraud Shading (operate per vertex) • Phong Shading (operate per pixel) • Programmable Shaders
Clipping • Only the primitives wholly or partially inside the view volume need to be passed on to the rasterizer stage. • Primitives (line or polygon) that are partially inside the view volume require clipping.
Screen Mapping • Primitives that survive the clipping are passed on to screen mapping stage. • X and Y coordinates of each primitive are transformed to screen coordinates. • Z coordinates are not affected and are kept for depth buffer checking.
Rasterizer Stage Overview • In this stage, all primitives are converted into pixels in the window. • The goal of this stage is to assign correct colors to the pixels. • It contains one or all of the following sub-stages: • Texture mapping • Fog • Translucency Test • Depth buffering • Antialiasing
Texturing • Texture mapping means “attaching” an image onto a 3D object. • Textures are important to bringing realism to a 3D scene. • Provide surface details • Add scene depth • Advance texture mapping • Multi-texturing • Bump mapping • Environment mapping • Pixel shader
Fog • An optional stage but help set the mood • Also helps give a scene an addition sense of depth of field • Added realism • E.g. allow distant objects to gracefully "fade away" rather than just pop out of the scene
Translucency Tests • Also called Alpha Test • Used to create effects such as glass, water, see-through views in CAD modeling, and translucent or transparent materials.
Depth Buffering • Also called Z-buffer algorithm • Used for visibility check • When multiple primitives are rendered to a certain pixel, the color of the pixel is the color of the primitive that is closest to the point of view of camera. • The only exception is when primitives are transparent. • This algorithm allows the primitives to be rendered in any order.
Anti-aliasing • Alias effect: the jagged or stair-stepped look of diagonal or curved lines in computer graphics. • Anti-aliasing algorithms sample, or examine and evaluate, the colors and shades of pixels adjoining curved or diagonal lines to present smoother looking line.
Display • Finally, the final 2D image is generated and rendered to frame buffer and displayed on screen. • Double-buffer technique may be used to reduce flashing • Front buffer for display, back buffer for rendering next frame. • A long trip down the graphics rendering pipeline • If application runs at 60 frame/second, this whole process will repeat every 17 ms.
Summary • The 3D graphics pipeline is the underlying tool for graphics rendering. • Three major stages: • Application • Geometry • Rasterizer
Readings • “ExtremeTech 3D Pipeline Tutorial” at http://www.extremetech.com/article2/0,1558,9722,00.asp
Overview of OpenGL • An open standard specification defining a cross platform API for writing 2D or 3D computer graphics applications • Defines over 250 function calls • Competes with Direct3D • Managed by an industry consortium (Khronos Group)
Notable OpenGL games • Doom 3 • Half-Life • Quake • Call of Duty • Second Life • Spore • Unreal Tournament
Notable OpenGL applications • Blender • Adobe Photoshop • Adobe After Effects • Adobe Premiere
Purposes of OpenGL • Hide the complexities of interfacing with different 3D graphics hardware • Hide the differing capabilities of hardware platforms, by requiring that all implementations support the full OpenGL feature set
OpenGL • OpenGL's basic operation is to accept primitives such as points, lines and polygons, and convert them into pixels. • OpenGL is a low-level, procedural API, requiring the programmer to dictate the exact steps required to render a scene. • Requires programmers to have a good knowledge of the graphics pipeline,
OpenGL Books • Red Book: OpenGL Programming Guide • Blue Book: OpenGl Reference Manual • Orange Book: OpenGL Shading Language
OpenGL Files • OpenGL files comes with MS Windows and/or Visual Studio installation • Header files • gl.h, glu.h • Glut.h (needs to be downloaded) • Static libraries • Opengl32.lib, glu32.lib • glut32.lib (needs to be downloaded) • DLLs • C:\windows\system32 • Opengl32.dll, glu32.dll • glut32.dll (needs to be downloaded)
How to compile OpenGL/GLUT programs? • http://www.cs.uiowa.edu/~cwyman/classes/fall04-22C151/howto/winGLUT.html • http://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/FOURTH_EDITION/vc
Sample OpenGL programs • Examples from “OpenGL Programming Guide” • http://www.opengl.org/resources/code/samples/redbook/ • OpenGL code samples: http://www.opengl.org/code/ • Simple code samples: http://www.opengl.org/resources/code/samples/simple/
A Simple OpenGL Program (1) #include <GL/glut.h> void display(void) { /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd();
A Simple OpenGL Program (2) /* start processing buffered OpenGL routines */ glFlush (); } void init (void) { /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); /* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); }
A Simple OpenGL Program (3) /* Declare initial window size, position, and display mode (single buffer and RGBA). Open window with "hello" in its title bar. Call initialization routines. Register callback function to display graphics. Enter main loop and process events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc(display); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
OpenGL Functions Overview (1) • Primitives: object creation • glBegin(), glEnd(); A glBegin()-glEnd() pair encloses an object definition. OpenGL functions called between glBegin() and glEnd() define the object • glVertex*(), glColor*(), glNormal*(), glMaterial*(), glCallList(), glCallLists(), glRect*(), gluSphere(), gluCylinder() • Attributes • glClearColor() and glClear() specify the color to be used when color buffers are cleared and to actually clear the buffer, respectively. • Attributes (all are modal) control how primitives are displayed: glPolygonMode(), glPointSize(), glLineStipple(), glLineWidth()
Overview of OpenGL Functions (2) • Display lists • glNewList(), glEndList(), glGenLists(), glListBase() • Display lists are collections of graphics functions. You create them, name them, and "execute" them. • You define objects in between glNewList() and glEndList(). • Modeling transformations • glScale*(), glTranslate*(), glRotate*() • Before invoking any matrix manipulating function, you normally call glLoadIdentity(). • Remember that glRotate() rotates about the origin so in order to rotate an object you usually must make sure that it is positioned at the origin.