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 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE); 104 105 glBindVertexArray(dbgVAO); 106 107 dbgShaderPoint.use(); 108 dbgShaderPoint.setUniform(mvpId, inGetCamera().matrix * transform); 109 dbgShaderPoint.setUniform(colorId, color); 110 111 glEnableVertexAttribArray(0); 112 glBindBuffer(GL_ARRAY_BUFFER, cVBO); 113 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null); 114 115 glDrawElements(GL_POINTS, indiceCount, GL_UNSIGNED_SHORT, null); 116 glDisableVertexAttribArray(0); 117 118 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE); 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 dbgShaderLine.use(); 131 dbgShaderLine.setUniform(mvpId, inGetCamera().matrix * transform); 132 dbgShaderLine.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 }