Rendering Geometric Primitives Reading: HB4, HB8-1 to HB8-6, HK9.7, 11 Computer Graphics Last updated on 06-02-2012 Rendering Geometric Primitives Reading: HB4, HB8-1 to HB8-6, HK9.7, 11
Rendering Goal interactive rendering offline rendering transform computer models into images may or may not be photo-realistic interactive rendering fast, but limited quality roughly follows a fixed patterns of operations rendering pipeline offline rendering ray tracing global illumination
Rendering Tasks that need to be performed project all 3D geometry onto the image plane geometric transformations determine which primitives or parts of primitives are visible hidden surface removal determine which pixels a geometric primitive covers scan conversion compute the color of every visible surface point lighting, shading, texture mapping
Rendering Pipeline What is the pipeline? abstract model for sequence of operations to transform geometric model into digital image abstraction of the way graphics hardware works underlying model for application programming interfaces (APIs) that allow programming of graphics hardware OpenGL Direct 3D Actual implementation details of rendering pipeline will vary
Rendering Pipeline Geometry Database Model/View Perspective Lighting Transform. Lighting Perspective Clipping Scan Conversion Depth Test Texturing Blending Frame- Buffer
Geometry Database Geometry database Application-specific data structure for holding geometric information Depends on specific needs of application triangle, points, mesh with connectivity information, curved surface
Model/View Transformation Geometry Database Model/View Transform. Modeling Transformation Map all geometric objects from local coordinate system into world coordinates Viewing transformation Map all geometry from world coordinates into camera coordinates
Lighting Geometry Database Model/View Transform. Lighting Lighting Compute brightness based on property of material and light position(s) Computation is performed per-vertex
Perspective Transformation Geometry Database Model/View Transform. Lighting Perspective Transform. Perspective Transformation Projecting the geometry onto the image plane Projective transformations and model/view transformations can all be expressed with 4x4 matrix operations
Clipping Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Clipping Removal of parts of the geometry that fall outside the visible screen or window region May require re-tessellation of geometry
Scan Conversion Scan Conversion Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Scan Conversion Turn 2D drawing primitives (lines, polygons etc.) into individual pixels Interpolate color across primitive Generate discrete fragments
Texture Mapping Texture Mapping “Gluing images onto geometry” Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Texturing Texture Mapping “Gluing images onto geometry” Color of every fragment is altered by looking up a new color value from an image
Depth Test Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Texturing Depth Test Depth Test Remove parts of geometry hidden behind other geometric objects Perform on every individual fragment
Blending Blending Final image: write fragments to pixels Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Texturing Depth Test Blending Blending Final image: write fragments to pixels Draw from farthest to nearest No blending – replace previous color Blending: combine new & old values with arithmetic operations
Frame Buffer Frame Buffer Geometry Database Model/View Transform. Lighting Perspective Clipping Scan Conversion Depth Test Texturing Blending Frame- Buffer Frame Buffer Video memory on graphics board that holds image Double-buffering: two separate buffers Draw into one while displaying other, then swap to avoid flicker 255 255 255 0 255 255 0 255 255 0 255 255 255 155 0 155 255 155
Pipeline Advantages Only local knowledge of the scene is necessary Modularity: logical separation of different components Easy to parallelize Earlier stages can already work on new data while later stages still work with previous data Similar to pipelining in modern CPUs But much more aggressive parallelization possible (special purpose hardware!) Important for hardware implementations Only local knowledge of the scene is necessary
Pipeline Disadvantages Limited flexibility Some algorithms would require different ordering of pipeline stages hard to achieve while still preserving compatibility Only local knowledge of scene is available shadows, global illumination difficult
3D Scene Representation Scene is usually approximated by 3D primitives Point Line segment Polygon Polyhedron Cylinder Curved surface Solid object etc.
OpenGL Attributes of Graphics Primitives Color, antialiasing, … Point attributes, Line attributes, Curve attributes, Character attributes
OpenGL Attributes of Graphics Primitives Attributes are any parameters that affect the way graphic primitives are displayed. Basic attributes Color Size Style Fill patterns
OpenGL Attributes of Graphics Primitives Attribute parameter A parameter that affects the way a primitive is to be displayed, ex: color, size. State system / State machine A graphics system that maintains a list for the current values of attributes
OpenGL State Variables All OpenGL state parameters have default values. Set the state once, remains until overwritten Changing the attribute settings Only affects those primitives that are specified after the OpenGL state is changed Lines: blue or orange, dotted or dashed, and fat or thin. State parameters can have more than two states, e.g., colors, styles, etc.
OpenGL Color & Gray Scale Color is a common attribute for all primitives Color information can be stored in two ways 1. RGB color codes 2. Color table Stored Color Values in Frame Buffer 1 2 3 4 5 6 7 Black Blue Green Cyan Red Magenta Yellow White GREEN Displayed Color
OpenGL Color & Gray Scale Color is a common attribute for all primitives Color information can be stored in two ways 1. RGB color codes 2. Color table
OpenGL Color Functions Set color display mode 1. OpenGL RGB and RGBA mode 2. OpenGL Color-Index Mode glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB ) GLUT_RGBA GLUT_INDEX glColor* (colorComponents); // Select current color glIndexi(196); glutSetColor (index, red, green, blue); // Select current color // Specify color into a table
OpenGL Color Functions OpenGL color blending Combining the color of overlapping objects Blending an object with the background Color-blending function Destination object: Current object in the frame buffer Source object: Other object in the frame buffer glEnable(GL_BLEND); Source glDisable(GL_BLEND); Destination Blending methods can be performed only in RGB or RGBA mode.
OpenGL Color Functions Blending Factors glColor4f(R, G, B, A), A is a blending factor Source color components are (Rs, Gs, Bs, As) Destination color components are (Rd, Gd, Bd, Ad) Source blending factors are (Sr, Sg, Sb, Sa) Destination blending factors are (Dr, Dg, Db, Da) The new blending color that is then loaded into the frame buffer is calculated as: (SrRs+DrRd, SrGs+DgGd, SbBs+DbBd, SaAs+DaAd) Source Destination
OpenGL Color Functions glBlendFunc (sFactor, dFactor); Default: GL_ONE GL_ZERO (1.0, 1.0, 1.0, 1.0) (0.0, 0.0, 0.0, 0.0) Ex. glBlendFunc(GL_SRC_ALPHA,GL_ONE); Ex: implement it by using glColor4f (0.8, 0.2, 0.2, 0.4); Source Destination
OpenGL Example: Blending 1/3 #include <GL/glut.h> void init (void) { glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // (sFactor, dFactor) glClearColor (1.0, 1.0, 1.0, 0.0); //(red, green, blue, alpha) }
OpenGL Example: Blending 2/3 void display(void) { glClear (GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); // Draw triangle glColor4f(1.0, 0.0, 0.0, 0.75); glVertex3f(0, -0.5, 0.0); glVertex3f(0.5, -0.5, 0.0); glVertex3f(0, 1, 0.0); glEnd(); glBegin(GL_POLYGON); // Draw rectangle glColor4f(0.0, 0.0, 1.0, 0.75); glVertex3f (-0.6, -0.6, 0); glVertex3f (0.6, -0.6, 0); glVertex3f (0.6, 0.6, 0); glVertex3f (-0.6, 0.6, 0); glFlush (); }
OpenGL Example: Blending 3/3 int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (600, 400); glutInitWindowPosition (200, 100); glutCreateWindow ("Hello Students"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
OpenGL Home Work Plot red, green & blue circles overlapping as shown in figure. Reading: HB4, RB
OpenGL Point Attributes & Functions Basic attributes Color Size A multiple of the pixel size (a square) glPointSize (size); // size is the number of pixels
OpenGL Line Attributes & Functions Basic attributes Color Width glLineWidth(width); Style glEnable(GL_LINE_STIPPLE); glLineStippel(repeatFactor, pattern); Other attributes Shade glShadeModel(GL_SMOOTH); How many times each bit is to be repeat 16-bit integer Default: 0xFFFF Solid line Lower-order bits are applied first
OpenGL Line Attributes & Functions Pixel mask A pattern of binary digits For example, 11111000 dashed line Inter-dash spacing
OpenGL Fill-Area Attribute Functions OpenGL fill-area routines for convex polygons only. Four steps: Define a fill pattern Invoke the polygon-fill routine Activate the polygon-fill feature Describe the polygons to be filled.
OpenGL Example void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH)} void triangle(void) glBegin (GL_TRIANGLES); glColor3f (1.0, 0.0, 0.0); glVertex2f (5.0, 5.0); glColor3f (0.0, 1.0, 0.0); glVertex2f (25.0, 5.0); glColor3f (0.0, 0.0, 1.0); glVertex2f (5.0, 25.0); glEnd(); } http://www.opengl.org/resources/code/samples/redbook/smooth.c
OpenGL Polygon Stippling
OpenGL Polygon Stippling byte fly[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x80, (byte) 0x01, (byte) 0xC0, (byte) 0x06, (byte) 0xC0, (byte) 0x03, (byte) 0x60, (byte) 0x04, (byte) 0x60, (byte) 0x06, (byte) 0x20, (byte) 0x04, (byte) 0x30, (byte) 0x0C, (byte) 0x20, (byte) 0x04, (byte) 0x18, (byte) 0x18, (byte) 0x20, (byte) 0x04, (byte) 0x0C, (byte) 0x30, (byte) 0x20, (byte) 0x04, (byte) 0x06, (byte) 0x60, (byte) 0x20, (byte) 0x44, (byte) 0x03, (byte) 0xC0, (byte) 0x22, (byte) 0x44, (byte) 0x01, (byte) 0x80, (byte) 0x22, (byte) 0x44, (byte) 0x01, (byte) 0x80, (byte) 0x22, (byte) 0x66, (byte) 0x01, (byte) 0x80, (byte) 0x66, (byte) 0x33, (byte) 0x01, (byte) 0x80, (byte) 0xCC, (byte) 0x19, (byte) 0x81, (byte) 0x81, (byte) 0x98, (byte) 0x0C, (byte) 0xC1, (byte) 0x83, (byte) 0x30, (byte) 0x07, (byte) 0xe1, (byte) 0x87, (byte) 0xe0, (byte) 0x03, (byte) 0x3f, (byte) 0xfc, (byte) 0xc0, (byte) 0x03, (byte) 0x31, (byte) 0x8c, (byte) 0xc0, (byte) 0x03, (byte) 0x33, (byte) 0xcc, (byte) 0xc0, (byte) 0x06, (byte) 0x64, (byte) 0x26, (byte) 0x60, (byte) 0x0c, (byte) 0xcc, (byte) 0x33, (byte) 0x30, (byte) 0x18, (byte) 0xcc, (byte) 0x33, (byte) 0x18, (byte) 0x10, (byte) 0xc4, (byte) 0x23, (byte) 0x08, (byte) 0x10, (byte) 0x63, (byte) 0xC6, (byte) 0x08, (byte) 0x10, (byte) 0x30, (byte) 0x0c, (byte) 0x08, (byte) 0x10, (byte) 0x18, (byte) 0x18, (byte) 0x08, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x08 };
3D Geometric Primitives http://mrl.snu.ac.kr/courses/CourseGraphics/ObjectRepresentation.ppt
Rendering Generate an image from geometric primitives Rendering Raster Image
What issues must be addressed by a 3D rendering system? 3D Rendering Example What issues must be addressed by a 3D rendering system?
3D Object Representation Polyhedra
OpenGL 3D Primitives (GLUT Support)
Cube Sphere glutWireCube(GLdouble size) size = length of a side OpenGL 3D Primitives Cube glutWireCube(GLdouble size) size = length of a side Sphere glutWireSphere(GLdouble radius, GLint nSlices, GLint nStacks) Approximated by polygonal faces nSlices = # of polygons around z-axis nStacks = # of bands along z-axis There are solid counterparts of the wire objects
Regular Polyhedron Tetrahedron (4 sided) Hexahedron (6 sided) OpenGL 3D Primitives Regular Polyhedron Tetrahedron (4 sided) glutWireTetrahedron() Hexahedron (6 sided) glutWireHexahedron() Octahedron (8 sided) glutWireOctahedron() Dodecahedron (12 sided) glutWireDodecahedron() Icosahedron (20 sided) glutWireIcosahedron() Display with center at the origin and with radius (distance from the center of polyhedron to any vertex) equal to Sqrt(3)
Cone OpenGL 3D Primitives Axis coincides with the z-axis glutWireCone(GLdouble baseRad, GLdouble height, GLint nSlices, GLint nStacks) Axis coincides with the z-axis Base rests on xy-plane and extends to +ve z = height baseRad: radius at z = 0 nSlices: The number of subdivisions around the z axis. nStacks: The number of subdivisions along the z axis.
Approximated by polygonal faces Teapots glutWireTeapot(GLdouble size) OpenGL 3D Primitives Torus glutWireTorus(GLdouble inRad, GLdouble outRad, GLint nSlices, GLint nStacks) Approximated by polygonal faces Teapots glutWireTeapot(GLdouble size)
Things to do OpenGL practice: HB 2.9
References http://www.ugrad.cs.ubc.ca/~cs314/ http://faculty.cs.tamu.edu/schaefer/teaching/441_Spring2012/index.html http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007/