1 module inochi2d.core.dbg;
2 import inochi2d;
3 import bindbc.opengl;
4 
5 package(inochi2d) {
6     int indiceCount;
7 
8     Shader dbgShader;
9     GLuint dbgVAO;
10     GLuint dbgVBO;
11     GLuint dbgIBO;
12 
13     GLuint cVBO;
14 
15     GLint mvpId;
16     GLint colorId;
17     void inInitDebug() {
18         dbgShader = new Shader(import("dbg.vert"), import("dbg.frag"));
19         glGenVertexArrays(1, &dbgVAO);
20         glGenBuffers(1, &dbgVBO);
21         glGenBuffers(1, &dbgIBO);
22 
23         mvpId = dbgShader.getUniformLocation("mvp");
24         colorId = dbgShader.getUniformLocation("color");
25     }
26 }
27 
28 private {
29     void inUpdateDbgVerts(vec3[] points) {
30 
31         // Generate bad line drawing indices
32         ushort[] vts = new ushort[points.length+1];
33         foreach(i; 0..points.length) {
34             vts[i] = cast(ushort)i;
35         }
36         vts[$-1] = 0;
37 
38         inUpdateDbgVerts(points, vts);
39     }
40 
41     void inUpdateDbgVerts(vec3[] points, ushort[] indices) {
42         glBindVertexArray(dbgVAO);
43         glBindBuffer(GL_ARRAY_BUFFER, dbgVBO);
44         glBufferData(GL_ARRAY_BUFFER, points.length*vec3.sizeof, points.ptr, GL_DYNAMIC_DRAW);
45         cVBO = dbgVBO;
46 
47     
48         indiceCount = cast(int)indices.length;
49         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dbgIBO);
50         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.length*ushort.sizeof, indices.ptr, GL_DYNAMIC_DRAW);
51     }
52 }
53 
54 bool inDbgDrawMeshOutlines = false;
55 bool inDbgDrawMeshVertexPoints = false;
56 bool inDbgDrawMeshOrientation = false;
57 
58 /**
59     Size of debug points
60 */
61 void inDbgPointsSize(float size) {
62     glPointSize(size);
63 }
64 
65 /**
66     Size of debug points
67 */
68 void inDbgLineWidth(float size) {
69     glLineWidth(size);
70 }
71 
72 /**
73     Draws points with specified color
74 */
75 void inDbgSetBuffer(vec3[] points) {
76     inUpdateDbgVerts(points);
77 }
78 
79 /**
80     Sets buffer to buffer owned by an other OpenGL object
81 */
82 void inDbgSetBuffer(GLuint vbo, GLuint ibo, int count) {
83     glBindVertexArray(dbgVAO);
84     glBindBuffer(GL_ARRAY_BUFFER, vbo);
85     cVBO = vbo;
86     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
87     indiceCount = count;
88 }
89 
90 /**
91     Draws points with specified color
92 */
93 void inDbgSetBuffer(vec3[] points, ushort[] indices) {
94     inUpdateDbgVerts(points, indices);
95 }
96 
97 /**
98     Draws current stored vertices as points with specified color
99 */
100 void inDbgDrawPoints(vec4 color, mat4 transform = mat4.identity) {
101     glEnable(GL_POINT_SMOOTH);
102     glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
103 
104         glBindVertexArray(dbgVAO);
105 
106         dbgShader.use();
107         dbgShader.setUniform(mvpId, inGetCamera().matrix * transform);
108         dbgShader.setUniform(colorId, color);
109 
110         glEnableVertexAttribArray(0);
111         glBindBuffer(GL_ARRAY_BUFFER, cVBO);
112         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null);
113         
114         glDrawElements(GL_POINTS, indiceCount, GL_UNSIGNED_SHORT, null);
115         glDisableVertexAttribArray(0);
116 
117     glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
118     glDisable(GL_POINT_SMOOTH);
119 }
120 
121 /**
122     Draws current stored vertices as lines with specified color
123 */
124 void inDbgDrawLines(vec4 color, mat4 transform = mat4.identity) {
125     glEnable(GL_LINE_SMOOTH);
126     glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
127     
128         glBindVertexArray(dbgVAO);
129 
130         dbgShader.use();
131         dbgShader.setUniform(mvpId, inGetCamera().matrix * transform);
132         dbgShader.setUniform(colorId, color);
133 
134         glEnableVertexAttribArray(0);
135         glBindBuffer(GL_ARRAY_BUFFER, cVBO);
136         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null);
137         
138         glDrawElements(GL_LINES, indiceCount, GL_UNSIGNED_SHORT, null);
139         glDisableVertexAttribArray(0);
140 
141     glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
142     glDisable(GL_LINE_SMOOTH);
143 }