|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Text; |
4 | 4 | using System.Xml; |
| 5 | +using OpenTK.Graphics.OpenGL4; |
| 6 | +using OpenTK.Windowing.Desktop; |
5 | 7 |
|
6 | 8 | namespace FreeFrame.Components.Shapes |
7 | 9 | { |
8 | 10 | public abstract class Shape |
9 | 11 | { |
| 12 | + static GameWindow? _window; |
| 13 | + private Shader _shader; |
| 14 | + private int _vertexBufferObject; |
| 15 | + private int _vertexArrayObject; |
| 16 | + private int _indexBufferObject; |
| 17 | + |
| 18 | + private int _indexCount; |
| 19 | + |
10 | 20 | public Shape() |
11 | 21 | { |
| 22 | + _vertexArrayObject = GL.GenVertexArray(); |
| 23 | + _vertexBufferObject = GL.GenBuffer(); |
| 24 | + _indexBufferObject = GL.GenBuffer(); |
| 25 | + _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag"); |
| 26 | + } |
| 27 | + public static void BindWindow(GameWindow window) => _window = window; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Convert given vertex position attributes in px to NDC |
| 31 | + /// </summary> |
| 32 | + /// <param name="vertexPositions">vertex position attribute</param> |
| 33 | + /// <returns>vertex position attribute in NDC</returns> |
| 34 | + /// <exception cref="Exception">Window should be binded before calling</exception> |
| 35 | + protected static float[] ConvertToNDC(params int[] vertexPositions) |
| 36 | + { |
| 37 | + if (_window == null) |
| 38 | + throw new Exception("Trying to convert to NDC but no Window is binded"); |
| 39 | + |
| 40 | + float[] result = new float[vertexPositions.Length]; |
| 41 | + for (int i = 0; i < vertexPositions.Length; i+=2) |
| 42 | + { |
| 43 | + result[i] = vertexPositions[i] / (float)_window.ClientSize.X / 2; |
| 44 | + result[i+1] = vertexPositions[i + 1] / (float)_window.ClientSize.Y / 2; |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Add the given vertex and index to the OpenGL buffers. And link those with an VAO. |
| 51 | + /// </summary> |
| 52 | + /// <param name="vertex">vertex array</param> |
| 53 | + /// <param name="index">index array</param> |
| 54 | + protected void ImplementObjects(float[] vertex, uint[] index) |
| 55 | + { |
| 56 | + // VAO |
| 57 | + GL.BindVertexArray(_vertexArrayObject); |
12 | 58 |
|
| 59 | + string label = $"VAO {GetType().Name}:"; |
| 60 | + GL.ObjectLabel(ObjectLabelIdentifier.VertexArray, _vertexArrayObject, label.Length, label); |
| 61 | + |
| 62 | + // VBO |
| 63 | + GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject); |
| 64 | + GL.BufferData(BufferTarget.ArrayBuffer, vertex.Length * sizeof(float), vertex, BufferUsageHint.StaticDraw); |
| 65 | + |
| 66 | + label = $"VBO {GetType().Name}:"; |
| 67 | + GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _vertexBufferObject, label.Length, label); |
| 68 | + |
| 69 | + // IBO |
| 70 | + GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferObject); |
| 71 | + GL.BufferData(BufferTarget.ElementArrayBuffer, index.Length * sizeof(uint), index, BufferUsageHint.StaticDraw); |
| 72 | + |
| 73 | + label = $"IBO {GetType().Name}:"; |
| 74 | + GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _indexBufferObject, label.Length, label); |
| 75 | + |
| 76 | + // Link Attributes |
| 77 | + GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); // x, y; |
| 78 | + GL.EnableVertexAttribArray(0); |
| 79 | + |
| 80 | + _indexCount = index.Length; |
| 81 | + } |
| 82 | + /// <summary> |
| 83 | + /// Trigge draw element throw OpenGL context |
| 84 | + /// </summary> |
| 85 | + public void Draw() |
| 86 | + { |
| 87 | + _shader.Use(); |
| 88 | + GL.BindVertexArray(_vertexArrayObject); |
| 89 | + GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0); |
13 | 90 | } |
14 | 91 |
|
15 | | - //TODO: add method to convert shape position and size to NDC |
| 92 | + /// <summary> |
| 93 | + /// Should update the size and the position to the new Window size |
| 94 | + /// </summary> |
| 95 | + public abstract void UpdateProperties(); |
16 | 96 | public abstract override string ToString(); |
17 | 97 | } |
| 98 | + |
18 | 99 | } |
0 commit comments