1 module polyplex.core.render.gl.debug2d; 2 import polyplex.core.render; 3 import polyplex.core.render.gl.shader; 4 import polyplex.core.render.gl.objects; 5 import polyplex.core.render.camera; 6 import polyplex.core.color; 7 import bindbc.opengl; 8 import bindbc.opengl.gl; 9 import polyplex.utils; 10 import polyplex.math; 11 import polyplex.utils.mathutils; 12 13 import std.stdio; 14 15 private struct DebugVertexLayout { 16 Vector2 ppPosition; 17 Vector4 ppColor; 18 } 19 20 public class GlDebugging2D { 21 private static GLShader shader; 22 private static Camera2D cm; 23 24 private static VertexBuffer!(DebugVertexLayout, Layout.Interleaved) buff; 25 private static int matr_indx; 26 27 /** 28 Prepares GlDebugging2D for rendering (backend, you don't need to run this yourself.) 29 */ 30 public static void PrepDebugging() { 31 buff = VertexBuffer!(DebugVertexLayout, Layout.Interleaved)([]); 32 shader = new GLShader(new ShaderCode(import("debug.vsh"), import("debug.fsh"))); 33 matr_indx = shader.GetUniform("ppProjection"); 34 cm = new Camera2D(Vector2(0, 0)); 35 cm.Update(); 36 } 37 38 private static void create_buffer(Rectangle rect, Color color) { 39 buff.Data = [ 40 DebugVertexLayout(Vector2(rect.X, rect.Y), color.GLfColor()), 41 DebugVertexLayout(Vector2(rect.X, rect.Y+rect.Height), color.GLfColor()), 42 DebugVertexLayout(Vector2(rect.X+rect.Width, rect.Y+rect.Height), color.GLfColor()), 43 DebugVertexLayout(Vector2(rect.X, rect.Y), color.GLfColor()), 44 DebugVertexLayout(Vector2(rect.X+rect.Width, rect.Y), color.GLfColor()), 45 DebugVertexLayout(Vector2(rect.X+rect.Width, rect.Y+rect.Height), color.GLfColor()), 46 ]; 47 } 48 49 private static void create_buffer_line(Vector2[] lines, Color color) { 50 buff.Data.length = lines.length*2; 51 foreach(i; 1 .. lines.length) { 52 buff.Data[(i*2)] = DebugVertexLayout(lines[i-1], color.GLfColor()); 53 buff.Data[(i*2)+1] = DebugVertexLayout(lines[i], color.GLfColor()); 54 } 55 } 56 57 private static void create_buffer_points(Vector2[] points, Color color) { 58 buff.Data.length = points.length; 59 foreach(i; 0 .. points.length) { 60 buff.Data[i] = DebugVertexLayout(points[i], color.GLfColor()); 61 } 62 } 63 64 /** 65 Resets the matrix for the debugging primitives. 66 */ 67 public static void ResetCamera() { 68 cm = new Camera2D(Vector2(0, 0)); 69 } 70 71 /** 72 Sets the matrix for the debugging primitives. 73 */ 74 public static void SetCamera(Camera2D cam) { 75 cm = cam; 76 } 77 78 /** 79 Draws dots based on specified points and color. 80 */ 81 public static void DrawDots(Vector2[] dot_points, Color color) { 82 create_buffer_points(dot_points, color); 83 buff.UpdateBuffer(); 84 shader.Attach(); 85 shader.SetUniform(matr_indx, cm.Project(Renderer.Window.ClientBounds.Width, Renderer.Window.ClientBounds.Height) * cm.Matrix); 86 buff.Draw(0, DrawType.Points); 87 buff.Unbind(); 88 shader.Detach(); 89 } 90 91 /** 92 Draws lines based on specified points and color. 93 */ 94 public static void DrawLines(Vector2[] line_points, Color color) { 95 if (line_points.length == 1) { 96 DrawDots(line_points, color); 97 return; 98 } 99 create_buffer_line(line_points, color); 100 buff.UpdateBuffer(); 101 shader.Attach(); 102 shader.SetUniform(matr_indx, cm.Project(Renderer.Window.ClientBounds.Width, Renderer.Window.ClientBounds.Height) * cm.Matrix); 103 buff.Draw(0, DrawType.Lines); 104 buff.Unbind(); 105 shader.Detach(); 106 } 107 108 /** 109 Draws a line rectangle (2 triangles), with the specified color. 110 */ 111 public static void DrawRectangle(Rectangle rect, Color color = Color.White) { 112 create_buffer(rect, color); 113 buff.UpdateBuffer(); 114 shader.Attach(); 115 shader.SetUniform(matr_indx, cm.Project(Renderer.Window.ClientBounds.Width, Renderer.Window.ClientBounds.Height) * cm.Matrix); 116 buff.Draw(0, DrawType.LineStrip); 117 buff.Unbind(); 118 shader.Detach(); 119 } 120 121 /** 122 Draws a filled rectangle, with the specified color. 123 */ 124 public static void DrawRectangleFilled(Rectangle rect, Color color = Color.White) { 125 create_buffer(rect, color); 126 buff.UpdateBuffer(); 127 shader.Attach(); 128 shader.SetUniform(matr_indx, cm.Project(Renderer.Window.ClientBounds.Width, Renderer.Window.ClientBounds.Height) * cm.Matrix); 129 buff.Draw(); 130 shader.Detach(); 131 buff.Unbind(); 132 } 133 }