123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833 |
- // Copyright (C) 2002-2012 Nikolaus Gebhardt
- // This file is part of the "Irrlicht Engine".
- // For conditions of distribution and use, see copyright notice in irrlicht.h
- #include "CMeshManipulator.h"
- #include "SMesh.h"
- #include "CMeshBuffer.h"
- #include "SAnimatedMesh.h"
- #include "os.h"
- #include "irrMap.h"
- namespace irr
- {
- namespace scene
- {
- static inline core::vector3df getAngleWeight(const core::vector3df& v1,
- const core::vector3df& v2,
- const core::vector3df& v3)
- {
- // Calculate this triangle's weight for each of its three vertices
- // start by calculating the lengths of its sides
- const f32 a = v2.getDistanceFromSQ(v3);
- const f32 asqrt = sqrtf(a);
- const f32 b = v1.getDistanceFromSQ(v3);
- const f32 bsqrt = sqrtf(b);
- const f32 c = v1.getDistanceFromSQ(v2);
- const f32 csqrt = sqrtf(c);
- // use them to find the angle at each vertex
- return core::vector3df(
- acosf((b + c - a) / (2.f * bsqrt * csqrt)),
- acosf((-b + c + a) / (2.f * asqrt * csqrt)),
- acosf((b - c + a) / (2.f * bsqrt * asqrt)));
- }
- //! Flips the direction of surfaces. Changes backfacing triangles to frontfacing
- //! triangles and vice versa.
- //! \param mesh: Mesh on which the operation is performed.
- void CMeshManipulator::flipSurfaces(scene::IMesh* mesh) const
- {
- if (!mesh)
- return;
- const u32 bcount = mesh->getMeshBufferCount();
- for (u32 b=0; b<bcount; ++b)
- {
- IMeshBuffer* buffer = mesh->getMeshBuffer(b);
- const u32 idxcnt = buffer->getIndexCount();
- if (buffer->getIndexType() == video::EIT_16BIT)
- {
- u16* idx = buffer->getIndices();
- for (u32 i=0; i<idxcnt; i+=3)
- {
- const u16 tmp = idx[i+1];
- idx[i+1] = idx[i+2];
- idx[i+2] = tmp;
- }
- }
- else
- {
- u32* idx = reinterpret_cast<u32*>(buffer->getIndices());
- for (u32 i=0; i<idxcnt; i+=3)
- {
- const u32 tmp = idx[i+1];
- idx[i+1] = idx[i+2];
- idx[i+2] = tmp;
- }
- }
- }
- }
- namespace
- {
- template <typename T>
- void recalculateNormalsT(IMeshBuffer* buffer, bool smooth, bool angleWeighted)
- {
- const u32 vtxcnt = buffer->getVertexCount();
- const u32 idxcnt = buffer->getIndexCount();
- const T* idx = reinterpret_cast<T*>(buffer->getIndices());
- if (!smooth)
- {
- for (u32 i=0; i<idxcnt; i+=3)
- {
- const core::vector3df& v1 = buffer->getPosition(idx[i+0]);
- const core::vector3df& v2 = buffer->getPosition(idx[i+1]);
- const core::vector3df& v3 = buffer->getPosition(idx[i+2]);
- const core::vector3df normal = core::plane3d<f32>(v1, v2, v3).Normal;
- buffer->getNormal(idx[i+0]) = normal;
- buffer->getNormal(idx[i+1]) = normal;
- buffer->getNormal(idx[i+2]) = normal;
- }
- }
- else
- {
- u32 i;
- for ( i = 0; i!= vtxcnt; ++i )
- buffer->getNormal(i).set(0.f, 0.f, 0.f);
- for ( i=0; i<idxcnt; i+=3)
- {
- const core::vector3df& v1 = buffer->getPosition(idx[i+0]);
- const core::vector3df& v2 = buffer->getPosition(idx[i+1]);
- const core::vector3df& v3 = buffer->getPosition(idx[i+2]);
- const core::vector3df normal = core::plane3d<f32>(v1, v2, v3).Normal;
- core::vector3df weight(1.f,1.f,1.f);
- if (angleWeighted)
- weight = getAngleWeight(v1,v2,v3);
- buffer->getNormal(idx[i+0]) += weight.X*normal;
- buffer->getNormal(idx[i+1]) += weight.Y*normal;
- buffer->getNormal(idx[i+2]) += weight.Z*normal;
- }
- for ( i = 0; i!= vtxcnt; ++i )
- buffer->getNormal(i).normalize();
- }
- }
- }
- //! Recalculates all normals of the mesh buffer.
- /** \param buffer: Mesh buffer on which the operation is performed. */
- void CMeshManipulator::recalculateNormals(IMeshBuffer* buffer, bool smooth, bool angleWeighted) const
- {
- if (!buffer)
- return;
- if (buffer->getIndexType()==video::EIT_16BIT)
- recalculateNormalsT<u16>(buffer, smooth, angleWeighted);
- else
- recalculateNormalsT<u32>(buffer, smooth, angleWeighted);
- }
- //! Recalculates all normals of the mesh.
- //! \param mesh: Mesh on which the operation is performed.
- void CMeshManipulator::recalculateNormals(scene::IMesh* mesh, bool smooth, bool angleWeighted) const
- {
- if (!mesh)
- return;
- const u32 bcount = mesh->getMeshBufferCount();
- for ( u32 b=0; b<bcount; ++b)
- recalculateNormals(mesh->getMeshBuffer(b), smooth, angleWeighted);
- }
- namespace
- {
- void calculateTangents(
- core::vector3df& normal,
- core::vector3df& tangent,
- core::vector3df& binormal,
- const core::vector3df& vt1, const core::vector3df& vt2, const core::vector3df& vt3, // vertices
- const core::vector2df& tc1, const core::vector2df& tc2, const core::vector2df& tc3) // texture coords
- {
- // choose one of them:
- //#define USE_NVIDIA_GLH_VERSION // use version used by nvidia in glh headers
- #define USE_IRR_VERSION
- #ifdef USE_IRR_VERSION
- core::vector3df v1 = vt1 - vt2;
- core::vector3df v2 = vt3 - vt1;
- normal = v2.crossProduct(v1);
- normal.normalize();
- // binormal
- f32 deltaX1 = tc1.X - tc2.X;
- f32 deltaX2 = tc3.X - tc1.X;
- binormal = (v1 * deltaX2) - (v2 * deltaX1);
- binormal.normalize();
- // tangent
- f32 deltaY1 = tc1.Y - tc2.Y;
- f32 deltaY2 = tc3.Y - tc1.Y;
- tangent = (v1 * deltaY2) - (v2 * deltaY1);
- tangent.normalize();
- // adjust
- core::vector3df txb = tangent.crossProduct(binormal);
- if (txb.dotProduct(normal) < 0.0f)
- {
- tangent *= -1.0f;
- binormal *= -1.0f;
- }
- #endif // USE_IRR_VERSION
- #ifdef USE_NVIDIA_GLH_VERSION
- tangent.set(0,0,0);
- binormal.set(0,0,0);
- core::vector3df v1(vt2.X - vt1.X, tc2.X - tc1.X, tc2.Y - tc1.Y);
- core::vector3df v2(vt3.X - vt1.X, tc3.X - tc1.X, tc3.Y - tc1.Y);
- core::vector3df txb = v1.crossProduct(v2);
- if ( !core::iszero ( txb.X ) )
- {
- tangent.X = -txb.Y / txb.X;
- binormal.X = -txb.Z / txb.X;
- }
- v1.X = vt2.Y - vt1.Y;
- v2.X = vt3.Y - vt1.Y;
- txb = v1.crossProduct(v2);
- if ( !core::iszero ( txb.X ) )
- {
- tangent.Y = -txb.Y / txb.X;
- binormal.Y = -txb.Z / txb.X;
- }
- v1.X = vt2.Z - vt1.Z;
- v2.X = vt3.Z - vt1.Z;
- txb = v1.crossProduct(v2);
- if ( !core::iszero ( txb.X ) )
- {
- tangent.Z = -txb.Y / txb.X;
- binormal.Z = -txb.Z / txb.X;
- }
- tangent.normalize();
- binormal.normalize();
- normal = tangent.crossProduct(binormal);
- normal.normalize();
- binormal = tangent.crossProduct(normal);
- binormal.normalize();
- core::plane3d<f32> pl(vt1, vt2, vt3);
- if(normal.dotProduct(pl.Normal) < 0.0f )
- normal *= -1.0f;
- #endif // USE_NVIDIA_GLH_VERSION
- }
- //! Recalculates tangents for a tangent mesh buffer
- template <typename T>
- void recalculateTangentsT(IMeshBuffer* buffer, bool recalculateNormals, bool smooth, bool angleWeighted)
- {
- if (!buffer || (buffer->getVertexType()!= video::EVT_TANGENTS))
- return;
- const u32 vtxCnt = buffer->getVertexCount();
- const u32 idxCnt = buffer->getIndexCount();
- T* idx = reinterpret_cast<T*>(buffer->getIndices());
- video::S3DVertexTangents* v =
- (video::S3DVertexTangents*)buffer->getVertices();
- if (smooth)
- {
- u32 i;
- for ( i = 0; i!= vtxCnt; ++i )
- {
- if (recalculateNormals)
- v[i].Normal.set( 0.f, 0.f, 0.f );
- v[i].Tangent.set( 0.f, 0.f, 0.f );
- v[i].Binormal.set( 0.f, 0.f, 0.f );
- }
- //Each vertex gets the sum of the tangents and binormals from the faces around it
- for ( i=0; i<idxCnt; i+=3)
- {
- // if this triangle is degenerate, skip it!
- if (v[idx[i+0]].Pos == v[idx[i+1]].Pos ||
- v[idx[i+0]].Pos == v[idx[i+2]].Pos ||
- v[idx[i+1]].Pos == v[idx[i+2]].Pos
- /*||
- v[idx[i+0]].TCoords == v[idx[i+1]].TCoords ||
- v[idx[i+0]].TCoords == v[idx[i+2]].TCoords ||
- v[idx[i+1]].TCoords == v[idx[i+2]].TCoords */
- )
- continue;
- //Angle-weighted normals look better, but are slightly more CPU intensive to calculate
- core::vector3df weight(1.f,1.f,1.f);
- if (angleWeighted)
- weight = getAngleWeight(v[i+0].Pos,v[i+1].Pos,v[i+2].Pos);
- core::vector3df localNormal;
- core::vector3df localTangent;
- core::vector3df localBinormal;
- calculateTangents(
- localNormal,
- localTangent,
- localBinormal,
- v[idx[i+0]].Pos,
- v[idx[i+1]].Pos,
- v[idx[i+2]].Pos,
- v[idx[i+0]].TCoords,
- v[idx[i+1]].TCoords,
- v[idx[i+2]].TCoords);
- if (recalculateNormals)
- v[idx[i+0]].Normal += localNormal * weight.X;
- v[idx[i+0]].Tangent += localTangent * weight.X;
- v[idx[i+0]].Binormal += localBinormal * weight.X;
-
- calculateTangents(
- localNormal,
- localTangent,
- localBinormal,
- v[idx[i+1]].Pos,
- v[idx[i+2]].Pos,
- v[idx[i+0]].Pos,
- v[idx[i+1]].TCoords,
- v[idx[i+2]].TCoords,
- v[idx[i+0]].TCoords);
- if (recalculateNormals)
- v[idx[i+1]].Normal += localNormal * weight.Y;
- v[idx[i+1]].Tangent += localTangent * weight.Y;
- v[idx[i+1]].Binormal += localBinormal * weight.Y;
- calculateTangents(
- localNormal,
- localTangent,
- localBinormal,
- v[idx[i+2]].Pos,
- v[idx[i+0]].Pos,
- v[idx[i+1]].Pos,
- v[idx[i+2]].TCoords,
- v[idx[i+0]].TCoords,
- v[idx[i+1]].TCoords);
- if (recalculateNormals)
- v[idx[i+2]].Normal += localNormal * weight.Z;
- v[idx[i+2]].Tangent += localTangent * weight.Z;
- v[idx[i+2]].Binormal += localBinormal * weight.Z;
- }
- // Normalize the tangents and binormals
- if (recalculateNormals)
- {
- for ( i = 0; i!= vtxCnt; ++i )
- v[i].Normal.normalize();
- }
- for ( i = 0; i!= vtxCnt; ++i )
- {
- v[i].Tangent.normalize();
- v[i].Binormal.normalize();
- }
- }
- else
- {
- core::vector3df localNormal;
- for (u32 i=0; i<idxCnt; i+=3)
- {
- calculateTangents(
- localNormal,
- v[idx[i+0]].Tangent,
- v[idx[i+0]].Binormal,
- v[idx[i+0]].Pos,
- v[idx[i+1]].Pos,
- v[idx[i+2]].Pos,
- v[idx[i+0]].TCoords,
- v[idx[i+1]].TCoords,
- v[idx[i+2]].TCoords);
- if (recalculateNormals)
- v[idx[i+0]].Normal=localNormal;
- calculateTangents(
- localNormal,
- v[idx[i+1]].Tangent,
- v[idx[i+1]].Binormal,
- v[idx[i+1]].Pos,
- v[idx[i+2]].Pos,
- v[idx[i+0]].Pos,
- v[idx[i+1]].TCoords,
- v[idx[i+2]].TCoords,
- v[idx[i+0]].TCoords);
- if (recalculateNormals)
- v[idx[i+1]].Normal=localNormal;
- calculateTangents(
- localNormal,
- v[idx[i+2]].Tangent,
- v[idx[i+2]].Binormal,
- v[idx[i+2]].Pos,
- v[idx[i+0]].Pos,
- v[idx[i+1]].Pos,
- v[idx[i+2]].TCoords,
- v[idx[i+0]].TCoords,
- v[idx[i+1]].TCoords);
- if (recalculateNormals)
- v[idx[i+2]].Normal=localNormal;
- }
- }
- }
- }
- //! Recalculates tangents for a tangent mesh buffer
- void CMeshManipulator::recalculateTangents(IMeshBuffer* buffer, bool recalculateNormals, bool smooth, bool angleWeighted) const
- {
- if (buffer && (buffer->getVertexType() == video::EVT_TANGENTS))
- {
- if (buffer->getIndexType() == video::EIT_16BIT)
- recalculateTangentsT<u16>(buffer, recalculateNormals, smooth, angleWeighted);
- else
- recalculateTangentsT<u32>(buffer, recalculateNormals, smooth, angleWeighted);
- }
- }
- //! Recalculates tangents for all tangent mesh buffers
- void CMeshManipulator::recalculateTangents(IMesh* mesh, bool recalculateNormals, bool smooth, bool angleWeighted) const
- {
- if (!mesh)
- return;
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for (u32 b=0; b<meshBufferCount; ++b)
- {
- recalculateTangents(mesh->getMeshBuffer(b), recalculateNormals, smooth, angleWeighted);
- }
- }
- namespace
- {
- //! Creates a planar texture mapping on the meshbuffer
- template<typename T>
- void makePlanarTextureMappingT(scene::IMeshBuffer* buffer, f32 resolution)
- {
- u32 idxcnt = buffer->getIndexCount();
- T* idx = reinterpret_cast<T*>(buffer->getIndices());
- for (u32 i=0; i<idxcnt; i+=3)
- {
- core::plane3df p(buffer->getPosition(idx[i+0]), buffer->getPosition(idx[i+1]), buffer->getPosition(idx[i+2]));
- p.Normal.X = fabsf(p.Normal.X);
- p.Normal.Y = fabsf(p.Normal.Y);
- p.Normal.Z = fabsf(p.Normal.Z);
- // calculate planar mapping worldspace coordinates
- if (p.Normal.X > p.Normal.Y && p.Normal.X > p.Normal.Z)
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = buffer->getPosition(idx[i+o]).Y * resolution;
- buffer->getTCoords(idx[i+o]).Y = buffer->getPosition(idx[i+o]).Z * resolution;
- }
- }
- else
- if (p.Normal.Y > p.Normal.X && p.Normal.Y > p.Normal.Z)
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = buffer->getPosition(idx[i+o]).X * resolution;
- buffer->getTCoords(idx[i+o]).Y = buffer->getPosition(idx[i+o]).Z * resolution;
- }
- }
- else
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = buffer->getPosition(idx[i+o]).X * resolution;
- buffer->getTCoords(idx[i+o]).Y = buffer->getPosition(idx[i+o]).Y * resolution;
- }
- }
- }
- }
- }
- //! Creates a planar texture mapping on the meshbuffer
- void CMeshManipulator::makePlanarTextureMapping(scene::IMeshBuffer* buffer, f32 resolution) const
- {
- if (!buffer)
- return;
- if (buffer->getIndexType()==video::EIT_16BIT)
- makePlanarTextureMappingT<u16>(buffer, resolution);
- else
- makePlanarTextureMappingT<u32>(buffer, resolution);
- }
- //! Creates a planar texture mapping on the mesh
- void CMeshManipulator::makePlanarTextureMapping(scene::IMesh* mesh, f32 resolution) const
- {
- if (!mesh)
- return;
- const u32 bcount = mesh->getMeshBufferCount();
- for ( u32 b=0; b<bcount; ++b)
- {
- makePlanarTextureMapping(mesh->getMeshBuffer(b), resolution);
- }
- }
- namespace
- {
- //! Creates a planar texture mapping on the meshbuffer
- template <typename T>
- void makePlanarTextureMappingT(scene::IMeshBuffer* buffer, f32 resolutionS, f32 resolutionT, u8 axis, const core::vector3df& offset)
- {
- u32 idxcnt = buffer->getIndexCount();
- T* idx = reinterpret_cast<T*>(buffer->getIndices());
- for (u32 i=0; i<idxcnt; i+=3)
- {
- // calculate planar mapping worldspace coordinates
- if (axis==0)
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = 0.5f+(buffer->getPosition(idx[i+o]).Z + offset.Z) * resolutionS;
- buffer->getTCoords(idx[i+o]).Y = 0.5f-(buffer->getPosition(idx[i+o]).Y + offset.Y) * resolutionT;
- }
- }
- else if (axis==1)
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = 0.5f+(buffer->getPosition(idx[i+o]).X + offset.X) * resolutionS;
- buffer->getTCoords(idx[i+o]).Y = 1.f-(buffer->getPosition(idx[i+o]).Z + offset.Z) * resolutionT;
- }
- }
- else if (axis==2)
- {
- for (u32 o=0; o!=3; ++o)
- {
- buffer->getTCoords(idx[i+o]).X = 0.5f+(buffer->getPosition(idx[i+o]).X + offset.X) * resolutionS;
- buffer->getTCoords(idx[i+o]).Y = 0.5f-(buffer->getPosition(idx[i+o]).Y + offset.Y) * resolutionT;
- }
- }
- }
- }
- }
- //! Creates a planar texture mapping on the meshbuffer
- void CMeshManipulator::makePlanarTextureMapping(scene::IMeshBuffer* buffer, f32 resolutionS, f32 resolutionT, u8 axis, const core::vector3df& offset) const
- {
- if (!buffer)
- return;
- if (buffer->getIndexType()==video::EIT_16BIT)
- makePlanarTextureMappingT<u16>(buffer, resolutionS, resolutionT, axis, offset);
- else
- makePlanarTextureMappingT<u32>(buffer, resolutionS, resolutionT, axis, offset);
- }
- //! Creates a planar texture mapping on the mesh
- void CMeshManipulator::makePlanarTextureMapping(scene::IMesh* mesh, f32 resolutionS, f32 resolutionT, u8 axis, const core::vector3df& offset) const
- {
- if (!mesh)
- return;
- const u32 bcount = mesh->getMeshBufferCount();
- for ( u32 b=0; b<bcount; ++b)
- {
- makePlanarTextureMapping(mesh->getMeshBuffer(b), resolutionS, resolutionT, axis, offset);
- }
- }
- //! Clones a static IMesh into a modifyable SMesh.
- // not yet 32bit
- SMesh* CMeshManipulator::createMeshCopy(scene::IMesh* mesh) const
- {
- if (!mesh)
- return 0;
- SMesh* clone = new SMesh();
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for ( u32 b=0; b<meshBufferCount; ++b)
- {
- const IMeshBuffer* const mb = mesh->getMeshBuffer(b);
- switch(mb->getVertexType())
- {
- case video::EVT_STANDARD:
- {
- SMeshBuffer* buffer = new SMeshBuffer();
- buffer->Material = mb->getMaterial();
- const u32 vcount = mb->getVertexCount();
- buffer->Vertices.reallocate(vcount);
- video::S3DVertex* vertices = (video::S3DVertex*)mb->getVertices();
- for (u32 i=0; i < vcount; ++i)
- buffer->Vertices.push_back(vertices[i]);
- const u32 icount = mb->getIndexCount();
- buffer->Indices.reallocate(icount);
- const u16* indices = mb->getIndices();
- for (u32 i=0; i < icount; ++i)
- buffer->Indices.push_back(indices[i]);
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- case video::EVT_2TCOORDS:
- {
- SMeshBufferLightMap* buffer = new SMeshBufferLightMap();
- buffer->Material = mb->getMaterial();
- const u32 vcount = mb->getVertexCount();
- buffer->Vertices.reallocate(vcount);
- video::S3DVertex2TCoords* vertices = (video::S3DVertex2TCoords*)mb->getVertices();
- for (u32 i=0; i < vcount; ++i)
- buffer->Vertices.push_back(vertices[i]);
- const u32 icount = mb->getIndexCount();
- buffer->Indices.reallocate(icount);
- const u16* indices = mb->getIndices();
- for (u32 i=0; i < icount; ++i)
- buffer->Indices.push_back(indices[i]);
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- case video::EVT_TANGENTS:
- {
- SMeshBufferTangents* buffer = new SMeshBufferTangents();
- buffer->Material = mb->getMaterial();
- const u32 vcount = mb->getVertexCount();
- buffer->Vertices.reallocate(vcount);
- video::S3DVertexTangents* vertices = (video::S3DVertexTangents*)mb->getVertices();
- for (u32 i=0; i < vcount; ++i)
- buffer->Vertices.push_back(vertices[i]);
- const u32 icount = mb->getIndexCount();
- buffer->Indices.reallocate(icount);
- const u16* indices = mb->getIndices();
- for (u32 i=0; i < icount; ++i)
- buffer->Indices.push_back(indices[i]);
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- default:
- break;
- }// end switch
- }// end for all mesh buffers
- clone->BoundingBox = mesh->getBoundingBox();
- return clone;
- }
- //! Creates a copy of the mesh, which will only consist of unique primitives
- // not yet 32bit
- IMesh* CMeshManipulator::createMeshUniquePrimitives(IMesh* mesh) const
- {
- if (!mesh)
- return 0;
- SMesh* clone = new SMesh();
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for ( u32 b=0; b<meshBufferCount; ++b)
- {
- const IMeshBuffer* const mb = mesh->getMeshBuffer(b);
- const s32 idxCnt = mb->getIndexCount();
- const u16* idx = mb->getIndices();
- switch(mb->getVertexType())
- {
- case video::EVT_STANDARD:
- {
- SMeshBuffer* buffer = new SMeshBuffer();
- buffer->Material = mb->getMaterial();
- video::S3DVertex* v =
- (video::S3DVertex*)mb->getVertices();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- for (s32 i=0; i<idxCnt; i += 3)
- {
- buffer->Vertices.push_back( v[idx[i + 0 ]] );
- buffer->Vertices.push_back( v[idx[i + 1 ]] );
- buffer->Vertices.push_back( v[idx[i + 2 ]] );
- buffer->Indices.push_back( i + 0 );
- buffer->Indices.push_back( i + 1 );
- buffer->Indices.push_back( i + 2 );
- }
- buffer->setBoundingBox(mb->getBoundingBox());
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- case video::EVT_2TCOORDS:
- {
- SMeshBufferLightMap* buffer = new SMeshBufferLightMap();
- buffer->Material = mb->getMaterial();
- video::S3DVertex2TCoords* v =
- (video::S3DVertex2TCoords*)mb->getVertices();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- for (s32 i=0; i<idxCnt; i += 3)
- {
- buffer->Vertices.push_back( v[idx[i + 0 ]] );
- buffer->Vertices.push_back( v[idx[i + 1 ]] );
- buffer->Vertices.push_back( v[idx[i + 2 ]] );
- buffer->Indices.push_back( i + 0 );
- buffer->Indices.push_back( i + 1 );
- buffer->Indices.push_back( i + 2 );
- }
- buffer->setBoundingBox(mb->getBoundingBox());
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- case video::EVT_TANGENTS:
- {
- SMeshBufferTangents* buffer = new SMeshBufferTangents();
- buffer->Material = mb->getMaterial();
- video::S3DVertexTangents* v =
- (video::S3DVertexTangents*)mb->getVertices();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- for (s32 i=0; i<idxCnt; i += 3)
- {
- buffer->Vertices.push_back( v[idx[i + 0 ]] );
- buffer->Vertices.push_back( v[idx[i + 1 ]] );
- buffer->Vertices.push_back( v[idx[i + 2 ]] );
- buffer->Indices.push_back( i + 0 );
- buffer->Indices.push_back( i + 1 );
- buffer->Indices.push_back( i + 2 );
- }
- buffer->setBoundingBox(mb->getBoundingBox());
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- break;
- default:
- break;
- }// end switch
- }// end for all mesh buffers
- clone->BoundingBox = mesh->getBoundingBox();
- return clone;
- }
- //! Creates a copy of a mesh, which will have identical vertices welded together
- // not yet 32bit
- IMesh* CMeshManipulator::createMeshWelded(IMesh *mesh, f32 tolerance) const
- {
- SMesh* clone = new SMesh();
- clone->BoundingBox = mesh->getBoundingBox();
- core::array<u16> redirects;
- for (u32 b=0; b<mesh->getMeshBufferCount(); ++b)
- {
- const IMeshBuffer* const mb = mesh->getMeshBuffer(b);
- // reset redirect list
- redirects.set_used(mb->getVertexCount());
- const u16* indices = 0;
- u32 indexCount = 0;
- core::array<u16>* outIdx = 0;
- switch(mb->getVertexType())
- {
- case video::EVT_STANDARD:
- {
- SMeshBuffer* buffer = new SMeshBuffer();
- buffer->BoundingBox = mb->getBoundingBox();
- buffer->Material = mb->getMaterial();
- clone->addMeshBuffer(buffer);
- buffer->drop();
- video::S3DVertex* v =
- (video::S3DVertex*)mb->getVertices();
- u32 vertexCount = mb->getVertexCount();
- indices = mb->getIndices();
- indexCount = mb->getIndexCount();
- outIdx = &buffer->Indices;
- buffer->Vertices.reallocate(vertexCount);
- for (u32 i=0; i < vertexCount; ++i)
- {
- bool found = false;
- for (u32 j=0; j < i; ++j)
- {
- if ( v[i].Pos.equals( v[j].Pos, tolerance) &&
- v[i].Normal.equals( v[j].Normal, tolerance) &&
- v[i].TCoords.equals( v[j].TCoords ) &&
- (v[i].Color == v[j].Color) )
- {
- redirects[i] = redirects[j];
- found = true;
- break;
- }
- }
- if (!found)
- {
- redirects[i] = buffer->Vertices.size();
- buffer->Vertices.push_back(v[i]);
- }
- }
-
- break;
- }
- case video::EVT_2TCOORDS:
- {
- SMeshBufferLightMap* buffer = new SMeshBufferLightMap();
- buffer->BoundingBox = mb->getBoundingBox();
- buffer->Material = mb->getMaterial();
- clone->addMeshBuffer(buffer);
- buffer->drop();
- video::S3DVertex2TCoords* v =
- (video::S3DVertex2TCoords*)mb->getVertices();
- u32 vertexCount = mb->getVertexCount();
- indices = mb->getIndices();
- indexCount = mb->getIndexCount();
- outIdx = &buffer->Indices;
- buffer->Vertices.reallocate(vertexCount);
- for (u32 i=0; i < vertexCount; ++i)
- {
- bool found = false;
- for (u32 j=0; j < i; ++j)
- {
- if ( v[i].Pos.equals( v[j].Pos, tolerance) &&
- v[i].Normal.equals( v[j].Normal, tolerance) &&
- v[i].TCoords.equals( v[j].TCoords ) &&
- v[i].TCoords2.equals( v[j].TCoords2 ) &&
- (v[i].Color == v[j].Color) )
- {
- redirects[i] = redirects[j];
- found = true;
- break;
- }
- }
- if (!found)
- {
- redirects[i] = buffer->Vertices.size();
- buffer->Vertices.push_back(v[i]);
- }
- }
- break;
- }
- case video::EVT_TANGENTS:
- {
- SMeshBufferTangents* buffer = new SMeshBufferTangents();
- buffer->BoundingBox = mb->getBoundingBox();
- buffer->Material = mb->getMaterial();
- clone->addMeshBuffer(buffer);
- buffer->drop();
- video::S3DVertexTangents* v =
- (video::S3DVertexTangents*)mb->getVertices();
- u32 vertexCount = mb->getVertexCount();
- indices = mb->getIndices();
- indexCount = mb->getIndexCount();
- outIdx = &buffer->Indices;
- buffer->Vertices.reallocate(vertexCount);
- for (u32 i=0; i < vertexCount; ++i)
- {
- bool found = false;
- for (u32 j=0; j < i; ++j)
- {
- if ( v[i].Pos.equals( v[j].Pos, tolerance) &&
- v[i].Normal.equals( v[j].Normal, tolerance) &&
- v[i].TCoords.equals( v[j].TCoords ) &&
- v[i].Tangent.equals( v[j].Tangent, tolerance ) &&
- v[i].Binormal.equals( v[j].Binormal, tolerance ) &&
- (v[i].Color == v[j].Color) )
- {
- redirects[i] = redirects[j];
- found = true;
- break;
- }
- }
- if (!found)
- {
- redirects[i] = buffer->Vertices.size();
- buffer->Vertices.push_back(v[i]);
- }
- }
- break;
- }
- default:
- os::Printer::log("Cannot create welded mesh, vertex type unsupported", ELL_ERROR);
- break;
- }
- // write the buffer's index list
- core::array<u16> &Indices = *outIdx;
- Indices.set_used(indexCount);
- for (u32 i=0; i<indexCount; ++i)
- {
- Indices[i] = redirects[ indices[i] ];
- }
- }
- return clone;
- }
- //! Creates a copy of the mesh, which will only consist of S3DVertexTangents vertices.
- // not yet 32bit
- IMesh* CMeshManipulator::createMeshWithTangents(IMesh* mesh, bool recalculateNormals, bool smooth, bool angleWeighted, bool calculateTangents) const
- {
- if (!mesh)
- return 0;
- // copy mesh and fill data into SMeshBufferTangents
- SMesh* clone = new SMesh();
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for (u32 b=0; b<meshBufferCount; ++b)
- {
- const IMeshBuffer* const original = mesh->getMeshBuffer(b);
- const u32 idxCnt = original->getIndexCount();
- const u16* idx = original->getIndices();
- SMeshBufferTangents* buffer = new SMeshBufferTangents();
- buffer->Material = original->getMaterial();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- core::map<video::S3DVertexTangents, int> vertMap;
- int vertLocation;
- // copy vertices
- const video::E_VERTEX_TYPE vType = original->getVertexType();
- video::S3DVertexTangents vNew;
- for (u32 i=0; i<idxCnt; ++i)
- {
- switch(vType)
- {
- case video::EVT_STANDARD:
- {
- const video::S3DVertex* v =
- (const video::S3DVertex*)original->getVertices();
- vNew = video::S3DVertexTangents(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords);
- }
- break;
- case video::EVT_2TCOORDS:
- {
- const video::S3DVertex2TCoords* v =
- (const video::S3DVertex2TCoords*)original->getVertices();
- vNew = video::S3DVertexTangents(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords);
- }
- break;
- case video::EVT_TANGENTS:
- {
- const video::S3DVertexTangents* v =
- (const video::S3DVertexTangents*)original->getVertices();
- vNew = v[idx[i]];
- }
- break;
- default:
- break;
- }
- core::map<video::S3DVertexTangents, int>::Node* n = vertMap.find(vNew);
- if (n)
- {
- vertLocation = n->getValue();
- }
- else
- {
- vertLocation = buffer->Vertices.size();
- buffer->Vertices.push_back(vNew);
- vertMap.insert(vNew, vertLocation);
- }
- // create new indices
- buffer->Indices.push_back(vertLocation);
- }
- buffer->recalculateBoundingBox();
- // add new buffer
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- clone->recalculateBoundingBox();
- if (calculateTangents)
- recalculateTangents(clone, recalculateNormals, smooth, angleWeighted);
- return clone;
- }
- //! Creates a copy of the mesh, which will only consist of S3DVertex2TCoords vertices.
- // not yet 32bit
- IMesh* CMeshManipulator::createMeshWith2TCoords(IMesh* mesh) const
- {
- if (!mesh)
- return 0;
- // copy mesh and fill data into SMeshBufferLightMap
- SMesh* clone = new SMesh();
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for (u32 b=0; b<meshBufferCount; ++b)
- {
- const IMeshBuffer* const original = mesh->getMeshBuffer(b);
- const u32 idxCnt = original->getIndexCount();
- const u16* idx = original->getIndices();
- SMeshBufferLightMap* buffer = new SMeshBufferLightMap();
- buffer->Material = original->getMaterial();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- core::map<video::S3DVertex2TCoords, int> vertMap;
- int vertLocation;
- // copy vertices
- const video::E_VERTEX_TYPE vType = original->getVertexType();
- video::S3DVertex2TCoords vNew;
- for (u32 i=0; i<idxCnt; ++i)
- {
- switch(vType)
- {
- case video::EVT_STANDARD:
- {
- const video::S3DVertex* v =
- (const video::S3DVertex*)original->getVertices();
- vNew = video::S3DVertex2TCoords(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords, v[idx[i]].TCoords);
- }
- break;
- case video::EVT_2TCOORDS:
- {
- const video::S3DVertex2TCoords* v =
- (const video::S3DVertex2TCoords*)original->getVertices();
- vNew = v[idx[i]];
- }
- break;
- case video::EVT_TANGENTS:
- {
- const video::S3DVertexTangents* v =
- (const video::S3DVertexTangents*)original->getVertices();
- vNew = video::S3DVertex2TCoords(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords, v[idx[i]].TCoords);
- }
- break;
- default:
- break;
- }
- core::map<video::S3DVertex2TCoords, int>::Node* n = vertMap.find(vNew);
- if (n)
- {
- vertLocation = n->getValue();
- }
- else
- {
- vertLocation = buffer->Vertices.size();
- buffer->Vertices.push_back(vNew);
- vertMap.insert(vNew, vertLocation);
- }
- // create new indices
- buffer->Indices.push_back(vertLocation);
- }
- buffer->recalculateBoundingBox();
- // add new buffer
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- clone->recalculateBoundingBox();
- return clone;
- }
- //! Creates a copy of the mesh, which will only consist of S3DVertex vertices.
- // not yet 32bit
- IMesh* CMeshManipulator::createMeshWith1TCoords(IMesh* mesh) const
- {
- if (!mesh)
- return 0;
- // copy mesh and fill data into SMeshBuffer
- SMesh* clone = new SMesh();
- const u32 meshBufferCount = mesh->getMeshBufferCount();
- for (u32 b=0; b<meshBufferCount; ++b)
- {
- IMeshBuffer* original = mesh->getMeshBuffer(b);
- const u32 idxCnt = original->getIndexCount();
- const u16* idx = original->getIndices();
- SMeshBuffer* buffer = new SMeshBuffer();
- buffer->Material = original->getMaterial();
- buffer->Vertices.reallocate(idxCnt);
- buffer->Indices.reallocate(idxCnt);
- core::map<video::S3DVertex, int> vertMap;
- int vertLocation;
- // copy vertices
- const video::E_VERTEX_TYPE vType = original->getVertexType();
- video::S3DVertex vNew;
- for (u32 i=0; i<idxCnt; ++i)
- {
- switch(vType)
- {
- case video::EVT_STANDARD:
- {
- video::S3DVertex* v =
- (video::S3DVertex*)original->getVertices();
- vNew = v[idx[i]];
- }
- break;
- case video::EVT_2TCOORDS:
- {
- video::S3DVertex2TCoords* v =
- (video::S3DVertex2TCoords*)original->getVertices();
- vNew = video::S3DVertex(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords);
- }
- break;
- case video::EVT_TANGENTS:
- {
- video::S3DVertexTangents* v =
- (video::S3DVertexTangents*)original->getVertices();
- vNew = video::S3DVertex(
- v[idx[i]].Pos, v[idx[i]].Normal, v[idx[i]].Color, v[idx[i]].TCoords);
- }
- break;
- default:
- break;
- }
- core::map<video::S3DVertex, int>::Node* n = vertMap.find(vNew);
- if (n)
- {
- vertLocation = n->getValue();
- }
- else
- {
- vertLocation = buffer->Vertices.size();
- buffer->Vertices.push_back(vNew);
- vertMap.insert(vNew, vertLocation);
- }
- // create new indices
- buffer->Indices.push_back(vertLocation);
- }
- buffer->recalculateBoundingBox();
- // add new buffer
- clone->addMeshBuffer(buffer);
- buffer->drop();
- }
- clone->recalculateBoundingBox();
- return clone;
- }
- //! Returns amount of polygons in mesh.
- s32 CMeshManipulator::getPolyCount(scene::IMesh* mesh) const
- {
- if (!mesh)
- return 0;
- s32 trianglecount = 0;
- for (u32 g=0; g<mesh->getMeshBufferCount(); ++g)
- trianglecount += mesh->getMeshBuffer(g)->getIndexCount() / 3;
- return trianglecount;
- }
- //! Returns amount of polygons in mesh.
- s32 CMeshManipulator::getPolyCount(scene::IAnimatedMesh* mesh) const
- {
- if (mesh && mesh->getFrameCount() != 0)
- return getPolyCount(mesh->getMesh(0));
- return 0;
- }
- //! create a new AnimatedMesh and adds the mesh to it
- IAnimatedMesh * CMeshManipulator::createAnimatedMesh(scene::IMesh* mesh, scene::E_ANIMATED_MESH_TYPE type) const
- {
- return new SAnimatedMesh(mesh, type);
- }
- namespace
- {
- struct vcache
- {
- core::array<u32> tris;
- float score;
- s16 cachepos;
- u16 NumActiveTris;
- };
- struct tcache
- {
- u16 ind[3];
- float score;
- bool drawn;
- };
- const u16 cachesize = 32;
- float FindVertexScore(vcache *v)
- {
- const float CacheDecayPower = 1.5f;
- const float LastTriScore = 0.75f;
- const float ValenceBoostScale = 2.0f;
- const float ValenceBoostPower = 0.5f;
- const float MaxSizeVertexCache = 32.0f;
- if (v->NumActiveTris == 0)
- {
- // No tri needs this vertex!
- return -1.0f;
- }
- float Score = 0.0f;
- int CachePosition = v->cachepos;
- if (CachePosition < 0)
- {
- // Vertex is not in FIFO cache - no score.
- }
- else
- {
- if (CachePosition < 3)
- {
- // This vertex was used in the last triangle,
- // so it has a fixed score.
- Score = LastTriScore;
- }
- else
- {
- // Points for being high in the cache.
- const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
- Score = 1.0f - (CachePosition - 3) * Scaler;
- Score = powf(Score, CacheDecayPower);
- }
- }
- // Bonus points for having a low number of tris still to
- // use the vert, so we get rid of lone verts quickly.
- float ValenceBoost = powf(v->NumActiveTris,
- -ValenceBoostPower);
- Score += ValenceBoostScale * ValenceBoost;
- return Score;
- }
- /*
- A specialized LRU cache for the Forsyth algorithm.
- */
- class f_lru
- {
- public:
- f_lru(vcache *v, tcache *t): vc(v), tc(t)
- {
- for (u16 i = 0; i < cachesize; i++)
- {
- cache[i] = -1;
- }
- }
- // Adds this vertex index and returns the highest-scoring triangle index
- u32 add(u16 vert, bool updatetris = false)
- {
- bool found = false;
- // Mark existing pos as empty
- for (u16 i = 0; i < cachesize; i++)
- {
- if (cache[i] == vert)
- {
- // Move everything down
- for (u16 j = i; j; j--)
- {
- cache[j] = cache[j - 1];
- }
- found = true;
- break;
- }
- }
- if (!found)
- {
- if (cache[cachesize-1] != -1)
- vc[cache[cachesize-1]].cachepos = -1;
- // Move everything down
- for (u16 i = cachesize - 1; i; i--)
- {
- cache[i] = cache[i - 1];
- }
- }
- cache[0] = vert;
- u32 highest = 0;
- float hiscore = 0;
- if (updatetris)
- {
- // Update cache positions
- for (u16 i = 0; i < cachesize; i++)
- {
- if (cache[i] == -1)
- break;
- vc[cache[i]].cachepos = i;
- vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
- }
- // Update triangle scores
- for (u16 i = 0; i < cachesize; i++)
- {
- if (cache[i] == -1)
- break;
- const u16 trisize = vc[cache[i]].tris.size();
- for (u16 t = 0; t < trisize; t++)
- {
- tcache *tri = &tc[vc[cache[i]].tris[t]];
- tri->score =
- vc[tri->ind[0]].score +
- vc[tri->ind[1]].score +
- vc[tri->ind[2]].score;
- if (tri->score > hiscore)
- {
- hiscore = tri->score;
- highest = vc[cache[i]].tris[t];
- }
- }
- }
- }
- return highest;
- }
- private:
- s32 cache[cachesize];
- vcache *vc;
- tcache *tc;
- };
- } // end anonymous namespace
- /**
- Vertex cache optimization according to the Forsyth paper:
- http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
- The function is thread-safe (read: you can optimize several meshes in different threads)
- \param mesh Source mesh for the operation. */
- IMesh* CMeshManipulator::createForsythOptimizedMesh(const IMesh *mesh) const
- {
- if (!mesh)
- return 0;
- SMesh *newmesh = new SMesh();
- newmesh->BoundingBox = mesh->getBoundingBox();
- const u32 mbcount = mesh->getMeshBufferCount();
- for (u32 b = 0; b < mbcount; ++b)
- {
- const IMeshBuffer *mb = mesh->getMeshBuffer(b);
- if (mb->getIndexType() != video::EIT_16BIT)
- {
- os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
- newmesh->drop();
- return 0;
- }
- const u32 icount = mb->getIndexCount();
- const u32 tcount = icount / 3;
- const u32 vcount = mb->getVertexCount();
- const u16 *ind = mb->getIndices();
- vcache *vc = new vcache[vcount];
- tcache *tc = new tcache[tcount];
- f_lru lru(vc, tc);
- // init
- for (u16 i = 0; i < vcount; i++)
- {
- vc[i].score = 0;
- vc[i].cachepos = -1;
- vc[i].NumActiveTris = 0;
- }
- // First pass: count how many times a vert is used
- for (u32 i = 0; i < icount; i += 3)
- {
- vc[ind[i]].NumActiveTris++;
- vc[ind[i + 1]].NumActiveTris++;
- vc[ind[i + 2]].NumActiveTris++;
- const u32 tri_ind = i/3;
- tc[tri_ind].ind[0] = ind[i];
- tc[tri_ind].ind[1] = ind[i + 1];
- tc[tri_ind].ind[2] = ind[i + 2];
- }
- // Second pass: list of each triangle
- for (u32 i = 0; i < tcount; i++)
- {
- vc[tc[i].ind[0]].tris.push_back(i);
- vc[tc[i].ind[1]].tris.push_back(i);
- vc[tc[i].ind[2]].tris.push_back(i);
- tc[i].drawn = false;
- }
- // Give initial scores
- for (u16 i = 0; i < vcount; i++)
- {
- vc[i].score = FindVertexScore(&vc[i]);
- }
- for (u32 i = 0; i < tcount; i++)
- {
- tc[i].score =
- vc[tc[i].ind[0]].score +
- vc[tc[i].ind[1]].score +
- vc[tc[i].ind[2]].score;
- }
- switch(mb->getVertexType())
- {
- case video::EVT_STANDARD:
- {
- video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
- SMeshBuffer *buf = new SMeshBuffer();
- buf->Material = mb->getMaterial();
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
- core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertex, const u16>::Node snode;
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
- // Output the best triangle
- u16 newind = buf->Vertices.size();
- snode *s = sind.find(v[tc[highest].ind[0]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[1]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[2]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
- tc[highest].drawn = true;
- for (u16 j = 0; j < 3; j++)
- {
- vcache *vert = &vc[tc[highest].ind[j]];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2], true);
- drawcalls++;
- }
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
- }
- break;
- case video::EVT_2TCOORDS:
- {
- video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
- SMeshBufferLightMap *buf = new SMeshBufferLightMap();
- buf->Material = mb->getMaterial();
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
- core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
- // Output the best triangle
- u16 newind = buf->Vertices.size();
- snode *s = sind.find(v[tc[highest].ind[0]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[1]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[2]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
- tc[highest].drawn = true;
- for (u16 j = 0; j < 3; j++)
- {
- vcache *vert = &vc[tc[highest].ind[j]];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2]);
- drawcalls++;
- }
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
- }
- break;
- case video::EVT_TANGENTS:
- {
- video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
- SMeshBufferTangents *buf = new SMeshBufferTangents();
- buf->Material = mb->getMaterial();
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
- core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
- // Output the best triangle
- u16 newind = buf->Vertices.size();
- snode *s = sind.find(v[tc[highest].ind[0]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[1]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- s = sind.find(v[tc[highest].ind[2]]);
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
- tc[highest].drawn = true;
- for (u16 j = 0; j < 3; j++)
- {
- vcache *vert = &vc[tc[highest].ind[j]];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2]);
- drawcalls++;
- }
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
- }
- break;
- default:
- break;
- }
- delete [] vc;
- delete [] tc;
- } // for each meshbuffer
- return newmesh;
- }
- } // end namespace scene
- } // end namespace irr
|