Example #1
0
        public static Engine.State Convert(State state, Tile tile, uint id)
        {
            Exception undefModel   = new Exception("Undefined model!");
            Exception undefTexture = new Exception("Undefined texture!");

            Box box = state.Transform == null
                ? new Box()
                : BoxFromTransform(state.Transform);

            if (tile.Models == null || state.Model == null)
            {
                throw undefModel;
            }

            Render.Model?model = Core.Unit.Resource.LoadModel(tile.Models[(int)state.Model]);

            if (model == null)
            {
                throw undefModel;
            }

            Render.Model tranformed = new Render.Model(
                model.Faces.Select(f => new Render.Face(f, box)).ToArray(),
                model.FullSides
                );

            if (tile.Textures == null)
            {
                throw undefTexture;
            }

            uint[]? layers = state.Layers ?? tile.Default?.Layers;

            if (layers == null)
            {
                throw undefTexture;
            }

            uint[] loaded = layers
                            .Select(l => Core.Unit.Atlas.LoadSprite(tile.Textures[l]))
                            .ToArray();

            return(new Engine.State(tranformed, id, loaded, box));
        }
Example #2
0
        private void RenderModel(Render.Model model, IMatrix4x4 viewProjectionMatrix, Matrix <double> modelMatrix, Vector <double> cameraPosition)
        {
            if (!model.Shader.SetUniformMatrix("viewProjectionTransformation", viewProjectionMatrix))
            {
                throw new Exception("Vertex shader is missing 'viewProjectionTransformation'");
            }
            if (!model.Shader.SetUniformMatrix("modelTransformation", modelMatrix))
            {
                throw new Exception("Vertex shader is missing 'modelTransformation'");
            }
            model.Shader.SetUniformVector("cameraPos", cameraPosition);

            Gl.BindVertexArray(model.Mesh.VertexArrayObjectId);
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, model.Texture?.Id ?? 0);
            Gl.UseProgram(model.Shader.Id);

            Gl.DrawElements(PrimitiveType.Triangles, model.Mesh.IndexCount, DrawElementsType.UnsignedInt, IntPtr.Zero);
        }