beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

23
Beginning Direct3D Game Programming: 5. Basics [email protected] Division of Digital Contents, DongSeo University. April 2016

Upload: jintaek-seo

Post on 13-Feb-2017

34 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Beginning Direct3D Game Programming:5. Basics

[email protected] of Digital Contents, DongSeo University.

April 2016

Page 2: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Compiling the Tutorial

C:\DxSdk_200806\Samples\C++\Direct3D\Tutorials\Tut01_CreateDevice\– CreateDevice_2008.sln

2

Page 3: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Fix error : identifier '__RPC__out_xcount_part'

error C2061: syntax error : identifier '__RPC__out_xcount_part'

3

Page 4: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Open Property Manager Open property manager for Microsoft.Cpp.Win32.user

4

Page 5: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Include Directory C:\DxSdk_200806\Include; must be located at last of in-

clude directory string.– $(VC_IncludePath);$(WindowsSDK_IncludePath);C:\

DxSdk_200806\Include;

5

Page 6: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Library Directory C:\DxSdk_200806\Lib\x86; must be located at last of li-

brary directory string.– $(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);C:\

DxSdk_200806\Lib\x86;

6

Page 7: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Tutorial 02: Rendering Vertices

The Vertices sample project creates the simplest shape, a triangle, and renders it to the display.– Step 1 - Defining a Custom Vertex Type – Step 2 - Setting Up the Vertex Buffer – Step 3 - Rendering the Display

7

Page 8: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Step 1 – Defining a Custom Vertex Type The Vertices sample project renders a 2D triangle by us-

ing three vertices.– struct CUSTOMVERTEX– {– FLOAT x, y, z, rhw; // The transformed position for the vertex.– DWORD color; // The vertex color.– };

The next step is to define the FVF that describes the contents of the vertices in the vertex buffer.– #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|

D3DFVF_DIFFUSE)

8

Page 9: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Step 2 – Setting Up the Vertex Buffer The following code fragment initializes the values for

three custom vertices.– CUSTOMVERTEX vertices[] =– {– { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color– { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },– { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },– };

The next step is to call IDirect3DDevice9::CreateVertexBuffer to create a vertex buffer as shown in the following code fragment.– if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),– 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB,

NULL ) ) )– return E_FAIL;

9

Page 10: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

After creating a vertex buffer, it is filled with data from the custom vertices.– VOID* pVertices;– if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )– return E_FAIL;

– memcpy( pVertices, vertices, sizeof(vertices) );

– g_pVB->Unlock();

10

Page 11: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Step 3 – Rendering the Display Rendering the display starts by clearing the back buffer

to a blue color and then calling BeginScene.– g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,

D3DCOLOR_XRGB(0,0,255), 1.0f, 0L );– g_pd3dDevice->BeginScene();

You need to set the stream source; in this case, use stream 0.– g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );

The next step is to call IDirect3DDevice9::SetFVF to identify the fixed function vertex shader.– g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

The next step is to use IDirect3DDevice9::DrawPrimitive to render the vertices in the vertex buffer.– g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );

11

Page 12: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

12

The last steps are to end the scene and then present the back buffer to the front buffer.– g_pd3dDevice->EndScene();– g_pd3dDevice->Present( NULL, NULL, NULL, NULL );

Page 13: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Primitives in Direct3D 9 A 3D primitive is a collection of vertices that form a sin-

gle 3D entity.– The simplest primitive is a collection of points in a 3D coordi-

nate system, which is called a point list. Direct3D devices can create and manipulate the follow-

ing types of primitives.– Point Lists (Direct3D 9) – Line Lists (Direct3D 9) – Line Strips (Direct3D 9) – Triangle Lists (Direct3D 9) – Triangle Strips (Direct3D 9) – Triangle Fans (Direct3D 9)

13

Page 14: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Point Listsstruct CUSTOMVERTEX{

float x,y,z;};

CUSTOMVERTEX Vertices[] = {

{-5.0, -5.0, 0.0},{ 0.0, 5.0, 0.0},{ 5.0, -5.0, 0.0},{10.0, 5.0, 0.0},{15.0, -5.0, 0.0},{20.0, 5.0, 0.0}

};

d3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, 3 );

14

Page 15: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Line ListsCUSTOMVERTEX Vertices[] = {

{-5.0, -5.0, 0.0},{ 0.0, 5.0, 0.0},{ 5.0, -5.0, 0.0},{10.0, 5.0, 0.0},{15.0, -5.0, 0.0},{20.0, 5.0, 0.0}

}; The code example below shows how to use IDirect3D-

Device9::DrawPrimitive to render this line list.//// It is assumed that d3dDevice is a valid// pointer to a IDirect3DDevice9 interface.//d3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 3 );

15

Page 16: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Line StripsCUSTOMVERTEX Vertices[] = {

{-5.0, -5.0, 0.0},{ 0.0, 5.0, 0.0},{ 5.0, -5.0, 0.0},{10.0, 5.0, 0.0},{15.0, -5.0, 0.0},{20.0, 5.0, 0.0}

};

The code example below shows how to use IDirect3D-Device9::DrawPrimitive to render this line strip.

d3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, 5 );

16

Page 17: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Triangle ListsCUSTOMVERTEX Vertices[] = {

{-5.0, -5.0, 0.0},{ 0.0, 5.0, 0.0},{ 5.0, -5.0, 0.0},{10.0, 5.0, 0.0},{15.0, -5.0, 0.0},{20.0, 5.0, 0.0}

}; The code example below shows how to use IDirect3D-

Device9::DrawPrimitive to render this triangle list.//// It is assumed that d3dDevice is a valid// pointer to a IDirect3DDevice9 interface.//d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

17

Page 18: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Triangle Strips A triangle strip is a series of connected triangles.

– Because the triangles are connected, the application does not need to repeatedly specify all three vertices for each triangle.

The system uses vertices v1, v2, and v3 to draw the first triangle; v2, v4, and v3 to draw the second triangle.

18

Page 19: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

CUSTOMVERTEX Vertices[] = {

{-5.0, -5.0, 0.0},{ 0.0, 5.0, 0.0},{ 5.0, -5.0, 0.0},{10.0, 5.0, 0.0},{15.0, -5.0, 0.0},{20.0, 5.0, 0.0}

};

The code example below shows how to use IDirect3D-Device9::DrawPrimitive to render this triangle strip.

d3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 4);

19

Page 20: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

Triangle Fans A triangle fan is similar to a triangle strip, except that all

the triangles share one vertex.

20

Page 21: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

CUSTOMVERTEX Vertices[] = {

{ 0.0, 0.0, 0.0},{-5.0, 5.0, 0.0},{-3.0, 7.0, 0.0},{ 0.0, 10.0, 0.0},{ 3.0, 7.0, 0.0},{ 5.0, 5.0, 0.0},

};

The code example below shows how to use IDirect3D-Device9::DrawPrimitive to render this triangle fan.

d3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 4 );

21

Page 22: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks

References

22

Page 23: Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks