From b299e9c4eab1dd3365ecf76d836d07100fac58ab Mon Sep 17 00:00:00 2001 From: hybrid Date: Fri, 7 Sep 2007 14:18:47 +0000 Subject: [PATCH] Reverted some stuff od the optimized .x splitting back to working version from Luke. Simplified some mesh loader parts in several formats. git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@920 dfc29bdd-3216-0410-991c-e03cc46cb475 --- source/Irrlicht/CAnimatedMeshMD3.cpp | 23 +++---- source/Irrlicht/COCTLoader.cpp | 90 ++++++++++++---------------- source/Irrlicht/COCTLoader.h | 2 +- source/Irrlicht/CXMeshFileLoader.cpp | 66 ++++++++++---------- source/Irrlicht/CXMeshFileLoader.h | 2 +- 5 files changed, 82 insertions(+), 101 deletions(-) diff --git a/source/Irrlicht/CAnimatedMeshMD3.cpp b/source/Irrlicht/CAnimatedMeshMD3.cpp index 9d4d7579..1dc2ca6a 100644 --- a/source/Irrlicht/CAnimatedMeshMD3.cpp +++ b/source/Irrlicht/CAnimatedMeshMD3.cpp @@ -77,7 +77,6 @@ CAnimatedMeshMD3::~CAnimatedMeshMD3() { if ( Mesh ) Mesh->drop (); - } @@ -199,19 +198,18 @@ IMeshBuffer * CAnimatedMeshMD3::createMeshBuffer ( const SMD3MeshBuffer * source return dest; } + //! build final mesh's vertices from frames frameA and frameB with linear interpolation. void CAnimatedMeshMD3::buildVertexArray ( u32 frameA, u32 frameB, f32 interpolate, const SMD3MeshBuffer * source, SMeshBuffer * dest ) { - u32 i; - u32 frameOffsetA = frameA * source->MeshHeader.numVertices; - u32 frameOffsetB = frameB * source->MeshHeader.numVertices; - + const u32 frameOffsetA = frameA * source->MeshHeader.numVertices; + const u32 frameOffsetB = frameB * source->MeshHeader.numVertices; const f32 scale = ( 1.f/ 64.f ); - for ( i = 0; i!= (u32)source->MeshHeader.numVertices; ++i ) + for (s32 i = 0; i != source->MeshHeader.numVertices; ++i) { video::S3DVertex &v = dest->Vertices [ i ]; @@ -239,11 +237,10 @@ void CAnimatedMeshMD3::buildVertexArray ( u32 frameA, u32 frameB, f32 interpolat //! build final mesh's tag from frames frameA and frameB with linear interpolation. void CAnimatedMeshMD3::buildTagArray ( u32 frameA, u32 frameB, f32 interpolate ) { - u32 i; - u32 frameOffsetA = frameA * Mesh->MD3Header.numTags; - u32 frameOffsetB = frameB * Mesh->MD3Header.numTags; + const u32 frameOffsetA = frameA * Mesh->MD3Header.numTags; + const u32 frameOffsetB = frameB * Mesh->MD3Header.numTags; - for ( i = 0; i!= (u32)Mesh->MD3Header.numTags; ++i ) + for ( s32 i = 0; i != Mesh->MD3Header.numTags; ++i ) { SMD3QuaterionTag &d = TagListIPol [ i ]; @@ -269,8 +266,6 @@ bool CAnimatedMeshMD3::loadModelFile( u32 modelIndex, io::IReadFile* file) if (!file) return false; - file->seek(0); - //! Check MD3Header { file->read( &Mesh->MD3Header, sizeof(SMD3Header) ); @@ -291,7 +286,7 @@ bool CAnimatedMeshMD3::loadModelFile( u32 modelIndex, io::IReadFile* file) SMD3Tag import; SMD3QuaterionTag exp; - u32 i,g; + u32 i; file->seek( Mesh->MD3Header.tagStart ); for (i = 0; i != totalTags; ++i ) @@ -340,7 +335,7 @@ bool CAnimatedMeshMD3::loadModelFile( u32 modelIndex, io::IReadFile* file) //! read skins (shaders) file->seek( offset + buf->MeshHeader.offset_shaders ); - for ( g = 0; g != (u32)buf->MeshHeader.numShader; ++g ) + for ( s32 g = 0; g != buf->MeshHeader.numShader; ++g ) { file->read( &skin, sizeof(skin) ); buf->Shader.push_back ( skin.name ); diff --git a/source/Irrlicht/COCTLoader.cpp b/source/Irrlicht/COCTLoader.cpp index 5b7335f3..ff79f3da 100644 --- a/source/Irrlicht/COCTLoader.cpp +++ b/source/Irrlicht/COCTLoader.cpp @@ -49,7 +49,8 @@ COCTLoader::~COCTLoader() // Doesn't really belong here, but it's jammed in for now. void COCTLoader::OCTLoadLights(irr::io::IReadFile* file, irr::scene::ISceneManager * scene, irr::scene::ISceneNode * parent, f32 radius, f32 intensityScale, bool rewind) { - if (rewind) file->seek(0); + if (rewind) + file->seek(0); octHeader header; file->read(&header, sizeof(octHeader)); @@ -64,14 +65,9 @@ void COCTLoader::OCTLoadLights(irr::io::IReadFile* file, irr::scene::ISceneManag //TODO: Skip past my extended data just for good form - u32 i; - for (i = 0; i < header.numLights; i++) + for (u32 i = 0; i < header.numLights; i++) { - f32 intensity; - intensity = lights[i].intensity * intensityScale; - - //irr::scene::ISceneNode* node = scene->addCubeSceneNode(30,parent,-1, core::vector3df(lights[i].pos[0], lights[i].pos[2], lights[i].pos[1])); - //node->getMaterial(0).AmbientColor = video::SColorf(lights[i].color[0] * intensity, lights[i].color[1] * intensity, lights[i].color[2] * intensity).toSColor(); + const f32 intensity = lights[i].intensity * intensityScale; scene->addLightSceneNode(parent, core::vector3df(lights[i].pos[0], lights[i].pos[2], lights[i].pos[1]), video::SColorf(lights[i].color[0] * intensity, lights[i].color[1] * intensity, lights[i].color[2] * intensity, 1.0f), @@ -81,27 +77,8 @@ void COCTLoader::OCTLoadLights(irr::io::IReadFile* file, irr::scene::ISceneManag //! given three points representing a face, return a face normal -void COCTLoader::GetFaceNormal(f32 a[3], f32 b[3], f32 c[3], f32 out[3]) { - f32 v1[3], v2[3]; - - v1[0] = a[0] - b[0]; - v1[1] = a[1] - b[1]; - v1[2] = a[2] - b[2]; - - v2[0] = b[0] - c[0]; - v2[1] = b[1] - c[1]; - v2[2] = b[2] - c[2]; - - out[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]); - out[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]); - out[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]); - - f32 dist = (f32)sqrt((out[0] * out[0]) + (out[1] * out[1]) + (out[2] * out[2])); - if (dist == 0.0f) dist = 0.001f; - - out[0] /= dist; - out[1] /= dist; - out[2] /= dist; +core::vector3df COCTLoader::GetFaceNormal(f32 a[3], f32 b[3], f32 c[3]) { + return core::plane3df(core::vector3df(a[0],a[1],a[2]), core::vector3df(b[0],c[1],c[2]), core::vector3df(c[0],c[1],c[2])).Normal; } @@ -154,40 +131,41 @@ IAnimatedMesh* COCTLoader::createMesh(irr::io::IReadFile* file) scene::SMeshBufferLightMap* buffer = new scene::SMeshBufferLightMap(); buffer->Material.MaterialType = video::EMT_LIGHTMAP; - buffer->Material.Wireframe = false; buffer->Material.Lighting = false; - Mesh->addMeshBuffer(buffer); - buffer->drop(); } // Build the mesh buffers - for (i = 0; i < header.numFaces; i++) { - if (faces[i].numVerts < 3) continue; + for (i = 0; i < header.numFaces; i++) + { + if (faces[i].numVerts < 3) + continue; - f32 normal[3]; - GetFaceNormal(verts[faces[i].firstVert].pos,verts[faces[i].firstVert+1].pos,verts[faces[i].firstVert+2].pos, normal); + const core::vector3df normal = + GetFaceNormal(verts[faces[i].firstVert].pos, + verts[faces[i].firstVert+1].pos, + verts[faces[i].firstVert+2].pos); - u32 textureID = core::min_(s32(faces[i].textureID), s32(header.numTextures - 1)) + 1; - u32 lightmapID = core::min_(s32(faces[i].lightmapID),s32(header.numLightmaps - 1)) + 1; + const u32 textureID = core::min_(s32(faces[i].textureID), s32(header.numTextures - 1)) + 1; + const u32 lightmapID = core::min_(s32(faces[i].lightmapID),s32(header.numLightmaps - 1)) + 1; SMeshBufferLightMap * meshBuffer = (SMeshBufferLightMap*)Mesh->getMeshBuffer(lightmapID * (header.numTextures + 1) + textureID); - u32 base = meshBuffer->Vertices.size(); + const u32 base = meshBuffer->Vertices.size(); // Add this face's verts u32 v; - for (v = 0; v < faces[i].numVerts; v++) + for (v = 0; v < faces[i].numVerts; ++v) { octVert * vv = &verts[faces[i].firstVert + v]; - video::S3DVertex2TCoords vert = video::S3DVertex2TCoords(); + video::S3DVertex2TCoords vert; vert.Pos.set(vv->pos[0], vv->pos[1], vv->pos[2]); vert.Color = irr::video::SColor(0,255,255,255); - vert.Normal.set(normal[0], normal[1], normal[2]); + vert.Normal.set(normal); if (textureID == 0) { - // No texure -- just a lightmap. Thus, use lightmap coords for texture 1. + // No texture -- just a lightmap. Thus, use lightmap coords for texture 1. // (the actual texture will be swapped later) vert.TCoords.set(vv->lc[0], vv->lc[1]); } @@ -204,15 +182,21 @@ IAnimatedMesh* COCTLoader::createMesh(irr::io::IReadFile* file) // This weird loop turns convex polygons into triangle strips. // I do it this way instead of a simple fan because it usually looks a lot better in wireframe, for example. u32 h = faces[i].numVerts - 1, l = 0, c; // High, Low, Center - for (v = 0; v < faces[i].numVerts - 2; v++) + for (v = 0; v < faces[i].numVerts - 2; ++v) { - if (v & 1) c = h - 1; else c = l + 1; + if (v & 1) + c = h - 1; + else + c = l + 1; meshBuffer->Indices.push_back(base + h); meshBuffer->Indices.push_back(base + l); meshBuffer->Indices.push_back(base + c); - if (v & 1) h--; else l++; + if (v & 1) + --h; + else + ++l; } } @@ -311,17 +295,17 @@ IAnimatedMesh* COCTLoader::createMesh(irr::io::IReadFile* file) // attach materials - u32 j; for (i = 0; i < header.numLightmaps + 1; i++) { - for (j = 0; j < header.numTextures + 1; j++) + for (u32 j = 0; j < header.numTextures + 1; j++) { u32 mb = i * (header.numTextures + 1) + j; SMeshBufferLightMap * meshBuffer = (SMeshBufferLightMap*)Mesh->getMeshBuffer(mb); meshBuffer->Material.Textures[0] = tex[j]; meshBuffer->Material.Textures[1] = lig[i]; - if (meshBuffer->Material.Textures[0] == 0) { + if (meshBuffer->Material.Textures[0] == 0) + { // This material has no texture, so we'll just show the lightmap if there is one. // We swapped the texture coordinates earlier. meshBuffer->Material.Textures[0] = meshBuffer->Material.Textures[1]; @@ -352,14 +336,15 @@ IAnimatedMesh* COCTLoader::createMesh(irr::io::IReadFile* file) } else { - i++; + ++i; } } // create bounding box - for (i = 0; i < Mesh->MeshBuffers.size(); i++) { - ((SMeshBufferLightMap*)Mesh->MeshBuffers[i])->recalculateBoundingBox(); + for (i = 0; i < Mesh->MeshBuffers.size(); ++i) + { + Mesh->MeshBuffers[i]->recalculateBoundingBox(); } Mesh->recalculateBoundingBox(); @@ -387,3 +372,4 @@ bool COCTLoader::isALoadableFileExtension(const c8* filename) } // end namespace irr #endif // _IRR_COMPILE_WITH_OCT_LOADER_ + diff --git a/source/Irrlicht/COCTLoader.h b/source/Irrlicht/COCTLoader.h index 284d4bc9..1a9340e1 100644 --- a/source/Irrlicht/COCTLoader.h +++ b/source/Irrlicht/COCTLoader.h @@ -78,7 +78,7 @@ namespace scene virtual IAnimatedMesh* createMesh(irr::io::IReadFile* file); private: - void GetFaceNormal(f32 a[3], f32 b[3], f32 c[3], f32 out[3]); + core::vector3df GetFaceNormal(f32 a[3], f32 b[3], f32 c[3]); struct octHeader { u32 numVerts; diff --git a/source/Irrlicht/CXMeshFileLoader.cpp b/source/Irrlicht/CXMeshFileLoader.cpp index 940d03d2..02a606e4 100644 --- a/source/Irrlicht/CXMeshFileLoader.cpp +++ b/source/Irrlicht/CXMeshFileLoader.cpp @@ -110,11 +110,16 @@ bool CXMeshFileLoader::load(io::IReadFile* file) //the same vertex can be used in many different meshbuffers, but it's slow to work out core::array< core::array< u32 > > verticesLink; - verticesLink.set_used(mesh->Vertices.size()); + verticesLink.reallocate(mesh->Vertices.size()); core::array< core::array< u32 > > verticesLinkBuffer; - verticesLinkBuffer.set_used(mesh->Vertices.size()); + verticesLinkBuffer.reallocate(mesh->Vertices.size()); + for (i=0;iVertices.size();++i) + { + verticesLink.push_back( core::array< u32 >() ); + verticesLinkBuffer.push_back( core::array< u32 >() ); + } - for (i=0;iFaceIndices.size();++i) + for (i=0;iFaceMaterialIndices.size();++i) { for (u32 id=i*3+0;id<=i*3+2;++id) { @@ -123,7 +128,7 @@ bool CXMeshFileLoader::load(io::IReadFile* file) for (u32 j=0; j < Array.size(); ++j) { - if (Array[j]==mesh->FaceIndices[i]) + if (Array[j]==mesh->FaceMaterialIndices[i]) { found=true; break; @@ -131,7 +136,7 @@ bool CXMeshFileLoader::load(io::IReadFile* file) } if (!found) - Array.push_back( mesh->FaceIndices[i] ); + Array.push_back( mesh->FaceMaterialIndices[i] ); } } @@ -153,9 +158,9 @@ bool CXMeshFileLoader::load(io::IReadFile* file) } } - for (i=0;iFaceIndices.size();++i) + for (i=0;iFaceMaterialIndices.size();++i) { - scene::SSkinMeshBuffer *buffer=mesh->Buffers[ mesh->FaceIndices[i] ]; + scene::SSkinMeshBuffer *buffer=mesh->Buffers[ mesh->FaceMaterialIndices[i] ]; for (u32 id=i*3+0;id<=i*3+2;++id) { @@ -163,7 +168,7 @@ bool CXMeshFileLoader::load(io::IReadFile* file) for (u32 j=0;j< Array.size() ;++j) { - if ( Array[j]== mesh->FaceIndices[i] ) + if ( Array[j]== mesh->FaceMaterialIndices[i] ) buffer->Indices.push_back( verticesLink[ mesh->Indices[id] ][j] ); } } @@ -189,10 +194,10 @@ bool CXMeshFileLoader::load(io::IReadFile* file) { for (u32 k=1; k < verticesLinkBuffer[id].size(); ++k) { - ISkinnedMesh::SWeight& WeightClone=AnimatedMesh->createWeight(Joint); - WeightClone.strength = Weight.strength; - WeightClone.vertex_id = verticesLink[id][k]; - WeightClone.buffer_id = verticesLinkBuffer[id][k]; + ISkinnedMesh::SWeight* WeightClone = AnimatedMesh->createWeight(Joint); + WeightClone->strength = Weight.strength; + WeightClone->vertex_id = verticesLink[id][k]; + WeightClone->buffer_id = verticesLinkBuffer[id][k]; } } } @@ -212,11 +217,11 @@ bool CXMeshFileLoader::load(io::IReadFile* file) verticesLink[i]=0; } - for (i=0;iFaceIndices.size();++i) + for (i=0;iFaceMaterialIndices.size();++i) { for (u32 id=i*3+0;id<=i*3+2;++id) { - verticesLinkBuffer[ mesh->Indices[id] ] = mesh->FaceIndices[i]; + verticesLinkBuffer[ mesh->Indices[id] ] = mesh->FaceMaterialIndices[i]; } } @@ -228,9 +233,9 @@ bool CXMeshFileLoader::load(io::IReadFile* file) buffer->Vertices_Standard.push_back( mesh->Vertices[i] ); } - for (i=0;iFaceIndices.size();++i) + for (i=0;iFaceMaterialIndices.size();++i) { - scene::SSkinMeshBuffer *buffer=mesh->Buffers[ mesh->FaceIndices[i] ]; + scene::SSkinMeshBuffer *buffer=mesh->Buffers[ mesh->FaceMaterialIndices[i] ]; for (u32 id=i*3+0;id<=i*3+2;++id) { @@ -1178,20 +1183,10 @@ bool CXMeshFileLoader::parseDataObjectMeshMaterialList(SXMesh &mesh) } // read material count - readInt(); + mesh.Materials.reallocate(readInt()); // read non triangulated face material index count - s32 nFaceIndices = readInt(); - - // read non triangulated face indices - - core::array nonTriFaceIndices; - nonTriFaceIndices.set_used(nFaceIndices); - - for (s32 i=0; i Indices; - core::array FaceIndices; // index of material for each face + core::array FaceMaterialIndices; // index of material for each face core::array Materials; // material array };