博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Direct3D中的绘制—例:绘制正方体

Posted on 2011-02-22 17:25  +石头+  阅读(643)  评论(0)    收藏  举报
  1. 创建顶点缓存和索引缓存
  2. 访问缓存内容
  3. 设置绘制状态
  4. 绘制的准备工作(1)指定数据流输入源。(2)设置顶点格式。(3)设置索引缓存
  5. 使用顶点缓存和索引缓存绘制所有的绘制方法要在IDirect3DDevice9::BeginScene()和IDirect3DDevice9::EndScene()之间绘制。
  6. const float PI = 3.1415926;
  7. IDirect3DDevice9* Device = 0;
  8. const int Width = 640;
  9. const int Height = 480;
  10. IDirect3DVertexBuffer9* VB = 0;
  11. IDirect3DIndexBuffer9* IB = 0;
  12. //Class and Struct
  13. struct Vertex
  14. {
  15.     Vertex(){}
  16.     Vertex(float x, float y, float z)
  17.     {
  18.         _x = x;
  19.         _y = y;
  20.         _z = z;
  21.     }
  22.     float _x, _y, _z;
  23.     static const DWORD FVF;
  24. };
  25. const DWORD Vertex::FVF = D3DFVF_XYZ;
  26. //Framework Function
  27. bool SetUp()
  28. {
  29.     //create vertex and index buffer
  30.     Device->CreateVertexBuffer(
  31.         8*sizeof(Vertex),
  32.         D3DUSAGE_WRITEONLY,
  33.         Vertex::FVF,
  34.         D3DPOOL_MANAGED,
  35.         &VB,
  36.         0);
  37.     Device->CreateIndexBuffer(
  38.         36*sizeof(WORD),
  39.         D3DUSAGE_WRITEONLY,
  40.         D3DFMT_INDEX16,
  41.         D3DPOOL_MANAGED,
  42.         &IB,
  43.         0);
  44.     //fill the buffer with the cube data
  45.     Vertex* vertices;
  46.     VB->Lock(0, 0, (void**)&vertices, 0);
  47.     // vertices of a unit cube
  48.     vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
  49.     vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
  50.     vertices[2] = Vertex( 1.0f, 1.0f, -1.0f);
  51.     vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);
  52.     vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
  53.     vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
  54.     vertices[6] = Vertex( 1.0f, 1.0f, 1.0f);
  55.     vertices[7] = Vertex( 1.0f, -1.0f, 1.0f);
  56.     VB->Unlock();
  57.     // define the triangles of the cube:
  58.     WORD* indices = 0;
  59.     IB->Lock(0, 0, (void**)&indices, 0);
  60.     // front side
  61.     indices[0] = 0; indices[1] = 1; indices[2] = 2;
  62.     indices[3] = 0; indices[4] = 2; indices[5] = 3;
  63.     // back side
  64.     indices[6] = 4; indices[7] = 6; indices[8] = 5;
  65.     indices[9] = 4; indices[10] = 7; indices[11] = 6;
  66.     // left side
  67.     indices[12] = 4; indices[13] = 5; indices[14] = 1;
  68.     indices[15] = 4; indices[16] = 1; indices[17] = 0;
  69.     // right side
  70.     indices[18] = 3; indices[19] = 2; indices[20] = 6;
  71.     indices[21] = 3; indices[22] = 6; indices[23] = 7;
  72.     // top
  73.     indices[24] = 1; indices[25] = 5; indices[26] = 6;
  74.     indices[27] = 1; indices[28] = 6; indices[29] = 2;
  75.     // bottom
  76.     indices[30] = 4; indices[31] = 0; indices[32] = 3;
  77.     indices[33] = 4; indices[34] = 3; indices[35] = 7;
  78.     IB->Unlock();
  79.     D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
  80.     D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
  81.     D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  82.     D3DXMATRIX V;
  83.     D3DXMatrixLookAtLH(&V, &position, &target, &up);
  84.     Device->SetTransform(D3DTS_VIEW, &V);
  85.     //set projection matrix
  86.     
  87.     D3DXMATRIX proj;
  88.     D3DXMatrixPerspectiveFovLH(
  89.         &proj,
  90.         D3DX_PI*0.5f,
  91.         (float)Width/(float)Height,
  92.         1.0f,
  93.         1000.0f);
  94.     Device->SetTransform(D3DTS_PROJECTION, &proj);
  95.     //Swtich to wirefarm mode
  96.     Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
  97.     return true;
  98. }
  99. void CleanUp()
  100. {
  101.     d3d::Release<IDirect3DVertexBuffer9*>(VB);
  102.     d3d::Release<IDirect3DIndexBuffer9*>(IB);
  103. }
  104. bool Display(float timeDetla)
  105. {
  106.     if(Device)
  107.     {
  108.         D3DXMATRIX Rx,Ry;
  109.         D3DXMatrixRotationX(&Rx, 3.14/4.0f);
  110.         static float y = 0.0f;
  111.         D3DXMatrixRotationY(&Ry, y);
  112.         y+=timeDetla;
  113.         if(y>=6.28)
  114.         {
  115.             y = 0.0f;
  116.         }
  117.         D3DXMATRIX p = Rx * Ry;
  118.         //把旋转后的矩阵转换到世界坐标系中
  119.         Device->SetTransform(D3DTS_WORLD, &p);
  120.         //绘制scence
  121.         Device->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  122.         Device->BeginScene();
  123.         Device->SetStreamSource(0, VB, 0, sizeof(Vertex));            //指定数据流输入源
  124.         Device->SetIndices(IB);                                        //设置索引缓存
  125.         Device->SetFVF(Vertex::FVF);                                //指定定点格式
  126.         
  127.         //绘制正方体
  128.         Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
  129.         Device->EndScene();
  130.         Device->Present(0,0,0,0);
  131.     }
  132.     return true;
  133. }