Entity ProcessObjectChunk(ThreeDSChunk chunk, Entity e) { while (chunk.BytesRead < chunk.Length) { ThreeDSChunk child = new ThreeDSChunk(this, reader); #if LOG_CHUNKS Logger.Log("ProcessObjectChunk: chunk = {0:X}, len = {1}", child.ID, chunk.Length); #endif switch (child.ID) { case Groups.C_OBJECT_MESH: ProcessObjectChunk(child, e); break; case Groups.C_OBJECT_VERTICES: e.vertices = ReadVertices(child); break; case Groups.C_OBJECT_FACES: e.triangles = ReadTriangles(child); #if LOG_FACES Logger.Log("Object Faces: {0}", e.indices.Length); #endif if (child.BytesRead < child.Length) { ProcessFaceChunk(child, e); } break; case Groups.C_OBJECT_UV: int cnt = reader.ReadUInt16(); child.BytesRead += 2; //Console.WriteLine ( " TexCoords: {0}", cnt ); e.texcoords = new TexCoord [cnt]; for (int ii = 0; ii < cnt; ii++) { e.texcoords [ii] = new TexCoord(reader.ReadSingle(), reader.ReadSingle()); } child.BytesRead += (cnt * (4 * 2)); break; default: SkipChunk(child); break; } chunk.BytesRead += child.BytesRead; child.SkipToEnd(reader); //Console.WriteLine ( " ID: {0} Length: {1} Read: {2}", chunk.ID.ToString("x"), chunk.Length , chunk.BytesRead ); } return(e); }
float[] ProcessColorChunk(ThreeDSChunk chunk) { ThreeDSChunk child = new ThreeDSChunk(this, reader); float red = 1.0f; float green = 1.0f; float blue = 1.0f; switch (child.ID) { // Each color component is represented by a single-precision floating point number. case Groups.C_COLOR_F: red = reader.ReadSingle(); green = reader.ReadSingle(); blue = reader.ReadSingle(); break; // Each color component is represented by a byte. case Groups.C_COLOR_24: red = (float)reader.ReadByte() / 255.0f; green = (float)reader.ReadByte() / 255.0f; blue = (float)reader.ReadByte() / 255.0f; break; } // Logger.Log("ProcessColorChunk: color = ({0},{1},{2}) len = {3}", red, green, blue, child.Length); chunk.BytesRead += (int)child.Length; child.SkipToEnd(reader); return(new float[] { red, green, blue }); }
Entity ProcessFaceChunk(ThreeDSChunk chunk, Entity e) { while (chunk.BytesRead < chunk.Length) { ThreeDSChunk child = new ThreeDSChunk(this, reader); #if LOG_CHUNKS Logger.Log("ProcessFaceChunk: chunk = {0:X}, len = {1}", child.ID, chunk.Length); #endif switch (child.ID) { case Groups.C_OBJECT_MATERIAL: string materialName = ProcessString(child); #if LOG_FACES Logger.Log("Uses Material: {0}", materialName); #endif Material material = null; if (materials.TryGetValue(materialName, out material)) { e.material = material; } #if LOG_MATERIAL else { Logger.Log(" Warning: Material '{0}' not found. ", materialName); } #endif //Console.WriteLine(" Warning: Material '{0}' not found. ", name2); //throw new Exception ( "Material not found!" ); int nfaces = reader.ReadUInt16(); child.BytesRead += 2; #if LOG_FACES Logger.Log("ProcessFaceChunk: {0} material faces", nfaces); #endif for (int i = 0; i < nfaces; i++) { int faceIndex = reader.ReadUInt16(); e.triangles[faceIndex].material = (material ?? Triangle.defaultMaterial); child.BytesRead += 2; } SkipChunk(child); break; default: SkipChunk(child); break; } chunk.BytesRead += child.BytesRead; child.SkipToEnd(reader); //Console.WriteLine ( " ID: {0} Length: {1} Read: {2}", chunk.ID.ToString("x"), chunk.Length , chunk.BytesRead ); } return(e); }
int ProcessPercentageChunk(ThreeDSChunk chunk) { ThreeDSChunk child = new ThreeDSChunk(this, reader); int per = reader.ReadUInt16(); child.BytesRead += 2; chunk.BytesRead += child.BytesRead; child.SkipToEnd(reader); return(per); }
void ProcessTexMapChunk(ThreeDSChunk chunk, Material m) { while (chunk.BytesRead < chunk.Length) { ThreeDSChunk child = new ThreeDSChunk(this, reader); switch (child.ID) { case Groups.C_MATMAPFILE: string name = ProcessString(child); #if LOG_MATERIAL Logger.Log("Material texture map: {0}", name); #endif //Console.WriteLine ( " Texture File: {0}", name ); // use System.Drawing to try and load this image /* * //FileStream fStream; * Bitmap bmp; * try * { * //fStream = new FileStream(base_dir + name, FileMode.Open, FileAccess.Read); * bmp = new Bitmap ( base_dir + name ); * } * catch ( Exception ex ) * { * // couldn't find the file * Console.WriteLine ( " ERROR: could not load file '{0}': {1}", base_dir + name, ex.Message ); * break; * } * * // Flip image (needed so texture is the correct way around!) * bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); * * System.Drawing.Imaging.BitmapData imgData = bmp.LockBits ( new Rectangle(new Point(0, 0), bmp.Size), * System.Drawing.Imaging.ImageLockMode.ReadOnly, * System.Drawing.Imaging.PixelFormat.Format32bppArgb); * // System.Drawing.Imaging.PixelFormat.Format24bppRgb ); * * m.BindTexture ( imgData.Width, imgData.Height, imgData.Scan0 ); * * bmp.UnlockBits(imgData); * bmp.Dispose(); */ /* * BinaryReader br = new BinaryReader(fStream); * * br.ReadBytes ( 14 ); // skip file header * * uint offset = br.ReadUInt32 ( ); * //br.ReadBytes ( 4 ); // skip image header * uint biWidth = br.ReadUInt32 (); * uint biHeight = br.ReadUInt32 (); * Console.WriteLine ( "w {0} h {1}", biWidth, biHeight ); * br.ReadBytes ( (int) offset - 12 ); // skip rest of image header * * byte[,,] tex = new byte [ biHeight , biWidth , 4 ]; * * for ( int ii=0 ; ii < biHeight ; ii++ ) * { * for ( int jj=0 ; jj < biWidth ; jj++ ) * { * tex [ ii, jj, 0 ] = br.ReadByte(); * tex [ ii, jj, 1 ] = br.ReadByte(); * tex [ ii, jj, 2 ] = br.ReadByte(); * tex [ ii, jj, 3 ] = 255; * //Console.Write ( ii + " " ); * } * } * * br.Close(); * fStream.Close(); * m.BindTexture ( (int) biWidth, (int) biHeight, tex ); */ break; default: SkipChunk(child); break; } chunk.BytesRead += child.BytesRead; child.SkipToEnd(reader); } }
void ProcessMaterialChunk(ThreeDSChunk chunk) { string name = string.Empty; Material m = new Material(); while (chunk.BytesRead < chunk.Length) { ThreeDSChunk child = new ThreeDSChunk(this, reader); #if LOG_CHUNKS Logger.Log("ProcessMaterialChunk: chunk = {0:X}, len = {1}", child.ID, chunk.Length); #endif switch (child.ID) { case Groups.C_MATNAME: name = ProcessString(child); #if LOG_MATERIAL Logger.Log("Material name: {0}", name); #endif break; case Groups.C_MATAMBIENT: m.Ambient = ProcessColorChunk(child); #if LOG_MATERIAL Logger.Log("Material ambient color: ({0},{1},{2})", m.Ambient[0], m.Ambient[1], m.Ambient[2]); #endif break; case Groups.C_MATDIFFUSE: m.Diffuse = ProcessColorChunk(child); #if LOG_MATERIAL Logger.Log("Material diffuse color: ({0},{1},{2})", m.Diffuse[0], m.Diffuse[1], m.Diffuse[2]); #endif break; case Groups.C_MATSPECULAR: m.Specular = ProcessColorChunk(child); #if LOG_MATERIAL Logger.Log("Material specular color: ({0},{1},{2})", m.Specular[0], m.Specular[1], m.Specular[2]); #endif break; case Groups.C_MATSHININESS: m.Shininess = ProcessPercentageChunk(child); #if LOG_MATERIAL Logger.Log("Material shininess: {0}", m.Shininess); #endif break; case Groups.C_MATMAP: ProcessPercentageChunk(child); ProcessTexMapChunk(child, m); break; default: SkipChunk(child); break; } chunk.BytesRead += child.BytesRead; child.SkipToEnd(reader); } // Don't add materials with duplicate names if (!materials.ContainsKey(name)) { materials.Add(name, m); } }
void ProcessChunk(ThreeDSChunk chunk) { // process chunks until there are none left while (chunk.BytesRead < chunk.Length) { // grab a chunk ThreeDSChunk child = new ThreeDSChunk(this, reader); #if LOG_CHUNKS Logger.Log("ProcessChunk: chunk = {0:X}, len = {1}", child.ID, chunk.Length); #endif // process based on ID switch (child.ID) { case Groups.C_VERSION: version = reader.ReadInt32(); child.BytesRead += 4; break; case Groups.C_OBJECTINFO: ThreeDSChunk obj_chunk = new ThreeDSChunk(this, reader); // not sure whats up with this chunk SkipChunk(obj_chunk); child.BytesRead += obj_chunk.BytesRead; ProcessChunk(child); break; case Groups.C_MATERIAL: ProcessMaterialChunk(child); break; case Groups.C_OBJECT: // string name = ProcessString(child); Entity e = ProcessObjectChunk(child); if (e.vertices != null && e.triangles != null) { e.CalculateNormals(); model.Entities.Add(e); } break; default: SkipChunk(child); break; } chunk.BytesRead += child.BytesRead; // Don't skip to the end of certain chunks, as they contain the entire file/model. if (child.ID != Groups.C_VERSION) { // Skip to the end of this chunk. child.SkipToEnd(reader); } //Console.WriteLine ( "ID: {0} Length: {1} Read: {2}", chunk.ID.ToString("x"), chunk.Length , chunk.BytesRead ); } }