Drawable.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../IO/File.h"
  27. #include "../Graphics/Geometry.h"
  28. #include "../Graphics/Material.h"
  29. #include "../Graphics/Octree.h"
  30. #include "../Graphics/Renderer.h"
  31. #include "../Graphics/VertexBuffer.h"
  32. #include "../Graphics/Zone.h"
  33. #include "../IO/Log.h"
  34. #include "../Scene/Scene.h"
  35. #include "../DebugNew.h"
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:6293)
  38. #endif
  39. namespace Urho3D
  40. {
  41. const char* GEOMETRY_CATEGORY = "Geometry";
  42. SourceBatch::SourceBatch() = default;
  43. SourceBatch::SourceBatch(const SourceBatch& batch) = default;
  44. SourceBatch::~SourceBatch() = default;
  45. SourceBatch& SourceBatch::operator =(const SourceBatch& rhs)= default;
  46. Drawable::Drawable(Context* context, unsigned char drawableFlags) :
  47. Component(context),
  48. boundingBox_(0.0f, 0.0f),
  49. drawableFlags_(drawableFlags),
  50. worldBoundingBoxDirty_(true),
  51. castShadows_(false),
  52. occluder_(false),
  53. occludee_(true),
  54. updateQueued_(false),
  55. zoneDirty_(false),
  56. octant_(nullptr),
  57. zone_(nullptr),
  58. viewMask_(DEFAULT_VIEWMASK),
  59. lightMask_(DEFAULT_LIGHTMASK),
  60. shadowMask_(DEFAULT_SHADOWMASK),
  61. zoneMask_(DEFAULT_ZONEMASK),
  62. viewFrameNumber_(0),
  63. distance_(0.0f),
  64. lodDistance_(0.0f),
  65. drawDistance_(0.0f),
  66. shadowDistance_(0.0f),
  67. sortValue_(0.0f),
  68. minZ_(0.0f),
  69. maxZ_(0.0f),
  70. lodBias_(1.0f),
  71. basePassFlags_(0),
  72. maxLights_(0),
  73. firstLight_(nullptr)
  74. {
  75. if (drawableFlags == DRAWABLE_UNDEFINED || drawableFlags > DRAWABLE_ANY)
  76. {
  77. URHO3D_LOGERROR("Drawable with undefined drawableFlags");
  78. }
  79. }
  80. Drawable::~Drawable()
  81. {
  82. RemoveFromOctree();
  83. }
  84. void Drawable::RegisterObject(Context* context)
  85. {
  86. URHO3D_ATTRIBUTE("Max Lights", int, maxLights_, 0, AM_DEFAULT);
  87. URHO3D_ATTRIBUTE("View Mask", int, viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  88. URHO3D_ATTRIBUTE("Light Mask", int, lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  89. URHO3D_ATTRIBUTE("Shadow Mask", int, shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  90. URHO3D_ACCESSOR_ATTRIBUTE("Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  91. }
  92. void Drawable::OnSetEnabled()
  93. {
  94. bool enabled = IsEnabledEffective();
  95. if (enabled && !octant_)
  96. AddToOctree();
  97. else if (!enabled && octant_)
  98. RemoveFromOctree();
  99. }
  100. void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  101. {
  102. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  103. if (distance < query.maxDistance_)
  104. {
  105. RayQueryResult result;
  106. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  107. result.normal_ = -query.ray_.direction_;
  108. result.distance_ = distance;
  109. result.drawable_ = this;
  110. result.node_ = GetNode();
  111. result.subObject_ = M_MAX_UNSIGNED;
  112. results.Push(result);
  113. }
  114. }
  115. void Drawable::UpdateBatches(const FrameInfo& frame)
  116. {
  117. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  118. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  119. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  120. for (unsigned i = 0; i < batches_.Size(); ++i)
  121. {
  122. batches_[i].distance_ = distance_;
  123. batches_[i].worldTransform_ = &worldTransform;
  124. }
  125. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  126. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  127. if (newLodDistance != lodDistance_)
  128. lodDistance_ = newLodDistance;
  129. }
  130. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  131. {
  132. // By default return the visible batch geometry
  133. if (batchIndex < batches_.Size())
  134. return batches_[batchIndex].geometry_;
  135. else
  136. return nullptr;
  137. }
  138. bool Drawable::DrawOcclusion(OcclusionBuffer* buffer)
  139. {
  140. return true;
  141. }
  142. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  143. {
  144. if (debug && IsEnabledEffective())
  145. debug->AddBoundingBox(GetWorldBoundingBox(), Color::GREEN, depthTest);
  146. }
  147. void Drawable::SetDrawDistance(float distance)
  148. {
  149. drawDistance_ = distance;
  150. MarkNetworkUpdate();
  151. }
  152. void Drawable::SetShadowDistance(float distance)
  153. {
  154. shadowDistance_ = distance;
  155. MarkNetworkUpdate();
  156. }
  157. void Drawable::SetLodBias(float bias)
  158. {
  159. lodBias_ = Max(bias, M_EPSILON);
  160. MarkNetworkUpdate();
  161. }
  162. void Drawable::SetViewMask(unsigned mask)
  163. {
  164. viewMask_ = mask;
  165. MarkNetworkUpdate();
  166. }
  167. void Drawable::SetLightMask(unsigned mask)
  168. {
  169. lightMask_ = mask;
  170. MarkNetworkUpdate();
  171. }
  172. void Drawable::SetShadowMask(unsigned mask)
  173. {
  174. shadowMask_ = mask;
  175. MarkNetworkUpdate();
  176. }
  177. void Drawable::SetZoneMask(unsigned mask)
  178. {
  179. zoneMask_ = mask;
  180. // Mark dirty to reset cached zone
  181. OnMarkedDirty(node_);
  182. MarkNetworkUpdate();
  183. }
  184. void Drawable::SetMaxLights(unsigned num)
  185. {
  186. maxLights_ = num;
  187. MarkNetworkUpdate();
  188. }
  189. void Drawable::SetCastShadows(bool enable)
  190. {
  191. castShadows_ = enable;
  192. MarkNetworkUpdate();
  193. }
  194. void Drawable::SetOccluder(bool enable)
  195. {
  196. occluder_ = enable;
  197. MarkNetworkUpdate();
  198. }
  199. void Drawable::SetOccludee(bool enable)
  200. {
  201. if (enable != occludee_)
  202. {
  203. occludee_ = enable;
  204. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  205. if (octant_ && !updateQueued_)
  206. octant_->GetRoot()->QueueUpdate(this);
  207. MarkNetworkUpdate();
  208. }
  209. }
  210. void Drawable::MarkForUpdate()
  211. {
  212. if (!updateQueued_ && octant_)
  213. octant_->GetRoot()->QueueUpdate(this);
  214. }
  215. const BoundingBox& Drawable::GetWorldBoundingBox()
  216. {
  217. if (worldBoundingBoxDirty_)
  218. {
  219. OnWorldBoundingBoxUpdate();
  220. worldBoundingBoxDirty_ = false;
  221. }
  222. return worldBoundingBox_;
  223. }
  224. bool Drawable::IsInView() const
  225. {
  226. // Note: in headless mode there is no renderer subsystem and no view frustum tests are performed, so return
  227. // always false in that case
  228. auto* renderer = GetSubsystem<Renderer>();
  229. return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && !viewCameras_.Empty();
  230. }
  231. bool Drawable::IsInView(Camera* camera) const
  232. {
  233. auto* renderer = GetSubsystem<Renderer>();
  234. return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && (!camera || viewCameras_.Contains(camera));
  235. }
  236. bool Drawable::IsInView(const FrameInfo& frame, bool anyCamera) const
  237. {
  238. return viewFrameNumber_ == frame.frameNumber_ && (anyCamera || viewCameras_.Contains(frame.camera_));
  239. }
  240. void Drawable::SetZone(Zone* zone, bool temporary)
  241. {
  242. zone_ = zone;
  243. // If the zone assignment was temporary (inconclusive) set the dirty flag so that it will be re-evaluated on the next frame
  244. zoneDirty_ = temporary;
  245. }
  246. void Drawable::SetSortValue(float value)
  247. {
  248. sortValue_ = value;
  249. }
  250. void Drawable::MarkInView(const FrameInfo& frame)
  251. {
  252. if (frame.frameNumber_ != viewFrameNumber_)
  253. {
  254. viewFrameNumber_ = frame.frameNumber_;
  255. viewCameras_.Resize(1);
  256. viewCameras_[0] = frame.camera_;
  257. }
  258. else
  259. viewCameras_.Push(frame.camera_);
  260. basePassFlags_ = 0;
  261. firstLight_ = nullptr;
  262. lights_.Clear();
  263. vertexLights_.Clear();
  264. }
  265. void Drawable::MarkInView(unsigned frameNumber)
  266. {
  267. if (frameNumber != viewFrameNumber_)
  268. {
  269. viewFrameNumber_ = frameNumber;
  270. viewCameras_.Clear();
  271. }
  272. }
  273. void Drawable::LimitLights()
  274. {
  275. // Maximum lights value 0 means unlimited
  276. if (!maxLights_ || lights_.Size() <= maxLights_)
  277. return;
  278. // If more lights than allowed, move to vertex lights and cut the list
  279. const BoundingBox& box = GetWorldBoundingBox();
  280. for (unsigned i = 0; i < lights_.Size(); ++i)
  281. lights_[i]->SetIntensitySortValue(box);
  282. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  283. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  284. lights_.Resize(maxLights_);
  285. }
  286. void Drawable::LimitVertexLights(bool removeConvertedLights)
  287. {
  288. if (removeConvertedLights)
  289. {
  290. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  291. {
  292. if (!vertexLights_[i]->GetPerVertex())
  293. vertexLights_.Erase(i);
  294. }
  295. }
  296. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  297. return;
  298. const BoundingBox& box = GetWorldBoundingBox();
  299. for (unsigned i = 0; i < vertexLights_.Size(); ++i)
  300. vertexLights_[i]->SetIntensitySortValue(box);
  301. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  302. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  303. }
  304. void Drawable::OnNodeSet(Node* node)
  305. {
  306. if (node)
  307. node->AddListener(this);
  308. }
  309. void Drawable::OnSceneSet(Scene* scene)
  310. {
  311. if (scene)
  312. AddToOctree();
  313. else
  314. RemoveFromOctree();
  315. }
  316. void Drawable::OnMarkedDirty(Node* node)
  317. {
  318. worldBoundingBoxDirty_ = true;
  319. if (!updateQueued_ && octant_)
  320. octant_->GetRoot()->QueueUpdate(this);
  321. // Mark zone assignment dirty when transform changes
  322. if (node == node_)
  323. zoneDirty_ = true;
  324. }
  325. void Drawable::AddToOctree()
  326. {
  327. // Do not add to octree when disabled
  328. if (!IsEnabledEffective())
  329. return;
  330. Scene* scene = GetScene();
  331. if (scene)
  332. {
  333. auto* octree = scene->GetComponent<Octree>();
  334. if (octree)
  335. octree->InsertDrawable(this);
  336. else
  337. URHO3D_LOGERROR("No Octree component in scene, drawable will not render");
  338. }
  339. else
  340. {
  341. // We have a mechanism for adding detached nodes to an octree manually, so do not log this error
  342. //URHO3D_LOGERROR("Node is detached from scene, drawable will not render");
  343. }
  344. }
  345. void Drawable::RemoveFromOctree()
  346. {
  347. if (octant_)
  348. {
  349. Octree* octree = octant_->GetRoot();
  350. if (updateQueued_)
  351. octree->CancelUpdate(this);
  352. // Perform subclass specific deinitialization if necessary
  353. OnRemoveFromOctree();
  354. octant_->RemoveDrawable(this);
  355. }
  356. }
  357. bool WriteDrawablesToOBJ(const PODVector<Drawable*>& drawables, File* outputFile, bool asZUp, bool asRightHanded, bool writeLightmapUV)
  358. {
  359. // Must track indices independently to deal with potential mismatching of drawables vertex attributes (ie. one with UV, another without, then another with)
  360. unsigned currentPositionIndex = 1;
  361. unsigned currentUVIndex = 1;
  362. unsigned currentNormalIndex = 1;
  363. bool anythingWritten = false;
  364. // Write the common "I came from X" comment
  365. outputFile->WriteLine("# OBJ file exported from Urho3D");
  366. for (unsigned i = 0; i < drawables.Size(); ++i)
  367. {
  368. Drawable* drawable = drawables[i];
  369. // Only write enabled drawables
  370. if (!drawable->IsEnabledEffective())
  371. continue;
  372. Node* node = drawable->GetNode();
  373. Matrix3x4 transMat = drawable->GetNode()->GetWorldTransform();
  374. Matrix3x4 n = transMat.Inverse();
  375. Matrix3 normalMat = Matrix3(n.m00_, n.m01_, n.m02_, n.m10_, n.m11_, n.m12_, n.m20_, n.m21_, n.m22_);
  376. normalMat = normalMat.Transpose();
  377. const Vector<SourceBatch>& batches = drawable->GetBatches();
  378. for (unsigned geoIndex = 0; geoIndex < batches.Size(); ++geoIndex)
  379. {
  380. Geometry* geo = drawable->GetLodGeometry(geoIndex, 0);
  381. if (geo == nullptr)
  382. continue;
  383. if (geo->GetPrimitiveType() != TRIANGLE_LIST)
  384. {
  385. URHO3D_LOGERRORF("%s (%u) %s (%u) Geometry %u contains an unsupported geometry type %u", node->GetName().Length() > 0 ? node->GetName().CString() : "Node", node->GetID(), drawable->GetTypeName().CString(), drawable->GetID(), geoIndex, (unsigned)geo->GetPrimitiveType());
  386. continue;
  387. }
  388. // If we've reached here than we're going to actually write something to the OBJ file
  389. anythingWritten = true;
  390. const unsigned char* vertexData;
  391. const unsigned char* indexData;
  392. unsigned elementSize, indexSize;
  393. const PODVector<VertexElement>* elements;
  394. geo->GetRawData(vertexData, elementSize, indexData, indexSize, elements);
  395. if (!vertexData || !elements)
  396. continue;
  397. bool hasPosition = VertexBuffer::HasElement(*elements, TYPE_VECTOR3, SEM_POSITION);
  398. if (!hasPosition)
  399. {
  400. URHO3D_LOGERRORF("%s (%u) %s (%u) Geometry %u contains does not have Vector3 type positions in vertex data", node->GetName().Length() > 0 ? node->GetName().CString() : "Node", node->GetID(), drawable->GetTypeName().CString(), drawable->GetID(), geoIndex);
  401. continue;
  402. }
  403. bool hasNormals = VertexBuffer::HasElement(*elements, TYPE_VECTOR3, SEM_NORMAL);
  404. bool hasUV = VertexBuffer::HasElement(*elements, TYPE_VECTOR2, SEM_TEXCOORD, 0);
  405. bool hasLMUV = VertexBuffer::HasElement(*elements, TYPE_VECTOR2, SEM_TEXCOORD, 1);
  406. if (elementSize > 0 && indexSize > 0)
  407. {
  408. unsigned vertexStart = geo->GetVertexStart();
  409. unsigned vertexCount = geo->GetVertexCount();
  410. unsigned indexStart = geo->GetIndexStart();
  411. unsigned indexCount = geo->GetIndexCount();
  412. // Name NodeID DrawableType DrawableID GeometryIndex ("Geo" is included for clarity as StaticModel_32_2 could easily be misinterpreted or even quickly misread as 322)
  413. // Generated object name example: Node_5_StaticModel_32_Geo_0 ... or ... Bob_5_StaticModel_32_Geo_0
  414. outputFile->WriteLine(String("o ").AppendWithFormat("%s_%u_%s_%u_Geo_%u", node->GetName().Length() > 0 ? node->GetName().CString() : "Node", node->GetID(), drawable->GetTypeName().CString(), drawable->GetID(), geoIndex));
  415. // Write vertex position
  416. unsigned positionOffset = VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_POSITION);
  417. for (unsigned j = 0; j < vertexCount; ++j)
  418. {
  419. Vector3 vertexPosition = *((const Vector3*)(&vertexData[(vertexStart + j) * elementSize + positionOffset]));
  420. vertexPosition = transMat * vertexPosition;
  421. // Convert coordinates as requested
  422. if (asRightHanded)
  423. vertexPosition.x_ *= -1;
  424. if (asZUp)
  425. {
  426. float yVal = vertexPosition.y_;
  427. vertexPosition.y_ = vertexPosition.z_;
  428. vertexPosition.z_ = yVal;
  429. }
  430. outputFile->WriteLine("v " + String(vertexPosition));
  431. }
  432. if (hasNormals)
  433. {
  434. unsigned normalOffset = VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_NORMAL);
  435. for (unsigned j = 0; j < vertexCount; ++j)
  436. {
  437. Vector3 vertexNormal = *((const Vector3*)(&vertexData[(vertexStart + j) * elementSize + normalOffset]));
  438. vertexNormal = normalMat * vertexNormal;
  439. vertexNormal.Normalize();
  440. if (asRightHanded)
  441. vertexNormal.x_ *= -1;
  442. if (asZUp)
  443. {
  444. float yVal = vertexNormal.y_;
  445. vertexNormal.y_ = vertexNormal.z_;
  446. vertexNormal.z_ = yVal;
  447. }
  448. outputFile->WriteLine("vn " + String(vertexNormal));
  449. }
  450. }
  451. // Write TEXCOORD1 or TEXCOORD2 if it was chosen
  452. if (hasUV || (hasLMUV && writeLightmapUV))
  453. {
  454. // if writing Lightmap UV is chosen, only use it if TEXCOORD2 exists, otherwise use TEXCOORD1
  455. unsigned texCoordOffset = (writeLightmapUV && hasLMUV) ? VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR2, SEM_TEXCOORD, 1) :
  456. VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR2, SEM_TEXCOORD, 0);
  457. for (unsigned j = 0; j < vertexCount; ++j)
  458. {
  459. Vector2 uvCoords = *((const Vector2*)(&vertexData[(vertexStart + j) * elementSize + texCoordOffset]));
  460. outputFile->WriteLine("vt " + String(uvCoords));
  461. }
  462. }
  463. // If we don't have UV but have normals then must write a double-slash to indicate the absence of UV coords, otherwise use a single slash
  464. const String slashCharacter = hasNormals ? "//" : "/";
  465. // Amount by which to offset indices in the OBJ vs their values in the Urho3D geometry, basically the lowest index value
  466. // Compensates for the above vertex writing which doesn't write ALL vertices, just the used ones
  467. unsigned indexOffset = M_MAX_INT;
  468. for (unsigned indexIdx = indexStart; indexIdx < indexStart + indexCount; indexIdx++)
  469. {
  470. if (indexSize == 2)
  471. indexOffset = Min(indexOffset, (unsigned)*((unsigned short*)(indexData + indexIdx * indexSize)));
  472. else
  473. indexOffset = Min(indexOffset, *((unsigned*)(indexData + indexIdx * indexSize)));
  474. }
  475. for (unsigned indexIdx = indexStart; indexIdx < indexStart + indexCount; indexIdx += 3)
  476. {
  477. // Deal with 16 or 32 bit indices
  478. unsigned longIndices[3];
  479. if (indexSize == 2)
  480. {
  481. //16 bit indices
  482. unsigned short indices[3];
  483. memcpy(indices, indexData + (indexIdx * indexSize), (size_t)indexSize * 3);
  484. longIndices[0] = indices[0] - indexOffset;
  485. longIndices[1] = indices[1] - indexOffset;
  486. longIndices[2] = indices[2] - indexOffset;
  487. }
  488. else
  489. {
  490. //32 bit indices
  491. unsigned indices[3];
  492. memcpy(indices, indexData + (indexIdx * indexSize), (size_t)indexSize * 3);
  493. longIndices[0] = indices[0] - indexOffset;
  494. longIndices[1] = indices[1] - indexOffset;
  495. longIndices[2] = indices[2] - indexOffset;
  496. }
  497. String output = "f ";
  498. if (hasNormals)
  499. {
  500. output.AppendWithFormat("%l/%l/%l %l/%l/%l %l/%l/%l",
  501. currentPositionIndex + longIndices[0],
  502. currentUVIndex + longIndices[0],
  503. currentNormalIndex + longIndices[0],
  504. currentPositionIndex + longIndices[1],
  505. currentUVIndex + longIndices[1],
  506. currentNormalIndex + longIndices[1],
  507. currentPositionIndex + longIndices[2],
  508. currentUVIndex + longIndices[2],
  509. currentNormalIndex + longIndices[2]);
  510. }
  511. else if (hasNormals || hasUV)
  512. {
  513. unsigned secondTraitIndex = hasNormals ? currentNormalIndex : currentUVIndex;
  514. output.AppendWithFormat("%l%s%l %l%s%l %l%s%l",
  515. currentPositionIndex + longIndices[0],
  516. slashCharacter.CString(),
  517. secondTraitIndex + longIndices[0],
  518. currentPositionIndex + longIndices[1],
  519. slashCharacter.CString(),
  520. secondTraitIndex + longIndices[1],
  521. currentPositionIndex + longIndices[2],
  522. slashCharacter.CString(),
  523. secondTraitIndex + longIndices[2]);
  524. }
  525. else
  526. {
  527. output.AppendWithFormat("%l %l %l",
  528. currentPositionIndex + longIndices[0],
  529. currentPositionIndex + longIndices[1],
  530. currentPositionIndex + longIndices[2]);
  531. }
  532. outputFile->WriteLine(output);
  533. }
  534. // Increment our positions based on what vertex attributes we have
  535. currentPositionIndex += vertexCount;
  536. currentNormalIndex += hasNormals ? vertexCount : 0;
  537. // is it possible to have TEXCOORD2 but not have TEXCOORD1, assume anything
  538. currentUVIndex += (hasUV || hasLMUV) ? vertexCount : 0;
  539. }
  540. }
  541. }
  542. return anythingWritten;
  543. }
  544. }