|
Posted on
2011-02-22 17:25
+石头+
阅读( 643)
评论()
收藏
举报
- 创建顶点缓存和索引缓存
- 访问缓存内容
- 设置绘制状态
- 绘制的准备工作(1)指定数据流输入源。(2)设置顶点格式。(3)设置索引缓存
- 使用顶点缓存和索引缓存绘制所有的绘制方法要在IDirect3DDevice9::BeginScene()和IDirect3DDevice9::EndScene()之间绘制。
- const
float
PI = 3.1415926;
- IDirect3DDevice9* Device = 0;
- const
int
Width = 640;
- const
int
Height = 480;
-
- IDirect3DVertexBuffer9* VB = 0;
- IDirect3DIndexBuffer9* IB = 0;
-
- //Class and Struct
- struct
Vertex
- {
- Vertex(){}
- Vertex(float
x, float
y, float
z)
- {
- _x = x;
- _y = y;
- _z = z;
- }
- float
_x, _y, _z;
- static
const
DWORD
FVF;
- };
- const
DWORD
Vertex::FVF = D3DFVF_XYZ;
-
- //Framework Function
- bool
SetUp()
- {
- //create vertex and index buffer
- Device->CreateVertexBuffer(
- 8*sizeof(Vertex),
- D3DUSAGE_WRITEONLY,
- Vertex::FVF,
- D3DPOOL_MANAGED,
- &VB,
- 0);
- Device->CreateIndexBuffer(
- 36*sizeof(WORD),
- D3DUSAGE_WRITEONLY,
- D3DFMT_INDEX16,
- D3DPOOL_MANAGED,
- &IB,
- 0);
- //fill the buffer with the cube data
- Vertex* vertices;
- VB->Lock(0, 0, (void**)&vertices, 0);
-
- // vertices of a unit cube
- vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
- vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
- vertices[2] = Vertex( 1.0f, 1.0f, -1.0f);
- vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);
- vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
- vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
- vertices[6] = Vertex( 1.0f, 1.0f, 1.0f);
- vertices[7] = Vertex( 1.0f, -1.0f, 1.0f);
-
- VB->Unlock();
-
- // define the triangles of the cube:
- WORD* indices = 0;
- IB->Lock(0, 0, (void**)&indices, 0);
-
- // front side
- indices[0] = 0; indices[1] = 1; indices[2] = 2;
- indices[3] = 0; indices[4] = 2; indices[5] = 3;
-
- // back side
- indices[6] = 4; indices[7] = 6; indices[8] = 5;
- indices[9] = 4; indices[10] = 7; indices[11] = 6;
-
- // left side
- indices[12] = 4; indices[13] = 5; indices[14] = 1;
- indices[15] = 4; indices[16] = 1; indices[17] = 0;
-
- // right side
- indices[18] = 3; indices[19] = 2; indices[20] = 6;
- indices[21] = 3; indices[22] = 6; indices[23] = 7;
-
- // top
- indices[24] = 1; indices[25] = 5; indices[26] = 6;
- indices[27] = 1; indices[28] = 6; indices[29] = 2;
-
- // bottom
- indices[30] = 4; indices[31] = 0; indices[32] = 3;
- indices[33] = 4; indices[34] = 3; indices[35] = 7;
-
- IB->Unlock();
-
- D3DXVECTOR3
position(0.0f, 0.0f, -5.0f);
- D3DXVECTOR3
target(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3
up(0.0f, 1.0f, 0.0f);
- D3DXMATRIX
V;
- D3DXMatrixLookAtLH(&V, &position, &target, &up);
-
- Device->SetTransform(D3DTS_VIEW, &V);
- //set projection matrix
-
- D3DXMATRIX
proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI*0.5f,
- (float)Width/(float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- //Swtich to wirefarm mode
-
- Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
- return
true;
- }
- void
CleanUp()
- {
- d3d::Release<IDirect3DVertexBuffer9*>(VB);
- d3d::Release<IDirect3DIndexBuffer9*>(IB);
- }
- bool
Display(float
timeDetla)
- {
- if(Device)
- {
- D3DXMATRIX
Rx,Ry;
- D3DXMatrixRotationX(&Rx, 3.14/4.0f);
- static
float
y = 0.0f;
- D3DXMatrixRotationY(&Ry, y);
- y+=timeDetla;
-
- if(y>=6.28)
- {
- y = 0.0f;
- }
- D3DXMATRIX
p = Rx * Ry;
- //把旋转后的矩阵转换到世界坐标系中
- Device->SetTransform(D3DTS_WORLD, &p);
- //绘制scence
- Device->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
- Device->BeginScene();
-
- Device->SetStreamSource(0, VB, 0, sizeof(Vertex)); //指定数据流输入源
- Device->SetIndices(IB); //设置索引缓存
- Device->SetFVF(Vertex::FVF); //指定定点格式
-
-
- //绘制正方体
- Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
- Device->EndScene();
- Device->Present(0,0,0,0);
- }
- return
true;
- }
|