CSc 65 Computer Game Architecture 3D Coordinate Systems 3 - Fundamentals of 3D Systems P (x,y,z) Right-handed Coordinate System P (x,y,z) Left-handed Coordinate System Points can be represented in homogeneous form: P [x y z ] 2 Synthetic Camera Paradigm ear Clipping Plane iew olume Projection Plane Far Clipping Plane Eye (camera) The Camera Two important camera attributes: Location Orientation of axes ote the coordinate system is left-handed + yaw ( azimuth ) + roll ( up ) World Objects + pitch ( colatitude or elevation ) 3 4 Generalized Camera Control Player controls position & orientation World points must be converted to camera points Game engine should handle this (it s game-independent) Additional Camera Settings FO, Aspect, ear & Far (Clipping) Controls projection onto 2D plane (& screen) Again, game engine should handle details w Line of sight World point Field of iew (FO) angle in Width Height w w near Aspect Ratio width / height 5 far 6
Sage Default Camera alues Sage Camera/Display Structure Loc [0 0 ], looking down negative fov 60, aspect, near0.0, far00 IDisplaySystem IRenderer ICamera DisplaySystem Renderer Camera JFrame Canvas 60 00 Game loop render() step causes camera view (contents of iew olume) to be drawn on Renderer s Canvas in DisplaySystem s JFrame 7 8 SAGE s ICamera Interface //This interface defines the functions provided by all camera implementations public interface ICamera { // get the camera s location/orientation public Point3D getlocation(); public ector3d getrightaxis(); public ector3d getpaxis(); public ector3d getiewdirection(); Camera Manipulation example: Move Forward change location along the view direction CurLoc //modify the camera s location/orientation (note that it is the user s //responsibility to insure the camera axes remain mutually perpendicular) public void setlocation(point3d loc); public void setrightaxis(ector3d right); public void setpaxis(ector3d up); public void setiewdirection(ector3d viewdir); public void setaxes(ector3d right, ector3d up, ector3d viewdir); //query/alter the iew olume associated with the camera public double[] getperspectivefrustumsettings(); public void setperspectivefrustum(double fov, double aspect, double near, double far); //... other methods to be seen later... CurLoc As ector iewdirector () ewloc ewloc CurLocAsector + (iewdirector*moveamount) 9 0 Defining Simple 3D Models Rasterization Output ertex 2 0 (0,, 0) Color red (-, -, ) Color green 2 (, -, ) Color blue 3 (, -, -) Color yellow 4 (-, -, -) Color magenta Scan lines (pixel raster rows) A 2x2x2 Pyramid Centered At The Origin Output ertex 0 2 Output ertex
Application Code Graphics API (OpenGL or Direct) Interpolation Result Pyramid Data Structures ertices 0 0 0 - - 2-3 - - 4 - - - Geometry one vertex red green blue yellow magenta Color one vertex index Triangles 0 0 2 0 2 3 2 0 3 4 3 0 4 4 4 2 5 4 3 2 Indexes 3 4 The Graphics Pipeline ertex Graphics Card Fragment ( pixel ) Primitive Assembly Primitive (Projection, Clipping) Fragment Testing (Blending, HSR, etc.) Implemented by the combination of the graphics driver (software), and graphics (hardware) card Can be FIED-FCTIO or PROGRAMMABLE, depending on implementation version ertices Other Data Rasterization Frame Buffer (Memory) Display 3D Transformations eeded for a wide variety of operations: Modeling Positioning & orienting objects in the 3D virtual world Camera positioning ( viewing ) Creating the 2D screen view of the 3D world view ( projection ) Making objects move, grow, spin, fly, etc. Client Side Server Side 5 6 Translation (column-major form): Scaling (column-major form): (x+t x ) (y+t y ) (z+t z ) 0 0 T x 0 0 T y 0 0 T z 0 0 0 * x y z (x*s x ) (y*s y ) (z*s z ) S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 * x y z 7 8
3D Rotation Recall 2D rotations can be about any point o For simplicity we define only 2D rotation about the origin o Other rotations require translation to/from the origin Similarly, 3D rotations can be about any line (any axis of rotation ) : Axis of Rotation Euler s Theorem Any rotation (or sequence of rotations) about a point is equivalent to a single rotation about some axis through that point. [Leonard Euler, 707-783] This is equivalent to saying: Rotation about an arbitrary line through the origin can be accomplished by an equivalent set of rotations about the,, and axes. Arbitrary Point Thus we can rotate about an arbitrary axis as follows:. Translate the axis so it goes through the origin, 2. Rotate by the appropriate Euler angles about,, and, and 3. ndo the translation 9 20 isualizing Euler s Theorem 3D Rotation Transforms Desired point rotation Axis of Rotation Rotation about by θ: 0 0 0 0 cos θ -sin θ 0 0 sin θ cos θ 0 0 0 0 Euler angle rotations to accomplish desired rotation 2 22 Rotation about by θ: Rotation about by θ: cos θ 0 sin θ 0 cos θ -sin θ 0 0 0 0 0 -sin θ 0 cos θ 0 0 0 0 sin θ cos θ 0 0 0 0 0 0 0 0 23 24
Rotation in Angle/Axis Form Rotation Axis [ ] Rotation Angle * * Positive rotation CCW as seen from vector (axis) head, looking toward tail at origin (right hand rule) rotate(60, x, y, z) Representing Transforms Package graphicslib3d Class Matrix3D : a 4x4 ( 3D ) matrix Methods for specifying translation, rotation, & scaling, obtaining transpose and inverse, etc. Similar to Java s AffineTransform (but 3D) Class ector3d : a 4-element ( 3D ) vector Methods for most common vector operations: add, dot- and cross-product, magnitude, normalize seful for representing, for example, a rotation axis 25 26 Sceneode Hierarchy Every object in a scene is an instance of class sage.scene.sceneode, which provides local translate, rotate, and scale matrices Predefined classes in package: sage.scene.shape <<abstract>> Sceneode -localtranslation : Matrix3D -localrotation : Matrix3D -localscale : Matrix3D... + translate(tx,ty,tz) + rotate(degrees,axis) + scale(sx, sy, sz) Apply the specified transform to the local matrix Cube Cylinder Line Pyramid Sphere Teapot... Detecting Camera Collisions Sage Sceneodes have bounding volumes (B s) called their world bound B contains() method can be used to check for camera collisions: for (Sceneode s : getgameworld()) // getgameworld is inherited from BaseGame { if (s.getworldbound()! null) { if (s.getworldbound().contains(cameraloc)) { // do whatever should happen when camera hits an object; // e.g., mark object for removal from game and increment score 27 28