예제 #1
0
        public void ExportWavefrontObject(Shader[] shaders)
        {
            WavefrontObject wfo = new WavefrontObject();
            wfo.Vertices = new List<Vector3>( _Vertices);
            wfo.VertexCount = _Vertices.Length;
            wfo.Texcoords = new List<Vector2>(Texcoords);
            wfo.TexcoordCount = Texcoords.Length;
            wfo.Normals = new List<Vector3>(Normals);
            wfo.NormalCount = Normals.Length;
            wfo.Materials = new Dictionary<string, int>();
            for (int i = 0; i < shaders.Length; i++)
                wfo.Materials.Add("Default_" + i.ToString(), i);
            wfo.GroupCount = Groups.Length;


            List<Face> FaceArray = new List<Face>(Indices.Length);

            for (int i = 0; i < Groups.Length; i++)
            {
                int Start = Groups[i].IndiceStart;
                int End = Groups[i].IndiceStart + Groups[i].IndiceCount - 2;
                bool Winding = true;

                for (int x = Start; x < End; x++)
                {
                    Face Temp = new Face(
                        Indices[x], 
                        Indices[x + 1], 
                        Indices[x + 2], 
                        Indices[x], 
                        Indices[x + 1], 
                        Indices[x + 2], 
                        Indices[x], 
                        Indices[x + 1], 
                        Indices[x + 2]
                        );
                    Temp.MaterialID = Groups[i].ShaderIndex;
                    Temp.GroupID = i;
                    if (!Temp.IsDegenerate)
                    {
                        FaceArray.Add(Temp);
                        if (Winding == false)
                        {
                            short y = Temp.VertexIndices[1];
                            short z = Temp.VertexIndices[2];
                            Temp.VertexIndices[1] = z;
                            Temp.VertexIndices[2] = y;
                            y = Temp.TexcoordIndices[1];
                            z = Temp.TexcoordIndices[2];
                            Temp.TexcoordIndices[1] = z;
                            Temp.TexcoordIndices[2] = y;
                            y = Temp.NormalIndices[1];
                            z = Temp.NormalIndices[2];
                            Temp.NormalIndices[1] = z;
                            Temp.NormalIndices[2] = y;
                            Winding = true;
                        }
                        else
                        {
                            Winding = false;
                        }

                    }
                    else
                    {
                        if (Winding == false) { Winding = true; }
                        else { Winding = false; }
                    }
                }
            }
            FaceArray.TrimExcess();
            wfo.Faces = FaceArray;
            wfo.FaceCount = FaceArray.Count;

            Wavefront.CreateWavefrontOBJFile(wfo, "O:\\test.obj");
        }
예제 #2
0
        public Model(Tag tag)
        {
            BinaryReader br = new BinaryReader(tag.TagStream);
            tag.TagStream.Position = 0;
            buffer = br.ReadBytes(Size);
            tag.TagStream.Position = 0;
            Name = tag.Strings[br.ReadInt32()];

            #region Bounding Boxes

            tag.TagStream.Position = 20;
            int Count = br.ReadInt32();
            if (Count == 1)
            {
                int Offset = br.ReadInt32();
                tag.TagStream.Position = Offset;
                Space = new CompressionInfo(tag.TagStream);
            }
            else throw new Exception();

            #endregion

            #region Mesh Regions

            tag.TagStream.Position = 28;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                Regions = new Region[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * Region.Size);
                    Regions[i] = new Region(tag);
                }
            }

            #endregion

            #region Mesh Sections

            tag.TagStream.Position = 36;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                Sections = new Section[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * Section.Size);
                    Sections[i] = new Section(tag, Space);
                }
            }

            #endregion

            #region Section Groups

            tag.TagStream.Position = 52;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                SectionGroups = new SectionGroup[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * MarkerGroup.Size);
                    SectionGroups[i] = new SectionGroup(tag.TagStream);
                }
            }

            #endregion

            #region Nodes

            tag.TagStream.Position = 72;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                Nodes = new Node[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * Node.Size);
                    Nodes[i] = new Node(tag);
                }
            }

            #endregion            

            #region Marker Groups

            tag.TagStream.Position = 88;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                MarkerGroups = new MarkerGroup[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * MarkerGroup.Size);
                    MarkerGroups[i] = new MarkerGroup(tag);
                }
            }

            #endregion

            #region Shaders

            tag.TagStream.Position = 96;
            Count = br.ReadInt32();
            if (Count > 0)
            {
                int Offset = br.ReadInt32();
                Shaders = new Shader[Count];
                for (int i = 0; i < Count; i++)
                {
                    tag.TagStream.Position = Offset + (i * Shader.Size);
                    Shaders[i] = new Shader(tag);
                }
            }

            #endregion
        }