1 / 64

SAGE Demo 0 Model Viewer

SAGE Demo 0 Model Viewer. SAGE Lecture Notes Ian Parberry University of North Texas. SAGE Demo 0. Demo 0 has two functions Getting you started on coding with SAGE Providing a tool that artists and programmers can use to check S3D models for compatibility and correctness. Introduction.

vera
Download Presentation

SAGE Demo 0 Model Viewer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SAGE Demo 0Model Viewer SAGE Lecture Notes Ian Parberry University of North Texas

  2. SAGE Demo 0 • Demo 0 has two functions • Getting you started on coding with SAGE • Providing a tool that artists and programmers can use to check S3D models for compatibility and correctness

  3. Introduction • Here’s what we’ll cover in this lecture • Introduction to the SAGE foundation code • Using the foundation code to develop a model viewer application

  4. SAGE Foundation Code • SAGE is based on foundation code modified from 3D Math Primer for Graphics and Game Development, by Fletcher Dunn and Ian Parberry

  5. High Level Overview • The book 3D Math Primer for Graphics and Game Development provides a code utility library • Common files are in a folder called common • Utilities are provided for • the standard math tools covered in class • code for managing • Windows • DirectX • includes a dialog box for displaying error messages on exit

  6. High Level Overview 2 • More utilities: • a renderer • primitive input handlers for mouse & keyboard • a model class, including importer for the S3D model file format

  7. Files in Common • AABB3.cpp, AABB3.h: Axially aligned bounding boxes (Section 12.4) • Bitmap.cpp, Bitmap.h: Simple class to hold a bitmap image • Camera.cpp, Camera.h: Camera class • CommonStuff.h, CommonStuff.cpp: Common stuff that doesn’t belong elsewhere • EditTriMesh.cpp, EditTriMesh.h: Editable triangle mesh class (Section 14.5)

  8. More Files in Common • EulerAngles.cpp, EulerAngles.h: Euler angle class (Section 10.3, 11.2) • FontCacheEntry.h, FontCacheEntry.cpp : Font class • MathUtil.cpp, MathUtil.h: Basic math utilities • Matrix4x3.cpp, Matrix4x3.h: Homogenous transformation matrix code (Section 11.5)

  9. Even More Files in Common • Model.cpp, Model.h: Simple class for a 3D model • Quaternion.cpp, Quaternion.h: Quaternion class (Section 10.4, 11.3) • Random.cpp, Random.h, Random2.cpp, Random2.h: Random Number Generator classes • Renderer.cpp, Renderer.h: Code for the rendering engine • RotationMatrix.cpp, RotationMatrix.h: Rotation matrix class (Section 11.4)

  10. Yet More Files in Common • TextureCacheEntry.cpp, TextureCacheEntry.h:Texture management classes • TriMesh.cpp, TriMesh.h: Triangle mesh class (Chapter 14) • Vector2.h, vector3.h: 2D and 3D vector classes (Chapter 6)

  11. Your Main Program • You need to provide function mainProgram() • This function should consist of: initialization while (!gQuitFlag) { // frame loop move objects render objects process input } shutdown

  12. Initialization • Create the application window: createAppWindow(“My Game"); • Select the video mode to mode • Initialize the renderer (second parameter is whether windowed) gRenderer.init(mode, true);

  13. Initialization 2 • Your initialization, eg. Reading in models: Model model; model.importS3d(“Model.s3d”); model.cache();

  14. Move Objects In vector parlance: add to the location vector the velocity vector times elapsed time • Similar for orientation • Use the frame rate timer provided by the renderer: float gRenderer.getTimeStep();

  15. Render Objects • Set up the camera gRenderer.setCamera(cameraLoc, cameraOrient); gRenderer.setZoom( fovToZoom(degToRad(dFov))); • Prepare the renderer gRenderer.beginScene(); gRenderer.clear(); • Set the scene (eg lighting)

  16. Render Objects 2 • Render the objects using an instance stack gRenderer.instance(kZeroVector, modelOrient); model.render(); gRenderer.instancePop(); • Show the result gRenderer.endScene(); gRenderer.flipPages();

  17. Process Input • Process input at the end of each frame. • Support provided in Demo 0 is limited to wrappering the Windows API input.

  18. Shutdown • Shut down the renderer gRenderer.shutdown(); • Destroy the application window destroyAppWindow();

  19. Making it Into a Game Engine • SAGE will add code for some of the features needed to make a game, for example: • Terrain (Demo 1) • Object manager (Demo 3) • DirectInput (Demo 3) • Particle engine (Demo 5) • Game shell • Sound manager (Demo 6) • Other things that can be added • Artificial intelligence • Physics • Networking

  20. Key Topics • Renderer • Camera • Reference Frames • Textures • Render States • Models

  21. Key Topics Cont. • GameBase • Resources • Input • DirectoryManager

  22. Renderer Overview • The render code has the following responsibilities • Initializing the scene • Drawing everything to the screen • Maintaining game data • Validating devices • Cleaning up memory • Maintaining the camera • Restoring a scene • Managing the window

  23. Renderer Functions • init() – Initializes the renderer • shutdown() – Cleans up the renderer before shutdown. • beginScene() – Called at the start of a frame • Checks to make sure we still have the device • endScene() – Called at the end of a frame • Checks again to make sure we still have the device

  24. Function Overview Cont. • flipPages() – Updates the screen • validateDevice() – Make sure you can still draw • restoreRenderStates() – Restores render states to their default values

  25. Renderer::init() • Initializes the renderer with the following parameters • VideoMode – The video mode attributes • xRes – The horizontal resolution of the screen • yRes – The vertical resoulution of the screen • bitsPerPixel – 16, 24, or 32-bit color • refreshHz – The refresh rate of the screen • Can only use kRefreshRateXxx constants • shaderDebug – True if you want shader debugging turning on • windowed – True if you want the game to run in windowed

  26. Renderer::flipPages() • Check to make sure we still have the device • Update the screen if we have the device • Update the timing if (pD3DDevice != NULL) { HRESULT result = pD3DDevice->Present(NULL,NULL,NULL,NULL); }

  27. Renderer::validateDevice() • validateDevice() performs the following functions • Test if device is valid • Checks if device is lost, and if so loops until it returns. HRESULT hr = pD3DDevice->TestCooperativeLevel(); if (hr == D3DERR_DEVICELOST) { while (1) { // let windows process gWindowsWrapper.idle(); // check device to see if it is ready hr = pD3DDevice->TestCooperativeLevel(); if (hr == D3DERR_DEVICENOTRESET) break; } }

  28. Renderer::validateDevice() Cont. • Restores non-managed resources // device is ready! restore all non managed resources gResourceManager.releaseAll(); // release pointer to buffers b/c these are not in the resource manager if (pOriginalBackBuffer) pOriginalBackBuffer->Release(); if (pOriginalDepthStencil) pOriginalDepthStencil->Release(); pD3DDevice->Reset(&presentParms); // get pointers to depth buffer and back buffer pD3DDevice->GetRenderTarget(0, &pOriginalBackBuffer); pD3DDevice->GetDepthStencilSurface(&pOriginalDepthStencil); gResourceManager.restoreAll(); restoreRenderStates();

  29. Renderer::restoreRenderStates() • Resets render states to default values setD3DRenderState(D3DRS_ZENABLE, zEnable); setD3DRenderState(D3DRS_ZWRITEENABLE, TRUE); setD3DRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); setD3DRenderState(D3DRS_ALPHABLENDENABLE, blendEnable); setD3DRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); setD3DRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); setD3DRenderState(D3DRS_AMBIENT, ambientLightColor); setD3DRenderState(D3DRS_FOGENABLE, fogEnable); setD3DRenderState(D3DRS_FOGCOLOR, fogColor); setD3DRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR); setD3DRenderState(D3DRS_RANGEFOGENABLE, TRUE); setD3DRenderState(D3DRS_FOGSTART, *(DWORD*)(&fogNear)); setD3DRenderState(D3DRS_FOGEND, *(DWORD*)(&fogFar)); setBackfaceMode(backfaceMode); setWireframe(wireframeOn);

  30. Renderer::restoreRenderStates() Cont. • Reset sample states to their default values • Reset Material properties to their default values • Reset Lights to their default values setD3DSamplerState(D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); setD3DSamplerState(D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); setD3DSamplerState(D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); // Set texture wrapping mode setD3DSamplerState(D3DSAMP_ADDRESSU , D3DTADDRESS_WRAP ); setD3DSamplerState(D3DSAMP_ADDRESSV , D3DTADDRESS_WRAP );

  31. Camera • The camera provides your view of the scene • The renderer handles camera management, and gives you access to the camera • Camera Attributes • Position • Orientation • Zoom • Clipping Planes

  32. Clipping Planes • Creates boundaries for visible objects • Near Clipping Plane • Objects closer to the camera then the near clipping plane are culled • Far Clipping Plane • Object farther away from the camera then the far clipping plane are culled

  33. Reference Frames • How the camera views a scene • Local Space • The coordinate space in which an object is first defined • World Space • The space where all objects in a scene are displayed • Object Space • The coordinate space in which an object is defined

  34. Reference Frame Implementation • Matrix representation • Using a stack for reference frames • For the model viewer we only use the reference frame stack to push the current reference frame onto before changing it so that we can pop it off when we are done and leave it unchanged.

  35. Textures • The Renderer manages textures using a texture cache • This loads the texture into memory // get a handle to the texture int txtHndl = gRenderer.cacheTexture("myImage.tga", true); // ... processing code // set the texture myImage.tga to be the current texture gRenderer.selectTexture(txtHndl); // draw model

  36. Render States Overview • The Fixed-Function Pipeline • Depth Buffer • Alpha Blending • Fog • Wireframe • Texture Clamping • Lighting • Back-Face Culling

  37. Depth Buffer • Z-Culling • Culls objects behind other objects based on their z-axis position • Transparency • Typically you have to disable Z-Culling when drawing transparent objects, and then draw them in z-order from back to front

  38. Alpha Blending • setSourceBlendMode() • setDestBlendMode() Color = ColorOld(1.0 – alpha) + ColorNew * Alpha

  39. Fog • Obscuring the far clipping plane • Using fog gradually decreases visibility of objects as they get farther away. This keeps them from just disappearing once they reach the far clipping plane. • Fog near plane • Objects closer to the camera the near fog plane don’t get obscured • Linear Interpolation • Fog in demo 00 is calculated using linear interpolation. This means the thickness of the fog is directly proportional to the distance from the camera.

  40. Wireframe Mode setWireFrame(true); setWireFrame(false);

  41. Texture Clamping • Tiling • When you tile a texture, it repeats if the uv coordinates of a vertex are greater than 1.0. 1.0 2.0 0.0

  42. Texture Clamping Cont. • Clamping • When you clamp a texture, uv coordinates greater than 1.0 are drawn with a solid color 1.0 2.0 0.0

  43. Lighting • Ambient Light • General level of light that affects everything in the scene the same • Directional Light • Light with a location and direction. • Spot Light • Surface Normals • Materials

  44. Back-Face Culling • Winding • CullMode • Clockwise • Polygons defined in a counter-clockwise order are not drawn • Counter-Clockwise • Polygons define in a clockwise order are not drawn • None • All polygons are drawn regardless of their order.

  45. Render Functions • DrawPrimitiveUP • Renders data as a sequence of geometric primitives without using vertex buffers • DrawPrimitive • Renders a sequence of non-indexed, geometric primitives from a vertex buffer • DrawIndexedPrimitive • Renders an indexed geometric primitive using a vertex buffer and an index buffer

  46. Models Overview • Initialization • Importing Models • Rendering Models • Bounding Boxes

  47. Initialization • BufferUsage Enumeration • NoBuffers • Don’t buffer the models geometry • StaticIndexBuffer • Buffer the models geometry and use an index buffer • StaticBuffers(default) • Buffer the models geometry without an index buffer

More Related