1 // Copyright (C) 2006-2012 Luke Hoschke
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
6 // File format designed by Mark Sibly for the Blitz3D engine and has been
\r
7 // declared public domain
\r
9 #include "IrrCompileConfig.h"
\r
10 #ifdef _IRR_COMPILE_WITH_B3D_LOADER_
\r
12 #include "CB3DMeshFileLoader.h"
\r
14 #include "IVideoDriver.h"
\r
15 #include "IFileSystem.h"
\r
19 #define _B3D_READER_DEBUG
\r
28 CB3DMeshFileLoader::CB3DMeshFileLoader(scene::ISceneManager* smgr)
\r
29 : AnimatedMesh(0), B3DFile(0), VerticesStart(0), NormalsInFile(false),
\r
30 HasVertexColors(false), ShowWarning(true)
\r
33 setDebugName("CB3DMeshFileLoader");
\r
38 //! returns true if the file maybe is able to be loaded by this class
\r
39 //! based on the file extension (e.g. ".bsp")
\r
40 bool CB3DMeshFileLoader::isALoadableFileExtension(const io::path& filename) const
\r
42 return core::hasFileExtension ( filename, "b3d" );
\r
46 //! creates/loads an animated mesh from the file.
\r
47 //! \return Pointer to the created mesh. Returns 0 if loading failed.
\r
48 //! If you no longer need the mesh, you should call IAnimatedMesh::drop().
\r
49 //! See IReferenceCounted::drop() for more information.
\r
50 IAnimatedMesh* CB3DMeshFileLoader::createMesh(io::IReadFile* file)
\r
56 AnimatedMesh = new scene::CSkinnedMesh();
\r
57 ShowWarning = true; // If true a warning is issued if too many textures are used
\r
62 AnimatedMesh->finalize();
\r
66 AnimatedMesh->drop();
\r
70 return AnimatedMesh;
\r
74 bool CB3DMeshFileLoader::load()
\r
78 NormalsInFile=false;
\r
79 HasVertexColors=false;
\r
81 //------ Get header ------
\r
83 SB3dChunkHeader header;
\r
84 B3DFile->read(&header, sizeof(header));
\r
85 #ifdef __BIG_ENDIAN__
\r
86 header.size = os::Byteswap::byteswap(header.size);
\r
89 if ( strncmp( header.name, "BB3D", 4 ) != 0 )
\r
91 os::Printer::log("File is not a b3d file. Loading failed (No header found)", B3DFile->getFileName(), ELL_ERROR);
\r
95 // Add main chunk...
\r
96 B3dStack.push_back(SB3dChunk(header, B3DFile->getPos()-8));
\r
98 // Get file version, but ignore it, as it's not important with b3d files...
\r
100 B3DFile->read(&fileVersion, sizeof(fileVersion));
\r
101 #ifdef __BIG_ENDIAN__
\r
102 fileVersion = os::Byteswap::byteswap(fileVersion);
\r
105 //------ Read main chunk ------
\r
107 while ( (B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos() )
\r
109 B3DFile->read(&header, sizeof(header));
\r
110 #ifdef __BIG_ENDIAN__
\r
111 header.size = os::Byteswap::byteswap(header.size);
\r
113 B3dStack.push_back(SB3dChunk(header, B3DFile->getPos()-8));
\r
115 if ( strncmp( B3dStack.getLast().name, "TEXS", 4 ) == 0 )
\r
117 if (!readChunkTEXS())
\r
120 else if ( strncmp( B3dStack.getLast().name, "BRUS", 4 ) == 0 )
\r
122 if (!readChunkBRUS())
\r
125 else if ( strncmp( B3dStack.getLast().name, "NODE", 4 ) == 0 )
\r
127 if (!readChunkNODE((CSkinnedMesh::SJoint*)0) )
\r
132 os::Printer::log("Unknown chunk found in mesh base - skipping");
\r
133 if (!B3DFile->seek(B3dStack.getLast().startposition + B3dStack.getLast().length))
\r
135 B3dStack.erase(B3dStack.size()-1);
\r
141 BaseVertices.clear();
\r
142 AnimatedVertices_VertexID.clear();
\r
143 AnimatedVertices_BufferID.clear();
\r
152 bool CB3DMeshFileLoader::readChunkNODE(CSkinnedMesh::SJoint *inJoint)
\r
154 CSkinnedMesh::SJoint *joint = AnimatedMesh->addJoint(inJoint);
\r
155 readString(joint->Name);
\r
157 #ifdef _B3D_READER_DEBUG
\r
158 core::stringc logStr;
\r
159 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
161 logStr += "read ChunkNODE";
\r
162 os::Printer::log(logStr.c_str(), joint->Name.c_str(), ELL_DEBUG);
\r
165 f32 position[3], scale[3], rotation[4];
\r
167 readFloats(position, 3);
\r
168 readFloats(scale, 3);
\r
169 readFloats(rotation, 4);
\r
171 joint->Animatedposition = core::vector3df(position[0],position[1],position[2]) ;
\r
172 joint->Animatedscale = core::vector3df(scale[0],scale[1],scale[2]);
\r
173 joint->Animatedrotation = core::quaternion(rotation[1], rotation[2], rotation[3], rotation[0]);
\r
175 //Build LocalMatrix:
\r
177 core::matrix4 positionMatrix;
\r
178 positionMatrix.setTranslation( joint->Animatedposition );
\r
179 core::matrix4 scaleMatrix;
\r
180 scaleMatrix.setScale( joint->Animatedscale );
\r
181 core::matrix4 rotationMatrix;
\r
182 joint->Animatedrotation.getMatrix_transposed(rotationMatrix);
\r
184 joint->LocalMatrix = positionMatrix * rotationMatrix * scaleMatrix;
\r
187 joint->GlobalMatrix = inJoint->GlobalMatrix * joint->LocalMatrix;
\r
189 joint->GlobalMatrix = joint->LocalMatrix;
\r
191 while(B3dStack.getLast().startposition + B3dStack.getLast().length > B3DFile->getPos()) // this chunk repeats
\r
193 SB3dChunkHeader header;
\r
194 B3DFile->read(&header, sizeof(header));
\r
195 #ifdef __BIG_ENDIAN__
\r
196 header.size = os::Byteswap::byteswap(header.size);
\r
199 B3dStack.push_back(SB3dChunk(header, B3DFile->getPos()-8));
\r
201 if ( strncmp( B3dStack.getLast().name, "NODE", 4 ) == 0 )
\r
203 if (!readChunkNODE(joint))
\r
206 else if ( strncmp( B3dStack.getLast().name, "MESH", 4 ) == 0 )
\r
208 VerticesStart=BaseVertices.size();
\r
209 if (!readChunkMESH(joint))
\r
212 else if ( strncmp( B3dStack.getLast().name, "BONE", 4 ) == 0 )
\r
214 if (!readChunkBONE(joint))
\r
217 else if ( strncmp( B3dStack.getLast().name, "KEYS", 4 ) == 0 )
\r
219 if(!readChunkKEYS(joint))
\r
222 else if ( strncmp( B3dStack.getLast().name, "ANIM", 4 ) == 0 )
\r
224 if (!readChunkANIM())
\r
229 os::Printer::log("Unknown chunk found in node chunk - skipping");
\r
230 if (!B3DFile->seek(B3dStack.getLast().startposition + B3dStack.getLast().length))
\r
232 B3dStack.erase(B3dStack.size()-1);
\r
236 B3dStack.erase(B3dStack.size()-1);
\r
242 bool CB3DMeshFileLoader::readChunkMESH(CSkinnedMesh::SJoint *inJoint)
\r
244 #ifdef _B3D_READER_DEBUG
\r
245 core::stringc logStr;
\r
246 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
248 logStr += "read ChunkMESH";
\r
249 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
253 B3DFile->read(&brushID, sizeof(brushID));
\r
254 #ifdef __BIG_ENDIAN__
\r
255 brushID = os::Byteswap::byteswap(brushID);
\r
258 NormalsInFile=false;
\r
259 HasVertexColors=false;
\r
261 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) //this chunk repeats
\r
263 SB3dChunkHeader header;
\r
264 B3DFile->read(&header, sizeof(header));
\r
265 #ifdef __BIG_ENDIAN__
\r
266 header.size = os::Byteswap::byteswap(header.size);
\r
269 B3dStack.push_back(SB3dChunk(header, B3DFile->getPos()-8));
\r
271 if ( strncmp( B3dStack.getLast().name, "VRTS", 4 ) == 0 )
\r
273 if (!readChunkVRTS(inJoint))
\r
276 else if ( strncmp( B3dStack.getLast().name, "TRIS", 4 ) == 0 )
\r
278 scene::SSkinMeshBuffer *meshBuffer = AnimatedMesh->addMeshBuffer();
\r
282 meshBuffer->Material=Materials[brushID].Material;
\r
285 if(readChunkTRIS(meshBuffer,AnimatedMesh->getMeshBuffers().size()-1, VerticesStart)==false)
\r
288 if (!NormalsInFile)
\r
292 for ( i=0; i<(s32)meshBuffer->Indices.size(); i+=3)
\r
294 core::plane3df p(meshBuffer->getVertex(meshBuffer->Indices[i+0])->Pos,
\r
295 meshBuffer->getVertex(meshBuffer->Indices[i+1])->Pos,
\r
296 meshBuffer->getVertex(meshBuffer->Indices[i+2])->Pos);
\r
298 meshBuffer->getVertex(meshBuffer->Indices[i+0])->Normal += p.Normal;
\r
299 meshBuffer->getVertex(meshBuffer->Indices[i+1])->Normal += p.Normal;
\r
300 meshBuffer->getVertex(meshBuffer->Indices[i+2])->Normal += p.Normal;
\r
303 for ( i = 0; i<(s32)meshBuffer->getVertexCount(); ++i )
\r
305 meshBuffer->getVertex(i)->Normal.normalize();
\r
306 BaseVertices[VerticesStart+i].Normal=meshBuffer->getVertex(i)->Normal;
\r
312 os::Printer::log("Unknown chunk found in mesh - skipping");
\r
313 if (!B3DFile->seek(B3dStack.getLast().startposition + B3dStack.getLast().length))
\r
315 B3dStack.erase(B3dStack.size()-1);
\r
319 B3dStack.erase(B3dStack.size()-1);
\r
327 int flags ;1=normal values present, 2=rgba values present
\r
328 int tex_coord_sets ;texture coords per vertex (eg: 1 for simple U/V) max=8
\r
329 but we only support 3
\r
330 int tex_coord_set_size ;components per set (eg: 2 for simple U/V) max=4
\r
332 float x,y,z ;always present
\r
333 float nx,ny,nz ;vertex normal: present if (flags&1)
\r
334 float red,green,blue,alpha ;vertex color: present if (flags&2)
\r
335 float tex_coords[tex_coord_sets][tex_coord_set_size] ;tex coords
\r
338 bool CB3DMeshFileLoader::readChunkVRTS(CSkinnedMesh::SJoint *inJoint)
\r
340 #ifdef _B3D_READER_DEBUG
\r
341 core::stringc logStr;
\r
342 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
344 logStr += "ChunkVRTS";
\r
345 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
348 const s32 max_tex_coords = 3;
\r
349 s32 flags, tex_coord_sets, tex_coord_set_size;
\r
351 B3DFile->read(&flags, sizeof(flags));
\r
352 B3DFile->read(&tex_coord_sets, sizeof(tex_coord_sets));
\r
353 B3DFile->read(&tex_coord_set_size, sizeof(tex_coord_set_size));
\r
354 #ifdef __BIG_ENDIAN__
\r
355 flags = os::Byteswap::byteswap(flags);
\r
356 tex_coord_sets = os::Byteswap::byteswap(tex_coord_sets);
\r
357 tex_coord_set_size = os::Byteswap::byteswap(tex_coord_set_size);
\r
360 if (tex_coord_sets >= max_tex_coords || tex_coord_set_size >= 4) // Something is wrong
\r
362 os::Printer::log("tex_coord_sets or tex_coord_set_size too big", B3DFile->getFileName(), ELL_ERROR);
\r
366 //------ Allocate Memory, for speed -----------//
\r
368 s32 numberOfReads = 3;
\r
372 NormalsInFile = true;
\r
373 numberOfReads += 3;
\r
377 numberOfReads += 4;
\r
378 HasVertexColors=true;
\r
381 numberOfReads += tex_coord_sets*tex_coord_set_size;
\r
383 const s32 memoryNeeded = (B3dStack.getLast().length / sizeof(f32)) / numberOfReads;
\r
385 BaseVertices.reallocate(memoryNeeded + BaseVertices.size() + 1);
\r
386 AnimatedVertices_VertexID.reallocate(memoryNeeded + AnimatedVertices_VertexID.size() + 1);
\r
388 //--------------------------------------------//
\r
390 while( (B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) // this chunk repeats
\r
393 f32 normal[3]={0.f, 0.f, 0.f};
\r
394 f32 color[4]={1.0f, 1.0f, 1.0f, 1.0f};
\r
395 f32 tex_coords[max_tex_coords][4];
\r
397 readFloats(position, 3);
\r
400 readFloats(normal, 3);
\r
402 readFloats(color, 4);
\r
404 for (s32 i=0; i<tex_coord_sets; ++i)
\r
405 readFloats(tex_coords[i], tex_coord_set_size);
\r
407 f32 tu=0.0f, tv=0.0f;
\r
408 if (tex_coord_sets >= 1 && tex_coord_set_size >= 2)
\r
410 tu=tex_coords[0][0];
\r
411 tv=tex_coords[0][1];
\r
414 f32 tu2=0.0f, tv2=0.0f;
\r
415 if (tex_coord_sets>1 && tex_coord_set_size>1)
\r
417 tu2=tex_coords[1][0];
\r
418 tv2=tex_coords[1][1];
\r
421 // Create Vertex...
\r
422 video::S3DVertex2TCoords Vertex(position[0], position[1], position[2],
\r
423 normal[0], normal[1], normal[2],
\r
424 video::SColorf(color[0], color[1], color[2], color[3]).toSColor(),
\r
427 // Transform the Vertex position by nested node...
\r
428 inJoint->GlobalMatrix.transformVect(Vertex.Pos);
\r
429 inJoint->GlobalMatrix.rotateVect(Vertex.Normal);
\r
432 BaseVertices.push_back(Vertex);
\r
434 AnimatedVertices_VertexID.push_back(-1);
\r
435 AnimatedVertices_BufferID.push_back(-1);
\r
438 B3dStack.erase(B3dStack.size()-1);
\r
444 bool CB3DMeshFileLoader::readChunkTRIS(scene::SSkinMeshBuffer *meshBuffer, u32 meshBufferID, s32 vertices_Start)
\r
446 #ifdef _B3D_READER_DEBUG
\r
447 core::stringc logStr;
\r
448 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
450 logStr += "ChunkTRIS";
\r
451 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
454 bool showVertexWarning=false;
\r
456 s32 triangle_brush_id; // Note: Irrlicht can't have different brushes for each triangle (using a workaround)
\r
457 B3DFile->read(&triangle_brush_id, sizeof(triangle_brush_id));
\r
458 #ifdef __BIG_ENDIAN__
\r
459 triangle_brush_id = os::Byteswap::byteswap(triangle_brush_id);
\r
462 SB3dMaterial *B3dMaterial;
\r
464 if (triangle_brush_id != -1)
\r
466 B3dMaterial = &Materials[triangle_brush_id];
\r
467 meshBuffer->Material = B3dMaterial->Material;
\r
472 const s32 memoryNeeded = B3dStack.getLast().length / sizeof(s32);
\r
473 meshBuffer->Indices.reallocate(memoryNeeded + meshBuffer->Indices.size() + 1);
\r
475 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) // this chunk repeats
\r
479 B3DFile->read(vertex_id, 3*sizeof(s32));
\r
480 #ifdef __BIG_ENDIAN__
\r
481 vertex_id[0] = os::Byteswap::byteswap(vertex_id[0]);
\r
482 vertex_id[1] = os::Byteswap::byteswap(vertex_id[1]);
\r
483 vertex_id[2] = os::Byteswap::byteswap(vertex_id[2]);
\r
487 vertex_id[0] += vertices_Start;
\r
488 vertex_id[1] += vertices_Start;
\r
489 vertex_id[2] += vertices_Start;
\r
491 for(s32 i=0; i<3; ++i)
\r
493 if ((u32)vertex_id[i] >= AnimatedVertices_VertexID.size())
\r
495 os::Printer::log("Illegal vertex index found", B3DFile->getFileName(), ELL_ERROR);
\r
499 if (AnimatedVertices_VertexID[ vertex_id[i] ] != -1)
\r
501 if ( AnimatedVertices_BufferID[ vertex_id[i] ] != (s32)meshBufferID ) //If this vertex is linked in a different meshbuffer
\r
503 AnimatedVertices_VertexID[ vertex_id[i] ] = -1;
\r
504 AnimatedVertices_BufferID[ vertex_id[i] ] = -1;
\r
505 showVertexWarning=true;
\r
508 if (AnimatedVertices_VertexID[ vertex_id[i] ] == -1) //If this vertex is not in the meshbuffer
\r
510 //Check for lightmapping:
\r
511 if (BaseVertices[ vertex_id[i] ].TCoords2 != core::vector2df(0.f,0.f))
\r
512 meshBuffer->convertTo2TCoords(); //Will only affect the meshbuffer the first time this is called
\r
514 //Add the vertex to the meshbuffer:
\r
515 if (meshBuffer->VertexType == video::EVT_STANDARD)
\r
516 meshBuffer->Vertices_Standard.push_back( BaseVertices[ vertex_id[i] ] );
\r
518 meshBuffer->Vertices_2TCoords.push_back(BaseVertices[ vertex_id[i] ] );
\r
520 //create vertex id to meshbuffer index link:
\r
521 AnimatedVertices_VertexID[ vertex_id[i] ] = meshBuffer->getVertexCount()-1;
\r
522 AnimatedVertices_BufferID[ vertex_id[i] ] = meshBufferID;
\r
526 // Apply Material/Color/etc...
\r
527 video::S3DVertex *Vertex=meshBuffer->getVertex(meshBuffer->getVertexCount()-1);
\r
529 if (!HasVertexColors)
\r
530 Vertex->Color=B3dMaterial->Material.DiffuseColor;
\r
531 else if (Vertex->Color.getAlpha() == 255)
\r
532 Vertex->Color.setAlpha( (s32)(B3dMaterial->alpha * 255.0f) );
\r
534 // Use texture's scale
\r
535 if (B3dMaterial->Textures[0])
\r
537 Vertex->TCoords.X *= B3dMaterial->Textures[0]->Xscale;
\r
538 Vertex->TCoords.Y *= B3dMaterial->Textures[0]->Yscale;
\r
541 if (B3dMaterial->Textures[1])
\r
543 Vertex->TCoords2.X *=B3dMaterial->Textures[1]->Xscale;
\r
544 Vertex->TCoords2.Y *=B3dMaterial->Textures[1]->Yscale;
\r
551 meshBuffer->Indices.push_back( AnimatedVertices_VertexID[ vertex_id[0] ] );
\r
552 meshBuffer->Indices.push_back( AnimatedVertices_VertexID[ vertex_id[1] ] );
\r
553 meshBuffer->Indices.push_back( AnimatedVertices_VertexID[ vertex_id[2] ] );
\r
556 B3dStack.erase(B3dStack.size()-1);
\r
558 if (showVertexWarning)
\r
559 os::Printer::log("B3dMeshLoader: Warning, different meshbuffers linking to the same vertex, this will cause problems with animated meshes");
\r
565 bool CB3DMeshFileLoader::readChunkBONE(CSkinnedMesh::SJoint *inJoint)
\r
567 #ifdef _B3D_READER_DEBUG
\r
568 core::stringc logStr;
\r
569 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
571 logStr += "read ChunkBONE";
\r
572 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
575 if (B3dStack.getLast().length > 8)
\r
577 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) // this chunk repeats
\r
579 u32 globalVertexID;
\r
581 B3DFile->read(&globalVertexID, sizeof(globalVertexID));
\r
582 B3DFile->read(&strength, sizeof(strength));
\r
583 #ifdef __BIG_ENDIAN__
\r
584 globalVertexID = os::Byteswap::byteswap(globalVertexID);
\r
585 strength = os::Byteswap::byteswap(strength);
\r
587 globalVertexID += VerticesStart;
\r
589 if (AnimatedVertices_VertexID[globalVertexID]==-1)
\r
591 os::Printer::log("B3dMeshLoader: Weight has bad vertex id (no link to meshbuffer index found)");
\r
593 else if (strength >0)
\r
595 CSkinnedMesh::SWeight *weight=AnimatedMesh->addWeight(inJoint);
\r
596 weight->strength=strength;
\r
597 //Find the meshbuffer and Vertex index from the Global Vertex ID:
\r
598 weight->vertex_id = AnimatedVertices_VertexID[globalVertexID];
\r
599 weight->buffer_id = AnimatedVertices_BufferID[globalVertexID];
\r
604 B3dStack.erase(B3dStack.size()-1);
\r
609 bool CB3DMeshFileLoader::readChunkKEYS(CSkinnedMesh::SJoint *inJoint)
\r
611 #ifdef _B3D_READER_DEBUG
\r
612 // Only print first, that's just too much output otherwise
\r
613 if ( !inJoint || (inJoint->PositionKeys.empty() && inJoint->ScaleKeys.empty() && inJoint->RotationKeys.empty()) )
\r
615 core::stringc logStr;
\r
616 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
618 logStr += "read ChunkKEYS";
\r
619 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
624 B3DFile->read(&flags, sizeof(flags));
\r
625 #ifdef __BIG_ENDIAN__
\r
626 flags = os::Byteswap::byteswap(flags);
\r
629 CSkinnedMesh::SPositionKey *oldPosKey=0;
\r
630 core::vector3df oldPos[2];
\r
631 CSkinnedMesh::SScaleKey *oldScaleKey=0;
\r
632 core::vector3df oldScale[2];
\r
633 CSkinnedMesh::SRotationKey *oldRotKey=0;
\r
634 core::quaternion oldRot[2];
\r
635 bool isFirst[3]={true,true,true};
\r
636 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) //this chunk repeats
\r
640 B3DFile->read(&frame, sizeof(frame));
\r
641 #ifdef __BIG_ENDIAN__
\r
642 frame = os::Byteswap::byteswap(frame);
\r
645 // Add key frames, frames in Irrlicht are zero-based
\r
649 readFloats(data, 3);
\r
650 if ((oldPosKey!=0) && (oldPos[0]==oldPos[1]))
\r
652 const core::vector3df pos(data[0], data[1], data[2]);
\r
653 if (oldPos[1]==pos)
\r
654 oldPosKey->frame = (f32)frame-1;
\r
657 oldPos[0]=oldPos[1];
\r
658 oldPosKey=AnimatedMesh->addPositionKey(inJoint);
\r
659 oldPosKey->frame = (f32)frame-1;
\r
660 oldPos[1].set(oldPosKey->position.set(pos));
\r
663 else if (oldPosKey==0 && isFirst[0])
\r
665 oldPosKey=AnimatedMesh->addPositionKey(inJoint);
\r
666 oldPosKey->frame = (f32)frame-1;
\r
667 oldPos[0].set(oldPosKey->position.set(data[0], data[1], data[2]));
\r
674 oldPos[0]=oldPos[1];
\r
675 oldPosKey=AnimatedMesh->addPositionKey(inJoint);
\r
676 oldPosKey->frame = (f32)frame-1;
\r
677 oldPos[1].set(oldPosKey->position.set(data[0], data[1], data[2]));
\r
682 readFloats(data, 3);
\r
683 if ((oldScaleKey!=0) && (oldScale[0]==oldScale[1]))
\r
685 const core::vector3df scale(data[0], data[1], data[2]);
\r
686 if (oldScale[1]==scale)
\r
687 oldScaleKey->frame = (f32)frame-1;
\r
690 oldScale[0]=oldScale[1];
\r
691 oldScaleKey=AnimatedMesh->addScaleKey(inJoint);
\r
692 oldScaleKey->frame = (f32)frame-1;
\r
693 oldScale[1].set(oldScaleKey->scale.set(scale));
\r
696 else if (oldScaleKey==0 && isFirst[1])
\r
698 oldScaleKey=AnimatedMesh->addScaleKey(inJoint);
\r
699 oldScaleKey->frame = (f32)frame-1;
\r
700 oldScale[0].set(oldScaleKey->scale.set(data[0], data[1], data[2]));
\r
706 if (oldScaleKey!=0)
\r
707 oldScale[0]=oldScale[1];
\r
708 oldScaleKey=AnimatedMesh->addScaleKey(inJoint);
\r
709 oldScaleKey->frame = (f32)frame-1;
\r
710 oldScale[1].set(oldScaleKey->scale.set(data[0], data[1], data[2]));
\r
715 readFloats(data, 4);
\r
716 if ((oldRotKey!=0) && (oldRot[0]==oldRot[1]))
\r
718 // meant to be in this order since b3d stores W first
\r
719 const core::quaternion rot(data[1], data[2], data[3], data[0]);
\r
720 if (oldRot[1]==rot)
\r
721 oldRotKey->frame = (f32)frame-1;
\r
724 oldRot[0]=oldRot[1];
\r
725 oldRotKey=AnimatedMesh->addRotationKey(inJoint);
\r
726 oldRotKey->frame = (f32)frame-1;
\r
727 oldRot[1].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
\r
728 oldRot[1].normalize();
\r
731 else if (oldRotKey==0 && isFirst[2])
\r
733 oldRotKey=AnimatedMesh->addRotationKey(inJoint);
\r
734 oldRotKey->frame = (f32)frame-1;
\r
735 // meant to be in this order since b3d stores W first
\r
736 oldRot[0].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
\r
737 oldRot[0].normalize();
\r
744 oldRot[0]=oldRot[1];
\r
745 oldRotKey=AnimatedMesh->addRotationKey(inJoint);
\r
746 oldRotKey->frame = (f32)frame-1;
\r
747 // meant to be in this order since b3d stores W first
\r
748 oldRot[1].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
\r
749 oldRot[1].normalize();
\r
754 B3dStack.erase(B3dStack.size()-1);
\r
759 bool CB3DMeshFileLoader::readChunkANIM()
\r
761 #ifdef _B3D_READER_DEBUG
\r
762 core::stringc logStr;
\r
763 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
765 logStr += "read ChunkANIM";
\r
766 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
769 s32 animFlags; //not stored\used
\r
770 s32 animFrames;//not stored\used
\r
771 f32 animFPS; //not stored\used
\r
773 B3DFile->read(&animFlags, sizeof(s32));
\r
774 B3DFile->read(&animFrames, sizeof(s32));
\r
775 readFloats(&animFPS, 1);
\r
777 AnimatedMesh->setAnimationSpeed(animFPS);
\r
778 os::Printer::log("FPS", io::path((double)animFPS), ELL_DEBUG);
\r
780 #ifdef __BIG_ENDIAN__
\r
781 animFlags = os::Byteswap::byteswap(animFlags);
\r
782 animFrames = os::Byteswap::byteswap(animFrames);
\r
785 B3dStack.erase(B3dStack.size()-1);
\r
790 bool CB3DMeshFileLoader::readChunkTEXS()
\r
792 #ifdef _B3D_READER_DEBUG
\r
793 core::stringc logStr;
\r
794 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
796 logStr += "read ChunkTEXS";
\r
797 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
800 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) //this chunk repeats
\r
802 Textures.push_back(SB3dTexture());
\r
803 SB3dTexture& B3dTexture = Textures.getLast();
\r
805 readString(B3dTexture.TextureName);
\r
806 B3dTexture.TextureName.replace('\\','/');
\r
807 #ifdef _B3D_READER_DEBUG
\r
808 os::Printer::log("read Texture", B3dTexture.TextureName.c_str(), ELL_DEBUG);
\r
811 B3DFile->read(&B3dTexture.Flags, sizeof(s32));
\r
812 B3DFile->read(&B3dTexture.Blend, sizeof(s32));
\r
813 #ifdef __BIG_ENDIAN__
\r
814 B3dTexture.Flags = os::Byteswap::byteswap(B3dTexture.Flags);
\r
815 B3dTexture.Blend = os::Byteswap::byteswap(B3dTexture.Blend);
\r
817 #ifdef _B3D_READER_DEBUG
\r
818 os::Printer::log("Flags", core::stringc(B3dTexture.Flags).c_str(), ELL_DEBUG);
\r
819 os::Printer::log("Blend", core::stringc(B3dTexture.Blend).c_str(), ELL_DEBUG);
\r
821 readFloats(&B3dTexture.Xpos, 1);
\r
822 readFloats(&B3dTexture.Ypos, 1);
\r
823 readFloats(&B3dTexture.Xscale, 1);
\r
824 readFloats(&B3dTexture.Yscale, 1);
\r
825 readFloats(&B3dTexture.Angle, 1);
\r
828 B3dStack.erase(B3dStack.size()-1);
\r
834 bool CB3DMeshFileLoader::readChunkBRUS()
\r
836 #ifdef _B3D_READER_DEBUG
\r
837 core::stringc logStr;
\r
838 for ( u32 i=1; i < B3dStack.size(); ++i )
\r
840 logStr += "read ChunkBRUS";
\r
841 os::Printer::log(logStr.c_str(), ELL_DEBUG);
\r
845 B3DFile->read(&n_texs, sizeof(u32));
\r
846 #ifdef __BIG_ENDIAN__
\r
847 n_texs = os::Byteswap::byteswap(n_texs);
\r
850 // number of texture ids read for Irrlicht
\r
851 const u32 num_textures = core::min_(n_texs, video::MATERIAL_MAX_TEXTURES);
\r
852 // number of bytes to skip (for ignored texture ids)
\r
853 const u32 n_texs_offset = (num_textures<n_texs)?(n_texs-num_textures):0;
\r
855 while((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) //this chunk repeats
\r
857 // This is what blitz basic calls a brush, like a Irrlicht Material
\r
859 core::stringc name;
\r
861 #ifdef _B3D_READER_DEBUG
\r
862 os::Printer::log("read Material", name, ELL_DEBUG);
\r
864 Materials.push_back(SB3dMaterial());
\r
865 SB3dMaterial& B3dMaterial=Materials.getLast();
\r
867 readFloats(&B3dMaterial.red, 1);
\r
868 readFloats(&B3dMaterial.green, 1);
\r
869 readFloats(&B3dMaterial.blue, 1);
\r
870 readFloats(&B3dMaterial.alpha, 1);
\r
871 readFloats(&B3dMaterial.shininess, 1);
\r
873 B3DFile->read(&B3dMaterial.blend, sizeof(B3dMaterial.blend));
\r
874 B3DFile->read(&B3dMaterial.fx, sizeof(B3dMaterial.fx));
\r
875 #ifdef __BIG_ENDIAN__
\r
876 B3dMaterial.blend = os::Byteswap::byteswap(B3dMaterial.blend);
\r
877 B3dMaterial.fx = os::Byteswap::byteswap(B3dMaterial.fx);
\r
879 #ifdef _B3D_READER_DEBUG
\r
880 os::Printer::log("Blend", core::stringc(B3dMaterial.blend).c_str(), ELL_DEBUG);
\r
881 os::Printer::log("FX", core::stringc(B3dMaterial.fx).c_str(), ELL_DEBUG);
\r
885 for (i=0; i<num_textures; ++i)
\r
888 B3DFile->read(&texture_id, sizeof(s32));
\r
889 #ifdef __BIG_ENDIAN__
\r
890 texture_id = os::Byteswap::byteswap(texture_id);
\r
892 //--- Get pointers to the texture, based on the IDs ---
\r
893 if ((u32)texture_id < Textures.size())
\r
895 B3dMaterial.Textures[i]=&Textures[texture_id];
\r
896 #ifdef _B3D_READER_DEBUG
\r
897 os::Printer::log("Layer", core::stringc(i).c_str(), ELL_DEBUG);
\r
898 os::Printer::log("using texture", Textures[texture_id].TextureName.c_str(), ELL_DEBUG);
\r
902 B3dMaterial.Textures[i]=0;
\r
904 // skip other texture ids
\r
905 for (i=0; i<n_texs_offset; ++i)
\r
908 B3DFile->read(&texture_id, sizeof(s32));
\r
909 #ifdef __BIG_ENDIAN__
\r
910 texture_id = os::Byteswap::byteswap(texture_id);
\r
912 if (ShowWarning && (texture_id != -1) && (n_texs>video::MATERIAL_MAX_TEXTURES))
\r
914 os::Printer::log("Too many textures used in one material", B3DFile->getFileName(), ELL_WARNING);
\r
915 ShowWarning = false;
\r
919 //Fixes problems when the lightmap is on the first texture:
\r
920 if (B3dMaterial.Textures[0] != 0)
\r
922 if (B3dMaterial.Textures[0]->Flags & 65536) // 65536 = secondary UV
\r
924 SB3dTexture *TmpTexture;
\r
925 TmpTexture = B3dMaterial.Textures[1];
\r
926 B3dMaterial.Textures[1] = B3dMaterial.Textures[0];
\r
927 B3dMaterial.Textures[0] = TmpTexture;
\r
931 //If a preceeding texture slot is empty move the others down:
\r
932 for (i=num_textures; i>0; --i)
\r
934 for (u32 j=i-1; j<num_textures-1; ++j)
\r
936 if (B3dMaterial.Textures[j+1] != 0 && B3dMaterial.Textures[j] == 0)
\r
938 B3dMaterial.Textures[j] = B3dMaterial.Textures[j+1];
\r
939 B3dMaterial.Textures[j+1] = 0;
\r
944 //------ Convert blitz flags/blend to irrlicht -------
\r
947 if (B3dMaterial.Textures[1])
\r
949 if (B3dMaterial.alpha==1.f)
\r
951 if (B3dMaterial.Textures[1]->Blend == 5) //(Multiply 2)
\r
952 B3dMaterial.Material.MaterialType = video::EMT_LIGHTMAP_M2;
\r
954 B3dMaterial.Material.MaterialType = video::EMT_LIGHTMAP;
\r
955 B3dMaterial.Material.Lighting = false;
\r
959 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
\r
960 B3dMaterial.Material.ZWriteEnable = video::EZW_OFF;
\r
963 else if (B3dMaterial.Textures[0]) //One texture:
\r
965 // Flags & 0x1 is usual SOLID, 0x8 is mipmap (handled before)
\r
966 if (B3dMaterial.Textures[0]->Flags & 0x2) //(Alpha mapped)
\r
968 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
\r
969 B3dMaterial.Material.ZWriteEnable = video::EZW_OFF;
\r
971 else if (B3dMaterial.Textures[0]->Flags & 0x4) //(Masked)
\r
972 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; // TODO: create color key texture
\r
973 else if (B3dMaterial.Textures[0]->Flags & 0x40)
\r
974 B3dMaterial.Material.MaterialType = video::EMT_SPHERE_MAP;
\r
975 else if (B3dMaterial.Textures[0]->Flags & 0x80)
\r
976 B3dMaterial.Material.MaterialType = video::EMT_SPHERE_MAP; // TODO: Should be cube map
\r
977 else if (B3dMaterial.alpha == 1.f)
\r
978 B3dMaterial.Material.MaterialType = video::EMT_SOLID;
\r
981 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
\r
982 B3dMaterial.Material.ZWriteEnable = video::EZW_OFF;
\r
987 if (B3dMaterial.alpha == 1.f)
\r
988 B3dMaterial.Material.MaterialType = video::EMT_SOLID;
\r
991 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
\r
992 B3dMaterial.Material.ZWriteEnable = video::EZW_OFF;
\r
996 B3dMaterial.Material.DiffuseColor = video::SColorf(B3dMaterial.red, B3dMaterial.green, B3dMaterial.blue, B3dMaterial.alpha).toSColor();
\r
997 B3dMaterial.Material.ColorMaterial=video::ECM_NONE;
\r
999 //------ Material fx ------
\r
1001 if (B3dMaterial.fx & 1) //full-bright
\r
1003 B3dMaterial.Material.AmbientColor = video::SColor(255, 255, 255, 255);
\r
1004 B3dMaterial.Material.Lighting = false;
\r
1007 B3dMaterial.Material.AmbientColor = B3dMaterial.Material.DiffuseColor;
\r
1009 if (B3dMaterial.fx & 2) //use vertex colors instead of brush color
\r
1010 B3dMaterial.Material.ColorMaterial=video::ECM_DIFFUSE_AND_AMBIENT;
\r
1012 if (B3dMaterial.fx & 4) //flatshaded
\r
1013 B3dMaterial.Material.GouraudShading = false;
\r
1015 if (B3dMaterial.fx & 16) //disable backface culling
\r
1016 B3dMaterial.Material.BackfaceCulling = false;
\r
1018 if (B3dMaterial.fx & 32) //force vertex alpha-blending
\r
1020 B3dMaterial.Material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
\r
1021 B3dMaterial.Material.ZWriteEnable = video::EZW_OFF;
\r
1024 B3dMaterial.Material.Shininess = B3dMaterial.shininess;
\r
1027 B3dStack.erase(B3dStack.size()-1);
\r
1033 void CB3DMeshFileLoader::readString(core::stringc& newstring)
\r
1036 while (B3DFile->getPos() <= B3DFile->getSize())
\r
1039 B3DFile->read(&character, sizeof(character));
\r
1042 newstring.append(character);
\r
1047 void CB3DMeshFileLoader::readFloats(f32* vec, u32 count)
\r
1049 B3DFile->read(vec, count*sizeof(f32));
\r
1050 #ifdef __BIG_ENDIAN__
\r
1051 for (u32 n=0; n<count; ++n)
\r
1052 vec[n] = os::Byteswap::byteswap(vec[n]);
\r
1056 } // end namespace scene
\r
1057 } // end namespace irr
\r
1060 #endif // _IRR_COMPILE_WITH_B3D_LOADER_
\r