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

其他书籍

开发平台:

HTML/CSS

  1. // Terrain.cpp: implementation of the CTerrain class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Terrain.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CTerrain::CTerrain(LPDIRECT3DDEVICE8 pD3DDevice, WORD wRows, WORD wCols, float rTileSize, WORD wMaxHeight)
  9. {
  10. m_pD3DDevice = pD3DDevice;
  11. m_pVertexBuffer = NULL;
  12. m_pIndexBuffer = NULL;
  13. m_pTexture = NULL;
  14. //Set a default size and position
  15. m_wRows = wRows;
  16. m_wCols = wCols;
  17. //Set the tile size for the terrain
  18. m_rTileSize = rTileSize;
  19. //Set the max height for any vertex
  20. m_wMaxHeight = wMaxHeight;
  21. //Setup counts for this object
  22. m_dwNumOfVertices = (m_wCols + 1) * (m_wRows + 1);
  23. m_dwNumOfPolygons = m_wRows * m_wCols * 2;
  24. m_dwNumOfIndices  = m_dwNumOfPolygons * 3;
  25. //Set material default values (R, G, B, A)
  26. D3DCOLORVALUE rgbaDiffuse  = {1.0, 1.0, 1.0, 0.0,};
  27. D3DCOLORVALUE rgbaAmbient  = {1.0, 1.0, 1.0, 0.0,};
  28. D3DCOLORVALUE rgbaSpecular = {0.0, 0.0, 0.0, 0.0,};
  29. D3DCOLORVALUE rgbaEmissive = {0.0, 0.0, 0.0, 0.0,};
  30. SetMaterial(rgbaDiffuse, rgbaAmbient, rgbaSpecular, rgbaEmissive, 0);
  31. //Initialize Vertex Buffer
  32.     if(SUCCEEDED(CreateVertexBuffer()))
  33. {
  34. if(CreateIndexBuffer())
  35. {
  36. UpdateVertices();
  37. }
  38. }
  39. }
  40. CTerrain::~CTerrain()
  41. {
  42. SafeRelease(m_pTexture);
  43. SafeRelease(m_pIndexBuffer);
  44. SafeRelease(m_pVertexBuffer);
  45. }
  46. DWORD CTerrain::Render()
  47. {
  48. m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(TERRAIN_CUSTOMVERTEX));
  49. m_pD3DDevice->SetVertexShader(TERRAIN_D3DFVF_CUSTOMVERTEX);
  50. if(m_pTexture != NULL)
  51. {
  52. //A texture has been set. We want our texture to be shaded based
  53. //on the current light levels, so used D3DTOP_MODULATE.
  54. m_pD3DDevice->SetTexture(0, m_pTexture);
  55. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  56. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  57. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
  58. }
  59. else
  60. {
  61. //No texture has been set
  62. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  63. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  64. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
  65. }
  66. //Select the material to use
  67. m_pD3DDevice->SetMaterial(&m_matMaterial);
  68. //Select index buffer
  69. m_pD3DDevice->SetIndices(m_pIndexBuffer, 0);
  70. //Render polygons from index buffer
  71. m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_dwNumOfVertices, 0, m_dwNumOfPolygons);
  72. //Return the number of polygons rendered
  73. return m_dwNumOfPolygons;
  74. }
  75. HRESULT CTerrain::CreateVertexBuffer()
  76. {
  77.     //Create the vertex buffer from our device.
  78.     if(FAILED(m_pD3DDevice->CreateVertexBuffer(m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX),
  79.                                                0, TERRAIN_D3DFVF_CUSTOMVERTEX,
  80.                                                D3DPOOL_DEFAULT, &m_pVertexBuffer)))
  81.     {
  82.         return E_FAIL;
  83.     }
  84.     return S_OK;
  85. }
  86. bool CTerrain::CreateIndexBuffer()
  87. {
  88. VOID* pBufferIndices;
  89. //Create the index buffer from our device
  90. if(FAILED(m_pD3DDevice->CreateIndexBuffer(m_dwNumOfIndices * sizeof(WORD), 
  91.   0, D3DFMT_INDEX16, D3DPOOL_MANAGED,
  92.   &m_pIndexBuffer)))
  93. {
  94.     return false;
  95. }
  96. //Set values for the index buffer
  97. WORD* pIndices = new WORD[m_dwNumOfIndices]; //Array holds the indices
  98. WORD a = m_wCols + 1;
  99. WORD b = 0;
  100. WORD c = a + 1;
  101. WORD x, z, i = 0;
  102. for(z = 0; z < m_wRows; z++)
  103. {
  104. for(x = 0; x < m_wCols; x++)
  105. {
  106. pIndices[i] = a;
  107. pIndices[i + 1] = b;
  108. pIndices[i + 2] = c;
  109. pIndices[i + 3] = b + 1;
  110. pIndices[i + 4] = c;
  111. pIndices[i + 5] = b;
  112. a++;
  113. b++;
  114. c++;
  115. i += 6;
  116. }
  117. a = c;
  118. b++;
  119. c++;
  120. }
  121. //Get a pointer to the index buffer indices and lock the index buffer    
  122. m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD), (BYTE**)&pBufferIndices, 0);
  123. //Copy our stored indices values into the index buffer
  124. memcpy(pBufferIndices, pIndices, m_dwNumOfIndices * sizeof(WORD));
  125. //Unlock the index buffer
  126. m_pIndexBuffer->Unlock();
  127. //Clean up
  128. delete pIndices;
  129. pIndices = NULL;
  130. return true;
  131. }
  132. D3DXVECTOR3 CTerrain::GetTriangeNormal(D3DXVECTOR3* vVertex1, D3DXVECTOR3* vVertex2, D3DXVECTOR3* vVertex3)
  133. {
  134. D3DXVECTOR3 vNormal;
  135. D3DXVECTOR3 v1;
  136. D3DXVECTOR3 v2;
  137. D3DXVec3Subtract(&v1, vVertex2, vVertex1);
  138. D3DXVec3Subtract(&v2, vVertex3, vVertex1);
  139. D3DXVec3Cross(&vNormal, &v1, &v2);
  140. D3DXVec3Normalize(&vNormal, &vNormal);
  141. return vNormal;
  142. }
  143. bool CTerrain::UpdateVertices()
  144. {
  145. DWORD i = 0;
  146. VOID* pVertices;
  147. WORD* pBufferIndices;
  148. D3DXVECTOR3 vNormal;
  149. DWORD dwVertex1;
  150. DWORD dwVertex2;
  151. DWORD dwVertex3;
  152. WORD* pNumOfSharedPolygons = new WORD[m_dwNumOfVertices]; //Array holds how many times this vertex is shared
  153. D3DVECTOR* pSumVertexNormal = new D3DVECTOR[m_dwNumOfVertices]; //Array holds sum of all face normals for shared vertex
  154. TERRAIN_CUSTOMVERTEX* pcvVertices = new TERRAIN_CUSTOMVERTEX[m_dwNumOfVertices]; //Array holds the veritces
  155. float x, z;
  156. //Centre terrain around the origin
  157. float zStart = (float)(0.0 - (m_wRows/2.0));
  158. float zEnd = (float)(m_wRows/2.0);
  159. float xStart = (float)(0.0 - (m_wCols/2.0));
  160. float xEnd = (float)(m_wCols/2.0);
  161. //Initialise the random number generator
  162. srand(timeGetTime());  
  163. //Clear memory and setup vertices for terrain
  164. for(z = zStart; z <= zEnd; z++)
  165. {
  166. for(x = xStart; x <= xEnd; x++)
  167. {
  168. pNumOfSharedPolygons[i] = 0;
  169. pSumVertexNormal[i] = D3DXVECTOR3(0,0,0);
  170. pcvVertices[i].x = x * m_rTileSize;
  171. //Make sure that the edges are all the same level
  172. if((z == zStart) || (z == zEnd) || (x == xStart) || (x == xEnd))
  173. {
  174. pcvVertices[i].y = 0.0;
  175. }
  176. else
  177. {
  178. //Set a random height for y
  179. pcvVertices[i].y = (float)(rand() % m_wMaxHeight);
  180. }
  181. pcvVertices[i].z = -z * m_rTileSize;
  182. pcvVertices[i].nx = 0.0;
  183. pcvVertices[i].ny = 0.0;
  184. pcvVertices[i].nz = 0.0;
  185. if(((int)z % 2) == 0)
  186. {
  187. if(((int)x % 2) == 0)
  188. {
  189. pcvVertices[i].tu = 0.0;
  190. pcvVertices[i].tv = 0.0;
  191. }
  192. else
  193. {
  194. pcvVertices[i].tu = 1.0;
  195. pcvVertices[i].tv = 0.0;
  196. }
  197. }
  198. else
  199. {
  200. if(((int)x % 2) == 0)
  201. {
  202. pcvVertices[i].tu = 0.0;
  203. pcvVertices[i].tv = 1.0;
  204. }
  205. else
  206. {
  207. pcvVertices[i].tu = 1.0;
  208. pcvVertices[i].tv = 1.0;
  209. }
  210. }
  211. i++;
  212. }
  213. }
  214. //Get a pointer to the index buffer indices and lock the index buffer    
  215. m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD), (BYTE**)&pBufferIndices, D3DLOCK_READONLY);
  216. //For each triangle, count the number of times each vertex is used and
  217. //add together the normals of faces that share a vertex
  218. for(i = 0; i < m_dwNumOfIndices; i += 3)
  219. {
  220. dwVertex1 = pBufferIndices[i];
  221. dwVertex2 = pBufferIndices[i + 1];
  222. dwVertex3 = pBufferIndices[i + 2];
  223. vNormal = GetTriangeNormal(&D3DXVECTOR3(pcvVertices[dwVertex1].x, pcvVertices[dwVertex1].y, pcvVertices[dwVertex1].z), 
  224.    &D3DXVECTOR3(pcvVertices[dwVertex2].x, pcvVertices[dwVertex2].y, pcvVertices[dwVertex2].z), 
  225.    &D3DXVECTOR3(pcvVertices[dwVertex3].x, pcvVertices[dwVertex3].y, pcvVertices[dwVertex3].z));
  226. pNumOfSharedPolygons[dwVertex1]++;
  227. pNumOfSharedPolygons[dwVertex2]++;
  228. pNumOfSharedPolygons[dwVertex3]++;
  229. pSumVertexNormal[dwVertex1].x += vNormal.x;
  230. pSumVertexNormal[dwVertex1].y += vNormal.y;
  231. pSumVertexNormal[dwVertex1].z += vNormal.z;
  232. pSumVertexNormal[dwVertex2].x += vNormal.x;
  233. pSumVertexNormal[dwVertex2].y += vNormal.y;
  234. pSumVertexNormal[dwVertex2].z += vNormal.z;
  235. pSumVertexNormal[dwVertex3].x += vNormal.x;
  236. pSumVertexNormal[dwVertex3].y += vNormal.y;
  237. pSumVertexNormal[dwVertex3].z += vNormal.z;
  238. }
  239. //Unlock the index buffer
  240. m_pIndexBuffer->Unlock();
  241. //For each vertex, calculate the average normal
  242. //CHAR DEBUG[255];
  243. for(i = 0; i < m_dwNumOfVertices; i++)
  244. {
  245. vNormal.x = pSumVertexNormal[i].x / pNumOfSharedPolygons[i];
  246. vNormal.y = pSumVertexNormal[i].y / pNumOfSharedPolygons[i];
  247. vNormal.z = pSumVertexNormal[i].z / pNumOfSharedPolygons[i];
  248. D3DXVec3Normalize(&vNormal, &vNormal);
  249. pcvVertices[i].nx = vNormal.x;
  250. pcvVertices[i].ny = vNormal.y;
  251. pcvVertices[i].nz = vNormal.z;
  252. //sprintf(DEBUG, "Vertex Data %d: x = %f, y = %f, z = %f, nx = %f, ny = %f, nz = %f, tu = %f, tv = %fn", i, pcvVertices[i].x, pcvVertices[i].y, pcvVertices[i].z, pcvVertices[i].nx, pcvVertices[i].ny, pcvVertices[i].nz, pcvVertices[i].tu, pcvVertices[i].tv);
  253. //OutputDebugString(DEBUG);
  254. }
  255. //Get a pointer to the vertex buffer vertices and lock the vertex buffer
  256.     if(FAILED(m_pVertexBuffer->Lock(0, m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX), (BYTE**)&pVertices, 0)))
  257.     {
  258.         OutputDebugString("Lock Vertex Buffer Failed!n");
  259. return false;
  260.     }
  261.     //Copy our stored vertices values into the vertex buffer
  262.     memcpy(pVertices, pcvVertices, m_dwNumOfVertices * sizeof(TERRAIN_CUSTOMVERTEX));
  263.     //Unlock the vertex buffer
  264.     m_pVertexBuffer->Unlock();
  265. //Clean up
  266. delete pNumOfSharedPolygons;
  267. delete pSumVertexNormal;
  268. delete pcvVertices;
  269. pNumOfSharedPolygons = NULL;
  270. pSumVertexNormal = NULL;
  271. pcvVertices = NULL;
  272. return true;
  273. }
  274. bool CTerrain::SetSize(WORD wRows, WORD wCols, float rTileSize, WORD wMaxHeight)
  275. {
  276. //Set size
  277. m_wRows = wRows;
  278. m_wCols = wCols;
  279. //Set the tile size for the terrain
  280. m_rTileSize = rTileSize;
  281. //Set the max height for any vertex
  282. m_wMaxHeight = wMaxHeight;
  283. //Setup counts for this object
  284. m_dwNumOfVertices = (m_wCols + 1)  * (m_wRows + 1);
  285. m_dwNumOfPolygons = m_wRows * m_wCols * 2;
  286. m_dwNumOfIndices  = m_dwNumOfPolygons * 3;
  287. UpdateVertices();
  288. return true;
  289. }
  290. bool CTerrain::SetTexture(const char *szTextureFilePath)
  291. {
  292. if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, szTextureFilePath, &m_pTexture)))
  293. {
  294. OutputDebugString("Failed to load texture.");
  295. return false;
  296. }
  297. return true;
  298. }
  299. bool CTerrain::SetMaterial(D3DCOLORVALUE rgbaDiffuse, D3DCOLORVALUE rgbaAmbient, D3DCOLORVALUE rgbaSpecular, D3DCOLORVALUE rgbaEmissive, float rPower)
  300. {
  301. //Set the RGBA for diffuse light reflected from this material. 
  302. m_matMaterial.Diffuse = rgbaDiffuse; 
  303. //Set the RGBA for ambient light reflected from this material. 
  304. m_matMaterial.Ambient = rgbaAmbient; 
  305. //Set the color and sharpness of specular highlights for the material. 
  306. m_matMaterial.Specular = rgbaSpecular; 
  307. m_matMaterial.Power = rPower;
  308. //Set the RGBA for light emitted from this material. 
  309. m_matMaterial.Emissive = rgbaEmissive;
  310. return true;
  311. }