LColor3 L3DS::ReadColor(const LChunk &chunk) { LColor3 col = black; GotoChunk(chunk); switch (chunk.id) { case COLOR_F: col.r = ReadFloat(); col.g = ReadFloat(); col.b = ReadFloat(); break; case COLOR_24: col.r = ReadByte()/255.0f; col.g = ReadByte()/255.0f; col.b = ReadByte()/255.0f; break; case LIN_COLOR_F: col.r = ReadFloat(); col.g = ReadFloat(); col.b = ReadFloat(); break; case LIN_COLOR_24: col.r = ReadByte()/255.0f; col.g = ReadByte()/255.0f; col.b = ReadByte()/255.0f; break; default: fprintf(stderr, "L3DS::ReadColor - error this is not a color chunk"); } return col; }
void L3DS::ReadFaceList(const LChunk &chunk, LMesh &mesh) { // variables unsigned short count, t; uint i; LTri tri; LChunk ch; char str[20]; //uint mat; // consistency checks if (chunk.id != TRI_FACELIST) { ErrorMsg("L3DS::ReadFaceList - internal error: wrong chunk passed as parameter"); return; } GotoChunk(chunk); tri.smoothingGroups = 1; // read the number of faces count = ReadShort(); mesh.SetTriangleArraySize(count); for (i=0; i<count; i++) { tri.a = ReadShort(); tri.b = ReadShort(); tri.c = ReadShort(); ReadShort(); mesh.SetTri(tri, i); } // now read the optional chunks ch = ReadChunk(); int mat_id; while (ch.end <= chunk.end) { switch (ch.id) { case TRI_MAT_GROUP: ReadASCIIZ(str, 20); mat_id=0; if (FindMaterial(str)!=NULL) mat_id = FindMaterial(str)->GetID(); mesh.AddMaterial(mat_id); count = ReadShort(); for (i=0; i<count; i++) { t = ReadShort(); mesh.GetTri(t).materialId = mat_id; } break; case TRI_SMOOTH_GROUP: for (i=0; i<mesh.GetTriangleCount(); i++) mesh.GetTri(i).smoothingGroups = (ulong) ReadInt(); break; } SkipChunk(ch); ch = ReadChunk(); } }
float L3DS::ReadPercentage(const LChunk &chunk) { GotoChunk(chunk); switch (chunk.id) { case INT_PERCENTAGE: return (ReadShort()/100.0f); case FLOAT_PERCENTAGE: return ReadFloat(); } fprintf(stderr, "L3DS::ReadPercentage - error, the chunk is not a percentage chunk"); return 0; }
void L3DS::ReadMap(const LChunk &chunk, LMap& map) { LChunk child; char str[20]; GotoChunk(chunk); child = ReadChunk(); while (child.end <= chunk.end) { switch (child.id) { case INT_PERCENTAGE: map.strength = ReadPercentage(child); break; case MAT_MAPNAME: ReadASCIIZ(str, 20); strcpy(map.mapName, str); break; case MAT_MAP_TILING: map.tiling = ReadShort(); break; case MAT_MAP_USCALE: map.uScale = ReadFloat(); break; case MAT_MAP_VSCALE: map.vScale = ReadFloat(); break; case MAT_MAP_UOFFSET: map.uOffset = ReadFloat(); break; case MAT_MAP_VOFFSET: map.vOffset = ReadFloat(); break; case MAT_MAP_ANG: map.angle = ReadFloat(); break; } SkipChunk(child); child = ReadChunk(); } }
void L3DS::ReadKeyframeData(const LChunk &parent) { uint frames = 0; LChunk node_hdr; node_hdr.id = NODE_HDR; char str[20]; LMesh *mesh; GotoChunk(parent); if (!FindChunk(node_hdr, parent)) return; GotoChunk(node_hdr); ReadASCIIZ(str, 19); mesh = FindMesh(str); if (mesh == 0) return; GotoChunk(parent); // read the pivot //LVector3 pivot = zero3; LChunk pivotchunk; pivotchunk.id = PIVOT; if (FindChunk(pivotchunk, parent)) { GotoChunk(pivotchunk); /*pivot.x =*/ ReadFloat(); /*pivot.y =*/ ReadFloat(); /*pivot.z =*/ ReadFloat(); } GotoChunk(parent); // read frame 0 from the position track //LVector3 pos = zero3; frames = 0; LChunk poschunk; poschunk.id = POS_TRACK_TAG; if (FindChunk(poschunk, parent)) { GotoChunk(poschunk); // read the trackheader structure ReadShort(); ReadInt(); ReadInt(); frames = ReadInt(); if (frames > 0) { ReadKeyheader(); /*pos.x =*/ ReadFloat(); /*pos.y =*/ ReadFloat(); /*pos.z =*/ ReadFloat(); } } GotoChunk(parent); // now read the rotation track //LVector4 rot = zero4; LChunk rotchunk; rotchunk.id = ROT_TRACK_TAG; frames = 0; if (FindChunk(rotchunk, parent)) { GotoChunk(rotchunk); // read the trackheader structure ReadShort(); ReadInt(); ReadInt(); frames = ReadInt(); if (frames > 0) { ReadKeyheader(); /*rot.x =*/ ReadFloat(); /*rot.y =*/ ReadFloat(); /*rot.z =*/ ReadFloat(); /*rot.w =*/ ReadFloat(); } } GotoChunk(parent); // now read the scaling chunk //LVector3 scale; //scale.x = 1; //scale.y = 1; //scale.z = 1; LChunk scalechunk; scalechunk.id = SCL_TRACK_TAG; frames = 0; if (FindChunk(scalechunk, parent)) { GotoChunk(scalechunk); // read the trackheader structure ReadShort(); ReadInt(); ReadInt(); frames = ReadInt(); if (frames > 0) { ReadKeyheader(); /*scale.x =*/ ReadFloat(); /*scale.y =*/ ReadFloat(); /*scale.z =*/ ReadFloat(); } } GotoChunk(parent); }
void L3DS::ReadMaterial(const LChunk &parent) { // variables LChunk chunk; LChunk child; char str[30]; LMaterial mat; short sh; GotoChunk(parent); chunk = ReadChunk(); while (chunk.end <= parent.end) { switch (chunk.id) { case MAT_NAME: ReadASCIIZ(str, 30); mat.SetName(str); break; case MAT_AMBIENT: child = ReadChunk(); mat.SetAmbientColor(ReadColor(child)); break; case MAT_DIFFUSE: child = ReadChunk(); mat.SetDiffuseColor(ReadColor(child)); break; case MAT_SPECULAR: child = ReadChunk(); mat.SetSpecularColor(ReadColor(child)); break; case MAT_SHININESS: child = ReadChunk(); mat.SetShininess(ReadPercentage(child)); break; case MAT_TRANSPARENCY: child = ReadChunk(); mat.SetTransparency(ReadPercentage(child)); break; case MAT_SHADING: sh = ReadShort(); switch (sh) { case 0: mat.SetShadingType(sWireframe); break; case 1: mat.SetShadingType(sFlat); break; case 2: mat.SetShadingType(sGouraud); break; case 3: mat.SetShadingType(sPhong); break; case 4: mat.SetShadingType(sMetal); break; } break; case MAT_WIRE: mat.SetShadingType(sWireframe); break; case MAT_TEXMAP: ReadMap(chunk, mat.GetTextureMap1()); break; case MAT_TEX2MAP: ReadMap(chunk, mat.GetTextureMap2()); break; case MAT_OPACMAP: ReadMap(chunk, mat.GetOpacityMap()); break; case MAT_BUMPMAP: ReadMap(chunk, mat.GetBumpMap()); break; case MAT_SPECMAP: ReadMap(chunk, mat.GetSpecularMap()); break; case MAT_REFLMAP: // AMZ not really a bugfix but it seems to work! #if 1 ReadMap(chunk, mat.GetReflectionMap()); #else child = ReadChunk(); mat.GetReflectionMap().strength = ReadPercentage(child); SkipChunk(child); child = ReadChunk(); if (child.id != MAT_MAPNAME) { fprintf(stderr, "L3DS::ReadMaterial - error, expected chunk not found"); return; } ReadASCIIZ(str, 30); if (strcmp(str, "") == 0) strcpy(mat.GetReflectionMap().mapName, "auto"); #endif break; } SkipChunk(chunk); chunk = ReadChunk(); } m_materials.push_back(mat); m_materials[m_materials.size()-1].SetID(uint(m_materials.size())-1); }
void L3DS::ReadMesh(const LChunk &parent) { unsigned short count, i; LVector4 p; LMatrix4 m; LVector2 t; p.w = 1.0f; LMesh mesh; mesh.SetName(m_objName); GotoChunk(parent); LChunk chunk = ReadChunk(); while (chunk.end <= parent.end) { switch (chunk.id) { case TRI_VERTEXLIST: count = ReadShort(); mesh.SetVertexArraySize(count); for (i=0; i < count; i++) { p.x = ReadFloat(); p.y = ReadFloat(); p.z = ReadFloat(); mesh.SetVertex(p, i); } break; case TRI_FACEMAPPING: count = ReadShort(); if (mesh.GetVertexCount() == 0) mesh.SetVertexArraySize(count); for (i=0; i < count; i++) { t.x = ReadFloat(); t.y = ReadFloat(); mesh.SetUV(t, i); } break; case TRI_FACELIST: ReadFaceList(chunk, mesh); break; case TRI_MATRIX: m._11 = ReadFloat(); m._12 = ReadFloat(); m._13 = ReadFloat(); m._21 = ReadFloat(); m._22 = ReadFloat(); m._23 = ReadFloat(); m._31 = ReadFloat(); m._32 = ReadFloat(); m._33 = ReadFloat(); m._41 = ReadFloat(); m._42 = ReadFloat(); m._43 = ReadFloat(); m._14 = 0.0f; m._24 = 0.0f; m._34 = 0.0f; m._44 = 1.0f; mesh.SetMatrix(m); break; default: break; } SkipChunk(chunk); if (chunk.end >= parent.end) break; chunk = ReadChunk(); } m_meshes.push_back(mesh); }
bool L3DS::Read3DS() { LChunk mainchunk; LChunk edit; edit.id = EDIT3DS; mainchunk = ReadChunk(); if (mainchunk.id != MAIN3DS) { fprintf(stderr, "L3DS::Read3DS - wrong file format"); return false; } if (!FindChunk(edit, mainchunk)) return false; LChunk obj; LChunk ml; GotoChunk(edit); obj.id = MAT_ENTRY; while (FindChunk(obj, edit)) { ReadMaterial(obj); SkipChunk(obj); } GotoChunk(edit); obj.id = EDIT_OBJECT; { while (FindChunk(obj, edit)) { ReadASCIIZ(m_objName, 99); ml = ReadChunk(); if (ml.id == OBJ_TRIMESH) ReadMesh(ml); else if (ml.id == OBJ_LIGHT) ReadLight(ml); else if (ml.id == OBJ_CAMERA) ReadCamera(ml); SkipChunk(obj); } } // read the keyframer data here to find out correct object orientation LChunk keyframer; keyframer.id = KFDATA; LChunk objtrack; objtrack.id = OBJECT_NODE_TAG; GotoChunk(mainchunk); if (FindChunk(keyframer, mainchunk)) { // keyframer chunk is present GotoChunk(keyframer); while (FindChunk(objtrack, keyframer)) { ReadKeyframeData(objtrack); SkipChunk(objtrack); } } for (uint i=0; i<m_meshes.size(); i++) m_meshes[i].Optimize(m_optLevel); m_pos = 0; strcpy(m_objName, ""); return true; }
void L3DS::ReadMesh(const LChunk &parent) { //ErrorMsg("RND:: Ok, try load mesh...\n"); unsigned short count, i; LVector4 p; LMatrix4 m; LVector2 t; p.w = 1.0f; LMesh mesh; mesh.SetName(m_objName); GotoChunk(parent); LChunk chunk = ReadChunk(); while (chunk.end <= parent.end) { //ErrorMsg("RND:: Ochunk.end <= parent.end\n"); switch (chunk.id) { case TRI_VERTEXLIST: //ErrorMsg("RND:: case TRI_VERTEXLIST\n"); count = ReadShort(); mesh.SetVertexArraySize(count); for (i=0; i < count; i++) { p.x = ReadFloat(); p.y = ReadFloat(); p.z = ReadFloat(); mesh.SetVertex(p, i); } break; case TRI_FACEMAPPING: //ErrorMsg("RND:: case TRI_FACEMAPPING\n"); count = ReadShort(); // printf("RND:: ReadShort"); if (count == 0 ) break; if (mesh.GetVertexCount() == 0) mesh.SetVertexArraySize(count); printf("count: %d",count); for (i=0; i < count; i++) { t.x = ReadFloat(); t.y = ReadFloat(); mesh.SetUV(t, i); // printf("RND:: set: %f %f",t.x,t.y); } break; case TRI_FACELIST: //ErrorMsg("RND:: case TRI_FACELIST\n"); ReadFaceList(chunk, mesh); break; case TRI_MATRIX: //ErrorMsg("RND:: case TRI_MATRIX\n"); m._11 = ReadFloat(); m._12 = ReadFloat(); m._13 = ReadFloat(); m._21 = ReadFloat(); m._22 = ReadFloat(); m._23 = ReadFloat(); m._31 = ReadFloat(); m._32 = ReadFloat(); m._33 = ReadFloat(); m._41 = ReadFloat(); m._42 = ReadFloat(); m._43 = ReadFloat(); m._14 = 0.0f; m._24 = 0.0f; m._34 = 0.0f; m._44 = 1.0f; mesh.SetMatrix(m); break; default: break; } SkipChunk(chunk); if (chunk.end >= parent.end) break; chunk = ReadChunk(); } m_meshes.push_back(mesh); }