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

其他书籍

开发平台:

HTML/CSS

  1. // Cuboid.cpp: implementation of the CCuboid class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Cuboid.h"
  5. #include <stdio.h>
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CCuboid::CCuboid(LPDIRECT3DDEVICE8 pD3DDevice)
  10. {
  11. m_pD3DDevice = pD3DDevice;
  12. m_pVertexBuffer = NULL;
  13. m_pTexture = NULL;
  14. //Set a default size and position
  15. m_rWidth = 10.0;
  16. m_rHeight = 10.0;
  17. m_rDepth = 10.0;
  18. m_rX = 0.0;
  19. m_rY = 0.0;
  20. m_rZ = 0.0;
  21. //Set material default values (R, G, B, A)
  22. D3DCOLORVALUE rgbaDiffuse  = {1.0, 1.0, 1.0, 0.0,};
  23. D3DCOLORVALUE rgbaAmbient  = {1.0, 1.0, 1.0, 0.0,};
  24. D3DCOLORVALUE rgbaSpecular = {0.0, 0.0, 0.0, 0.0,};
  25. D3DCOLORVALUE rgbaEmissive = {0.0, 0.0, 0.0, 0.0,};
  26. SetMaterial(rgbaDiffuse, rgbaAmbient, rgbaSpecular, rgbaEmissive, 0);
  27. //Initialize Vertex Buffer
  28.     if(SUCCEEDED(CreateVertexBuffer()))
  29. {
  30. UpdateVertices();
  31. }
  32. }
  33. CCuboid::~CCuboid()
  34. {
  35. SafeRelease(m_pTexture);
  36. SafeRelease(m_pVertexBuffer);
  37. }
  38. bool CCuboid::Render()
  39. {
  40. m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(CUBOID_CUSTOMVERTEX));
  41. m_pD3DDevice->SetVertexShader(CUBOID_D3DFVF_CUSTOMVERTEX);
  42. if(m_pTexture != NULL)
  43. {
  44. //A texture has been set. We want our texture to be shaded based
  45. //on the current light levels, so used D3DTOP_MODULATE.
  46. m_pD3DDevice->SetTexture(0, m_pTexture);
  47. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  48. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  49. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
  50. }
  51. else
  52. {
  53. //No texture has been set
  54. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  55. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  56. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
  57. }
  58. if(FAILED(m_pD3DDevice->SetMaterial(&m_matMaterial)))
  59. {
  60. //Set material failed
  61. OutputDebugString("SetMaterial Failed!n");
  62. }
  63. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);
  64. return true;
  65. }
  66. HRESULT CCuboid::CreateVertexBuffer()
  67. {
  68.     //Create the vertex buffer from our device.
  69.     if(FAILED(m_pD3DDevice->CreateVertexBuffer(36 * sizeof(CUBOID_CUSTOMVERTEX),
  70.                                                0, CUBOID_D3DFVF_CUSTOMVERTEX,
  71.                                                D3DPOOL_DEFAULT, &m_pVertexBuffer)))
  72.     {
  73.         return E_FAIL;
  74.     }
  75.     return S_OK;
  76. }
  77. D3DVECTOR CCuboid::GetTriangeNormal(D3DXVECTOR3* vVertex1, D3DXVECTOR3* vVertex2, D3DXVECTOR3* vVertex3)
  78. {
  79. D3DXVECTOR3 vNormal;
  80. D3DXVECTOR3 v1;
  81. D3DXVECTOR3 v2;
  82. D3DXVec3Subtract(&v1, vVertex2, vVertex1);
  83. D3DXVec3Subtract(&v2, vVertex3, vVertex1);
  84. D3DXVec3Cross(&vNormal, &v1, &v2);
  85. D3DXVec3Normalize(&vNormal, &vNormal);
  86. return vNormal;
  87. }
  88. bool CCuboid::UpdateVertices()
  89. {
  90. VOID* pVertices;
  91. D3DVECTOR vNormal;
  92. CUBOID_CUSTOMVERTEX cvVertices[] =
  93. {
  94. //Top Face
  95. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 0
  96. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 1
  97. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 2
  98. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 3
  99. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 4
  100. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 5
  101. //Face 1
  102. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 6
  103. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 7
  104. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 8
  105. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 9
  106. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 10
  107. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 11
  108. //Face 2
  109. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 12
  110. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 13
  111. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 14
  112. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 15
  113. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 16
  114. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 17
  115. //Face 3
  116. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 18
  117. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 19
  118. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 20
  119. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 21
  120. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 22
  121. {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 23
  122. //Face 4
  123. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 24
  124. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 25
  125. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 26
  126. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 27
  127. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 28
  128. {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 29
  129. //Bottom Face
  130. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,}, //Vertex 30
  131. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 31
  132. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 32
  133. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,}, //Vertex 33
  134. {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,}, //Vertex 34
  135. {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, //Vertex 35
  136. };
  137. //Set all vertex normals
  138. int i;
  139. CHAR DEBUG[255];
  140. for(i = 0; i < 36; i += 3)
  141. {
  142. vNormal = GetTriangeNormal(&D3DXVECTOR3(cvVertices[i].x, cvVertices[i].y, cvVertices[i].z), &D3DXVECTOR3(cvVertices[i + 1].x, cvVertices[i + 1].y, cvVertices[i + 1].z), &D3DXVECTOR3(cvVertices[i + 2].x, cvVertices[i + 2].y, cvVertices[i + 2].z));
  143. cvVertices[i].nx = vNormal.x;
  144. cvVertices[i].ny = vNormal.y;
  145. cvVertices[i].nz = vNormal.z;
  146. cvVertices[i + 1].nx = vNormal.x;
  147. cvVertices[i + 1].ny = vNormal.y;
  148. cvVertices[i + 1].nz = vNormal.z;
  149. cvVertices[i + 2].nx = vNormal.x;
  150. cvVertices[i + 2].ny = vNormal.y;
  151. cvVertices[i + 2].nz = vNormal.z;
  152. sprintf(DEBUG, "Vertices %d - %d: x = %f, y = %f, z = %fn", i, i + 2, vNormal.x, vNormal.y, vNormal.z);
  153. OutputDebugString(DEBUG);
  154. }
  155. //Get a pointer to the vertex buffer vertices and lock the vertex buffer
  156.     if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
  157.     {
  158.         return false;
  159.     }
  160.     //Copy our stored vertices values into the vertex buffer
  161.     memcpy(pVertices, cvVertices, sizeof(cvVertices));
  162.     //Unlock the vertex buffer
  163.     m_pVertexBuffer->Unlock();
  164. return true;
  165. }
  166. bool CCuboid::SetSize(float rWidth, float rHeight, float rDepth)
  167. {
  168. m_rWidth = rWidth;
  169. m_rHeight = rHeight;
  170. m_rDepth = rDepth;
  171. UpdateVertices();
  172. return true;
  173. }
  174. bool CCuboid::SetPosition(float x, float y, float z)
  175. {
  176. m_rX = x;
  177. m_rY = y;
  178. m_rZ = z;
  179. UpdateVertices();
  180. return true;
  181. }
  182. bool CCuboid::SetTexture(const char *szTextureFilePath)
  183. {
  184. if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, szTextureFilePath, &m_pTexture)))
  185. {
  186. return false;
  187. }
  188. UpdateVertices();
  189. return true;
  190. }
  191. bool CCuboid::SetMaterial(D3DCOLORVALUE rgbaDiffuse, D3DCOLORVALUE rgbaAmbient, D3DCOLORVALUE rgbaSpecular, D3DCOLORVALUE rgbaEmissive, float rPower)
  192. {
  193. //Set the RGBA for diffuse light reflected from this material. 
  194. m_matMaterial.Diffuse = rgbaDiffuse; 
  195. //Set the RGBA for ambient light reflected from this material. 
  196. m_matMaterial.Ambient = rgbaAmbient; 
  197. //Set the color and sharpness of specular highlights for the material. 
  198. m_matMaterial.Specular = rgbaSpecular; 
  199. m_matMaterial.Power = rPower;
  200. //Set the RGBA for light emitted from this material. 
  201. m_matMaterial.Emissive = rgbaEmissive;
  202. return true;
  203. }