Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.

Similar presentations

Presentation on theme: "Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys."— Presentation transcript:

1 Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys

2 Texture Mapping Textures are images applied to objects Texture modifies the color assignment to a fragment –Texture color can modify the material color used in the shading model, or it can be a decal Use glTexCoord to assign a texture coordinate to a vertex

3 Texture Mapping Code glBegin( GL_QUADS ); glTexCoord2f( 0, 0 ); glVertex3f( a, b, c ); glTexCoord2f( 1, 0 ); glVertex3f( a, b, d ); glTexCoord2f( 1, 1 ); glVertex3f( a, e, d ); glTexCoord2f( 0, 1 ); glVertex3f( a, e, c ); glEnd();

4 Specifying the Texture Image glTexImage2D( target, level, components, width height, border, format, type, data ) target is GL_TEXTURE_2D level is (almost always) 0 components = 3 or 4 (RGB/RGBA) width/height MUST be a power of 2 border = 0 (usually) format = GL_RGB or GL_RGBA (usually) type = GL_UNSIGNED_BYTE, GL_FLOAT, etc…

5 More on Texture Optimizations for efficiency Mipmapping Filtering Texture Coordinate generation Texture Matrix Environment Mapping

6 Animation To generate a simple animation: –Encapsulate all drawing commands in a display function –Use an idle callback to change your objects or drawing parameters, and then force a redraw glutPostRedisplay will cause GLUT to call your display function as soon as it gets a chance You can use glutPostRedisplay in the display callback itself, but this gives up on rate control

7 Animation Example In main( ) glutIdleFunc( spin ); glutDisplayFunc( display ); In spin( ) ComputeNewAngle() glutPostRedisplay( ); In display( ) glClear( … ); glRotate( theta, … );

8 Double Buffering The time required to perform a redraw is not the same as the monitor’s refresh rate We might see part of an image if we’re not in sync The solution is to use two buffers: front and back –Front buffer is displayed –Back buffer is drawn into Switch between them with glutSwapBuffers() glutSwapBuffers() will switch the buffers exactly when the vertical retrace is completed Demo

9 Lighting (Chapter 5) Rendering Modes –Wireframe: glPolygonMode(GL_FRONT, GL_LINE) Can also use Polygon Offsets to superimpose wireframe Hidden line elimination? (polygons in black…) –Flat: glShadeModel(GL_FLAT) –Smooth (Goraud): glShadeModel(GL_SMOOTH) –Texture Mapped –Examples: color plates These specify how to interpolate vertex colors –How do we compute vertex colors? Lighting –glEnable(GL_LIGHT0) ; glEnable(GL_LIGHTING) ;

10 Specifying Normals Normals are specified through glNormal Normals are associated with vertices Specifying a normal sets the current normal –Remains unchanged until user alters it –Usual sequence: glNormal, glVertex, glNormal, glVertex, glNormal, glVertex… Usually, we want unit normals for shading –glEnable( GL_NORMALIZE ) –This is slow – either normalize them yourself or don’t use glScale Evaluators will generate normals for curved surfaces

11 Light Sources Point –Position, Color [separate diffuse/specular] –Attenuation Directional Spotlights –Spot exponent –Spot cutoff Emission, Ambient All parameters: pp 185 Transforms: Light is geometry (Modelview matrix) –Projection matrix doesn’t act. Eye coordinates important –3 types of light motions (pp )

12 Specifying a Light Source GLfloat light1_ambient[4] = {.1f,.1f,.1f,.0f}; glLightfv( GL_LIGHT0, GL_AMBIENT, light1_ambient ); Same for specular, diffuse, and position glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); Use glLightf for attenuation and spotlight properties Light positions are multiplied by the model view matrix

13 Specifying a Material Similar to setting the light source parameters: GLfloat mat_diffuse[4] = { 1.0f, 0.0f, 0.0f, 0.0f }; glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse ) It is possible to have different material properties for the front and back of polygons See OpenGL book for a more detailed explanation of what you can do with a material

14 Lighting Equation Hack (common in computer graphics) Details and physical basis later Terms –Ambient –Diffuse –Specular –Emissive Materials, Light sources

15 Steps Simple example (pp 181) Normals for each vertex (glNormal) –Normalize (sparingly): glEnable(GL_NORMALIZE) –For each vertex, part of OpenGL state –Evaluators for curved surfaces (later) Lights –glLightModel, glLight, GL_LIGHT0…7 Materials –glMaterialfv

16 Materials, Lighting model Materials –Similar to light: glMaterialfv (diffuse, specular, Phong exponent). Front, back can be different Lighting model [pp 197] – Ambient intensity – Local/distant viewpoint –Lighting differently on front/back –Separate specular for texturing? Lighting equation, mathematics [pp214; ]

17 Putting it together 3D pipeline –Geometry –Transforms –Hidden Surface removal (z-buffering) Depth buffer: glEnable(GL_DEPTH_TEST) ; glDepthFunc(GL_LESS) ; –Lighting and shading 2D pipeline [pp 295] –glDrawPixels, glReadPixels, glCopyPixels, glBitmap –glRasterPos to control where pixels go –glPixelStore to control way in which images are stored Texture Mapping

18 Display Lists (Chapter 7) Immediate Mode –Primitives sent to display as soon as specified – no memory in graphics system Retained Mode –Primitives in display list –Can be stored on server, displayed in new state etc. Cached commands. Immutable, unreadable. –Performance gain Example –Display lists: pages 262, 263, Hierarchical 269, 270 Other optimizations: Vertex arrays, texture objects, cull,…

19 Other Topics Blending, Antialiasing, Fog, 2D (Chapters 6,8) Buffers (Chapter 10) –Fragment tests (scissor,alpha,stencil,depth,blending) –Color(front,back,left,right), Depth, Stencil –Accumulation Buffer Tessellators and Quadrics (Chapter 11) Evaluators and NURBS (Chapter 12) Selection and FeedBack (Chapter 13) More stuff (Chapter 14, appendices) Extensions:

20 Review Questions Consider a z-buffer with near plane 1, far plane infinity –Assuming 16 bits precision, what is resolution at 1? –At a distance 10? –At a distance 100? What is order in OpenGL of following ops? –Modelview transform to vertex geometry –Projection transform –Dehomogenization –Depth test –Clipping –Lighting –Transformation of normals (how?)

Download ppt "Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys."

Similar presentations

Ads by Google