Exemple #1
0
        public void SetModel(Md3Model model)
        {
            this.Text = string.Format(@"Model '{0}' Properties", model.Name);

            trvModelProperties.Nodes.Clear();
            TreeNode root         = trvModelProperties.Nodes.Add(model.Name);
            TreeNode texturesNode = root.Nodes.Add(string.Format("Textures ({0}): ", model.textures.Count));

            foreach (string filename in model.realnames)
            {
                texturesNode.Nodes.Add(filename);
            }

            root.Nodes.Add("Num Vertices: " + model.TotalVertices.ToString());
            root.Nodes.Add("Num Faces: " + model.TotalFaces.ToString());
            root.Nodes.Add("Num Frames: " + model.frames.Length.ToString());
            TreeNode tagsNode = root.Nodes.Add(string.Format("Tags ({0}): ", model.tags.Length));

            for (int i = 0; i < model.tags.Length; i++)
            {
                TreeNode tagNode = tagsNode.Nodes.Add(model.tags [i].name);
                tagNode.Nodes.Add("Origin: " + model.tags [i].origin.ToString());
                tagNode.Nodes.Add("Axis X: " + model.tags [i].axisX.ToString());
                tagNode.Nodes.Add("Axis Y: " + model.tags [i].axisY.ToString());
                tagNode.Nodes.Add("Axis Z: " + model.tags [i].axisZ.ToString());
            }

            TreeNode meshesNode = root.Nodes.Add(string.Format("Meshes ({0}): ", model.dxMeshes.Length));

            for (int i = 0; i < model.meshes.Length; i++)
            {
                Md3Mesh  mesh     = model.meshes [i];
                TreeNode meshNode = meshesNode.Nodes.Add(mesh.name);
                meshNode.Nodes.Add("Texture: " + mesh.textures [0].name);
                meshNode.Nodes.Add("Num Vertices: " + mesh.numVertices.ToString());
                meshNode.Nodes.Add("Num Faces: " + mesh.numFaces.ToString());
                meshNode.Nodes.Add("Num Textures: " + mesh.numTextures.ToString());

                TreeNode framesNode = meshNode.Nodes.Add(string.Format("Frames ({0}): ", mesh.numFrames));

                for (int j = 0; j < model.frames.Length; j++)
                {
                    Md3Frame frame     = model.frames [j];
                    TreeNode frameNode = framesNode.Nodes.Add(string.Format("Frame [{0}] {1}:", j, frame.name));
                    frameNode.Nodes.Add("Local Origin: " + frame.localOrigin.ToString());
                    frameNode.Nodes.Add("Raduis: " + frame.radius.ToString());
                    frameNode.Nodes.Add("Mins: " + frame.mins.ToString());
                    frameNode.Nodes.Add("Maxs: " + frame.maxs.ToString());
                }
            }

            trvModelProperties.ExpandAll();
        }
Exemple #2
0
        public Md3Model(string filename, Device d3dDevice, Q3ModelViewerForm parent)
        {
            this.d3dDevice = d3dDevice;
            this.parent    = parent;

            int lastSlash = filename.LastIndexOf('/');

            name = filename.Substring(lastSlash + 1);
            name = name.Substring(0, name.Length - 4);

            MemoryStream ms = new MemoryStream();

            Q3FileSystem.WriteResourceToStream(filename, ms);

            header.FromUnsafe(( Md3HeaderUnsafe )ReadStruct(ms, typeof(Md3HeaderUnsafe)));

            // Frames
            ms.Position = header.framesStart;
            frames      = new Md3Frame [header.numFrames];

            for (int i = 0; i < header.numFrames; i++)
            {
                frames [i].FromUnsafe(( Md3FrameUnsafe )ReadStruct(ms, typeof(Md3FrameUnsafe)));
            }

            // Tags
            ms.Position = header.tagsStart;
            tags        = new Md3Tag [header.numTags];

            for (int i = 0; i < header.numTags; i++)
            {
                tags [i].FromUnsafe(( Md3TagUnsafe )ReadStruct(ms, typeof(Md3TagUnsafe)));
            }

            // Meshes
            int meshStart = header.meshesStart;

            ms.Position = meshStart;
            meshes      = new Md3Mesh [header.numMeshes];

            for (int i = 0; i < header.numMeshes; i++)
            {
                Md3Mesh md3Mesh = new Md3Mesh();
                md3Mesh.FromUnsafe(( Md3MeshUnsafe )ReadStruct(ms, typeof(Md3MeshUnsafe)));

                // Mesh Textures
                ms.Position      = meshStart + md3Mesh.texturesStart;
                md3Mesh.textures = new Md3Texture [md3Mesh.numTextures];

                for (int j = 0; j < md3Mesh.numTextures; j++)
                {
                    md3Mesh.textures [j].FromUnsafe(( Md3TextureUnsafe )ReadStruct(ms, typeof(Md3TextureUnsafe)));
                }

                // Mesh Faces
                ms.Position   = meshStart + md3Mesh.facesStart;
                md3Mesh.faces = new Md3Face [md3Mesh.numFaces];

                for (int j = 0; j < md3Mesh.numFaces; j++)
                {
                    md3Mesh.faces [j].FromUnsafe(( Md3FaceUnsafe )ReadStruct(ms, typeof(Md3FaceUnsafe)));
                }

                // Mesh TexCoords
                ms.Position       = meshStart + md3Mesh.texCoordsStart;
                md3Mesh.texCoords = new Md3TexCoord [md3Mesh.numVertices];

                for (int j = 0; j < md3Mesh.numVertices; j++)
                {
                    md3Mesh.texCoords [j] = ( Md3TexCoord )ReadStruct(ms, typeof(Md3TexCoord));
                }

                // Vertices
                ms.Position      = meshStart + md3Mesh.verticesStart;
                md3Mesh.vertices = new Md3PositionNormal [md3Mesh.numVertices];

                for (int j = 0; j < md3Mesh.numVertices; j++)
                {
                    md3Mesh.vertices [j].FromUnsafe(( Md3PositionNormalUnsafe )ReadStruct(ms, typeof(Md3PositionNormalUnsafe)));
                }

                meshes [i]  = md3Mesh;
                ms.Position = meshStart + md3Mesh.size;
                meshStart   = ( int )ms.Position;
            }

            // Load to dx mesh objects
            dxMeshes = new Mesh [meshes.Length];
            radiuses = new float [meshes.Length];
            centers  = new Vector3 [meshes.Length];

            for (int i = 0; i < meshes.Length; i++)
            {
                Md3Mesh md3Mesh = meshes [i];
                Mesh    mesh    = new Mesh(md3Mesh.numFaces, md3Mesh.numVertices,
                                           MeshFlags.WriteOnly,
                                           CustomVertex.PositionNormalTextured.Format, d3dDevice);

                GraphicsStream vbStream = mesh.VertexBuffer.Lock(0, 0, LockFlags.Discard);

                for (int j = 0; j < md3Mesh.numVertices; j++)
                {
                    Md3TexCoord texCoord = md3Mesh.texCoords [j];
                    vbStream.Write(new CustomVertex.PositionNormalTextured(md3Mesh.vertices [j].pos, Vector3.Empty, texCoord.u, texCoord.v));
                }

                vbStream.Position = 0;
                radiuses [i]      = Geometry.ComputeBoundingSphere(vbStream,
                                                                   md3Mesh.numVertices,
                                                                   CustomVertex.PositionNormalTextured.Format,
                                                                   out centers [i]);

                mesh.VertexBuffer.Unlock();

                GraphicsStream ibStream = mesh.IndexBuffer.Lock(0, 0, LockFlags.Discard);

                for (int j = 0; j < md3Mesh.numFaces; j++)
                {
                    Md3Face face = md3Mesh.faces [j];
                    ibStream.Write(( short )face.indices [0]);
                    ibStream.Write(( short )face.indices [1]);
                    ibStream.Write(( short )face.indices [2]);
                }

                mesh.IndexBuffer.Unlock();

                dxMeshes [i] = mesh;

                // Load textures
                for (int j = 0; j < md3Mesh.numTextures; j++)
                {
                    string  realname = "";
                    Texture t        = null;

                    try { t = LoadTexture(md3Mesh.textures [j].name, out realname); }
                    catch {}

                    textures.Add(t != null ? t : parent.textureNotFound);
                    realnames.Add(realname);
                }
            }

            if (header.numMeshes > 0)
            {
                totalCenter = centers [0];
                totalRadius = radiuses [0];

                for (int i = 1; i < centers.Length; i++)
                {
                    totalCenter = (totalCenter + centers [i]) * 0.5f;
                    totalRadius = ((totalCenter - centers [i]).Length() + totalRadius + radiuses [i]) / 2;
                }
            }
        }