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