aboutsummaryrefslogtreecommitdiff
path: root/Unicity.Renderer/GraphicsRenderer.cs
blob: 6508a4c39b60fd8a00ebd27b25264bd1788d5ff3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using Unicity.Renderer.Shapes;

namespace Unicity.Renderer
{
    public class GraphicsRenderer : IDisposable
    {
        public RenderWindow window { get; }
        public Camera camera = null;

        Shader shader = null;
         
        public GraphicsRenderer(RenderWindow window, Camera camera)
        {
            this.window = window;
            this.camera = camera;

            Console.WriteLine("Compiling GLSL shaders...");

            string vertexShader = Encoding.UTF8.GetString(Properties.Resources.vertexShader);
            string fragmentShader = Encoding.UTF8.GetString(Properties.Resources.fragmentShader);
            shader = new Shader(vertexShader, fragmentShader);
            shader.Use();

            Console.WriteLine("Reticulating splines...");

            GL.ClearDepth(1.0f);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);

            camera.Initialize();
            window.Update += Window_Update;

            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
        }

        private void Window_Update(object sender, EventArgs e)
        {
            camera.UpdateView(shader);
            shader.Use();
        }

        public void SetClearColor(float red, float green, float blue, float alpha)
        {
            GL.ClearColor(red, green, blue, alpha);
        }

        public void SetDrawColor(float red, float green, float blue)
        {
            shader.SetUniform("color", new Vector3(red, green, blue));
        }

        public void DrawShape(Shape shape)
        {
            shape.Draw(shader, this);
        }

        public void DrawShapes(Shape[] shapes)
        {
            foreach (Shape shape in shapes)
            {
                DrawShape(shape);
            }
        }

        public void ClearScreen()
        {
            window.window.MakeCurrent();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        }

        bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            // Return of already disposed
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                // Free managed objects here
            }

            // Dispose of any unmanaged resources
            window?.Dispose();
            shader.Delete();

            // Set disposed flag to true
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}