Drawable.cpp 22 KB

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