Chapter III Rasterization 3 D Graphics for Game

  • Slides: 32
Download presentation
Chapter III Rasterization 3 D Graphics for Game Programming

Chapter III Rasterization 3 D Graphics for Game Programming

Rasterization Stage § The vertices processed by the vertex program enter a hard-wired stage

Rasterization Stage § The vertices processed by the vertex program enter a hard-wired stage and are assembled to build primitives such as triangles. Each primitive is further processed to determine its 2 D form appearing on the screen, and is rasterized into a set of fragments. § The hard-wired stage is generally named primitive assembly and rasterization. Direct 3 D simply calls this stage rasterization or rasterizer. § The hard-wired rasterization stage performs the following: § § § Clipping Perspective division Back-face culling Viewport transform Scan conversion (rasterization in a narrow sense) 3 D Graphics for Game Programming 3 -2 2

Clipping § Clipping is performed in the clip space, but the following figure presents

Clipping § Clipping is performed in the clip space, but the following figure presents its concept in the camera space, for the sake of intuitive understanding. § § § ‘Completely outside’ triangles are discarded. ‘Completely inside’ triangles are accepted. ‘Intersecting’ triangles are clipped. § As a result of clipping, vertices may be added to and deleted from the triangle. § Clipping in the (homogeneous) clip space is a little complex but welldeveloped algorithm. 3 D Graphics for Game Programming 3 -3 3

Perspective Division § Unlike affine transforms, the last row of Mproj is not (0

Perspective Division § Unlike affine transforms, the last row of Mproj is not (0 0 0 1) but (0 0 -1 0). When Mproj is applied to (x, y, z, 1), the w-coordinate of the transformed vertex is –z. § In order to convert from the homogeneous (clip) space to the Cartesian space, each vertex should be divided by its w-coordinate (which equals –z). 3 D Graphics for Game Programming 3 -4 4

Perspective Division (cont’d) § Note that –z is a positive value representing the distance

Perspective Division (cont’d) § Note that –z is a positive value representing the distance from the xy-plane of the camera space. Division by –z makes distant objects smaller. It is perspective division. The result is defined in NDC (normalized device coordinates). 3 D Graphics for Game Programming

Back-face Culling § The polygons facing away from the viewpoint of the camera are

Back-face Culling § The polygons facing away from the viewpoint of the camera are discarded. Such polygons are called back-faces. (The polygons facing the camera are called front-faces. ) § In the camera space, the normal of a triangle can be used to determine whether the triangle is a front-face or a back-face. A triangle is taken as a back-face if the camera (EYE) is in the opposite side of the triangle's normal. Otherwise, it is a front-face. § For the purpose, compute the dot product of the triangle normal n and the vector c connecting the camera position and a vertex of the triangle. 3 D Graphics for Game Programming 3 -6 6

Back-face Culling (cont’d) § Unfortunately, back-face culling in the camera space is expensive. §

Back-face Culling (cont’d) § Unfortunately, back-face culling in the camera space is expensive. § The projection transform makes all the connecting vectors parallel to the z-axis. The universal connecting vector represents the parallelized projection lines. Then, by viewing the triangles along the universal connecting vector, we can distinguish the back-faces from the front-faces. 3 D Graphics for Game Programming

Back-face Culling (cont’d) § Viewing a triangle along the universal connecting vector is equivalent

Back-face Culling (cont’d) § Viewing a triangle along the universal connecting vector is equivalent to orthographically projecting the triangle onto the xy-plane. § A 2 D triangle with CW-ordered vertices is a back-face, and a 2 D triangle with CCW-ordered vertices is a front-face. § Compute the following determinant, where the first row represents the 2 D vector connecting v 1 and v 2, and the second row represents the 2 D vector connecting v 1 and v 3. If it is positive, CCW. If negative, CW. If 0, edge-on face. § Note that, if the vertices are ordered CW in the clip space, the reverse holds, i. e. , the front-face has CW-ordered vertices in 2 D. 3 D Graphics for Game Programming 3 -8 8

Back-face Culling – Open. GL Example § Open. GL and Direct 3 D allow

Back-face Culling – Open. GL Example § Open. GL and Direct 3 D allow us to control the face culling mechanism based on vertex ordering. 3 D Graphics for Game Programming 3 -9 9

Coordinate Systems – RHS vs. LHS § Notice the difference between 3 ds Max

Coordinate Systems – RHS vs. LHS § Notice the difference between 3 ds Max and Open. GL: The vertical axis is the zaxis in 3 ds Max, but is the y-axis in Open. GL. 3 D Graphics for Game Programming 3 -10 10

Coordinate Systems – 3 ds Max to Open. GL If the scene is exported

Coordinate Systems – 3 ds Max to Open. GL If the scene is exported as is to Open. GL, the objects appear flipped. 3 D Graphics for Game Programming At the time of export, flip the yz -axes while making the objects immovable. 3 -11 11

Coordinate Systems – 3 ds Max to Open. GL (cont’d) 3 D Graphics for

Coordinate Systems – 3 ds Max to Open. GL (cont’d) 3 D Graphics for Game Programming 3 -12 12

Coordinate Systems – 3 ds Max to Direct 3 D § Assume that the

Coordinate Systems – 3 ds Max to Direct 3 D § Assume that the yz-axes have been flipped. Then, ‘ 3 ds Max to Direct 3 D’ problem is reduced to ‘Open. GL to Direct 3 D. ’ § Placing an RHS-based model into an LHS (or vice versa) has the effect of making the model reflected by the xy-plane mirror, as shown in (a). § The problem can be easily resolved if we enforce one more reflection, as shown in (b). Reflecting the reflected returns to the original! § Reflection with respect to the xy-plane is equivalent to negating the zcoordinates. 3 D Graphics for Game Programming 3 -13 13

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conceptual flow

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conceptual flow from 3 ds Max to Direct 3 D 3 D Graphics for Game Programming 3 -14 14

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conceptual flow

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conceptual flow from 3 ds Max to Direct 3 D 3 D Graphics for Game Programming 3 -15 15

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conversion from

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § Conversion from 3 ds Max to Direct 3 D requires yz-axis flip followed by znegation. The combination is simply yz-swap. 3 D Graphics for Game Programming 3 -16 16

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § When only

Coordinate Systems – 3 ds Max to Direct 3 D (cont’d) § When only yz-swap is done, we have the following image. Instead of backfaces, front-faces are culled. § The yz-swap does not change the CCW order of vertices, and therefore the front-faces have CCW-ordered vertices in 2 D. In Direct 3 D, the vertices of a back-face are assumed to appear CCW-ordered in 2 D, and the default is to cull the faces with the CCW-ordered vertices. § The solution is to change the Direct 3 D culling mode such that the faces with CW-ordered vertices are culled. 3 D Graphics for Game Programming 3 -17 17

Viewport § A window at the computer screen is associated with its own screen

Viewport § A window at the computer screen is associated with its own screen space. It is a 3 D space and right-handed. § A viewport is defined in the screen space. typedef struct _D 3 DVIEWPORT 9 { DWORD X; DWORD Y; DWORD Width; DWORD Height; float Min. Z; float Max. Z; } D 3 DVIEWPORT 9; 3 D Graphics for Game Programming typedef struct D 3 D 10_VIEWPORT { INT Top. Left. X; INT Top. Left. Y; UINT Width; UINT Height; FLOAT Min. Depth; FLOAT Max. Depth; } D 3 D 10_VIEWPORT; 3 -18 18

Viewport Transform In most applications, Min. Z and Max. Z are set to 0.

Viewport Transform In most applications, Min. Z and Max. Z are set to 0. 0 and 1. 0, respectively, and both of Min. X and Min. Y are zero. 3 D Graphics for Game Programming 3 -19 19

Scan Conversion § The last substage in the rasterizer is scan conversion, which is

Scan Conversion § The last substage in the rasterizer is scan conversion, which is often called “rasterization in a narrow sense. ” § It defines the screen-space pixel locations covered by the primitive and interpolates the per-vertex attributes to determine the per-fragment attributes at each pixel location. 3 D Graphics for Game Programming 3 -20 20

Scan Conversion (cont’d) 3 D Graphics for Game Programming 3 -21 21

Scan Conversion (cont’d) 3 D Graphics for Game Programming 3 -21 21

Scan Conversion (cont’d) 3 D Graphics for Game Programming 3 -22 22

Scan Conversion (cont’d) 3 D Graphics for Game Programming 3 -22 22

Top-left Rule § When a pixel is on the edge shared by two triangles,

Top-left Rule § When a pixel is on the edge shared by two triangles, we have to decide which triangle it belongs to. § A triangle may have right, left, top or bottom edges. § A pixel belongs to a triangle if it lies on the top or left edge of the triangle. 3 D Graphics for Game Programming 3 -23 23

Perspective Correction 3 D Graphics for Game Programming 3 -24 24

Perspective Correction 3 D Graphics for Game Programming 3 -24 24

Object Picking § An object is picked by placing the mouse cursor on it

Object Picking § An object is picked by placing the mouse cursor on it or clicking it. § Mouse clicking simply returns the 2 D pixel coordinates (xs, ys). Given (xs, ys), we can consider a ray described by the start point (xs, ys, 0) and the direction vector (0, 0, 1). § The ray will be transformed back to the world or object space, and then rayobject intersection test will be done. For now, let’s transform the ray to the camera space 3 D Graphics for Game Programming 3 -25 25

Object Picking (cont’d) direction vector of the camera-space ray (CS_Direc) 3 D Graphics for

Object Picking (cont’d) direction vector of the camera-space ray (CS_Direc) 3 D Graphics for Game Programming 3 -26 26

Object Picking (cont’d) § Let us transform the direction vector of the camera-space ray

Object Picking (cont’d) § Let us transform the direction vector of the camera-space ray (CS_Direc) into the world space (WS_Direc). § For now, assume that the start point of the camera-space ray is the origin. Then, the start point of the world-space ray (WS_Start) is simply EYE. 3 D Graphics for Game Programming 3 -27 27

Object Picking (cont’d) § In principle, we have to perform the ray intersection test

Object Picking (cont’d) § In principle, we have to perform the ray intersection test with every triangle in the triangle list. A faster but less accurate method is to approximate each mesh with a bounding sphere that completely contains the mesh, and then do the raysphere intersection test. 3 D Graphics for Game Programming 3 -28 28

Object Picking (cont’d) § Bounding volumes § Bounding volume creation 3 D Graphics for

Object Picking (cont’d) § Bounding volumes § Bounding volume creation 3 D Graphics for Game Programming 3 -29 29

Object Picking (cont’d) § For the ray-sphere intersection test, let us represent the ray

Object Picking (cont’d) § For the ray-sphere intersection test, let us represent the ray in a parametric representation. § Collect only positive ts. (Given two positive ts for a sphere, choose the smaller. ) 3 D Graphics for Game Programming 3 -30 30

Object Picking (cont’d) § The bounding sphere hit first by the ray is the

Object Picking (cont’d) § The bounding sphere hit first by the ray is the one with the smallest t with the range constrain [n, f]. 3 D Graphics for Game Programming 3 -31 31

Object Picking (cont’d) § Ray-sphere intersection test is often performed at the preprocessing step,

Object Picking (cont’d) § Ray-sphere intersection test is often performed at the preprocessing step, and discards the polygon mesh that is guaranteed not to intersect the ray. 3 D Graphics for Game Programming 3 -32 32