Exemple #1
0
        void LoadMesh(string name, string fileName)
        {
            Name = name;
            XmlDocument XMLDoc = null;
            XmlElement XMLRoot;
            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName))
                {
                    // tiedosto muistiin
                    string data = file.ReadToEnd();
                    XMLDoc = new XmlDocument();
                    XMLDoc.LoadXml(data);
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }

            // Validate the File
            XMLRoot = XMLDoc.DocumentElement;
            if (XMLRoot.Name != "mesh")
            {
                Log.Error("Error [" + fileName + "] Invalid .mesh.xml File. Missing <mesh>");
            }

            bool isPath = false; // jos meshi on pathi
            if (name.StartsWith("Path_")) isPath = true;

            // Process the mesh
            processMesh(XMLRoot, isPath);

            if (isPath == false)
            {
                Vbo = new VBO();
                Vbo.DataToVBO(VertexBuffer, IndexBuffer, VBO.VertexMode.UV1);

                Boundings = new BoundingSphere();
                Boundings.CreateBoundingVolume(this);

                // lataa shader
                string shader = Material.ShaderName;
                if (shader != "")
                {
                    Vbo.Shader = GLSLShader.Load(shader);
                }
            }
            else
            {
                Path path = new Path();
                path.AddPath(name, VertexBuffer);
            }
        }
Exemple #2
0
 public static bool ObjectInFrustum(float x, float y, float z, BoundingSphere bound, Vector3 scale)
 {
     if (bound == null) return true;
     float max = scale.X;
     if (scale.Y > max) max = scale.Y;
     if (scale.Z > max) max = scale.Z;
     if (SphereInFrustum(x, y, z, bound.R * max) == 0) return false;
     return true;
 }
Exemple #3
0
 public static bool ObjectInFrustum(Vector3 position, BoundingSphere bound, Vector3 scale)
 {
     return ObjectInFrustum(position.X, position.Y, position.Z, bound, scale);
 }
Exemple #4
0
        /// <summary>
        /// lataa md5-animaatio.
        /// </summary>
        public override void LoadMD5Animation(string animName, string fileName)
        {
            if (fileName == null || fileName == "") return;

            Animation anim = new Animation();
            anim.animName = animName;

            Buffer t = new Buffer();
            MD5JointInfo[] jointInfos = null;
            MD5BaseFrameJoint[] baseFrame = null;
            float[] animFrameData = null;
            int numAnimatedComponents = 0;
            int frame_index;
            int i;

            using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName))
            {
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if (line == "") continue;

                    // Read number of joints
                    if (ParseLine(ref t, line, "numFrames %d"))
                    {
                        /* Allocate memory for skeleton frames and bounding boxes */
                        anim.numFrames = t.ibuffer[0];
                        if (anim.numFrames > 0)
                        {
                            anim.bboxes = new MD5BoundingBox[anim.numFrames];
                        }
                    }

                    if (ParseLine(ref t, line, "numJoints %d"))
                    {
                        /* Allocate memory for joints of each frame */
                        anim.numJoints = t.ibuffer[0];
                        if (anim.numJoints > 0)
                        {
                            /* Allocate temporary memory for building skeleton frames */
                            jointInfos = new MD5JointInfo[anim.numJoints];
                            baseFrame = new MD5BaseFrameJoint[anim.numJoints];
                        }
                        anim.skelFrames = new MD5Joint[anim.numFrames, anim.numJoints];
                    }

                    if (ParseLine(ref t, line, "frameRate %d"))
                    {
                        anim.frameRate = t.ibuffer[0];
                    }

                    if (ParseLine(ref t, line, "numAnimatedComponents %d"))
                    {
                        numAnimatedComponents = t.ibuffer[0];
                        if (numAnimatedComponents > 0)
                        {
                            /* Allocate memory for animation frame data */
                            animFrameData = new float[numAnimatedComponents];
                        }

                    }

                    if (line.Equals("hierarchy {"))
                    {
                        for (i = 0; i < anim.numJoints; ++i)
                        {
                            /* Read whole line */
                            line = file.ReadLine();
                            Cleanstring(ref line);

                            /* Read joint info */
                            ParseLine(ref t, line, "%s %d %d %d");
                            jointInfos[i].name = t.sbuffer;
                            jointInfos[i].parent = t.ibuffer[0];
                            jointInfos[i].flags = t.ibuffer[1];
                            jointInfos[i].startIndex = t.ibuffer[2];
                        }
                    }

                    if (line.Equals("bounds {"))
                    {
                        for (i = 0; i < anim.numFrames; ++i)
                        {
                            /* Read whole line */
                            line = file.ReadLine();
                            Cleanstring(ref line);

                            /* Read bounding box */
                            ParseLine(ref t, line, "( %f %f %f ) ( %f %f %f )");
                            anim.bboxes[i].min.X = t.fbuffer[0];
                            anim.bboxes[i].min.Y = t.fbuffer[1];
                            anim.bboxes[i].min.Z = t.fbuffer[2];
                            anim.bboxes[i].max.X = t.fbuffer[3];
                            anim.bboxes[i].max.Y = t.fbuffer[4];
                            anim.bboxes[i].max.Z = t.fbuffer[5];
                        }
                    }

                    if (line.Equals("baseframe {"))
                    {
                        for (i = 0; i < anim.numJoints; ++i)
                        {
                            /* Read whole line */
                            line = file.ReadLine();
                            Cleanstring(ref line);

                            /* Read base frame joint */
                            ParseLine(ref t, line, "( %f %f %f ) ( %f %f %f )");

                            if (t.fbuffer.Length == 6)
                            {
                                baseFrame[i].pos.X = t.fbuffer[0];
                                baseFrame[i].pos.Y = t.fbuffer[1];
                                baseFrame[i].pos.Z = t.fbuffer[2];
                                baseFrame[i].orient.X = t.fbuffer[3];
                                baseFrame[i].orient.Y = t.fbuffer[4];
                                baseFrame[i].orient.Z = t.fbuffer[5];

                                /* Compute the w component */
                                QuaternionExt.ComputeW(ref baseFrame[i].orient);

                            }
                        }
                    }

                    if (ParseLine(ref t, line, "frame %d"))
                    {
                        frame_index = t.ibuffer[0];

                        /* Read frame data */
                        for (i = 0; i < numAnimatedComponents; )
                        {
                            line = file.ReadLine();
                            if (line[0] == '}') break;
                            Cleanstring(ref line);
                            string[] splt = line.Split(' ');
                            for (int ww = 0; ww < splt.Length; ww++) animFrameData[i++] = MathExt.GetFloat(splt[ww]);
                        }
                        /* Build frame skeleton from the collected data */
                        BuildFrameSkeleton(ref jointInfos, ref baseFrame, ref animFrameData, frame_index, anim.numJoints, ref anim);
                    }
                }

                anim.curFrame = 0;
                anim.nextFrame = 1;

                anim.lastTime = 0;
                anim.maxTime = 1.0f / anim.frameRate;

                /* Allocate memory for animated skeleton */
                skeleton = new MD5Joint[anim.numJoints];
                animated = true;

                Vector3 min = new Vector3(9999, 9999, 9999);
                Vector3 max = new Vector3(-9999, -9999, -9999);

                // laske bboxit
                for (int q = 0; q < anim.numFrames; q++)
                {
                    if (anim.bboxes[q].min.X < min.X) min.X = anim.bboxes[q].min.X;
                    if (anim.bboxes[q].min.Y < min.Y) min.Y = anim.bboxes[q].min.Y;
                    if (anim.bboxes[q].min.Z < min.Z) min.Z = anim.bboxes[q].min.Z;

                    if (anim.bboxes[q].max.X > max.X) max.X = anim.bboxes[q].max.X;
                    if (anim.bboxes[q].max.Y > max.Y) max.Y = anim.bboxes[q].max.Y;
                    if (anim.bboxes[q].max.Z > max.Z) max.Z = anim.bboxes[q].max.Z;
                }

                Boundings = new BoundingSphere();
                Boundings.CreateBoundingVolume(this, min, max);

                Update(0);
                animations.Add(anim);

                Log.WriteLine("Animation: " + fileName, false);

                SetAnimation(animName);
            }
        }