Terrain.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DrawableEvents.h"
  26. #include "Geometry.h"
  27. #include "Image.h"
  28. #include "IndexBuffer.h"
  29. #include "Log.h"
  30. #include "Material.h"
  31. #include "Node.h"
  32. #include "Octree.h"
  33. #include "Profiler.h"
  34. #include "ResourceEvents.h"
  35. #include "Scene.h"
  36. #include "Terrain.h"
  37. #include "TerrainPatch.h"
  38. #include "VertexBuffer.h"
  39. #include "DebugNew.h"
  40. OBJECTTYPESTATIC(Terrain);
  41. static const unsigned DEFAULT_PATCH_SIZE = 16;
  42. static const unsigned DEFAULT_LOD_LEVELS = 3;
  43. static const unsigned MAX_LOD_LEVELS = 4;
  44. static const unsigned MIN_PATCH_SIZE = 4;
  45. static const unsigned MAX_PATCH_SIZE = 128;
  46. static const Vector3 DEFAULT_SPACING(1.0f, 0.25f, 1.0f);
  47. Terrain::Terrain(Context* context) :
  48. Component(context),
  49. indexBuffer_(new IndexBuffer(context)),
  50. patchSize_(DEFAULT_PATCH_SIZE),
  51. spacing_(DEFAULT_SPACING),
  52. size_(IntVector2::ZERO),
  53. patchWorldSize_(Vector2::ZERO),
  54. patchWorldOrigin_(Vector2::ZERO),
  55. patchesX_(0),
  56. patchesZ_(0),
  57. numLodLevels_(DEFAULT_LOD_LEVELS),
  58. visible_(true),
  59. castShadows_(false),
  60. occluder_(false),
  61. occludee_(true),
  62. viewMask_(DEFAULT_VIEWMASK),
  63. lightMask_(DEFAULT_LIGHTMASK),
  64. shadowMask_(DEFAULT_SHADOWMASK),
  65. zoneMask_(DEFAULT_ZONEMASK),
  66. drawDistance_(0.0f),
  67. shadowDistance_(0.0f),
  68. lodBias_(1.0f),
  69. maxLights_(0),
  70. terrainDirty_(false)
  71. {
  72. indexBuffer_->SetShadowed(true);
  73. }
  74. Terrain::~Terrain()
  75. {
  76. }
  77. void Terrain::RegisterObject(Context* context)
  78. {
  79. context->RegisterFactory<Terrain>();
  80. }
  81. void Terrain::SetSpacing(const Vector3& spacing)
  82. {
  83. if (spacing != spacing_)
  84. {
  85. spacing_ = spacing;
  86. CreateGeometry();
  87. MarkNetworkUpdate();
  88. }
  89. }
  90. void Terrain::SetPatchSize(unsigned size)
  91. {
  92. size = Clamp((int)size, (int)MIN_PATCH_SIZE, (int)MAX_PATCH_SIZE);
  93. if (size != patchSize_ && IsPowerOfTwo(size))
  94. {
  95. patchSize_ = size;
  96. unsigned lodSize = size;
  97. numLodLevels_ = 1;
  98. while (lodSize > MIN_PATCH_SIZE && numLodLevels_ < MAX_LOD_LEVELS)
  99. {
  100. lodSize >>= 1;
  101. ++numLodLevels_;
  102. }
  103. CreateGeometry();
  104. MarkNetworkUpdate();
  105. }
  106. }
  107. bool Terrain::SetHeightMap(Image* image)
  108. {
  109. if (!image)
  110. return false;
  111. if (image->IsCompressed())
  112. {
  113. LOGERROR("Can not use a compressed image as a terrain heightmap");
  114. return false;
  115. }
  116. // Unsubscribe from the reload event of previous image (if any), then subscribe to the new
  117. if (heightMap_)
  118. UnsubscribeFromEvent(heightMap_, E_RELOADFINISHED);
  119. if (image)
  120. SubscribeToEvent(image, E_RELOADFINISHED, HANDLER(Terrain, HandleHeightMapReloadFinished));
  121. heightMap_ = image;
  122. CreateGeometry();
  123. MarkNetworkUpdate();
  124. return true;
  125. }
  126. void Terrain::SetMaterial(Material* material)
  127. {
  128. material_ = material;
  129. for (unsigned i = 0; i < patches_.Size(); ++i)
  130. patches_[i]->batches_[0].material_ = material;
  131. MarkNetworkUpdate();
  132. }
  133. void Terrain::SetDrawDistance(float distance)
  134. {
  135. drawDistance_ = distance;
  136. for (unsigned i = 0; i < patches_.Size(); ++i)
  137. patches_[i]->SetDrawDistance(distance);
  138. MarkNetworkUpdate();
  139. }
  140. void Terrain::SetShadowDistance(float distance)
  141. {
  142. shadowDistance_ = distance;
  143. for (unsigned i = 0; i < patches_.Size(); ++i)
  144. patches_[i]->SetShadowDistance(distance);
  145. MarkNetworkUpdate();
  146. }
  147. void Terrain::SetLodBias(float bias)
  148. {
  149. lodBias_ = bias;
  150. for (unsigned i = 0; i < patches_.Size(); ++i)
  151. patches_[i]->SetLodBias(bias);
  152. MarkNetworkUpdate();
  153. }
  154. void Terrain::SetViewMask(unsigned mask)
  155. {
  156. viewMask_ = mask;
  157. for (unsigned i = 0; i < patches_.Size(); ++i)
  158. patches_[i]->SetViewMask(mask);
  159. MarkNetworkUpdate();
  160. }
  161. void Terrain::SetLightMask(unsigned mask)
  162. {
  163. lightMask_ = mask;
  164. for (unsigned i = 0; i < patches_.Size(); ++i)
  165. patches_[i]->SetLightMask(mask);
  166. MarkNetworkUpdate();
  167. }
  168. void Terrain::SetShadowMask(unsigned mask)
  169. {
  170. shadowMask_ = mask;
  171. for (unsigned i = 0; i < patches_.Size(); ++i)
  172. patches_[i]->SetShadowMask(mask);
  173. MarkNetworkUpdate();
  174. }
  175. void Terrain::SetZoneMask(unsigned mask)
  176. {
  177. zoneMask_ = mask;
  178. for (unsigned i = 0; i < patches_.Size(); ++i)
  179. patches_[i]->SetZoneMask(mask);
  180. MarkNetworkUpdate();
  181. }
  182. void Terrain::SetMaxLights(unsigned num)
  183. {
  184. maxLights_ = num;
  185. for (unsigned i = 0; i < patches_.Size(); ++i)
  186. patches_[i]->SetMaxLights(num);
  187. MarkNetworkUpdate();
  188. }
  189. void Terrain::SetVisible(bool enable)
  190. {
  191. visible_ = enable;
  192. for (unsigned i = 0; i < patches_.Size(); ++i)
  193. patches_[i]->SetVisible(enable);
  194. MarkNetworkUpdate();
  195. }
  196. void Terrain::SetCastShadows(bool enable)
  197. {
  198. castShadows_ = enable;
  199. for (unsigned i = 0; i < patches_.Size(); ++i)
  200. patches_[i]->SetCastShadows(enable);
  201. MarkNetworkUpdate();
  202. }
  203. void Terrain::SetOccluder(bool enable)
  204. {
  205. occluder_ = enable;
  206. for (unsigned i = 0; i < patches_.Size(); ++i)
  207. patches_[i]->SetOccluder(enable);
  208. MarkNetworkUpdate();
  209. }
  210. void Terrain::SetOccludee(bool enable)
  211. {
  212. occluder_ = enable;
  213. for (unsigned i = 0; i < patches_.Size(); ++i)
  214. patches_[i]->SetOccludee(enable);
  215. MarkNetworkUpdate();
  216. }
  217. Image* Terrain::GetHeightMap() const
  218. {
  219. return heightMap_;
  220. }
  221. Material* Terrain::GetMaterial() const
  222. {
  223. return material_;
  224. }
  225. float Terrain::GetHeight(const Vector3& worldPosition) const
  226. {
  227. if (node_)
  228. {
  229. Vector3 position = node_->GetWorldTransform().Inverse() * worldPosition;
  230. float xPos = (position.x_ - patchWorldOrigin_.x_) / spacing_.x_;
  231. float zPos = (position.z_ - patchWorldOrigin_.y_) / spacing_.z_;
  232. float xFrac = xPos - floorf(xPos);
  233. float zFrac = zPos - floorf(zPos);
  234. float h1, h2, h3;
  235. if (xFrac + zFrac >= 1.0f)
  236. {
  237. h1 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos + 1);
  238. h2 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  239. h3 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  240. xFrac = 1.0f - xFrac;
  241. zFrac = 1.0f - zFrac;
  242. }
  243. else
  244. {
  245. h1 = GetRawHeight((unsigned)xPos, (unsigned)zPos);
  246. h2 = GetRawHeight((unsigned)xPos + 1, (unsigned)zPos);
  247. h3 = GetRawHeight((unsigned)xPos, (unsigned)zPos + 1);
  248. }
  249. float h = h1 * (1.0f - xFrac - zFrac) + h2 * xFrac + h3 * zFrac;
  250. /// \todo This assumes that the terrain scene node is upright
  251. return node_->GetWorldScale().y_ * h + node_->GetWorldPosition().y_;
  252. }
  253. else
  254. return 0.0f;
  255. }
  256. void Terrain::UpdatePatchGeometry(TerrainPatch* patch)
  257. {
  258. BoundingBox box;
  259. unsigned vertexDataRow = patchSize_ + 1;
  260. VertexBuffer* vertexBuffer = patch->vertexBuffer_;
  261. if (vertexBuffer->GetVertexCount() != vertexDataRow * vertexDataRow)
  262. vertexBuffer->SetSize(vertexDataRow * vertexDataRow, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  263. patch->cpuVertexData_ = new Vector3[vertexDataRow * vertexDataRow];
  264. float* vertexData = (float*)vertexBuffer->Lock(0, vertexBuffer->GetVertexCount());
  265. float* cpuVertexData = (float*)patch->cpuVertexData_.Get();
  266. if (vertexData)
  267. {
  268. unsigned x = patch->x_;
  269. unsigned z = patch->z_;
  270. for (unsigned z1 = 0; z1 <= patchSize_; ++z1)
  271. {
  272. for (unsigned x1 = 0; x1 <= patchSize_; ++x1)
  273. {
  274. int xPos = x * patchSize_ + x1;
  275. int zPos = z * patchSize_ + z1;
  276. // Position
  277. Vector3 position((float)x1 * spacing_.x_, GetRawHeight(xPos, zPos), (float)z1 * spacing_.z_);
  278. *vertexData++ = position.x_;
  279. *vertexData++ = position.y_;
  280. *vertexData++ = position.z_;
  281. *cpuVertexData++ = position.x_;
  282. *cpuVertexData++ = position.y_;
  283. *cpuVertexData++ = position.z_;
  284. box.Merge(position);
  285. // Normal
  286. Vector3 normal = GetNormal(xPos, zPos);
  287. *vertexData++ = normal.x_;
  288. *vertexData++ = normal.y_;
  289. *vertexData++ = normal.z_;
  290. // Texture coordinate
  291. Vector2 texCoord((float)xPos / (float)size_.x_, 1.0f - (float)zPos / (float)size_.y_);
  292. *vertexData++ = texCoord.x_;
  293. *vertexData++ = texCoord.y_;
  294. // Tangent
  295. Vector3 xyz = (Vector3::RIGHT - normal * normal.DotProduct(Vector3::RIGHT)).Normalized();
  296. *vertexData++ = xyz.x_;
  297. *vertexData++ = xyz.y_;
  298. *vertexData++ = xyz.z_;
  299. *vertexData++ = 1.0f;
  300. }
  301. }
  302. vertexBuffer->Unlock();
  303. vertexBuffer->ClearDataLost();
  304. }
  305. patch->boundingBox_ = box;
  306. patch->geometry_->SetIndexBuffer(indexBuffer_);
  307. patch->geometry_->SetDrawRange(TRIANGLE_LIST, 0, indexBuffer_->GetIndexCount());
  308. patch->OnMarkedDirty(patch->GetNode());
  309. }
  310. void Terrain::UpdatePatchLOD(TerrainPatch* patch, unsigned lod, unsigned northLod, unsigned southLod, unsigned westLod,
  311. unsigned eastLod)
  312. {
  313. }
  314. void Terrain::OnMarkedDirty(Node* node)
  315. {
  316. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  317. for (Vector<SharedPtr<TerrainPatch> >::Iterator i = patches_.Begin(); i != patches_.End(); ++i)
  318. SetPatchTransform(*i);
  319. }
  320. void Terrain::CreateGeometry()
  321. {
  322. Scene* scene = GetScene();
  323. Octree* octree = scene ? scene->GetComponent<Octree>() : 0;
  324. if (!heightMap_ || !node_ || !octree)
  325. return;
  326. PROFILE(CreateTerrainGeometry);
  327. patches_.Clear();
  328. patchNodes_.Clear();
  329. // Determine total terrain size & copy heightmap data
  330. patchesX_ = (heightMap_->GetWidth() - 1) / patchSize_;
  331. patchesZ_ = (heightMap_->GetHeight() - 1) / patchSize_;
  332. size_ = IntVector2(patchesX_ * patchSize_ + 1, patchesZ_ * patchSize_ + 1);
  333. patchWorldSize_ = Vector2(spacing_.x_ * (float)patchSize_, spacing_.z_ * (float)patchSize_);
  334. patchWorldOrigin_ = Vector2(-0.5f * (float)patchesX_ * patchWorldSize_.x_, -0.5f * (float)patchesZ_ * patchWorldSize_.y_);
  335. heightData_ = new float[size_.x_ * size_.y_];
  336. const unsigned char* src = heightMap_->GetData();
  337. float* dest = heightData_;
  338. unsigned imgComps = heightMap_->GetComponents();
  339. unsigned imgRow = heightMap_->GetWidth() * imgComps;
  340. for (int z = 0; z < size_.y_; ++z)
  341. {
  342. for (int x = 0; x < size_.x_; ++x)
  343. *dest++ = (float)src[imgRow * (size_.y_ - 1 - z) + imgComps * x] * spacing_.y_;
  344. }
  345. // Create scene nodes for patches
  346. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  347. for (unsigned z = 0; z < patchesZ_; ++z)
  348. {
  349. for (unsigned x = 0; x < patchesX_; ++x)
  350. {
  351. // Note: terrain nodes are not created as part of the scene, as it's runtime generated scene hierarchy
  352. // we never want to save, serialize through network, or show in the editor
  353. Node* patchNode = new Node(context_);
  354. patchNode->SetID(FIRST_LOCAL_ID);
  355. TerrainPatch* patch = new TerrainPatch(context_);
  356. patch->owner_ = this;
  357. patch->x_ = x;
  358. patch->z_ = z;
  359. patchNode->AddComponent(patch, FIRST_LOCAL_ID, LOCAL);
  360. octree->AddManualDrawable(patch);
  361. // Copy initial drawable parameters
  362. patch->batches_[0].material_ = material_;
  363. patch->SetDrawDistance(drawDistance_);
  364. patch->SetShadowDistance(shadowDistance_);
  365. patch->SetLodBias(lodBias_);
  366. patch->SetViewMask(viewMask_);
  367. patch->SetLightMask(lightMask_);
  368. patch->SetShadowMask(shadowMask_);
  369. patch->SetZoneMask(zoneMask_);
  370. patch->SetMaxLights(maxLights_);
  371. patch->SetVisible(visible_);
  372. patch->SetCastShadows(castShadows_);
  373. patch->SetOccluder(occluder_);
  374. patch->SetOccludee(occludee_);
  375. patches_.Push(SharedPtr<TerrainPatch>(patch));
  376. patchNodes_.Push(SharedPtr<Node>(patchNode));
  377. }
  378. }
  379. // Create the shared index data
  380. /// \todo Create LOD levels
  381. indexBuffer_->SetSize(patchSize_ * patchSize_ * 6, false);
  382. unsigned vertexDataRow = patchSize_ + 1;
  383. unsigned short* indexData = (unsigned short*)indexBuffer_->Lock(0, indexBuffer_->GetIndexCount());
  384. if (indexData)
  385. {
  386. for (unsigned z = 0; z < patchSize_; ++z)
  387. {
  388. for (unsigned x = 0; x < patchSize_; ++x)
  389. {
  390. *indexData++ = x + (z + 1) * vertexDataRow;
  391. *indexData++ = x + z * vertexDataRow + 1;
  392. *indexData++ = x + z * vertexDataRow;
  393. *indexData++ = x + (z + 1) * vertexDataRow;
  394. *indexData++ = x + (z + 1) * vertexDataRow + 1;
  395. *indexData++ = x + z * vertexDataRow + 1;
  396. }
  397. }
  398. indexBuffer_->Unlock();
  399. }
  400. // Create vertex data for patches, and set transform
  401. for (Vector<SharedPtr<TerrainPatch> >::Iterator i = patches_.Begin(); i != patches_.End(); ++i)
  402. {
  403. SetPatchTransform(*i);
  404. UpdatePatchGeometry(*i);
  405. }
  406. using namespace TerrainCreated;
  407. VariantMap eventData;
  408. eventData[P_NODE] = (void*)node_;
  409. SendEvent(E_TERRAINCREATED, eventData);
  410. }
  411. void Terrain::SetPatchTransform(TerrainPatch* patch)
  412. {
  413. Node* patchNode = patch->GetNode();
  414. unsigned x = patch->x_;
  415. unsigned z = patch->z_;
  416. Matrix3x4 patchTransform(Vector3(patchWorldOrigin_.x_ + (float)x * patchWorldSize_.x_, 0.0f, patchWorldOrigin_.y_ +
  417. (float)z * patchWorldSize_.y_), Quaternion::IDENTITY, 1.0f);
  418. Matrix3x4 combinedTransform = node_->GetWorldTransform() * patchTransform;
  419. patchNode->SetTransform(combinedTransform.Translation(), combinedTransform.Rotation(), combinedTransform.Scale());
  420. }
  421. float Terrain::GetRawHeight(unsigned x, unsigned z) const
  422. {
  423. if (!heightData_)
  424. return 0.0f;
  425. x = Clamp((int)x, 0, (int)size_.x_ - 1);
  426. z = Clamp((int)z, 0, (int)size_.y_ - 1);
  427. return heightData_[z * size_.x_ + x];
  428. }
  429. Vector3 Terrain::GetNormal(unsigned x, unsigned z) const
  430. {
  431. float baseHeight = GetRawHeight(x, z);
  432. float nSlope = GetRawHeight(x, z - 1) - baseHeight;
  433. float neSlope = GetRawHeight(x + 1, z - 1) - baseHeight;
  434. float eSlope = GetRawHeight(x + 1, z) - baseHeight;
  435. float seSlope = GetRawHeight(x + 1, z + 1) - baseHeight;
  436. float sSlope = GetRawHeight(x, z + 1) - baseHeight;
  437. float swSlope = GetRawHeight(x - 1, z + 1) - baseHeight;
  438. float wSlope = GetRawHeight(x - 1, z) - baseHeight;
  439. float nwSlope = GetRawHeight(x - 1, z - 1) - baseHeight;
  440. return (Vector3(0.0f, 1.0f, nSlope) +
  441. Vector3(-neSlope, 1.0f, neSlope) +
  442. Vector3(-eSlope, 1.0f, 0.0f) +
  443. Vector3(-seSlope, 1.0f, -seSlope) +
  444. Vector3(0.0f, 1.0f, -sSlope) +
  445. Vector3(swSlope, 1.0f, -swSlope) +
  446. Vector3(wSlope, 1.0f, 0.0f) +
  447. Vector3(nwSlope, 1.0f, nwSlope)).Normalized();
  448. }
  449. void Terrain::HandleHeightMapReloadFinished(StringHash eventType, VariantMap& eventData)
  450. {
  451. CreateGeometry();
  452. }