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

Post on 05-Jan-2016

221 views 0 download

Tags:

Transcript of 3D Programming and DirectX API. Content Mathematics Mathematics Prepare to Write a 3D program...

3D Programming and DirectX 3D Programming and DirectX APIAPI

ContentContent

• MathematicsMathematics

• Prepare to Write a 3D programPrepare to Write a 3D program

• Program StructureProgram Structure

• How does the computer calculate 3D How does the computer calculate 3D image?image?

• IllustrationIllustration

• Other Features in DirectX LibraryOther Features in DirectX Library

MathematicsMathematics

• Don’t worry! If you see this chapter in the Don’t worry! If you see this chapter in the book, just skip it, because you’ve learned.book, just skip it, because you’ve learned.

• Importance of “Transform”Importance of “Transform”– TranslationTranslation– RotationRotation– ScalingScaling– …………

• Vector & MatrixVector & Matrix• Other Advanced AlgorithmOther Advanced Algorithm

Prepare to Write a 3D Prepare to Write a 3D programprogram

• Using Direct3DUsing Direct3D• Compiler - VS.NET 2003Compiler - VS.NET 2003• Download DirectX SDKDownload DirectX SDK

– http://http://msdn.microsoft.com/library/default.asp?urlmsdn.microsoft.com/library/default.asp?url=/library/en-us/=/library/en-us/dnanchor/html/anch_directx.aspdnanchor/html/anch_directx.asp

– DirectX 9.0 SDK Update - (February 2005)DirectX 9.0 SDK Update - (February 2005)

• An Easy BookAn Easy Book• MSDN – http://www.msdn.comMSDN – http://www.msdn.com

Program StructureProgram Structure

• Initialize D3D Library – Create DeviceInitialize D3D Library – Create Device

• Loading or Building ModelLoading or Building Model

• Set Light, Material, and TextureSet Light, Material, and Texture

• Calculate Transform Matrix, View Calculate Transform Matrix, View Matrix , and Projection MatrixMatrix , and Projection Matrix

• Render!Render!

Initialize D3D LibraryInitialize D3D Library

• IDirect3D9*IDirect3D9*• Direct3DCreate9(D3D_SDK_VERSION)Direct3DCreate9(D3D_SDK_VERSION)• IDirect3DDevice9*IDirect3DDevice9*• CreateDevice()CreateDevice()

IDirect3D *IDirect3D *CreateDevice()CreateDevice()Direct3DCreate9()Direct3DCreate9()

D3DLibrarD3DLibraryy

IDirect3DDevice9*IDirect3DDevice9*

3D Device( 3D 3D Device( 3D Card)Card)

How does the computer How does the computer calculate 3D image?calculate 3D image?

• CPU and GPU (Graphic Process Unit)CPU and GPU (Graphic Process Unit)

• 3D Library – OpenGL and 3D Library – OpenGL and Direct3DDirect3D

How does the computer How does the computer calculate 3D image? calculate 3D image?

• PipelinePipeline

Data in Model Space World

Space

World Matrix

View Matrix

Projection Matrix

CPU GPU

Camera Space

View Space

(your screen)

Vertex Data

Light, Texture, Material

Loading and Building ModalLoading and Building Modal

• Vertex/Index BufferVertex/Index Buffer

• The Structure of VertexThe Structure of Vertex

V1

V2

V3

V4

V1

V2

V3

V4

1 2 3 1 4 2 …

Index Buffer

Vertex Buffer

struct Vertexstruct Vertex{{ float x, y, z;float x, y, z; float nx, ny, nz;float nx, ny, nz; float u, v; // texture coordinatesfloat u, v; // texture coordinates};};#define FVF_VERTEX (#define FVF_VERTEX (D3DFVF_XYZD3DFVF_XYZ | | D3DFVF_NORMALD3DFVF_NORMAL | | D3DFVF_TEX1D3DFVF_TEX1))Demo 1-TexCubeDemo 1-TexCube

Loading and Building ModalLoading and Building Modal

X Y Z nX nY nZ U V …

Loading and Building ModalLoading and Building Modal

• X-fileX-file– When the model is complex, we can’t build When the model is complex, we can’t build

it in our code.it in our code.– Using 3D graphic tool, ex. Maya, 3D studio MUsing 3D graphic tool, ex. Maya, 3D studio M

AX, Lightwave, AX, Lightwave, MilkShakeMilkShake– Direct3D have already provide funcs to load Direct3D have already provide funcs to load

and parse X-fileand parse X-file– Static Model and Animation ModelStatic Model and Animation Model

Set Light, Material, and Set Light, Material, and TextureTexture

• Light - light sourceLight - light sourceD3DLIGHT9 light;D3DLIGHT9 light;light.Type = D3DLIGHT_DIRECTIONAL;light.Type = D3DLIGHT_DIRECTIONAL;light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 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.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);

Demo 2-TexCube 3-TexCube 4-TexCube

Set Light, Material, and Set Light, Material, and TextureTexture

• 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

Set Light, Material, and Set Light, Material, and TextureTexture

• Texture – surface of 3D objectTexture – surface of 3D object

Demo 4-Tex 5-Tex

Calculate World Matrix, View Calculate World Matrix, View Matrix , and Projection MatrixMatrix , and Projection Matrix

• World MatrixWorld Matrix– D3DXMatrixTranslation(…)D3DXMatrixTranslation(…)– D3DXMatrixRotationX(…)D3DXMatrixRotationX(…)– D3DXMatrixIdentity(…)D3DXMatrixIdentity(…)– D3DXMatrixScaling(…)D3DXMatrixScaling(…)

Calculate World Matrix, View Calculate World Matrix, View Matrix , and Projection MatrixMatrix , and Projection Matrix

• View MatrixView Matrix– D3DXMatrixLookAtLH(&V, &position, D3DXMatrixLookAtLH(&V, &position,

&target, &up);&target, &up);

Calculate World Matrix, View Calculate World Matrix, View Matrix , and Projection MatrixMatrix , and Projection Matrix

• Projection MatrixProjection MatrixD3DXMatrixPerspectiveFovLH();D3DXMatrixPerspectiveFovLH();

Demo 7-TexCube 8-TexCube

Render!!Render!!

• Prepare for renderPrepare for renderDevice->SetTransform(Device->SetTransform(D3DTS_VIEWD3DTS_VIEW, &V);, &V);

Device->SetTransform(Device->SetTransform(D3DTS_WORLDD3DTS_WORLD, &V);, &V);

Device->SetTransform(Device->SetTransform(D3DTS_PROJECTIOND3DTS_PROJECTION, &V);, &V);

• Start and Finish RenderStart and Finish RenderDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff

ffffff, 1.0f, 0);ffffff, 1.0f, 0);

Device->BeginScene();Device->BeginScene();

…………./* render render render !!!! */…../* render render render !!!! */….

Device->EndScene();Device->EndScene();

Render !!Render !!

if( mtrl )if( mtrl )device->SetMaterial(mtrl);device->SetMaterial(mtrl);

if( tex )if( tex )device->SetTexture(0, tex);device->SetTexture(0, tex);

device->SetStreamSource(0, _vb, 0, sizedevice->SetStreamSource(0, _vb, 0, sizeof(Vertex));of(Vertex));device->SetIndices(_ib);device->SetIndices(_ib);device->SetFVF(FVF_VERTEX);device->SetFVF(FVF_VERTEX);device->DrawIndexedPrimitive(device->DrawIndexedPrimitive(

D3DPT_TRIANGLELIST, D3DPT_TRIANGLELIST, 0, 0, 0, 0, 24,24,0,0,12); 12);

Set MaterialSet Texture

Set Index Buffer

Tell D3D your vertex formatJust Draw It!!

More SkillMore Skill

• Terrain – Height MapTerrain – Height Map• ShadowShadow• Other Algorithm…Other Algorithm…• Good website :Good website :

www.gamedev.netwww.gamedev.net

SamplesSamples

• AirplaneAirplane• Walking ManWalking Man• BasicHLSLBasicHLSL • ShadowVolumeShadowVolume

• TerrainTerrain

Other Features in DirectX Other Features in DirectX LibraryLibrary

• DirectPlayDirectPlay– Network GameNetwork Game

• DirectSoundDirectSound– Handle sound effectHandle sound effect