Example #1
0
        public void SaveP3D(string path)
        {
            BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create));
            String       message;

            // Write header
            writer.Write(MagicWord);
            writer.Write((byte)0x02);
            writer.Write(Size.GetXSize());
            writer.Write(Size.GetYSize());
            writer.Write(Size.GetZSize());

            // Write textures chunk
            message = TextureChunk.WriteChunk(writer);
            if (!message.Equals(String.Empty))
            {
                MessageBox.Show("Saving failed while writing Textures Chunk, Message: " + message);
                return;
            }

            // Write lights chunk
            message = LightsChunk.WriteChunk(writer);
            if (!message.Equals(String.Empty))
            {
                MessageBox.Show("Saving failed while writing Lights Chunk, Message: " + message);
                return;
            }

            // Write meshes chunk
            //MeshesChunk.CalculateMeshChunkSize();
            message = MeshesChunk.WriteChunk(writer);
            if (!message.Equals(String.Empty))
            {
                MessageBox.Show("Saving failed while writing Meshes Chunk, Message: " + message);
                return;
            }

            // Write userdata chunk
            message = UserDataChunk.WriteChunk(writer);
            if (!message.Equals(String.Empty))
            {
                MessageBox.Show("Saving failed while writing User Data Chunk, Message: " + message);
                return;
            }

            writer.Close();
        }
Example #2
0
 public Mesh(MeshesChunk parent)
 {
     Parent = parent;
     Application.Current.Dispatcher.BeginInvoke((Action)(() => addTreeItem()));
 }
Example #3
0
        public async Task LoadP3D(Stream data)
        {
            using (var reader = new BinaryReader(data))
            {
                reader.BaseStream.Position = 0;
                bool success;

                byte[] magicWord = reader.ReadBytes(3);
                if (!Utility.ByteArrayCompare(magicWord, MagicWord))
                {
                    MessageBox.Show("Magic word isn't valid, got " + Encoding.ASCII.GetString(magicWord) + " Should be: " + Encoding.ASCII.GetString(MagicWord));
                    return;
                }

                Version = reader.ReadByte();
                //MessageBox.Show("Version: " + version.ToString());

                Size = new Extent
                {
                    XSize          = reader.ReadSingle(),
                    YSize          = reader.ReadSingle(),
                    ZSize          = reader.ReadSingle(),
                    IsImportedSize = true
                };

                //MessageBox.Show(size.xSize.ToString() + " - " + size.ySize.ToString() + " - " + size.zSize.ToString());

                StatusUpdated(new StatusUpdatedEventArguments("Loading textures info from file", 25));
                await Task.Delay(0).ConfigureAwait(false);

                TextureChunk = new TextureChunk(this);
                byte[] texHeader = reader.ReadBytes(3);
                if (!Utility.ByteArrayCompare(texHeader, TextureChunk.ChunkIdTemplate))
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Expected TEX chunk header, got " + Encoding.ASCII.GetString(texHeader));
                    return;
                }

                TextureChunk.Size   = reader.ReadInt32();
                TextureChunk.TexNum = reader.ReadByte();


                success = TextureChunk.ReadChunk(reader);
                if (!success)
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Couldn't parse Textures chunk");
                    return;
                }

                StatusUpdated(new StatusUpdatedEventArguments("Loading lights from file", 40));
                await Task.Delay(0).ConfigureAwait(false);

                LightsChunk = new LightsChunk(this);
                byte[] lightsHeader = reader.ReadBytes(6);
                if (!Utility.ByteArrayCompare(lightsHeader, LightsChunk.ChunkIdTemplate))
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Expected LIGHTS chunk header, got " + Encoding.ASCII.GetString(lightsHeader));
                    return;
                }

                LightsChunk.Size      = reader.ReadInt32();
                LightsChunk.LightsNum = reader.ReadInt16();

                success = LightsChunk.ReadChunk(reader);
                if (!success)
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Couldn't parse Lights chunk");
                    return;
                }

                StatusUpdated(new StatusUpdatedEventArguments("Loading meshes from file", 40));
                await Task.Delay(0).ConfigureAwait(false);

                MeshesChunk = new MeshesChunk(this);
                byte[] meshesHeader = reader.ReadBytes(6);
                if (!Utility.ByteArrayCompare(meshesHeader, MeshesChunk.ChunkIdTemplate))
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Expected MESHES chunk header, got " + Encoding.ASCII.GetString(meshesHeader));
                    return;
                }

                MeshesChunk.Size      = reader.ReadInt32();
                MeshesChunk.MeshesNum = reader.ReadInt16();


                success = await MeshesChunk.ReadChunk(reader);

                if (!success)
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Couldn't parse Meshes chunk");
                    return;
                }


                MeshesChunk.SeparateSubMeshesEdges();
                MeshesChunk.SeparateUVVertices();
                MeshesChunk.ClearUnusedVertices();


                StatusUpdated(new StatusUpdatedEventArguments("Loading userdata from file", 80));
                await Task.Delay(0).ConfigureAwait(false);

                UserDataChunk = new UserDataChunk(this);
                byte[] userDataHeader = reader.ReadBytes(4);
                if (!Utility.ByteArrayCompare(userDataHeader, UserDataChunk.ChunkIdTemplate))
                {
                    StatusUpdated(new StatusUpdatedEventArguments("Failed to load file", 0));
                    MessageBox.Show("Expected USER chunk header, got " + Encoding.ASCII.GetString(userDataHeader));
                    return;
                }
                UserDataChunk.Size     = reader.ReadInt32();
                UserDataChunk.UserData = reader.ReadBytes(UserDataChunk.Size);

                StatusUpdated(new StatusUpdatedEventArguments("Finished loading", 100));
                await Task.Delay(0).ConfigureAwait(false);

                reader.Close();
            }
        }
Example #4
0
        //        public void LoadP3DAssimp(Scene scene)
        //        {
        //            Version = 0x02;
        //            Size = new Extent();
        //
        //            TextureChunk = new TextureChunk(this);
        //            foreach (Material mat in scene.Materials)
        //            {
        //                if (!TextureChunk.IsTextureInList(mat.TextureDiffuse.FilePath.ToLower()))
        //                {
        //                    TextureChunk.TextureNames.Add(new TextureName(TextureChunk,mat.TextureDiffuse.FilePath.ToLower()));
        //                }
        //            }
        //            TextureChunk.CalculateSizeFromTexturesList();
        //            TextureChunk.CalculateTexNum();
        //
        //            LightsChunk = new LightsChunk(this);
        //            foreach (Assimp.Light light in scene.Lights)
        //            {
        //                string name = light.Name;
        //                float x = light.Position.X;
        //                float y = light.Position.Y;
        //                float z = light.Position.Z;
        //                float radius = 1; //ASSIMP doesn't save point light radius
        //                int color = LightsChunk.ColorFromRGB(Convert.ToInt32(light.ColorDiffuse.R*255), Convert.ToInt32(light.ColorDiffuse.G * 255), Convert.ToInt32(light.ColorDiffuse.B * 255));
        //                bool corona = false;
        //                bool lensFlare = false;
        //                bool lightUpEnv = true;
        //                LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv));
        //            }
        //            LightsChunk.CalculateSizeFromLightsList();
        //            LightsChunk.CalculateLightsNum();
        //
        //            MeshesChunk = new MeshesChunk(this);
        //            MeshesChunk.MeshesNum = Convert.ToInt16(scene.MeshCount);
        //            MeshesChunk.Size = 0;
        //            //MessageBox.Show(scene.RootNode.Name);
        //            foreach (Assimp.Mesh mesh in scene.Meshes)
        //            {
        //               //MessageBox.Show();
        //            }
        //        }

        public void Load3DS(Lib3dsFile scene)
        {
            Version = 0x02;
            Size    = new Extent();

            TextureChunk = new TextureChunk(this);
            foreach (Lib3dsMaterial mat in scene.materials)
            {
                if (mat.texture1_map.name.ToLower().Equals(String.Empty))
                {
                    continue;
                }
                if (!TextureChunk.IsTextureInList(mat.texture1_map.name.ToLower()))
                {
                    TextureChunk.TextureNames.Add(new TextureName(TextureChunk, mat.texture1_map.name.ToLower()));
                }
            }
            if (TextureChunk.TextureNames.Count == 0)
            {
                TextureChunk.TextureNames.Add(new TextureName(TextureChunk, "colwhite.tga"));
            }
            TextureChunk.CalculateSizeFromTexturesList();
            TextureChunk.CalculateTexNum();

            LightsChunk = new LightsChunk(this);
            foreach (Lib3dsLight light in scene.lights)
            {
                string name       = light.name;
                float  x          = light.position[0];
                float  y          = light.position[2];
                float  z          = light.position[1];
                float  radius     = light.inner_range;
                int    color      = LightsChunk.ColorFromRGB(Convert.ToInt32(light.color[0] * 255), Convert.ToInt32(light.color[1] * 255), Convert.ToInt32(light.color[2] * 255));
                bool   corona     = false;
                bool   lensFlare  = false;
                bool   lightUpEnv = true;
                LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv));
            }
            LightsChunk.CalculateSizeFromLightsList();
            LightsChunk.CalculateLightsNum();

            MeshesChunk           = new MeshesChunk(this);
            MeshesChunk.MeshesNum = Convert.ToInt16(scene.meshes.Count);
            MeshesChunk.Size      = 0;
            //MessageBox.Show(scene.RootNode.Name);
            foreach (Lib3dsMesh mesh in scene.meshes)
            {
                Mesh newMesh = new Mesh(MeshesChunk);
                newMesh.Parse3DSMesh(scene, mesh);
                newMesh.SortPolygonsAndGenerateTextureInfo();
                newMesh.SeparateHardEdges();
                newMesh.SeparateUVVertices();
                newMesh.CalculateExtent();
                MeshesChunk.Meshes.Add(newMesh);
            }

            //MeshesChunk.SeparateSubMeshesEdges();
            MeshesChunk.ClearUnusedVertices();
            MeshesChunk.CheckFlagsValidity();
            MeshesChunk.CalculateMeshChunkSize();
            P3DVertex origin = MeshesChunk.CalculateMeshesLocalPos();

            MeshesChunk.MoveMeshesToOrigin();
            LightsChunk.CalculateLightsPostionRelativeToOrigin(origin);

            Size.CalculateExtentFromMeshes(MeshesChunk.Meshes);

            UserDataChunk          = new UserDataChunk(this);
            UserDataChunk.Size     = 4;
            UserDataChunk.UserData = new byte[] { 0, 0, 0, 0 };
        }