Example #1
0
        private void init(IShader shader, ITexture2D texture, Stream stream, string metricsFileName, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                // load characters
                var metrics = new FontMetrics();
                metrics.Load(stream);

                Characters = new Character[metrics.Characters.Length];
                for (int i = 0; i != metrics.Characters.Length; ++i)
                {
                    var character = metrics.Characters[i];
                    Characters[i] = new Character(character.Key, new Vector2(character.X, character.Y), new Vector2(character.Width, character.Height));
                }

                // get shader variables
                this.texture     = texture;
                this.shader      = shader;
                shaderCamera     = shader.Variable("Camera");
                shaderPosition   = shader.Variable("Position");
                shaderSize       = shader.Variable("Size");
                shaderPositionUV = shader.Variable("PositionUV");
                shaderSizeUV     = shader.Variable("SizeUV");
                texelOffset      = shader.Variable("TexelOffset");
                shaderColor      = shader.Variable("Color");
                shaderTexture    = shader.Resource("DiffuseTexture");

                // create buffers
                var layoutDesc = BufferLayoutDescAPI.New(BufferLayoutTypes.Position2);
                layout = BufferLayoutAPI.New(this, shader, layoutDesc);

                var Indices = new int[6]
                {
                    0, 1, 2,
                    0, 2, 3
                };

                var Vertices = new float[8]
                {
                    0, 0,
                    0, 1,
                    1, 1,
                    1, 0,
                };

                vertexBuffer = VertexBufferAPI.New(this, layoutDesc, BufferUsages.Default, VertexBufferTopologys.Triangle, Vertices, Indices);
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
        }
Example #2
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;
            }
        }