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