28
Lecture 7: Direct3D Basics (II) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology

Lecture 7: Direct3D Basics (II)

  • Upload
    kimama

  • View
    65

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 7: Direct3D Basics (II). Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology. Projection Transform. World Transform. View Transform. Direct3D Transformation. Triangle List (Vertex Pool). Transformed vertices. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 7: Direct3D Basics (II)

Lecture 7: Direct3D Basics (II)

Prof. Hsien-Hsin Sean LeeSchool of Electrical and Computer

EngineeringGeorgia Institute of Technology

Page 2: Lecture 7: Direct3D Basics (II)

2

Direct3D Transformation

Projection Transform

World Transform

View Transform

Triangle List(Vertex Pool)

Transformed vertices

Page 3: Lecture 7: Direct3D Basics (II)

3

D3D World Transformation Matrices• Translation

D3DXMatrixTranslation(D3DXMATRIX *Out, x, y, z);

• ScalingD3DXMatrixScaling(D3DXMATRIX *Out, sx, sy, sz);

• RotationD3DXMatrixRotationX(D3DXMATRIX *Out, float angle);

D3DXMatrixRotationY(D3DXMATRIX *Out, float angle);

D3DXMatrixRotationZ(D3DXMATRIX *Out, float angle);

D3DXMatrixRotationAxis(D3DXMATRIX *Out, D3DXVECTOR3 *v, float angle);

• SetupIDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix);

D3DTS_WORLD

Page 4: Lecture 7: Direct3D Basics (II)

4

D3D World Transformation

gd3dDevice->BeginScene();. . . .

D3DXMatrixTranslation(&matXlat, 2.0f, 4.0f, 1.0f+inc);D3DXMatrixRotationX(&matRX, D3DXToRadian(inc_theta));D3DXMatrixRotationY(&matRY, D3DXToRadian(inc_theta*1.5));D3DXMatrixRotationZ(&matRZ, D3DXToRadian(inc_theta*2.2));D3DXMatrixScaling(&matScale, 2.0f, 2.0f, 2.0f); gd3dDevice->SetTransform(

D3DTS_WORLD, &(matXlat * matRZ * matRY * matRX * matScale));. . . .

gd3dDevice->DrawPrimitve(…);gd3dDevice->EndScene();gd3dDevice->Present(0, 0, 0, 0);

• All world transformation matrices can be simply combined

Page 5: Lecture 7: Direct3D Basics (II)

5

D3D View Transformation• Build a Left-Handed, Look-at Matrix

D3DXMatrixLookAtLH(

D3DXMATRIX *Out,

D3DXVECTOR3 *Eye,

D3DXVECTOR3 *pAt,

D3DXVECTOR3 *pUp);

• Setup

x

z

y

Eye(4, -1, -3)

Usually (0, 1, 0) the world is upward

pAt(-3, 3, 1)

IDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix);

D3DTS_VIEW

Page 6: Lecture 7: Direct3D Basics (II)

6

D3D View Transformation

gd3dDevice->BeginScene();. . . .

D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), // camera loc&D3DXVECTOR3(0.0f, 0.0f, 0.0f), // look-at target &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

gd3dDevice->SetTransform(D3DTS_VIEW, &matView); . . . .

gd3dDevice->DrawPrimitve(…);gd3dDevice->EndScene();gd3dDevice->Present(0, 0, 0, 0);

Page 7: Lecture 7: Direct3D Basics (II)

7

D3D Projection Transformation• Build a Left-Handed, Look-at Matrix

D3DXMatrixPerspectiveFovLH(

D3DXMATRIX *Out,

FLOAT fovy,

FLOAT Aspect,

FLOAT near,

FLOAT far);

• SetupIDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix);

D3DTS_PROJECTION

Eye

fovy/2

far

near

Width

Height

Aspect = width/height

Page 8: Lecture 7: Direct3D Basics (II)

8

D3D World Transformation gd3dDevice->BeginScene();

. . . . gd3dDevice->SetTransform(D3DTS_PROJECTION, &matProjection);

. . . . gd3dDevice->DrawPrimitve(…); gd3dDevice->EndScene(); gd3dDevice->Present(0, 0, 0, 0);}

buildProjMatrix(){ // Every time Window is resized, aspect ratio changed float w = (float)md3dPP.BackBufferWidth; float h = (float)md3dPP.BackBufferHeight; D3DXPerspectiveProjectionFovLH(

&matProjection, D3DXToRadian(45),w/h,1.0f,1000.0f);

}

Page 9: Lecture 7: Direct3D Basics (II)

9

Example 1:Transformation

FirstTransformDecl(See Demo in Visual Studio)

Page 10: Lecture 7: Direct3D Basics (II)

10

Example 2: First 3D Object Transformation

FirstXformIdxTetrahedron(See Demo in Visual Studio)

Page 11: Lecture 7: Direct3D Basics (II)

11

Texture Mapping/* Declaration of an object class */LPDIRECT3DTEXTURE9 texture_brick;

. . . . . . /* buildVertexBuffer() */D3DXCreateTextureFromFile(gd3dDevice,

"C:\\Tech\\Texture Maps\\brick.jpg",&texture_brick);

v[8]= VertexPCT(3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 8, 0);v[9]= VertexPCT(3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 8, 8);v[10]=VertexPCT(3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 0, 8);v[11]=VertexPCT(3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 0, 0);

. . . . . . /* drawScene() */

gd3dDevice->BeginScene();gd3dDevice->SetTexture(0, texture_brick);

gd3dDevice->DrawPrimitive(… );gd3dDevice->EndScene();

(0,0)

(0,1)

(1,0)

(1,1)

IDirect3DDevice9::SetTexture( DWORD Stage, IDirect3DBaseTexture9  *pTexture);

Stage ID (0 to 7)

Page 12: Lecture 7: Direct3D Basics (II)

12

Example 3: 3D Object Texturing

FirstTextureSee Demo in Visual Studio)

Page 13: Lecture 7: Direct3D Basics (II)

13

Adding Control for Object Movement

• These functions simply change – “Delay” in the delay_loop()– “Translation matrix” of an

object under control • E.g., z coordinate for

zooming

/* MainWndProc() */case WM_KEYDOWN: if (wParam == 'S')

slow(); else if( wParam == 'A')

fast(); if (wParam == VK_UP)

zoom_out(); else if (wParam == VK_DOWN)

zoom_in(); if (wParam == VK_LEFT)

move_left(); else if (wParam == VK_RIGHT)

move_right();

Page 14: Lecture 7: Direct3D Basics (II)

14

Example 4: Textured Object Movement

FirstTextureWithZSee Demo in Visual Studio)

Page 15: Lecture 7: Direct3D Basics (II)

15

Rotate Camera (Looking Around)

/* draw_scene() */float lookatX, lookatZ;

// rotate the camera on the XZ planelookatX = radius * sin(move);lookatZ = radius * cos(move);

D3DXMatrixLookAtLH(&matView,&D3DXVECTOR3(0.0f, 30.0f, 0.0f), // camera position&D3DXVECTOR3(lookatX, 16.0f+up, lookatZ), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

gd3dDevice->SetTransform(D3DTS_VIEW, &matView);

move

+x

+zTop view

Page 16: Lecture 7: Direct3D Basics (II)

16

Example 5:Moving Camera Around

MovingCameraSee Demo in Visual Studio)

Page 17: Lecture 7: Direct3D Basics (II)

17

D3DLIGHT9

typedef struct D3DLIGHT9 { D3DLIGHTTYPE Type; D3DCOLORVALUE Diffuse; D3DCOLORVALUE Specular; D3DCOLORVALUE Ambient; D3DVECTOR Position; D3DVECTOR Direction; float Range; float Falloff; float Attenuation0; float Attenuation1; float Attenuation2; float Theta; float Phi;

} D3DLIGHT9, *LPD3DLIGHT;

/* SetLightSource */

D3DVECTOR vecPos =

{90.0f, 0.0f, 100.0f};

light[0].Type = D3DLIGHT_POINT;

light[0].Diffuse.r = 0.0f;

light[0].Diffuse.g = 0.0f;

light[0].Diffuse.b = 1.0f;

light[0].Diffuse.a = 1.0f;

light[0].Position = vecPos;

light[0].Attenuation0 = 1.0f;

light[0].Attenuation1 = 0.0f;

light[0].Attenuation2 = 0.0f;

light[0].Range = 300.0f;

. . . . .

gd3dDevice->SetLight(0, &(light[0]))

POINT, DIRECTIONAL,

SPOTLIGHT

Ignored by Directional Light

Ignored by Point Light

Page 18: Lecture 7: Direct3D Basics (II)

18

Set Material Properties

/* SetLightSource */

material.Diffuse.r = 1.0f;

material.Diffuse.g = 1.0f;

material.Diffuse.b = 1.0f;

material.Ambient.r = 0.3f;

material.Ambient.g = 0.3f;

material.Ambient.b = 0.3f;

material.Diffuse.a = 1.0f;

material.Ambient.a = 1.0f;

gd3dDevice->SetMaterial(&material)); // this is globally used

Page 19: Lecture 7: Direct3D Basics (II)

19

Enable Lighting /* SetRenderState */

gd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

gd3dDevice->SetRenderState(D3DRS_AMBIENT,

D3DCOLOR_XRGB(200, 200, 200)); // Set light ambient

/* DrawScene() */

gd3dDevice->LightEnable(0, lightswitch[0]);

gd3dDevice->LightEnable(1, lightswitch[1]);

gd3dDevice->LightEnable(2, lightswitch[2]);

gd3dDevice->LightEnable(3, lightswitch[3]);

Page 20: Lecture 7: Direct3D Basics (II)

20

Example 6:Ambient and Diffuse Light with Various Light Sources

FirstAmbDiffLightingSee Demo in Visual Studio)

Page 21: Lecture 7: Direct3D Basics (II)

21

D3D Mesh Objects• Pre-constructed Objects

/* buildVertexBuffer() */

LPD3DXMESH meshSphere;   

D3DXCreateSphere(gd3dDevice,

10.0f, // radius

100, // num of slices about axis

100, // num of stacks along axis

&meshSphere, NULL);

/* DrawScene() */

. . . . .

meshSphere->DrawSubset(0);   

Page 22: Lecture 7: Direct3D Basics (II)

22

Example 7:Spot Light Source (Diffuse)

FirstSpotLightSee Demo in Visual Studio)

Page 23: Lecture 7: Direct3D Basics (II)

23

Adding Specular Effectgd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

/* Set Light Source */light[4].Specular.r = 1.0f;light[4].Specular.g = 1.0f;light[4].Specular.b = 1.0f;light[4].Specular.a = 1.0f;gd3dDevice->SetLight(4, &(light[4]);. . . gd3dDevice->LightEnable(4, TRUE);

/* Set Material */material.Specular.r = 0.8f;material.Specular.g = 0.8f;material.Specular.b = 0.8f;material.Power = 20; . . . . . gd3dDevice->SetMaterial(&material);

Page 24: Lecture 7: Direct3D Basics (II)

24

Example 8:Spot Light Source

Specular Lighting Effect

FirstSpeuclarLightSee Demo in Visual Studio)

Page 25: Lecture 7: Direct3D Basics (II)

25

Color (Alpha) Blending/* SetRenderState() */

gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);       

gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);

gd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);       

gd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);   

/* Set New Material for the Object to be transparent */

material.Diffuse.a = 0.85f;

material.Ambient.a = 0.85f;

material.Specular.a = 0.9f;

Page 26: Lecture 7: Direct3D Basics (II)

26

Example 9:Alpha Blending

Translucency Effect

FirstBlendingSee Demo in Visual Studio)

Page 27: Lecture 7: Direct3D Basics (II)

27

Multi-Texturing /* DrawScene() */

gd3dDevice->SetTexture(0, texture_monalisa);

gd3dDevice->SetTexture(1, texture_lightmap);

gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

gd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA);

gd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);

gd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);

gd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

gd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);

gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 60, 54, 2);

gd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);

gd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);

RGBA=Arg1*Arg2

Page 28: Lecture 7: Direct3D Basics (II)

28

Example 10:Multi-Texturing

Light Map Effect

MultiTexturingSee Demo in Visual Studio)