当前位置:   article > 正文

Direct3D绘制旋转立方体例程_c++d3d旋转的正方体

c++d3d旋转的正方体

初始化文件见Direct3D的初始化_direct3dcreate9_寂寂寂寂寂蝶丶的博客-CSDN博客

D3DPractice.cpp

  1. #include <windows.h>
  2. #include "d3dUtility.h"
  3. #include <d3dx9math.h>
  4. IDirect3DDevice9* Device = NULL;
  5. IDirect3DVertexBuffer9* VB = NULL;
  6. IDirect3DIndexBuffer9* IB = NULL;
  7. const int Width = 1024;
  8. const int Height = 768;
  9. struct Vertex
  10. {
  11. Vertex(){}
  12. Vertex(float x, float y, float z)
  13. :_x(x), _y(y), _z(z)
  14. {
  15. }
  16. float _x, _y, _z;
  17. static const DWORD FVF;
  18. };
  19. const DWORD Vertex::FVF = D3DFVF_XYZ;
  20. #if 0
  21. Display方法有两项任务,更新场景和绘制场景,我们想让立方体旋转起来,必须在程序生成的每帧图像中给旋转角一定的增量,
  22. 从而指定立方体的旋转方式,通过更新每帧图像中立方体的角度,立方体在每帧图像中就被微微地旋转,从而产生转动的视觉效果
  23. 本例中我们是用世界变换来指定立方体的方向
  24. #endif
  25. bool DisPlay(float timeDelta)
  26. {
  27. if (Device)
  28. {
  29. D3DXMATRIX Rx, Ry;
  30. //x轴旋转矩阵
  31. D3DXMatrixRotationX(&Rx, 3.14f / 4.0f);
  32. static float y = 0.0f;
  33. //y轴旋转矩阵
  34. D3DXMatrixRotationY(&Ry, y);
  35. y += timeDelta;
  36. //重置
  37. if (y >= 6.28f)
  38. y = 0.0f;
  39. D3DXMATRIX p = Rx * Ry;
  40. Device->SetTransform(D3DTS_WORLD, &p);
  41. /*
  42. STDMETHOD(Clear)(THIS_ DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) PURE;
  43. Count: pRects数组中矩形的数目
  44. pRects:要执行清除操作的屏幕矩形数组,该参数允许我们只对表面的部分区域进行清除操作
  45. Flags:指定所要清除的表面
  46. D3DCLEAR_TARGET 绘制目标表面,通常指后台缓存
  47. D3DCLEAR_ZBUFFER 深度缓存
  48. D3DCLEAR_STENCIL 模板缓存
  49. Color:指定将绘制目标体设置为何种颜色
  50. Z:深度缓存所需要设定的值
  51. Stencil:模板缓存所需要设定的值
  52. */
  53. Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  54. if (SUCCEEDED(Device->BeginScene()))
  55. {
  56. //指定数据流输入源
  57. Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
  58. //设置索引缓存
  59. Device->SetIndices(IB);
  60. //设置顶点格式
  61. Device->SetFVF(Vertex::FVF);
  62. //通过索引缓存来绘制绘制
  63. Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
  64. //Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  65. Device->EndScene();
  66. }
  67. Device->Present(0, 0, 0, 0); //提交后台缓存
  68. }
  69. return true;
  70. }
  71. bool InitVertexBuffer()
  72. {
  73. Device->CreateVertexBuffer(8 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &VB, 0);
  74. Device->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &IB, 0);
  75. //顶点缓存
  76. Vertex* vertices;
  77. VB->Lock(0, 0, (void**)&vertices, 0);
  78. vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
  79. vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
  80. vertices[2] = Vertex(1.0f, 1.0f, -1.0f);
  81. vertices[3] = Vertex(1.0f, -1.0f, -1.0f);
  82. vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
  83. vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
  84. vertices[6] = Vertex(1.0f, 1.0f, 1.0f);
  85. vertices[7] = Vertex(1.0f, -1.0f, 1.0f);
  86. VB->Unlock();
  87. //索引缓存
  88. WORD* indices = 0;
  89. IB->Lock(0, 0, (void**)&indices, 0);
  90. //front side
  91. indices[0] = 0;indices[1] = 1;indices[2] = 2;
  92. indices[3] = 0;indices[4] = 2;indices[5] = 3;
  93. //back side
  94. indices[6] = 4;indices[7] = 6;indices[8] = 5;
  95. indices[9] = 4;indices[10] = 7;indices[11] = 6;
  96. //left side
  97. indices[12] = 4;indices[13] = 5;indices[14] = 1;
  98. indices[15] = 4;indices[16] = 1;indices[17] = 0;
  99. //right side
  100. indices[18] = 3;indices[19] = 2;indices[20] = 6;
  101. indices[21] = 3;indices[22] = 6;indices[23] = 7;
  102. //top
  103. indices[24] = 1;indices[25] = 5;indices[26] = 6;
  104. indices[27] = 1;indices[28] = 6;indices[29] = 2;
  105. //bottom
  106. indices[30] = 4;indices[31] = 0;indices[32] = 3;
  107. indices[33] = 4;indices[34] = 3;indices[35] = 7;
  108. IB->Unlock();
  109. //取景变换(观察者坐标系)
  110. D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
  111. D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
  112. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  113. D3DXMATRIX V;
  114. D3DXMatrixLookAtLH(&V, &position, &target, &up);
  115. Device->SetTransform(D3DTS_VIEW, &V);
  116. //投影变换
  117. D3DXMATRIX proj;
  118. D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, (float)Width / (float)Height, 1.0f, 1000.0f);
  119. Device->SetTransform(D3DTS_PROJECTION, &proj);
  120. Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
  121. return true;
  122. }
  123. #if 0
  124. 创建顶点缓存和索引缓存,对缓存进行锁定,将构成立方体的顶点数据以及构成立方体的三角形单元的索引数据分别写入顶点缓存和索引缓存。然后将摄像
  125. 机沿Z轴负方向平移几个单位,以使绘制在世界坐标系原点的立方体处于摄像机的视场内。然后再实施投影变换,最终,将填充模式的绘制状态设为线框模式
  126. #endif
  127. bool SetUp()
  128. {
  129. return InitVertexBuffer();
  130. }
  131. void CleanUp()
  132. {
  133. d3d::Release<IDirect3DVertexBuffer9*>(VB);
  134. d3d::Release<IDirect3DIndexBuffer9*>(IB);
  135. }
  136. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  137. {
  138. if (!d3d::InitD3D(hInstance, 800, 600, true, D3DDEVTYPE_HAL, &Device))
  139. {
  140. ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
  141. return 0;
  142. }
  143. if (!SetUp())
  144. {
  145. ::MessageBox(0, L"SetUp() - FAILED", 0, 0);
  146. return 0;
  147. }
  148. d3d::EnterMsgLoop(DisPlay);
  149. CleanUp();
  150. Device->Release();
  151. return 0;
  152. }

运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/883837
推荐阅读
相关标签
  

闽ICP备14008679号