Computergrafik Today Rendering pipeline s View volumes, clipping Viewport Matthias Zwicker Universität Bern Herbst 2008 Rendering pipeline Rendering pipeline Hardware & software that draws 3D scenes on the screen Most operations performed by specialized hardware (GPU) Access to hardware through low-level 3D API (DirectX, OpenGL) jogl is a Java binding to OpenGL All scene data flows through the pipeline at least once for each frame Rendering pipeline Rendering pipeline implements object order algorithm Loop over all objects Draw triangles one by one (rasterization) Alternatives? Advantages, disadvantages? Object vs. order Object order Rasterization type algorithms Desirable memory access pattern ( streaming, data locality, avoid random scene access) Suitable for real time rendering (OpenGL, DirectX) Popular for production rendering (Pixar RenderMan), where scenes often do not fit in RAM Full global illumination not possible with purely object order algorithm order Ray tracing type algorithms Undesirable memory access pattern (random scene access) Requires sophisticaed data structures for fast scene access Full global illumination possible Most popular for photo-realistic image synthesis Rendering engine Rendering engine (jtrt) Rendering pipeline Additional software layer encapsulating low-level API (OpenGL, DirectX, ) Additional functionality Layered software architecture common in industry Game engines http://en.wikipedia.org/wiki /Game_engine 1
Rendering pipeline stages Rendering pipeline stages Geometry Vertices and how they are connected Triangles, lines, points, triangle strips Attributes such as color Specified in object Processed by the rendering pipeline one-by-one Textures, lights, etc. Transform object to camera MODELVIEW matrix Specified by GL_MODELVIEW matrix in OpenGL User computes GL_MODELVIEW matrix as discussed Rendering pipeline stages Rendering pipeline stages Look up light sources Compute color for each vertex Later in the course Project 3D vertices to 2D image positions GL_PROJECTION matrix This lecture Rendering pipeline stages Rendering pipeline stages Draw primitives (triangles, lines, etc.) Determine what is visible Next lecture Pixel colors 2
Today Rendering pipeline s View volumes, clipping Viewport Object, world, camera coords. Object World Objects in camera We have things lined up the way we like them on screen x to the right y up -z going into the screen Objects to look at are in front of us, i.e. have negative z values But objects are still in 3D Today: how to project them into 2D s Given 3D points (vertices) in camera, determine corresponding 2D image Orthographic projection Simply ignore z-coordinate Use camera space xy as image Orthographic projection Project points to x-y plane along parallel lines Graphical illustrations, architecture Most common for computer graphics Simplified model of human eye, or camera lens (pinhole camera) Things farther away seem smaller Discovery/formalization attributed to Filippo Brunelleschi in the early 1400 s 3
Project along rays that converge in center of projection Parallel lines no longer parallel, converge at one point Center of projection 3D scene 2D image plane Earliest example La Trinitá (1427) by Masaccio The math: simplified case The math: simplified case plane plane Can express this using homogeneous, 4x4 matrices The math: simplified case matrix Homogeneous division matrix Homogeneous coord.!= 1! Homogeneous division Using projection matrix, homogeneous division seems more complicated than just multiplying all by d/z, so why do it? Will allow us to handle different types of projections in a unified way define arbitrary view volumes 4
Background Projective space: the space of one-dimensional vector subspaces of a given vector space Elements of projective spaces are 1D vector subspaces Each element of 1D subspace is equivalent (represents same element of projective space) For example, projective space P 3 represented using R 4 and homogeneous Each point along 4D ray is equivalent to same 3D point at w=1 x λx x/w y λy y/w equivalent z λz z/w w λw 1 1D vector subspace, arbitrary scalar value Equivalent element, for any Background Projective mapping: any non-singular linear mapping on homogeneous, for example, Generalization of affine mappings, arbitrary homogeneous coordinate Projective mappings preserve straight lines, but not parallel lines Much more theory http://www.math.toronto.edu/mathnet/questioncorner/projective.html http://en.wikipedia.org/wiki/projective_space Background P 3 can be interpreted as consisting of R 3 and its points at infinity Points are said to be at infinity if w = 0 Represented by direction vector Do parallel lines intersect at infinity? In projective geometry, yes. http://www.math.toronto.edu/mathnet/questioncorner/infin ity.html Points at infinity (2,1,0,1) (2,1,0,0.5) ~ (4,2,0,1) Point at infinity, direction vector (2,1,0,0) (2,1,0,0.33333) ~ (6,3,0,1) 2D line intersection Two line equations Intersection c b x 0 = 0 0 / c 1 b 1 y 0 = a 0 c 0 a 1 c 1 a 0 x + b 0 y + c 0 =0 a 1 x + b 1 y + c 1 =0 a 0 b 0 a 1 b 1 / a 0 b 0 a 1 b 1 Determinant Using homogeneous Line equations a 0 x + b 0 y + wc 0 =0 a 1 x + b 1 y + wc 1 =0 h a0 b 0 x i c 0 y w h a1 b 1 x i c 1 y w Lines parallel: division by zero 5
Using homogeneous Line equations a 0 x + b 0 y + wc 0 =0 a 1 x + b 1 y + wc 1 =0 h a0 b 0 x i c 0 y w h a1 b 1 x i c 1 y w Intersection: any scalar multiple of = x 0 a 0 a y 0 b 0 1 b w 0 1 c 0 c x 0 /w 0 1 y 0 /w 0 Lines not parallel: intersection is 1 Lines parallel: w =0, intersection at infinity! Projective space Projective space [xyzw] homogeneous includes points at infinity (w=0) projective mappings (perspective projection) Vector space Affine space [xyz] [xyz1], [xyz0] represents vectors homogeneous coords. linear mappings distinguishes points (rotation around origin, and vectors scaling, shear) affine mappings (translation) In practice Use 4x4 homogeneous matrices normally Modeling & viewing s are affine mappings points keep w=1 no need to divide by w when doing modeling operations or transforming into camera space is a projective transform Resulting w coordinate not always 1 Divide by w (perspective division, homogeneous division) after multiplying with projection matrix OpenGL rendering pipeline (graphics hardware) does this automatically Realistic image formation More than perspective projection Lens effects Focus, depth of field Fish-eye lens Realistic image formation Chromatic aberration Motion blur Today Rendering pipeline s View volumes, clipping Viewport Often too complicated for hardware rendering pipeline 6
View volumes Define 3D volume seen by camera Perspective view volume General view volume Perspective view volume Orthographic view volume World World Defined by 6 parameters, in camera Left, right, top, bottom boundaries Near, far clipping planes Clipping planes to avoid numerical problems Divide by zero Low precision for distant objects Usually symmetric, i.e., left=-right, top=-bottom Perspective view volume Symmetric view volume Orthographic view volume y y=top FOV -z z=-near Only 4 parameters Vertical field of view (FOV) aspect ratio (width/height) Near, far clipping planes z=-far right left aspect ratio= top bottom right top tan(fov /2) top near Parametrized by 6 parameters Right, left, top, bottom, near, far If symmetric Width, height, near, far Clipping Need to identify objects outside view volume Avoid division by zero Efficiency, don t draw objects outside view volume Performed by OpenGL rendering pipeline Clipping always to canonic view volume Cube[-1..1]x[-1..1]x[-1..1] centered at origin Need to transform desired view frustum to canonic view frustum Clipping Primitives Clip to view frustum 7
Canonic view volume matrix is set such that User defined view volume is transformed into canonic view volume, i.e., cube [-1,1]x[-1,1]x[-1,1] Multiplying vertices of view volume by projection matrix and performing homogeneous divide yields canonic view volume Perspective and orthographic projection are treated exactly the same way matrix matrix Canonic view volume Clipping matrix General view frustum matrix Compare to simple projection matrix from before Simple projection General view frustum matrix Symmetric view frustum with field of view, aspect ratio, near and far clip planes y y=top FOV z=-near 1 0 0 0 aspect tan(fov /2) 1 0 0 0 P persp (FOV,aspect,near, far) tan(fov /2) near far 0 0 near far 0 0 1 0 z=-far -z 2 near far near far Orthographic projection matrix 2 right left 0 0 right left right left 2 top bottom 0 0 P ortho (right,left,top,bottom,near, far) top bottom top bottom 2 far near 0 0 far near far near 0 0 0 1 2 0 0 0 width 2 0 0 0 P ortho (width,height,near, far) height 2 far near 0 0 far near far near 0 0 0 1 w = 1 after mult. with orthographic projection matrix 8
Questions? Today Rendering pipeline s View volumes Viewport Viewport After applying projection matrix, image points are in normalized view Per definition range [-1..1] x [-1..1] Map points to image (i.e., pixel) User defined range [x0 x1] x [y0 y1] Scale and translation The complete transform Mapping a 3D point in object to pixel Object space Dx 0, x 1, y 0, y 1 2 0 0 x 0 x 1 2 2 x 1 x 0 0 y 1 y 0 2 0 y 0 y 1 0 0 1 2 12 0 0 0 1 The complete transform Mapping a 3D point in object to pixel The complete transform Mapping a 3D point in object to pixel Object space World space Object space World space space 9
The complete transform Mapping a 3D point in object to pixel The complete transform Mapping a 3D point in object to pixel Object space World space space Canonic view volume Object space World space space Canonic view volume space The complete transform Mapping a 3D point in object to pixel OpenGL OpenGL GL_MODELVIEW matrix OpenGL GL_PROJECTION matrix Pixel OpenGL rendering pipeline performs these matrix multiplications automatically User just specifies the matrices Seejrtr.GLRenderContext.draw OpenGL GL_MODELVIEW, C -1 M Up to you to define GL_PROJECTION, P Utility routines to set it by specifying view volume: glfrustum(), glperspective(), glortho() Do not use utility functions for project 2 You will implement a software renderer in project 3, which will not rely on any OpenGL Viewport, D Specify implicitly via glviewport() No direct access Coming up Next lecture Drawing (rasterization) Visibility (z-buffering) Exercise session Project 2, interactive viewing 10