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