Exemplo n.º 1
0
        void LoadModel(string path)
        {
            FileStream   stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryReader br     = new BinaryReader(stream);
            md2_t        header = new md2_t();

            header.ident      = br.ReadInt32();
            header.version    = br.ReadInt32();
            header.skinheight = br.ReadInt32();
            header.skinwidth  = br.ReadInt32();
            header.framesize  = br.ReadInt32();

            header.num_skins  = br.ReadInt32();
            header.num_xyz    = br.ReadInt32();
            header.num_st     = br.ReadInt32();
            header.num_tris   = br.ReadInt32();
            header.num_glcmds = br.ReadInt32();
            header.num_frames = br.ReadInt32();

            header.ofs_skins  = br.ReadInt32();
            header.ofs_st     = br.ReadInt32();
            header.ofs_tris   = br.ReadInt32();
            header.ofs_frames = br.ReadInt32();
            header.ofs_glcmds = br.ReadInt32();
            header.ofs_end    = br.ReadInt32();


            br.BaseStream.Seek(header.ofs_skins, SeekOrigin.Begin);

            skins = new List <string>();

            for (int i = 0; i < header.num_skins; i++)
            {
                string s = new string(br.ReadChars(64));
                skins.Add(s);
            }

            UVData = new List <vec2>();
            br.BaseStream.Seek(header.ofs_st, SeekOrigin.Begin);
            for (int i = 0; i < header.num_st; i++)
            {
                vec2 uv = new vec2();
                uv.x = ((float)br.ReadInt16()) / header.skinwidth;
                uv.y = ((float)br.ReadInt16()) / header.skinheight;
                UVData.Add(uv);
            }
            int x = header.num_xyz - header.num_st;

            if (x > 0)
            {
                for (int i = 0; i < x; i++)
                {
                    UVData.Add(new vec2(0));
                }
            }

            indices = new List <int>();
            br.BaseStream.Seek(header.ofs_tris, SeekOrigin.Begin);
            for (int i = 0; i < header.num_tris; i++)
            {
                indices.Add(br.ReadUInt16());
                indices.Add(br.ReadUInt16());
                indices.Add(br.ReadUInt16());
                br.ReadUInt16();
                br.ReadUInt16();
                br.ReadUInt16();

                //loop on indices each 3 consecutive vertices compute normal
            }


            br.BaseStream.Seek(header.ofs_frames, SeekOrigin.Begin);

            vVertices     = new List <List <vec3> >();
            vNormalsindex = new List <List <int> >();
            vNormals      = new List <List <vec3> >();
            for (int i = 0; i < header.num_frames; i++)
            {
                br.BaseStream.Seek((header.ofs_frames + (i * header.framesize)), SeekOrigin.Begin);

                vVertices.Add(new List <vec3>(header.num_xyz));
                vNormalsindex.Add(new List <int>(header.num_xyz));
                vNormals.Add(new List <vec3>(header.num_xyz));

                vec3   scale = new vec3();
                byte[] buffer;
                buffer  = br.ReadBytes(4);
                scale.x = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                scale.y = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                scale.z = BitConverter.ToSingle(buffer, 0);

                vec3 trans = new vec3();
                buffer  = br.ReadBytes(4);
                trans.x = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                trans.y = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                trans.z = BitConverter.ToSingle(buffer, 0);

                string name = new string(br.ReadChars(16));

                List <vertex_t> vts = new List <vertex_t>();
                for (int j = 0; j < header.num_xyz; j++)
                {
                    vertex_t vt = new vertex_t();
                    vt.v = br.ReadBytes(3);
                    vt.lightnormalindex = br.ReadByte();
                    vts.Add(vt);
                }
                frame_t f = new frame_t();
                f.scale     = scale.to_array();
                f.translate = trans.to_array();
                f.name      = name.ToCharArray();
                f.verts     = vts;

                for (int j = 0; j < header.num_xyz; j++)
                {
                    vVertices[i].Add(new vec3(0));
                    vNormals[i].Add(new vec3(0));
                    vVertices[i][j] = new vec3()
                    {
                        x = f.translate[0] + ((float)f.verts[j].v[0]) * f.scale[0],
                        y = f.translate[1] + ((float)f.verts[j].v[1]) * f.scale[1],
                        z = f.translate[2] + ((float)f.verts[j].v[2]) * f.scale[2]
                    };

                    vNormalsindex[i].Add(f.verts[j].lightnormalindex);
                }
                for (int j = 0; j < indices.Count - 3; j += 3)
                {
                    vec3 n1     = vVertices[i][indices[j + 1]] - vVertices[i][indices[j]];
                    vec3 n2     = vVertices[i][indices[j + 2]] - vVertices[i][indices[j]];
                    vec3 normal = glm.cross(n1, n2);
                    vNormals[i][indices[j]]     = normal;
                    vNormals[i][indices[j + 1]] = normal;
                    vNormals[i][indices[j + 2]] = normal;
                }
            }
            Texture tex = null;

            if (skins.Count > 1)
            {
                tex = new Texture(skins[1], 20, false);
            }
            else
            {
                FileInfo fi       = new FileInfo(path);
                string   name     = fi.Name;
                int      index    = fi.FullName.IndexOf(name, StringComparison.CurrentCulture);
                string   filePath = "";
                for (int i = 0; i < index; i++)
                {
                    filePath += fi.FullName[i];
                }
                string[] extensions = { "jpg", "jpeg", "png", "bmp" };
                for (int i = 0; i < 4; i++)
                {
                    string stry = filePath + name.Split('.')[0] + "." + extensions[i];
                    tex = new Texture(stry, 20, false);
                    if (tex.width > 0)
                    {
                        break;
                    }
                }
            }

            mVertices = vVertices[0];


            m               = new Model();
            m.indices       = indices;
            m.texture       = tex;
            m.vertices      = vVertices[0];
            m.uvCoordinates = UVData;
            m.normals       = vNormals[0];
            m.Initialize();

            vs = new float[m.vertices.Count * 3];
            ns = new float[m.vertices.Count * 3];
        }
Exemplo n.º 2
0
        public void LoadModel(string path, bool flip = true)
        {
            FileStream   stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryReader br     = new BinaryReader(stream);
            md2_t        header = new md2_t();

            header.ident      = br.ReadInt32();
            header.version    = br.ReadInt32();
            header.skinheight = br.ReadInt32();
            header.skinwidth  = br.ReadInt32();
            header.framesize  = br.ReadInt32();

            header.num_skins  = br.ReadInt32();
            header.num_xyz    = br.ReadInt32();
            header.num_st     = br.ReadInt32();
            header.num_tris   = br.ReadInt32();
            header.num_glcmds = br.ReadInt32();
            header.num_frames = br.ReadInt32();

            header.ofs_skins  = br.ReadInt32();
            header.ofs_st     = br.ReadInt32();
            header.ofs_tris   = br.ReadInt32();
            header.ofs_frames = br.ReadInt32();
            header.ofs_glcmds = br.ReadInt32();
            header.ofs_end    = br.ReadInt32();


            br.BaseStream.Seek(header.ofs_skins, SeekOrigin.Begin);

            skins = new List <string>();

            for (int i = 0; i < header.num_skins; i++)
            {
                string s = new string(br.ReadChars(64));
                skins.Add(s);
            }

            UVData = new List <vec2>();
            br.BaseStream.Seek(header.ofs_st, SeekOrigin.Begin);
            for (int i = 0; i < header.num_st; i++)
            {
                vec2 uv = new vec2();
                uv.x = ((float)br.ReadInt16()) / header.skinwidth;
                uv.y = ((float)br.ReadInt16()) / header.skinheight;
                UVData.Add(uv);
            }
            int x = header.num_xyz - header.num_st;

            if (x > 0)
            {
                for (int i = 0; i < x; i++)
                {
                    UVData.Add(new vec2(0));
                }
            }

            indices = new List <int>();
            br.BaseStream.Seek(header.ofs_tris, SeekOrigin.Begin);
            for (int i = 0; i < header.num_tris; i++)
            {
                indices.Add(br.ReadUInt16());
                indices.Add(br.ReadUInt16());
                indices.Add(br.ReadUInt16());
                br.ReadUInt16();
                br.ReadUInt16();
                br.ReadUInt16();

                //loop on indices each 3 consecutive vertices compute normal
            }


            br.BaseStream.Seek(header.ofs_frames, SeekOrigin.Begin);

            vVertices     = new List <List <vec3> >();
            vNormalsindex = new List <List <int> >();
            vNormals      = new List <List <vec3> >();
            for (int i = 0; i < header.num_frames; i++)
            {
                br.BaseStream.Seek((header.ofs_frames + (i * header.framesize)), SeekOrigin.Begin);

                vVertices.Add(new List <vec3>(header.num_xyz));
                vNormalsindex.Add(new List <int>(header.num_xyz));
                vNormals.Add(new List <vec3>(header.num_xyz));

                vec3   scale = new vec3();
                byte[] buffer;
                buffer  = br.ReadBytes(4);
                scale.x = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                scale.y = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                scale.z = BitConverter.ToSingle(buffer, 0);

                vec3 trans = new vec3();
                buffer  = br.ReadBytes(4);
                trans.x = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                trans.y = BitConverter.ToSingle(buffer, 0);
                buffer  = br.ReadBytes(4);
                trans.z = BitConverter.ToSingle(buffer, 0);

                string name = new string(br.ReadChars(16));

                List <vertex_t> vts = new List <vertex_t>();
                for (int j = 0; j < header.num_xyz; j++)
                {
                    vertex_t vt = new vertex_t();
                    vt.v = br.ReadBytes(3);
                    vt.lightnormalindex = br.ReadByte();
                    vts.Add(vt);
                }
                frame_t f = new frame_t();
                f.scale     = scale.to_array();
                f.translate = trans.to_array();
                f.name      = name.ToCharArray();
                f.verts     = vts;

                for (int j = 0; j < header.num_xyz; j++)
                {
                    vVertices[i].Add(new vec3(0));
                    vNormals[i].Add(new vec3(0));
                    vVertices[i][j] = new vec3()
                    {
                        x = f.translate[0] + ((float)f.verts[j].v[0]) * f.scale[0],
                        y = f.translate[1] + ((float)f.verts[j].v[1]) * f.scale[1],
                        z = f.translate[2] + ((float)f.verts[j].v[2]) * f.scale[2]
                    };

                    vNormalsindex[i].Add(f.verts[j].lightnormalindex);
                }
                for (int j = 0; j < indices.Count - 3; j += 3)
                {
                    vec3 n1     = vVertices[i][indices[j + 1]] - vVertices[i][indices[j]];
                    vec3 n2     = vVertices[i][indices[j + 2]] - vVertices[i][indices[j]];
                    vec3 normal = glm.cross(n1, n2);
                    vNormals[i][indices[j]]     = normal;
                    vNormals[i][indices[j + 1]] = normal;
                    vNormals[i][indices[j + 2]] = normal;
                }
            }
            Texture tex = null;

            if (skins.Count > 1)
            {
                tex = new Texture(skins[1], 20, true);
            }
            else
            {
                FileInfo fi       = new FileInfo(path);
                string   name     = fi.Name;
                int      index    = fi.FullName.IndexOf(name, StringComparison.CurrentCulture);
                string   filePath = "";
                for (int i = 0; i < index; i++)
                {
                    filePath += fi.FullName[i];
                }
                string[] extensions = { "jpg", "jpeg", "png", "bmp" };
                for (int i = 0; i < 4; i++)
                {
                    string stry = filePath + name.Split('.')[0] + "." + extensions[i];
                    tex = new Texture(stry, 20, true);
                    if (tex.width > 0)
                    {
                        break;
                    }
                }
            }

            br.BaseStream.Seek(header.ofs_glcmds, SeekOrigin.Begin);
            List <int> cmds = new List <int>();

            renderModes       = new List <int>();
            numRenderVertices = new List <int>();
            anotherUV         = new List <vec2>();

            AvVertices = new List <List <vec3> >();
            AvNormals  = new List <List <vec3> >();
            CvNormals  = new List <vec3>();
            CvVertices = new List <vec3>();


            vec3 maxx = new vec3(int.MinValue, 0, 0);
            vec3 maxy = new vec3(0, int.MinValue, 0);
            vec3 maxz = new vec3(0, 0, int.MinValue);
            vec3 minz = new vec3(0, 0, int.MaxValue);
            vec3 minx = new vec3(int.MaxValue, 0, 0);
            vec3 miny = new vec3(0, int.MaxValue, 0);

            for (int j = 0; j < header.num_frames; j++)
            {
                AvVertices.Add(new List <vec3>());
                AvNormals.Add(new List <vec3>());
            }

            for (int i = 0; i < header.num_glcmds; i++)
            {
                cmds.Add(br.ReadInt32());
            }
            int k = 0;

            while (true)
            {
                int action;
                if (cmds.Count > 0)
                {
                    action = cmds[k];
                    if (action == 0)
                    {
                        break;
                    }
                }
                else
                {
                    action = 1;
                }

                int renderMode  = action < 0 ? Gl.GL_TRIANGLE_FAN : Gl.GL_TRIANGLE_STRIP;
                int numVertices = action < 0 ? -action : action;
                k++;

                renderModes.Add(renderMode);
                numRenderVertices.Add(numVertices);
                for (int i = 0; i < numVertices; i++)
                {
                    unsafe
                    {
                        int   sd = cmds[k++];
                        int   td = cmds[k++];
                        float s  = *((float *)(&sd));
                        float t  = *((float *)(&td));
                        if (flip)
                        {
                            t = 1 - t;
                        }
                        int vi = cmds[k++];
                        anotherUV.Add(new vec2(s, t));
                        for (int j = 0; j < header.num_frames; j++)
                        {
                            if (vVertices[j][vi].x < minx.x)
                            {
                                minx = vVertices[j][vi];
                            }
                            if (vVertices[j][vi].y < miny.y)
                            {
                                miny = vVertices[j][vi];
                            }
                            if (vVertices[j][vi].z < minz.z)
                            {
                                minz = vVertices[j][vi];
                            }

                            if (vVertices[j][vi].x > maxx.x)
                            {
                                maxx = vVertices[j][vi];
                            }
                            if (vVertices[j][vi].y > maxy.y)
                            {
                                maxy = vVertices[j][vi];
                            }
                            if (vVertices[j][vi].z > maxz.z)
                            {
                                maxz = vVertices[j][vi];
                            }


                            AvVertices[j].Add(vVertices[j][vi]);
                            AvNormals[j].Add(vNormals[j][vi]);
                            CvVertices.Add(new vec3(0, 0, 0));
                            CvNormals.Add(new vec3(0, 0, 0));
                        }
                    }
                }
            }

            minxyz = new vec3(minx.x, miny.y, minz.z);
            maxxyz = new vec3(maxx.x, maxy.y, maxz.z);

            mVertices       = vVertices[0];
            m               = new Model();
            m.indices       = indices;
            m.texture       = tex;
            m.vertices      = vVertices[0];
            m.uvCoordinates = UVData;
            m.normals       = vNormals[0];

            vs = new float[AvVertices[0].Count * 3];
            ns = new float[AvNormals[0].Count * 3];


            if (AvVertices.Count > 0)
            {
                vertexBufferID = GPU.GenerateBuffer(AvVertices[0]);
            }
            if (AvNormals.Count > 0)
            {
                normalBufferID = GPU.GenerateBuffer(AvNormals[0]);
            }
            if (anotherUV.Count > 0)
            {
                uvBufferID = GPU.GenerateBuffer(anotherUV);
            }
        }