Viewing and Modeling Computer Science Department The Universit of Texas at Austin
A Simplified Graphics ipeline Application Vertex batching & assembl Triangle assembl Triangle clipping NDC to window space Triangle rasterization Fragment shading Depth testing Depth buffer Color update Framebuffer
A few more steps expanded Application Vertex batching & assembl Vertex transformation Lighting Texture coordinate generation Triangle assembl User defined clipping Back face culling View frustum clipping NDC to window space Triangle rasterization Fragment shading erspective divide Depth testing Depth buffer Color update Framebuffer
Conceptual Vertex Transformation glvertex* AI commands object-space coordinates (x o, o,z o,w o ) Modelview matrix ee-space coordinates (x e, e,z e,w e ) (x e, e,z e,w e ) User-defined clip planes clipped ee-space coordinates (x e, e,z e,w e ) rojection matrix clip-space coordinates (x c, c,z c,w c ) View-frustum clip planes clipped clip-space coordinates (x c, c,z c,w c ) erspective division normalized device coordinates (NDC) (x n, n,z n,/w c ) Viewport + Depth Range transformation window-space coordinates (x w, w,z w,/w c ) to primitive rasterization
ipeline View modelview transformation nonsingular projection transformation clipping homogeneous for perspective perspective division 4D 3D projection 3D 2D
Computer Viewing There are three aspects of the viewing process, all of which are implemented in the pipeline, ositioning the camera Setting the model-view matrix Selecting a lens Setting the projection matrix Clipping Setting the view volume
The World and Camera Frames When we work with representations, we work with n- tuples or arras of scalars Changes in frame are then defined b 4 x 4 matrices In OpenGL, the base frame that we start with is the world frame Eventuall we represent entities in the camera frame b changing the world representation using the model-view matrix Initiall these frames are the same (M=I)
Vertex Transformation Object-space vertex position transformed b a general linear projective transformation Expressed as a 4x4 matrix = o o o o c c c c w z x m m m m m m m m m m m m m m m m w z x 5 7 3 4 6 2 3 9 5 2 8 4
The OpenGL Camera In OpenGL, initiall the object and camera frames are the same Default model-view matrix is an identit The camera is located at origin and points in the negative z direction OpenGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin Default projection matrix is an identit
Moving the Camera If objects are on both sides of z=, we must move camera frame!!!! " # $ $ $ $ % & d M =
Moving the Camera Frame If we want to visualize object with both positive and negative z values we can either Move the camera in the positive z direction Translate the camera frame Move the objects in the negative z direction Translate the world frame Both of these views are equivalent and are determined b the model-view matrix Want a translation (gltranslatef(.,.,-d);) d >
Translate Transform rototpe gltranslatef(glfloat x, GLfloat, GLfloat z) ost-concatenates this matrix z x
gltranslatef Matrix Modelview specification glloadidentit(); gltranslatef(,,-4) x translate=, translate=, z translate=-4 oint at (,,) would move to (,,-4) Down the negative Z axis Matrix 4 = z x the translation vector
General Camera Motion We can position the camera anwhere b a sequence of rotations and translations Example: side view Move camera to the origin Rotate the camera Model-view matrix C = RT
OpenGL code Remember that last transformation specified is first to be applied glmatrixmode(gl_modelview) glloadidentit(); glrotatef(9.,.,.,.); gltranslatef(.,., -d);
A Better Viewing Matrix rimar OpenGL libraries Look at Transform Link with lglut for GLUT Concept Link with lglu for GLU Given the following Link with lgl for OpenGL a 3D world-space ee position a 3D world-space center of view position (looking at ), and an 3D world-space up vector Then an affine (non-projective) 4x4 matrix can be constructed For a view transform mapping world-space to ee-space A read implementation The OpenGL Utilit librar (GLU) provides it glulookat(gldouble eex, GLdouble ee, GLdouble eez, GLdouble atx, GLdouble atz, GLdouble atz, GLdouble upx, GLdouble up, GLdouble upz);
glulookat glulookat(eex, ee, eez, atx, at, atz, upx, up, upz)
Look At in ractice Consider our prior view situation Instead of an arbitrar view we just translated b 4 in negative Z direction gltranslatef(,,4) What this means in Look At parameters (eex,ee,eez) = (,,4) (atx,at,atz) = (,,) (upx,up,upz) = (,,) Not surprising both are just translates in Z since the Look At parameters alread have use looking down the negative Z axis gltranslatef(,,-4) 4 Same matrix; same transform glulookat(,,4,,,,,,) 4
The Look At Algorithm Vector math Z = ee at Z = normalize(z) /* normalize means Z / length(z) */ Y = up X = Y Z /* means vector cross product! */ Y = Z X /* orthgonalize */ X = normalize(x) Y = normalize(y) Then build the following affine 4x4 matrix X x Yx Z x X Y Z X Y Z z z z X ee Y ee Z ee Warning: Algorithm is prone to failure if normalize divides b zero (or ver nearl does) So. Don t let Z or up be zero length vectors 2. Don t let Z and up be coincident vectors
Look At Examples glulookat(,,4,,,,,,); // ee (x,,z) // at (x,,z) // up (x,,z) Same as the gltranslatef(,,-4) as expected glulookat(,2.5,,,,,,,); // ee (x,,z) // at (x,,z) // up (x,,z) Similar to original, but just a little off angle due to slightl perturbed ee vector
Look At Major Ee Changes glulookat(-2.5,,,,,,,,); Ee is above the scene // ee (x,,z) // at (x,,z) // up (x,,z) glulookat(-2.5,-,,,,,,,); Ee is below the scene // ee (x,,z) // at (x,,z) // up (x,,z)
Look At Changes to AT and U glulookat(,,4, 2,-3,,,,); // ee (x,,z) // at (x,,z) // up (x,,z) Original ee position, but at position shifted glulookat(,,4,,,,,,); Ee is below the scene // ee (x,,z) // at (x,,z) // up (x,,z)
The LookAt Function The GLU librar contains the function glulookat to form the required modelview matrix through a simple interface Note the need for setting an up direction Still need to initialize Can concatenate with modeling transformations Example: isometric view of cube aligned with axes glmatrixmode(gl_modelview): glloadidentit(); glulookat(.,.,.,.,.,.,.,...);
Other Viewing AIs The LookAt function is onl one possible AI for positioning the camera Others include View reference point, view plane normal, view up (HIGS, GKS-3D) Yaw, pitch, roll Elevation, azimuth, twist Direction angles
Two Transforms in Sequence OpenGL thinks of the projective transform as reall two 4x4 matrix transforms FIRST object-space to ee-space x z w e e e e = 2 3 4 5 6 7 8 9 2 3 4 5 x z w o o o o 6 Multipl-Add operations SECOND ee-space to clip-space x z w c c c c = 2 3 4 5 6 7 8 9 2 3 4 5 x z w e e e e Another 6 Multipl-Add operations
Modelview-rojection Transform Matrixes can associate (combine) Combination of the modelview and projection matrix = modelview-projection matrix or often simpl the matrix 2 3 4 5 6 7 8 9 4 5 = 2 3 Matrix multiplication is associative (but not commutative) A(BC) = (AB)C, but ABC CBA 2 3 4 5 6 7 8 9 2 3 4 5 2 3 4 5 6 7 8 9 concatenation is 64 Multipl-Add operations, done b OpenGL driver 2 3 4 5
Specifing the Transforms Specified in two parts First the projection glmatrixmode(gl_rojection); glloadidentit(); glfrustum(-4, +4, // left & right -3, +3, // top & bottom 5, 8); // near & far Second the model-view glmatrixmode(gl_modelview); glloadidentit(); gltranslatef(,, -4); So objects centered at (,,) would be at (,,-4) in ee-space Resulting projection matrix.25.667.333 4.667 Resulting modelview matrix
Modelview-rojection Matrix Transform composition via matrix multiplication 4.667.333.667.25 = 4 5.2.333.667.25 Resulting modelview-projection matrix
Now Draw Some Objects Draw a wireframe cube glcolor3f(,,); // red glutwirecube(6); 6x6x6 unit cube centered at origin (,,) Draw a teapot in the cube glcolor3f(,,); // blue glutsolidteapot(2.); centered at the origin (,,) handle and spout point down the X axis top and bottom in the Y axis As we d expect given a frustum transform, the cube is in perspective The teapot is too but more obvious to observe with a wireframe cube
What We ve Accomplished Simple perspective With glfrustum Establishes how ee-space maps to clip-space Simple viewing With gltranslatef Establishes how world-space maps to ee-space (,,4) (,,) All we reall did was wheel the camera 4 units up the Z axis No actual modeling transforms, just viewing Modeling would be rotating, scaling, or otherwise transform the objects with the view Arguabl the modelview matrix is reall just a view matrix in this example
Some Simple Modeling Tr some modeling transforms to move teapot But leave the cube alone for reference glushmatrix(); { gltranslatef(.5, -.5, ); glutsolidteapot(2.); } glopmatrix(); glushmatrix(); { glscalef(.5,.,.5); glutsolidteapot(2.); } glopmatrix(); glushmatrix(); { glrotatef(3,,,); glutsolidteapot(2.); } glopmatrix(); We bracket the modeling transform with glushmatrix/glopmatrix commands so the modeling transforms are localized to the particular object
Add Some Lighting Some lighting makes the modeling more intuitive glushmatrix(); { gltranslatef(.5, -.5, ); glutsolidteapot(2.); } glopmatrix(); glushmatrix(); { glscalef(.5,.,.5); glutsolidteapot(2.); } glopmatrix(); glushmatrix(); { glrotatef(3,,,); glutsolidteapot(2.); } glopmatrix(); We ve not discussed lighting et but per-vertex lighting allows a virtual light source to interact with the object s surface orientation and material properties
Modelview-rojection Matrix Let s consider the combined modelview matrix with the rotation glrotate(3,,,) defines a rotation matrix Rotating 3 degrees around an axis in the (,,) direction.97.3333.244.244.97.3333.3333.244.97.25.667.333.667 4.97.3333.244.244.97.3333.3333.244.97 projection view model
Combining All Three Matrix-b-matrix multiplication is associative so VM = (V M) = ( V) M OpenGL keeps V and M together because ee-space is a convenient space for lighting " $ $ $ $ # " $ $ $ $ # 4 view.25.667.333.667 % " ' $ ' $ ' $ ' $ & # % " ' $ ' $ ' $ ' $ & #.97.244.3333.3333.97.244.244.3333.97 model.97.244.3333.3333.97.244.244.3333.97 4 % ' ' ' ' & % ' ' ' ' & projection " $ $ $ $ #.384.35.467.5556.578.467.2766.3778.32 5.2.244.3333.97 4 modelview-projection modelview % ' ' ' ' &
Object- to Clip-space = object object object object world world world world w z x w z x.97.3333.244.244.97.3333.3333.244.97 = world world world world ee ee ee ee w z x w z x 4 = ee ee ee ee clip clip clip clip w z x w z x.667.333.667.25 = object object object object clip clip clip clip w z x w z x 4.97.3333.244 5.2.32.3778.2766.467.578.5556.467.35.384 = object object object object ee ee ee ee w z x w z x 4.97.3333.244.244.97.3333.3333.244.97 = ee ee ee ee clip clip clip clip w z x w z x.667.333.667.25 object-to-world-to-ee-to-clip object-to-ee-to-clip object-to-clip modelview projection projection modelview-projection model view
Complex Scene Example Each character, wall, ceiling, floor, and light have their own modeling transformation Universit of Texas at Austin CS354 - Computer Graphics
Representing Objects Interested in object s boundar Various approaches rocedural representations Often fractal Sierpinski gasket Fractal tree [hilip Winston] Explicit polgon (triangle) meshes B far, the most popular method Curved surface patches Often displacement mapped Implicit representation Blobb, volumetric Quake 2 ke frame triangle meshes Utah Teapot Blobb modeling in RenderMan
Focus on Triangle Meshes Easiest approach to representing object boundaries So what is a mesh and how should it be stored? Simplest view A set of triangles, each with its own 3 vertices Essentiall triangle soup Yet triangles in meshes share edges b design Sharing edges implies sharing vertices More sophisticated view Store single set of unique vertexes in arra Then each primitive (triangle) specifies 3 indices into arra of vertexes More compact Vertex data size >> index size Avoids redundant vertex data Separates topolog (how the mesh is connected) from its geometr (vertex positions and attributes) Connectivit can be deduced more easil Makes mesh processing algorithms easier Geometr data can change without altering the topolog
Consider a Tetrahedron Simplest closed volume Consists of 4 triangles and 4 vertices (and 4 edges) (x2,2,z2) v2 v3 (x3,3,z3) v (x,,z) v (x,,z) triangle list : v,v,v2 : v,v3,v2 2: v3,v,v2 3: v,v,v3 topolog vertex list : (x,,z) : (x,,z) 2: (x2,2,z2) 3: (x3,3,z3) geometr potentiall on-gu!
Benefits of Vertex Arra Approach Unique vertices are stored once Saves memor On CU, on disk, and on GU Matches OpenGL vertex arra model of operation And this matches the efficient GU mode of operation The GU can cache post-transformed vertex results b vertex index Saves retransformation and redundant vertex fetching Direct3D has the same model Allows vertex data to be stored on-gu for even faster vertex processing OpenGL supported vertex buffer objects for this
Next Lecture More about triangle mesh representation Scene graphs