Exemplo n.º 1
0
        private void convertDeltaVerticesToAbsolute(float[] vertices, int meshIndex, ushort groupIndex, int currentFrameIndex)
        {
            // find the previous frame
            int previousFrame            = -1;
            int previousFrameVertexIndex = -1;

            for (int frame = currentFrameIndex - 1; frame >= 0; frame--)
            {
                if (_frames[frame * _numMeshes + meshIndex].Active && _frames[frame * _numMeshes + meshIndex].Vertices != null)
                {
                    for (int vertex = 0; vertex < _frames[frame * _numMeshes + meshIndex].Vertices.Count; vertex++)
                    {
                        if (_frames[frame * _numMeshes + meshIndex].Vertices[vertex].SectionIndex == groupIndex)
                        {
                            previousFrame            = frame * (int)_numMeshes + meshIndex;
                            previousFrameVertexIndex = vertex;
                            break;
                        }
                    }
                }

                // did we find a frame yet?
                if (previousFrame >= 0)
                {
                    break;
                }
            }

            // maybe we didn't find a frame. In that case use vertices from the model itself
            if (previousFrame < 0)
            {
                ModelResource model = SceneManager.GetModelByName(_modelName, true);
                if (model != null)
                {
                    float[] modelVerts = model.Meshes[meshIndex].sections[groupIndex].vertices;

                    const int stride = 3 + 3 + 2;
                    for (int i = 0; i < vertices.Length / 3; i++)
                    {
                        vertices[i * 3 + 0] += modelVerts[i * stride + 0];
                        vertices[i * 3 + 1] += modelVerts[i * stride + 1];
                        vertices[i * 3 + 2] += modelVerts[i * stride + 2];
                    }
                }
            }
            else
            {
                // we found a frame!
                for (int i = 0; i < vertices.Length / 3; i++)
                {
                    vertices[i * 3 + 0] += _frames[previousFrame].Vertices[previousFrameVertexIndex].Vertices[i * 3 + 0];
                    vertices[i * 3 + 1] += _frames[previousFrame].Vertices[previousFrameVertexIndex].Vertices[i * 3 + 1];
                    vertices[i * 3 + 2] += _frames[previousFrame].Vertices[previousFrameVertexIndex].Vertices[i * 3 + 2];
                }
            }
        }
Exemplo n.º 2
0
        public static void SetModelTexture(string model, int meshIndex, int groupIndex, string texture)
        {
            // find the model
            Graphics.ModelResource m = GetSceneModel(model);

            if (m != null)
            {
                m.ReplaceTexture(meshIndex, groupIndex, texture + ".BMP");
            }
        }
Exemplo n.º 3
0
        public Resource.Resource Load(string name, Resource.ResourceManager content)
        {
            try
            {
                if (name.IndexOf('.') < 0)
                {
                    name += ".MOD";
                }

                System.IO.Stream stream = FileSystem.Open(name);

                ModelResource resource = new ModelResource(name, stream, content);

                stream.Close();

                return(resource);
            }
            catch (System.IO.FileNotFoundException)
            {
                Logger.WriteError("Unable to find model: {0}", name);

                return(new ModelResource(name, content));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies the animation to a model
        /// </summary>
        /// <returns>True if the animation is still playing, false otherwise</returns>
        public bool Animate(ModelResource model, int timeSinceStart, int timeSinceLastFrame, bool loop, bool absolute)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (model.Meshes.Length != _numMeshes)
            {
                throw new ArgumentException("The model is not compatible with this animation");
            }

            bool stillPlaying = false;

            for (int i = 0; i < model.Meshes.Length; i++)
            {
                int   frame1, frame2;
                float percent;
                getFrames(timeSinceStart, i, out frame1, out frame2, out percent);

                if (frame1 >= 0)
                {
                    stillPlaying = true;
                }

                if (frame1 >= 0 && frame2 >= 0)
                {
                    if (_animationFrames[i][frame1].Transform != null &&
                        _animationFrames[i][frame2].Transform != null)
                    {
                        Math.Matrix animatedTransform;
                        FrameTransformation.LerpToMatrix(percent, ref _animationFrames[i][frame1].Transform, ref _animationFrames[i][frame2].Transform, out animatedTransform);
                        //model.Meshes[i].AnimatedTransformMatrix = animatedTransform;
                        model.Meshes[i].SetTransform(animatedTransform);
                        //model.Meshes[i].SetTransform(_animationFrames[i][frame1].Transform.Original);
                        model.Meshes[i].AnimatedTransformIsAbsolute = absolute;
                    }

                    // TODO: interpolate
                    FrameSectionVertices[] v = _animationFrames[i][frame1].Vertices;
                    if (v != null)
                    {
                        for (int j = 0; j < v.Length; j++)
                        {
                            if (model.Meshes[i].sections[v[j].SectionIndex].AnimatedVertices == null)
                            {
                                model.Meshes[i].sections[v[j].SectionIndex].AnimatedVertices = new float[model.Meshes[i].sections[v[j].SectionIndex].vertices.Length];
                                Array.Copy(model.Meshes[i].sections[v[j].SectionIndex].vertices, model.Meshes[i].sections[v[j].SectionIndex].AnimatedVertices, model.Meshes[i].sections[v[j].SectionIndex].vertices.Length);
                            }
                            float[] dest   = model.Meshes[i].sections[v[j].SectionIndex].AnimatedVertices;
                            float[] source = v[j].Vertices;


                            for (int k = 0; k < source.Length / 3; k++)
                            {
                                const int stride = 3 + 3 + 2;
                                dest[k * stride + 0] = source[k * 3 + 0];
                                dest[k * stride + 1] = source[k * 3 + 1];
                                dest[k * stride + 2] = source[k * 3 + 2];
                            }
                        }
                    }

                    float[] boundingBox = _animationFrames[i][frame1].BoundingBox;
                    if (boundingBox != null)
                    {
                        // TODO: interpolate the bounding boxes
                        model.Meshes[i].SetAABB(new AxisAlignedBoundingBox(boundingBox));
                    }
                }


                /*if (frame.Vertices != null)
                 * {
                 *  for (int j = 0; j < frame.Vertices.Length; j++)
                 *  {
                 *      float[] dest = model.Meshes[i].sections[frame.Vertices[j].SectionIndex].vertices;
                 *      float[] source = frame.Vertices[j].Vertices;
                 *
                 *      if (frame.Vertices[j].Delta)
                 *      {
                 *          for (int k = 0; k < source.Length / 3; k++)
                 *          {
                 *              const int stride = 3 + 3 + 2;
                 *              dest[k * stride + 0] += source[k * 3 + 0];
                 *              dest[k * stride + 1] += source[k * 3 + 1];
                 *              dest[k * stride + 2] += source[k * 3 + 2];
                 *          }
                 *
                 *          //model.Meshes[i].VerticesModifiedWithDelta = true;
                 *      }
                 *      else
                 *      {
                 *          for (int k = 0; k < source.Length / 3; k++)
                 *          {
                 *              const int stride = 3 + 3 + 2;
                 *              dest[k * stride + 0] = source[k * 3 + 0];
                 *              dest[k * stride + 1] = source[k * 3 + 1];
                 *              dest[k * stride + 2] = source[k * 3 + 2];
                 *          }
                 *      }
                 *  }
                 * }*/


                //model.Meshes[i].AnimatedTransformMatrix = transform;// *model.Meshes[i].TransformMatrix;
            }

            return(stillPlaying);
        }