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