700 likes | 889 Views
Image Synthesis. DirectX/OpenGL. Graphics APIs. Graphics APIs . Graphics APIs . Graphics-APIs. DX/GL concepts: Implements the graphics-pipeline Geometric primitives: Points, lines, polygons Image primitives: Images and bitmaps State machine: Colors, materials,...
E N D
Image Synthesis DirectX/OpenGL
Graphics-APIs • DX/GL concepts: • Implements the graphics-pipeline • Geometric primitives: • Points, lines, polygons • Image primitives: • Images and bitmaps • State machine: • Colors, materials,... • Low-level and immediate mode API • Higher modeling and animation conceptsnot available, no scenegraph • Phong-Lighting, Gouraud-Shading, Textures, Mip-Mapping, Alpha-Blending, Z-Buffer, Stencil-Buffer, Accumulation-Buffer, ...
The OpenGL Structure application program OpenGL Motif widget or similar GLUT GLX, AGLor WGL GLU GL X, Win32, Mac O/S software and/or hardware
Introduction to OpenGL Overview (“A trip down the rendering pipeline”)
The “fixed function” Rendering Pipeline Geometry Processing Modelview Transform Lighting Perspective Transform Clipping Geometry Scan Conversion Framebuffer Texturing Fragment Tests Blending Rasterization Fragment Processing
GL_LINES GL_POLYGON GL_LINE_STRIP GL_LINE_LOOP GL_POINTS GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN GL_TRIANGLE_STRIP GL_QUAD_STRIP OpenGL: Geometric primitives Every primitive defined by Vertices (in homogeneous coordinates)
Geometric primitives Note:DX/GL break down everything to triangles • Trianglesare flat • A linear interpolationfunctionexistswithintriangles
glVertex3fv( v ) Data Type Vector Number of components b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double omit “v” for scalar form glVertex2f( x, y ) 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) OpenGL: Geometric primitives
OpenGL: Geometric primitives OpenGL commands • Commands and parameters are send to the graphics system • High performance applications use a different mode to send DX/GL primitives glBegin(GL_TRIANGLES); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glVertex3f(x4, y4, z4); glVertex3f(x5, y5, z5); glVertex3f(x6, y6, z6); glEnd(); glBegin(GL_TRIANGLES); glVertex3fv(p1); glVertex3fv(p2); glVertex3fv(p3); glVertex3fv(p4); glVertex3fv(p5); glVertex3fv(p6);glEnd();
Primitives and vertex-attributes • How to group primitives glBegin( primType); ...; glEnd(); • How to render primitives? Vertex-attributesglColor*(); glNormal*(); glTexCoord(); Global “fixed-function” statesglPointSize(size); glShadeModel(GL_SMOOTH);glEnable(GL_LIGHTING);...
States • DX/GL Systems are state machines • State variables define the current state • Can be set and requested individually • Current state defines how things get rendered glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glBegin(GL_TRIANGLES); glColor3v(c1); glNormal3v(n1); glVertex3v(t1); glColor3v(c2); glVertex3v(t2); glVertex3v(t3);glEnd();
The OpenGL Rendering Pipeline OpenGL rendering pipeline – Geometry data Per-vertexoperationsand primitive assembly Vertexdata • • • Textureassembly Fragment processing Rasterization Framebuffer
Geometry processing • Vertex operations and primitve assembly • Coordinate/normal transformation • Lighting calculations • Texture coordinate generation & transformation • Perspective projection • Front/back face culling • Clipping • Result is geometric primitives with color, depth, texCoords etc.
Geometry processing • Polygonal models (triangle meshes) Light and material properties: Light source type Color Reflectivity Tansparency ... Per-vertex: Coordinate (x,y,z) Color (RGB) Normal (Nx,Ny,Nz) TexCoords(u,v,w)
Geometry processing Transformation – camera analogy • Modeling: move and scale model • Viewing: position and orientation of camera • Projection: adjust camera lens • Viewport: photograph viewing volume camera Model tripot
Geometry processing normalized device eye object clip window • v • e • r • t • e • x • Modelview • Matrix • Projection • Matrix • Perspective • Division Viewport Transform Modelview • Projection Modelview l l l The transformation pipeline l l l • Matrix-Stacks: • Modelview • Projection • Texture
Geometry processing • Transformation matrices • Choose matrix stackglMatrixMode(GL_MODELVIEW or GL_PROJECTION) • Load or multiply matrix (from right)glLoadMatrix(), glMultMatrix() • Manage stackglLoadIdentity(),glPushMatrix(), glPopMatrix() • Spezial Modelview-TransformationsglRotate(), glTranslate(), glScale()
Geometry processing • Transformation matrices
Geometry processing Transformations • Affine transformations in homogeneous coordinates Rotation Shear Scaling Translation Homogeneous part Projection
Geometry processing Viewing and modeling • Camera movement (foward) is equivalent to movement of objects towards the camera • Viewing transformation equivalent to multiple modeling transformations glTranslatef( 0.0, 0.0, -5.0 );gluLookAt( 0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
Geometry processing • Projective transformationsglFrustum(left,right,bottom,top,zNear,zFar)gluPerspective(fovy,aspect,zNear,zFar)glOrtho(left,right,bottom,top,zNear,zFar) • glFrustum allows for non-symmetric viewing pyramids
RGBA-Color Lighting Texture Shading flat/smooth Color Clamping Geometry processing • Lighting and shading • Two ways to determine color of a vertex • Vertex attribut glColor() • Material color and illuminationglColorMaterial, glEnable(GL_LIGHTING)
ambient diffuse specular attenuation ModelviewTransformation Matrix M Geometry processing Lighting and shading – Phong lighting Vertex P Light source- positioncolor Vertex color Material- colors globalambient Light Normal N
R L + E H = N H L + E L E Geometry processing • Phong lighting • cos() = N * L • cos() = N * H (Blinn-Phong)
Geometry processing Extended Phong lighting
Geometry processing • Turn on lightglEnable( GL_LIGHTING )glEnable( GL_LIGHTn ) • Light source propertiesglLightf{v}( light, property, value ) • Material propertiesglMaterialfv( face, property, value ) • Light source types • Spotlight: • Local (point light): into all direction • At infinity (directional light; directed): w=0 in position • Front and back faces illuminatedglEnable( GL_LIGHT_MODEL_TWO_SIDE)
Rasterization The graphics pipeline Geometry Processing Modelview Transform Lighting Perspective Transform Clipping Geometry Scan Conversion Framebuffer Texturing Fragment Tests Blending Rasterization Fragment Processing
Rasterization • Conversion of geometric data into fragments • Fragment: • Represents a point of geometry • Has color, texCoord, depth etc. • Associated with a pixel • But not yet a pixel (not stored in the framebuffer) • Pixel: • Color of a pixel in the framebuffer
Rasterization • Scan conversion of geometric primitives • For each covered pixel a fragment is generated • Attributes like color, depth, texture coordinates etc. are generated from projected vertex attributes • During fragment generation OpenGL states, e.g. shading model, line width, polygon mode etc. are taken into account
Rasterization • Rasterization of geometric data Geometry processing Vertex transformation Lighting Clipping ... Rasterization Fragment Processing Texturing Per-fragment tests Per-fragment comparison Blending ... Screen coord (X,Y) Depth (z) Color (RGB) TexCoords (u,v,w) ...
Rasterization Once a fragment is generated, per-vertex attributes need to be assigned properly • Determination of color and other properties for all pixels along a scanline is called Shading • This is done via interpolation (see exercise for more information) Rasterization v0 scanline v2 v1
Rasterization Smooth or Gouraud shading (1971) Idea: better quality by respecting color variations on the polygon • Per vertex lighting • Linear interpolation of color using barycentric interpolation in triangles • In screen space during scan conversion
Rasterization Phong shading (1975): higher quality shading by interpolating normals rather than colors • Evaluates lighting model for every pixel • Not supported by OpenGL
Rasterization - OpenGL shading Phong lighting and shading Phong lighting Gouraud shading
Rasterization • Scan conversion of primitives Rasterization Interpolation of per-vertex attributes along scanlines v0 scanline v2 z v1 Depth values: non-linear in [Znear, Zfar] due to perspective projection Zfar Znear
Fragment processing Graphics APIs and the graphics pipeline Geometry Processing Modelview Transform Lighting Perspective Transform Clipping Geometry Scan Conversion Framebuffer Texturing Fragment Tests Blending Rasterization Fragment Processing
Fragment Processing • Fragment operations (apply before fragments beome pixels) • Texturing • Fog calculation • Tests and comparisons • Blending • Dithering • Logical operations and masking
Texture Mapping Texture mapping • For each vertex specify vertex position, normal, color andtexture coordinate glBegin(GL_TRIANGLES); glColor4f(R0,G0,B0,0); glColor4f(R1,G1,B1,1); glColor4f(R2,G2,B2,2); glNormal3f(N0x,N0y,N0z); glNormal3f(N1x,N1y,N1z); glNormal3f(N2x,N2y,N2z); glTexCoord3f(T0x,T0y,T0z); glTexCoord3f(T1x,T1y,T1z); glTexCoord3f(T2x,T2y,T2z); glVertex3f(V0x,V0y,V0z)); glVertex3f(V1x,V1y,V1z)); glVertex3f(V2x,V2y,V2z)); glEnd();
Texture Mapping Hardware supported texture interpolation • For each fragment, the appropriate texture value is fetched from the texture array • Interpolation of texture values is supported by the hardware Texture elements Per-fragment sample point
Texture Mapping Texture mapping • Shape (geometry) & appearance (texture)
Texture Mapping Warping (texture coordinates outside (0,1)) clamp/clamp clamp/repeat repeat/clamp repeat/repeat
Texture Mapping Texture filtering • Combining texels to determine pixel color • Minification: pixels are larger than one texel • Magnification: pixels are smaller than one texel • Filtering methods • Nearest: choose the texel closest to the pixel center • Linear: (bi/tri)-linear texture interpolation • MipMap: (bi/tri)-linear interpolation in a stack of textures of decreasing resolution
Texture Mapping Texture filtering Pixel Pixel Textur Textur magnification minification