Exemplo n.º 1
0
 public void TrySaveCache(String fileName)
 {
     try
     {
         using (var fs = new FileStream(fileName + ".cache", FileMode.Create, FileAccess.Write))
         {
             using (BinaryWriter bw = new BinaryWriter(fs))
             {
                 bw.Write(VERSION);
                 bw.Write(m_Vertices.Count);
                 foreach (var v in m_Vertices)
                 {
                     byte[] rawVertex = StructTools.RawSerialize(v);
                     bw.Write(rawVertex);
                 }
                 bw.Write(m_Indices.Count);
                 foreach (var i in m_Indices)
                 {
                     bw.Write(i);
                 }
                 bw.Write(m_MaterialIndices.Count);
                 foreach (var i in m_MaterialIndices)
                 {
                     bw.Write(i);
                 }
                 bw.Write(m_Materials.Count);
                 foreach (var i in m_Materials)
                 {
                     bw.Write(i);
                 }
                 bw.Write(m_MatTexMappingDiffuse.Count);
                 foreach (var i in m_MatTexMappingDiffuse)
                 {
                     bw.Write(i.Key);
                     bw.Write(i.Value);
                 }
                 bw.Write(m_MatTexMappingNormalMap.Count);
                 foreach (var i in m_MatTexMappingNormalMap)
                 {
                     bw.Write(i.Key);
                     bw.Write(i.Value);
                 }
                 byte[] rawBBMin = StructTools.RawSerialize(m_BoundingBoxMin);
                 byte[] rawBBMax = StructTools.RawSerialize(m_BoundingBoxMax);
                 bw.Write(rawBBMin);
                 bw.Write(rawBBMax);
             }
         }
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 2
0
        private void BufferWriteType(CustomConstantBufferDefinition.ConstantType type, Object value)
        {
            switch (type)
            {
            case CustomConstantBufferDefinition.ConstantType.ConstantType_Float:
                m_Writer.Write((Single)value);
                break;

            case CustomConstantBufferDefinition.ConstantType.ConstantType_Int:
                m_Writer.Write((Int32)value);
                break;

            default:
                m_Writer.Write(StructTools.RawSerialize(value));
                break;
            }
        }
Exemplo n.º 3
0
        public bool TryLoadCache(String fileName)
        {
            try
            {
                using (var fs = new FileStream(fileName + ".cache", FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        int ver = br.ReadInt32();
                        if (ver == VERSION)
                        {
                            int    numVertices  = br.ReadInt32();
                            int    sizeOfVertex = Marshal.SizeOf(typeof(MyVertex));
                            byte[] verticesData = br.ReadBytes(sizeOfVertex * (int)numVertices);
                            for (int i = 0; i < numVertices; ++i)
                            {
                                // read vert
                                MyVertex vert = StructTools.RawDeserialize <MyVertex>(verticesData, i * sizeOfVertex);
                                m_Vertices.Add(vert);
                            }

                            uint numIndices = br.ReadUInt32();
                            for (uint i = 0; i < numIndices; ++i)
                            {
                                // read index
                                m_Indices.Add(br.ReadInt32());
                            }

                            uint numMaterialIndices = br.ReadUInt32();
                            for (uint i = 0; i < numMaterialIndices; ++i)
                            {
                                // read material index
                                m_MaterialIndices.Add(br.ReadInt32());
                            }

                            uint numMaterials = br.ReadUInt32();
                            for (uint i = 0; i < numMaterials; ++i)
                            {
                                // read material
                                m_Materials.Add(br.ReadString());
                            }

                            uint numMaterialsTexMappingDiffuse = br.ReadUInt32();
                            for (uint i = 0; i < numMaterialsTexMappingDiffuse; ++i)
                            {
                                // read material
                                string s1 = br.ReadString();
                                string s2 = br.ReadString();
                                m_MatTexMappingDiffuse.Add(s1, s2);
                            }

                            uint numMaterialsTexMappingNormal = br.ReadUInt32();
                            for (uint i = 0; i < numMaterialsTexMappingNormal; ++i)
                            {
                                // read material
                                string s1 = br.ReadString();
                                string s2 = br.ReadString();
                                m_MatTexMappingNormalMap.Add(s1, s2);
                            }

                            int    sizeOfVector3 = Marshal.SizeOf(typeof(Vector3));
                            byte[] bbData        = br.ReadBytes(sizeOfVector3 * 2);
                            m_BoundingBoxMin = StructTools.RawDeserialize <Vector3>(bbData, 0);
                            m_BoundingBoxMax = StructTools.RawDeserialize <Vector3>(bbData, sizeOfVector3);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }