Cuboid.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:5k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. // Cuboid.cpp: implementation of the CCuboid class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Cuboid.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CCuboid::CCuboid(LPDIRECT3DDEVICE8 pD3DDevice)
  9. {
  10. m_pD3DDevice = pD3DDevice;
  11. m_pVertexBuffer = NULL;
  12. //Set a default size and position
  13. m_rWidth = 10.0;
  14. m_rHeight = 10.0;
  15. m_rDepth = 10.0;
  16. m_rX = 0.0;
  17. m_rY = 0.0;
  18. m_rZ = 0.0;
  19. //Initialize Vertex Buffer
  20.     if(SUCCEEDED(CreateVertexBuffer()))
  21. {
  22. UpdateVertices();
  23. }
  24. }
  25. CCuboid::~CCuboid()
  26. {
  27. SafeRelease(m_pVertexBuffer);
  28. }
  29. bool CCuboid::Render()
  30. {
  31. m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(CUBIOD_CUSTOMVERTEX));
  32.     m_pD3DDevice->SetVertexShader(CUBIOD_D3DFVF_CUSTOMVERTEX);
  33.     m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top
  34. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides
  35. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom
  36. return true;
  37. }
  38. HRESULT CCuboid::CreateVertexBuffer()
  39. {
  40.     //Create the vertex buffer from our device.
  41.     if(FAILED(m_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUBIOD_CUSTOMVERTEX),
  42.                                                0, CUBIOD_D3DFVF_CUSTOMVERTEX,
  43.                                                D3DPOOL_DEFAULT, &m_pVertexBuffer)))
  44.     {
  45.         return E_FAIL;
  46.     }
  47.     return S_OK;
  48. }
  49. bool CCuboid::UpdateVertices()
  50. {
  51. VOID* pVertices;
  52.     
  53.     //Store each point of the cube together with it's colour
  54. //Make sure that the points of a polygon are specified in a clockwise direction,
  55. //this is because anti-clockwise faces will be culled
  56. //We will use a three triangle strips to render these polygons (Top, Sides, Bottom).
  57.     CUBIOD_CUSTOMVERTEX cvVertices[] =
  58.     {
  59. //Top Face
  60. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 - Blue 
  61.         {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red 
  62.         {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 - Red 
  63. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 - Green 
  64. //Face 1
  65. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 - Red 
  66.         {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 - Blue 
  67.         {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 - Green 
  68. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 - Red 
  69. //Face 2
  70. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 - Blue 
  71.         {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 - Green
  72. //Face 3
  73.         {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 - Green 
  74. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 - Red 
  75. //Face 4
  76.         {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 - Red 
  77.         {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 - Blue
  78. //Bottom Face
  79. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 - Green 
  80.         {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 - Blue 
  81.         {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 - Red 
  82. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 - Green
  83.     };
  84. //Get a pointer to the vertex buffer vertices and lock the vertex buffer
  85.     if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
  86.     {
  87.         return false;
  88.     }
  89.     //Copy our stored vertices values into the vertex buffer
  90.     memcpy(pVertices, cvVertices, sizeof(cvVertices));
  91.     //Unlock the vertex buffer
  92.     m_pVertexBuffer->Unlock();
  93. return true;
  94. }
  95. bool CCuboid::SetSize(float rWidth, float rHeight, float rDepth)
  96. {
  97. m_rWidth = rWidth;
  98. m_rHeight = rHeight;
  99. m_rDepth = rDepth;
  100. UpdateVertices();
  101. return true;
  102. }
  103. bool CCuboid::SetPosition(float x, float y, float z)
  104. {
  105. m_rX = x;
  106. m_rY = y;
  107. m_rZ = z;
  108. UpdateVertices();
  109. return true;
  110. }