Game Programming - Sun

Transcription

Game Programming - Sun
Game Programming
Drawing in Direct3D
April 07, 2005
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Global Variables
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Setup ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Vertex / Index Buffers
„
A chunk of contiguous memory that contains
vertex data / index data
HRESULT
HRESULT IDirect3DDevice9::CreateVertexBuffer
IDirect3DDevice9::CreateVertexBuffer ((
UINT
UINT Length,
Length,
DWORD
DWORD Usage,
Usage,
DWORD
FVF,
DWORD FVF,
D3DPOOL
D3DPOOL Pool,
Pool,
IDirect3DVertexBuffer9**
IDirect3DVertexBuffer9** ppVertexBuffer,
ppVertexBuffer,
HANDLE*
pSharedHandle
HANDLE* pSharedHandle
);
);
HRESULT
HRESULT IDirect3DDevice9::CreateIndexBuffer
IDirect3DDevice9::CreateIndexBuffer ((
UINT
UINT Length,
Length,
DWORD
DWORD Usage,
Usage,
D3DFORMAT
D3DFORMAT Format,
Format,
D3DPOOL
D3DPOOL Pool,
Pool,
IDirect3DIndexBuffer9**
IDirect3DIndexBuffer9** ppIndexBuffer,
ppIndexBuffer,
HANDLE*
HANDLE* pSharedHandle
pSharedHandle
);
);
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Parameters for Creating
Buffers
„
„
Length – number of bytes
Usage – additional properties
„
D3DUSAGE_DYNAMIC
„ D3DUSAGE_POINTS
„ D3DUSAGE_SOFTWAREPROCESSING
„ D3DUSAGE_WRITEONLY
„
„
„
„
FVF – flexible vertex format
Pool – memory pool in which the buffer is placed
pShareHandle – Not used (set to zero)
Format – size of indices
„
D3DFMT_INDEX16
„ D3DFMT_INDEX32
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sun-Jeong Kim
Memory
Display
Video
Memory
„
Memory pool
„
„
„
„
„
AGP
Memory
System
Memory
Read & Write
D3DPOOL_DEFAULT
D3DPOOL_MANAGED
D3DPOOL_SYSTEMMEM
D3DPOOL_SCRATCH
Static vs. Dynamic buffers
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Example of Creating Buffers
„
Static vertex buffer for eight vertices
IDirect3DVertexBuffer9* vb;
_device->CreateVertexBuffer (
8 * sizeof( Vertex ),
0,
D3DFVF_XYZ,
D3DPOOL_MANAGED,
&vb,
0);
„
Dynamic index buffer for 36 16-bit indices
IDirect3DIndexBuffer9* ib;
_device->CreateIndexBuffer (
36 * sizeof( WORD ),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&ib,
0);
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sun-Jeong Kim
Accessing a Buffer’s Memory
„
Lock / Unlock method
„
obtain a pointer to the internal memory contents
(Figure 3.1)
HRESULT
HRESULT IDirect3DVertexBuffer9::Lock
IDirect3DVertexBuffer9::Lock ((
UINT
UINT OffsetToLock,
OffsetToLock,
UINT
UINT SizeToLock,
SizeToLock,
BYTE**
BYTE** ppbData,
ppbData,
DWORD
DWORD Flags
Flags
);
);
HRESULT
HRESULT IDirect3DIndexBuffer9::Lock
IDirect3DIndexBuffer9::Lock ((
UINT
UINT OffsetToLock,
OffsetToLock,
UINT
SizeToLock,
UINT SizeToLock,
BYTE**
BYTE** ppbData,
ppbData,
DWORD
DWORD Flags
Flags
);
);
※ Specifying zero for OffsetToLock and SizeToLock parameters
Æ Shortcut to lock the entire buffer
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Parameters for Accessing
Buffers
„
„
„
„
OffsetToLock – offset from the start of the buffer
SizeToLock – number of bytes to lock
ppbData – pointer to the start of the locked memory
Flags
„
D3DLOCK_DISCARD
„ D3DLOCK_NOOVERWRITE
„ D3DLOCK_READONLY
„
Example:
Vertex* vertices;
_vb->Lock(0, 0, (void**)&vertices, 0);
vertices[0] = Vertex(-1.0f, 0.0f, 2.0f);
vertices[1] = Vertex( 0.0f, 1.0f, 2.0f);
vertices[2] = Vertex( 1.0f, 0.0f, 2.0f);
_vb->Unlock();
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Filling the Vertex Buffer
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Filling the Index Buffer
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Setting the Rendering States
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Render States
„
How geometry is rendered
„
change them if your application requires something
other than the defaults
HRESULT
HRESULT IDirect3DDevice9::SetRenderState
IDirect3DDevice9::SetRenderState ((
D3DRENDERSTATETYPE
D3DRENDERSTATETYPE State,
State,
DWORD
DWORD Value
Value
);
);
„
Example
_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
MSDN
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Cleanup ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
EnterMsgLoop ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
WndProc ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Display ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Drawing Preparations
Set
Setthe
thestream
streamsource
source
Set
Setthe
thevertex
vertexformat
format
Set
Setthe
theindex
indexbuffer
buffer
_device->SetStreamSource (
0,
vb,
0,
sizeof(Vertex) );
_device->SetFVF (
D3DFVF_XYZ |
D3DFVF_DIFFUSE |
D3DFVF_TEX1 );
_device->SetIndices ( ib );
HRESULT
HRESULT IDirect3DDevice9::SetStreamSource
IDirect3DDevice9::SetStreamSource ((
UINT
UINT StreamNumber,
StreamNumber,
IDirect3dVertexBuffer9*
IDirect3dVertexBuffer9* pStreamData,
pStreamData,
UINT
UINT OffsetInBytes,
OffsetInBytes,
UINT
UINT Stride
Stride
);
);
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sun-Jeong Kim
Drawing with Vertex / Index
Buffers
„
IDirect3DDevice9::DrawPrimitive
HRESULT
HRESULT IDirect3DDevice9::DrawPrimitive
IDirect3DDevice9::DrawPrimitive ((
D3DPRIMITIVETYPE
D3DPRIMITIVETYPE PrimitiveType,
PrimitiveType,
UINT
StartVertex,
UINT StartVertex,
UINT
UINT PrimitiveCount
PrimitiveCount
);
);
„
IDirect3DDevice9::DrawIndexedPrimitive
HRESULT
HRESULT IDirect3DDevice9::DrawIndexedPrimitive
IDirect3DDevice9::DrawIndexedPrimitive ((
D3DPRIMITIVETYPE
D3DPRIMITIVETYPE Type,
Type,
INT
INT BaseVertexIndex,
BaseVertexIndex,
UINT
UINT MinIndex,
MinIndex,
UINT
NumVertices
UINT NumVertices
UINT
UINT StartVertex,
StartVertex,
UINT
UINT PrimitiveCount
PrimitiveCount
);
);
„
Begin / End Scene
Sun-Jeong Kim
_device->BeginSecne();
_device->DrawPrimitive(...);
_device->EndScene();
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Draw the Scene
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
MSDN
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sample: Triangle
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Setup ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Display ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sample: Teapot
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Setup ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Display ( )
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
D3DX Geometric Objects
„
Some method to generate the mesh data of
simple 3D objects (Figure 3.3)
„
„
„
„
„
„
D3DXCreateBox
D3DXCreateSphere
D3DXCreateCylinder
D3DXCreateTeapot
D3DXCreatePolygon
D3DXCreateTorus
ID3DXMesh *mesh = 0;
D3DXCreateTeapot( _device, &mesh, 0 );
_device->BeginSecne();
mesh->DrawSubset(0);
_device->EndScene();
mesh->Release();
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Sample: D3DXCreate
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Global Variables
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Creating the Objects
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
World Transformation
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Animating the Camera
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Draw the Scene
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University
Exercise
„
Change the program “D3DXCreate”
„
„
to rotate each object on the own local y-axis
to control animating the camera with arrow keys
Sun-Jeong Kim
http://www.hallym.ac.kr/~sunkim/teach/2005/gp1
Graphics Lab @ Korea University

Similar documents