Presentation is loading. Please wait.

Presentation is loading. Please wait.

3D Programming and DirectX API. Content Mathematics Mathematics Prepare to Write a 3D program Prepare to Write a 3D program Program Structure Program.

Similar presentations


Presentation on theme: "3D Programming and DirectX API. Content Mathematics Mathematics Prepare to Write a 3D program Prepare to Write a 3D program Program Structure Program."— Presentation transcript:

1 3D Programming and DirectX API

2 Content Mathematics Mathematics Prepare to Write a 3D program Prepare to Write a 3D program Program Structure Program Structure How does the computer calculate 3D image? How does the computer calculate 3D image? Illustration Illustration Other Features in DirectX Library Other Features in DirectX Library

3 Mathematics Don ’ t worry! If you see this chapter in the book, just skip it, because you ’ ve learned. Don ’ t worry! If you see this chapter in the book, just skip it, because you ’ ve learned. Importance of “ Transform ” Importance of “ Transform ” –Translation –Rotation –Scaling – …… Vector & Matrix Vector & Matrix Other Advanced Algorithm Other Advanced Algorithm

4 Prepare to Write a 3D program Using Direct3D Using Direct3D Compiler - VS.NET 2003 Compiler - VS.NET 2003 Download DirectX SDK Download DirectX SDK –http://msdn.microsoft.com/library/default.asp?url=/library/en- us/dnanchor/html/anch_directx.asp http://msdn.microsoft.com/library/default.asp?url=/library/en- us/dnanchor/html/anch_directx.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en- us/dnanchor/html/anch_directx.asp –DirectX 9.0 SDK Update - (February 2005) An Easy Book An Easy Book MSDN – http://www.msdn.com MSDN – http://www.msdn.com

5 Program Structure Initialize D3D Library – Create Device Initialize D3D Library – Create Device Loading or Building Model Loading or Building Model Set Light, Material, and Texture Set Light, Material, and Texture Calculate Transform Matrix, View Matrix, and Projection Matrix Calculate Transform Matrix, View Matrix, and Projection Matrix Render! Render!

6 Initialize D3D Library IDirect3D9* IDirect3D9* Direct3DCreate9(D3D_SDK_VERSION) Direct3DCreate9(D3D_SDK_VERSION) IDirect3DDevice9* IDirect3DDevice9* CreateDevice() CreateDevice() IDirect3D * CreateDevice() Direct3DCreate9() D3DLibrary IDirect3DDevice9* 3D Device( 3D Card)

7 How does the computer calculate 3D image? CPU and GPU (Graphic Process Unit) CPU and GPU (Graphic Process Unit) 3D Library – OpenGL and Direct3D 3D Library – OpenGL and Direct3D

8 How does the computer calculate 3D image? Pipeline Pipeline Data in Model Space World Space World Matrix View Matrix Projection Matrix CPUGPU Camera Space View Space (your screen) Vertex Data Light, Texture, Material

9 Loading and Building Modal Vertex/Index Buffer Vertex/Index Buffer The Structure of Vertex The Structure of Vertex V1V2 V3 V4 V1 V2 V3 V4 123142 … … Index Buffer Vertex Buffer

10 struct Vertex { float x, y, z; float x, y, z; float nx, ny, nz; float nx, ny, nz; float u, v; // texture coordinates float u, v; // texture coordinates}; #define FVF_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1) Demo 1-TexCube Loading and Building Modal XYZnXnY nZU V…

11 Loading and Building Modal X-file X-file –When the model is complex, we can ’ t build it in our code. –Using 3D graphic tool, ex. Maya, 3D studio MAX, Lightwave, MilkShake –Direct3D have already provide funcs to load and parse X-file –Static Model and Animation Model

12 Set Light, Material, and Texture Light - light source Light - light source D3DLIGHT9 light; light.Type = D3DLIGHT_DIRECTIONAL; light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); light.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f); light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f); Demo 2-TexCube 3-TexCube 4-TexCube

13 Set Light, Material, and Texture Material – reflection of light Material – reflection of light {1.0, 1.0, 0.0, 1.0} {1.0, 0.0, 1.0, 1.0} {0.0, 1.0, 1.0, 1.0} Demo EffetEdit

14 Set Light, Material, and Texture Texture – surface of 3D object Texture – surface of 3D object Demo 4-Tex 5-Tex

15 Calculate World Matrix, View Matrix, and Projection Matrix World Matrix World Matrix –D3DXMatrixTranslation( … ) –D3DXMatrixRotationX( … ) –D3DXMatrixIdentity( … ) –D3DXMatrixScaling( … )

16 Calculate World Matrix, View Matrix, and Projection Matrix View Matrix View Matrix –D3DXMatrixLookAtLH(&V, &position, &target, &up);

17 Calculate World Matrix, View Matrix, and Projection Matrix Projection Matrix Projection MatrixD3DXMatrixPerspectiveFovLH();

18 Demo 7-TexCube 8-TexCube

19 Render!! Prepare for render Prepare for render Device->SetTransform(D3DTS_VIEW, &V); Device->SetTransform(D3DTS_WORLD, &V); Device->SetTransform(D3DTS_PROJECTION, &V); Start and Finish Render Start and Finish Render Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); Device->BeginScene(); ……./* render render render !!!! */…. Device->EndScene();

20 Render !! if( mtrl ) device->SetMaterial(mtrl); if( tex ) device->SetTexture(0, tex); device->SetStreamSource(0, _vb, 0, sizeof(Vertex)); device->SetIndices(_ib);device->SetFVF(FVF_VERTEX);device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,24,0,12); Set Material Set Texture Set Index Buffer Tell D3D your vertex format Just Draw It!!

21 More Skill Terrain – Height Map Terrain – Height Map Shadow Shadow Other Algorithm … Other Algorithm … Good website : www.gamedev.net Good website : www.gamedev.net

22 Samples Airplane Airplane Walking Man Walking Man BasicHLSL BasicHLSL ShadowVolume ShadowVolume Terrain Terrain

23 Other Features in DirectX Library DirectPlay DirectPlay –Network Game DirectSound DirectSound –Handle sound effect


Download ppt "3D Programming and DirectX API. Content Mathematics Mathematics Prepare to Write a 3D program Prepare to Write a 3D program Program Structure Program."

Similar presentations


Ads by Google