Inheritance: DisposableResource, ILoadable
Ejemplo n.º 1
0
        public InstanceModel(Model model)
        {
            Model = model;
            PlaySpeed = 1;
            CurrentFrame = model.FrameStart;

            // orientation
            Position = Vector3.Zero;
            Scale = Vector3.One;
            Rotation = Matrix3.Identity;

            // objects
            Objects = new InstanceObject[model.Objects.Length];
            for (int i = 0; i != Objects.Length; ++i)
            {
                var type = model.Objects[i].GetType();
                if (type == typeof(ObjectMesh)) Objects[i] = new InstanceObjectMesh((ObjectMesh)model.Objects[i]);
                else if (type == typeof(ObjectArmature)) Objects[i] = new InstanceObjectArmature((ObjectArmature)model.Objects[i]);
                else Debug.ThrowError("InstanceModel", "Unsuported Object type: " + type);
            }

            for (int i = 0; i != Objects.Length; ++i)
            {
                Objects[i].bindObjects(model.Objects[i]);
            }
        }
Ejemplo n.º 2
0
 public ObjectArmature(BinaryReader reader, Model model)
     : base(reader, model)
 {
     string armatureName = reader.ReadString();
     foreach (var armature in model.Armatures)
     {
         if (armatureName == armature.Name)
         {
             Armature = armature;
             break;
         }
     }
 }
Ejemplo n.º 3
0
        public ObjectMesh(BinaryReader reader, Model model)
            : base(reader, model)
        {
            string meshName = reader.ReadString();
            foreach (var mesh in model.Meshes)
            {
                if (meshName == mesh.Name)
                {
                    Mesh = mesh;
                    break;
                }
            }

            if (Mesh == null) Debug.ThrowError("ObjectMesh", "Failed to find Mesh");
        }
Ejemplo n.º 4
0
        public Mesh(BinaryReader reader, Model model, int classicInstanceCount)
            : base(model)
        {
            try
            {
                Name = reader.ReadString();

                // material
                int materialIndex = reader.ReadInt32();
                if (materialIndex != -1) Material = model.Materials[materialIndex];

                // elements
                int elementCount = reader.ReadInt32();
                var elements = new List<BufferLayoutElement>();
                for (int i = 0; i != elementCount; ++i)
                {
                    elements.Add(new BufferLayoutElement((BufferLayoutElementTypes)reader.ReadInt32(), (BufferLayoutElementUsages)reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32()));
                }
                LayoutDesc = BufferLayoutDescAPI.New(elements);

                // vertices
                int vertexFloatCount = reader.ReadInt32();
                var vertices = new float[vertexFloatCount];
                for (int i = 0; i != vertexFloatCount; ++i)
                {
                    vertices[i] = reader.ReadSingle();
                }
                VertexBuffer = VertexBufferAPI.New(this, LayoutDesc, BufferUsages.Default, VertexBufferTopologys.Triangle, vertices);

                // indices
                int indexCount = reader.ReadInt32();
                var indices = new int[indexCount];
                for (int i = 0; i != indexCount; ++i)
                {
                    indices[i] = reader.ReadInt32();
                }
                IndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, indices);

                // create instancing buffers
                ClassicInstanceCount = classicInstanceCount;
                if (classicInstanceCount > 0)
                {
                    var intancingElements = new List<BufferLayoutElement>();
                    foreach (var element in elements) intancingElements.Add(element);
                    intancingElements.Add(new BufferLayoutElement(BufferLayoutElementTypes.Float, BufferLayoutElementUsages.IndexClassic, 0, 0, LayoutDesc.FloatCount));
                    InstancingLayoutDesc = BufferLayoutDescAPI.New(intancingElements);

                    int instanceVertexFloatCount = (vertexFloatCount * classicInstanceCount) + (VertexBuffer.VertexCount * classicInstanceCount);
                    var instancingVertices = new float[instanceVertexFloatCount];
                    int vi = 0;
                    for (int i = 0; i != classicInstanceCount; ++i)
                    {
                        int vOffset = 0;
                        for (int i2 = 0; i2 != VertexBuffer.VertexCount; ++i2)
                        {
                            for (int i3 = 0; i3 != VertexBuffer.VertexFloatArraySize; ++i3)
                            {
                                instancingVertices[vi] = vertices[vOffset];
                                ++vi;
                                ++vOffset;
                            }

                            instancingVertices[vi] = i;
                            ++vi;
                        }
                    }
                    InstancingVertexBuffer = VertexBufferAPI.New(this, InstancingLayoutDesc, BufferUsages.Default, VertexBuffer.Topology, instancingVertices);

                    int instanceIndexCount = (indexCount * classicInstanceCount);
                    var instancingIndices = new int[instanceIndexCount];
                    int ii = 0, iOffset = 0;
                    for (int i = 0; i != classicInstanceCount; ++i)
                    {
                        for (int i2 = 0; i2 != indexCount; ++i2)
                        {
                            instancingIndices[ii] = indices[i2] + iOffset;
                            ++ii;
                        }

                        iOffset += VertexBuffer.VertexCount;
                    }
                    InstancingIndexBuffer = IndexBufferAPI.New(this, BufferUsages.Default, instancingIndices);
                }
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }