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