| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | #include <limits.h> |
| 23 | #include <stddef.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "sokol/sokol_gfx.h" |
| 27 | |
| 28 | #include "chipmunk/chipmunk_private.h" |
| 29 | #include "ChipmunkDebugDraw.h" |
| 30 | |
| 31 | cpTransform ChipmunkDebugDrawVPMatrix; |
| 32 | float ChipmunkDebugDrawPointLineScale = 1.0f; |
| 33 | |
| 34 | #define GLSL33(x) "#version 330\n" #x |
| 35 | |
| 36 | static sg_bindings bindings; |
| 37 | static sg_pipeline pipeline; |
| 38 | |
| 39 | typedef struct {float x, y;} float2; |
| 40 | typedef struct {uint8_t r, g, b, a;} RGBA8; |
| 41 | typedef struct {float2 pos; float2 uv; float r; RGBA8 fill, outline;} Vertex; |
| 42 | typedef uint16_t Index; |
| 43 | |
| 44 | static RGBA8 cp_to_rgba(cpSpaceDebugColor c){return (RGBA8){(uint8_t)(0xFF*c.r), (uint8_t)(0xFF*c.g), (uint8_t)(0xFF*c.b), (uint8_t)(0xFF*c.a)};} |
| 45 | |
| 46 | typedef struct { |
| 47 | float U_vp_matrix[16]; |
| 48 | } Uniforms; |
| 49 | |
| 50 | // Meh, just max out 16 bit index size. |
| 51 | #define VERTEX_MAX (64*1024) |
| 52 | #define INDEX_MAX (128*1024) |
| 53 | |
| 54 | static sg_buffer VertexBuffer, IndexBuffer; |
| 55 | static size_t VertexCount, IndexCount; |
| 56 | |
| 57 | static Vertex Vertexes[VERTEX_MAX]; |
| 58 | static uint16_t Indexes[INDEX_MAX]; |
| 59 | |
| 60 | void |
| 61 | ChipmunkDebugDrawInit(void) |
| 62 | { |
| 63 | VertexBuffer = sg_make_buffer(&(sg_buffer_desc){ |
| 64 | .label = "ChipmunkDebugDraw Vertex Buffer", |
| 65 | .size = VERTEX_MAX*sizeof(Vertex), |
| 66 | .type = SG_BUFFERTYPE_VERTEXBUFFER, |
| 67 | .usage = SG_USAGE_STREAM, |
| 68 | }); |
| 69 | |
| 70 | IndexBuffer = sg_make_buffer(&(sg_buffer_desc){ |
| 71 | .label = "ChipmunkDebugDraw Index Buffer", |
| 72 | .size = INDEX_MAX*sizeof(Index), |
| 73 | .type = SG_BUFFERTYPE_INDEXBUFFER, |
| 74 | .usage = SG_USAGE_STREAM, |
| 75 | }); |
| 76 | |
| 77 | bindings = (sg_bindings){ |
| 78 | .vertex_buffers[0] = VertexBuffer, |
| 79 | .index_buffer = IndexBuffer, |
| 80 | }; |
| 81 | |
| 82 | sg_shader shd = sg_make_shader(&(sg_shader_desc){ |
| 83 | .vs.uniform_blocks[0] = { |
| 84 | .size = sizeof(Uniforms), |
| 85 | .uniforms[0] = {.name = "U_vp_matrix", .type = SG_UNIFORMTYPE_MAT4}, |
| 86 | }, |
| 87 | .vs.source = GLSL33( |
| 88 | layout(location = 0) in vec2 IN_pos; |
| 89 | layout(location = 1) in vec2 IN_uv; |
| 90 | layout(location = 2) in float IN_radius; |
| 91 | layout(location = 3) in vec4 IN_fill; |
| 92 | layout(location = 4) in vec4 IN_outline; |
| 93 | |
| 94 | uniform mat4 U_vp_matrix; |
| 95 | |
| 96 | out struct { |
| 97 | vec2 uv; |
| 98 | vec4 fill; |
| 99 | vec4 outline; |
| 100 | } FRAG; |
| 101 | |
| 102 | void main(){ |
| 103 | gl_Position = U_vp_matrix*vec4(IN_pos + IN_radius*IN_uv, 0, 1); |
| 104 | FRAG.uv = IN_uv; |
| 105 | FRAG.fill = IN_fill; |
| 106 | FRAG.fill.rgb *= IN_fill.a; |
| 107 | FRAG.outline = IN_outline; |
| 108 | FRAG.outline.a *= IN_outline.a; |
| 109 | } |
| 110 | ), |
| 111 | .fs.source = GLSL33( |
| 112 | in struct { |
| 113 | vec2 uv; |
| 114 | vec4 fill; |
| 115 | vec4 outline; |
| 116 | } FRAG; |
| 117 | |
| 118 | out vec4 OUT_color; |
| 119 | |
| 120 | void main(){ |
| 121 | float len = length(FRAG.uv); |
| 122 | float fw = length(fwidth(FRAG.uv)); |
| 123 | float mask = smoothstep(-1, fw - 1, -len); |
| 124 | |
| 125 | float outline = 1 - fw; |
| 126 | float outline_mask = smoothstep(outline - fw, outline, len); |
| 127 | vec4 color = FRAG.fill + (FRAG.outline - FRAG.fill*FRAG.outline.a)*outline_mask; |
| 128 | OUT_color = color*mask; |
| 129 | } |
| 130 | ), |
| 131 | }); |
| 132 | |
| 133 | pipeline = sg_make_pipeline(&(sg_pipeline_desc){ |
| 134 | .shader = shd, |
| 135 | .blend = { |
| 136 | .enabled = true, |
| 137 | .src_factor_rgb = SG_BLENDFACTOR_ONE, |
| 138 | .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA |
| 139 | }, |
| 140 | .index_type = SG_INDEXTYPE_UINT16, |
| 141 | .layout = { |
| 142 | .attrs = { |
| 143 | [0] = {.offset = offsetof(Vertex, pos), .format = SG_VERTEXFORMAT_FLOAT2}, |
| 144 | [1] = {.offset = offsetof(Vertex, uv), .format = SG_VERTEXFORMAT_FLOAT2}, |
| 145 | [2] = {.offset = offsetof(Vertex, r), .format = SG_VERTEXFORMAT_FLOAT}, |
| 146 | [3] = {.offset = offsetof(Vertex, fill), .format = SG_VERTEXFORMAT_UBYTE4N}, |
| 147 | [4] = {.offset = offsetof(Vertex, outline), .format = SG_VERTEXFORMAT_UBYTE4N}, |
| 148 | } |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | static Vertex *push_vertexes(size_t vcount, const Index *index_src, size_t icount){ |
| 154 | cpAssertHard(VertexCount + vcount <= VERTEX_MAX || IndexCount + icount <= INDEX_MAX, "Geometry buffer full."); |
| 155 | |
| 156 | Vertex *vertex_dst = Vertexes + VertexCount; |
| 157 | size_t base = VertexCount; |
| 158 | VertexCount += vcount; |
| 159 | |
| 160 | Index *index_dst = Indexes + IndexCount; |
| 161 | for(size_t i = 0; i < icount; i++) index_dst[i] = index_src[i] + (Index)base; |
| 162 | IndexCount += icount; |
| 163 | |
| 164 | return vertex_dst; |
| 165 | } |
| 166 | |
| 167 | void ChipmunkDebugDrawDot(cpFloat size, cpVect pos, cpSpaceDebugColor fillColor) |
| 168 | { |
| 169 | float r = (float)(size*0.5f*ChipmunkDebugDrawPointLineScale); |
| 170 | RGBA8 fill = cp_to_rgba(fillColor); |
| 171 | Vertex *vertexes = push_vertexes(4, (Index[]){0, 1, 2, 0, 2, 3}, 6); |
| 172 | vertexes[0] = (Vertex){{(float)pos.x, (float)pos.y}, {-1, -1}, r, fill, fill}; |
| 173 | vertexes[1] = (Vertex){{(float)pos.x, (float)pos.y}, {-1, 1}, r, fill, fill}; |
| 174 | vertexes[2] = (Vertex){{(float)pos.x, (float)pos.y}, { 1, 1}, r, fill, fill}; |
| 175 | vertexes[3] = (Vertex){{(float)pos.x, (float)pos.y}, { 1, -1}, r, fill, fill}; |
| 176 | } |
| 177 | |
| 178 | void ChipmunkDebugDrawCircle(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor) |
| 179 | { |
| 180 | float r = (float)radius + ChipmunkDebugDrawPointLineScale; |
| 181 | RGBA8 fill = cp_to_rgba(fillColor), outline = cp_to_rgba(outlineColor); |
| 182 | Vertex *vertexes = push_vertexes(4, (Index[]){0, 1, 2, 0, 2, 3}, 6); |
| 183 | vertexes[0] = (Vertex){{(float)pos.x, (float)pos.y}, {-1, -1}, r, fill, outline}; |
| 184 | vertexes[1] = (Vertex){{(float)pos.x, (float)pos.y}, {-1, 1}, r, fill, outline}; |
| 185 | vertexes[2] = (Vertex){{(float)pos.x, (float)pos.y}, { 1, 1}, r, fill, outline}; |
| 186 | vertexes[3] = (Vertex){{(float)pos.x, (float)pos.y}, { 1, -1}, r, fill, outline}; |
| 187 | |
| 188 | ChipmunkDebugDrawSegment(pos, cpvadd(pos, cpvmult(cpvforangle(angle), 0.75f*radius)), outlineColor); |
| 189 | } |
| 190 | |
| 191 | void ChipmunkDebugDrawSegment(cpVect a, cpVect b, cpSpaceDebugColor color) |
| 192 | { |
| 193 | ChipmunkDebugDrawFatSegment(a, b, 0.0f, color, color); |
| 194 | } |
| 195 | |
| 196 | void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor) |
| 197 | { |
| 198 | static const Index indexes[] = {0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7}; |
| 199 | Vertex *vertexes = push_vertexes(8, indexes, 18); |
| 200 | |
| 201 | cpVect t = cpvnormalize(cpvsub(b, a)); |
| 202 | |
| 203 | float r = (float)radius + ChipmunkDebugDrawPointLineScale; |
| 204 | RGBA8 fill = cp_to_rgba(fillColor), outline = cp_to_rgba(outlineColor); |
| 205 | |
| 206 | vertexes[0] = (Vertex){{(float)a.x, (float)a.y}, {(float)(-t.x + t.y), (float)(-t.x - t.y)}, r, fill, outline}; |
| 207 | vertexes[1] = (Vertex){{(float)a.x, (float)a.y}, {(float)(-t.x - t.y), (float)(+t.x - t.y)}, r, fill, outline}; |
| 208 | vertexes[2] = (Vertex){{(float)a.x, (float)a.y}, {(float)(-0.0 + t.y), (float)(-t.x + 0.0)}, r, fill, outline}; |
| 209 | vertexes[3] = (Vertex){{(float)a.x, (float)a.y}, {(float)(-0.0 - t.y), (float)(+t.x + 0.0)}, r, fill, outline}; |
| 210 | vertexes[4] = (Vertex){{(float)b.x, (float)b.y}, {(float)(+0.0 + t.y), (float)(-t.x - 0.0)}, r, fill, outline}; |
| 211 | vertexes[5] = (Vertex){{(float)b.x, (float)b.y}, {(float)(+0.0 - t.y), (float)(+t.x - 0.0)}, r, fill, outline}; |
| 212 | vertexes[6] = (Vertex){{(float)b.x, (float)b.y}, {(float)(+t.x + t.y), (float)(-t.x + t.y)}, r, fill, outline}; |
| 213 | vertexes[7] = (Vertex){{(float)b.x, (float)b.y}, {(float)(+t.x - t.y), (float)(+t.x + t.y)}, r, fill, outline}; |
| 214 | } |
| 215 | |
| 216 | #define MAX_POLY_VERTEXES 64 |
| 217 | // Fill needs (count - 2) triangles. |
| 218 | // Outline needs 4*count triangles. |
| 219 | #define MAX_POLY_INDEXES (3*(5*MAX_POLY_VERTEXES - 2)) |
| 220 | |
| 221 | void ChipmunkDebugDrawPolygon(int count, const cpVect *verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor) |
| 222 | { |
| 223 | RGBA8 fill = cp_to_rgba(fillColor), outline = cp_to_rgba(outlineColor); |
| 224 | |
| 225 | Index indexes[MAX_POLY_INDEXES]; |
| 226 | |
| 227 | // Polygon fill triangles. |
| 228 | for(int i = 0; i < count - 2; i++){ |
| 229 | indexes[3*i + 0] = 0; |
| 230 | indexes[3*i + 1] = 4*(i + 1); |
| 231 | indexes[3*i + 2] = 4*(i + 2); |
| 232 | } |
| 233 | |
| 234 | // Polygon outline triangles. |
| 235 | Index *cursor = indexes + 3*(count - 2); |
| 236 | for(int i0 = 0; i0 < count; i0++){ |
| 237 | int i1 = (i0 + 1)%count; |
| 238 | cursor[12*i0 + 0] = 4*i0 + 0; |
| 239 | cursor[12*i0 + 1] = 4*i0 + 1; |
| 240 | cursor[12*i0 + 2] = 4*i0 + 2; |
| 241 | cursor[12*i0 + 3] = 4*i0 + 0; |
| 242 | cursor[12*i0 + 4] = 4*i0 + 2; |
| 243 | cursor[12*i0 + 5] = 4*i0 + 3; |
| 244 | cursor[12*i0 + 6] = 4*i0 + 0; |
| 245 | cursor[12*i0 + 7] = 4*i0 + 3; |
| 246 | cursor[12*i0 + 8] = 4*i1 + 0; |
| 247 | cursor[12*i0 + 9] = 4*i0 + 3; |
| 248 | cursor[12*i0 + 10] = 4*i1 + 0; |
| 249 | cursor[12*i0 + 11] = 4*i1 + 1; |
| 250 | } |
| 251 | |
| 252 | float inset = (float)-cpfmax(0, 2*ChipmunkDebugDrawPointLineScale - radius); |
| 253 | float outset = (float)radius + ChipmunkDebugDrawPointLineScale; |
| 254 | float r = outset - inset; |
| 255 | |
| 256 | Vertex *vertexes = push_vertexes(4*count, indexes, 3*(5*count - 2)); |
| 257 | for(int i=0; i<count; i++){ |
| 258 | cpVect v0 = verts[i]; |
| 259 | cpVect v_prev = verts[(i+(count - 1))%count]; |
| 260 | cpVect v_next = verts[(i+(count + 1))%count]; |
| 261 | |
| 262 | cpVect n1 = cpvnormalize(cpvrperp(cpvsub(v0, v_prev))); |
| 263 | cpVect n2 = cpvnormalize(cpvrperp(cpvsub(v_next, v0))); |
| 264 | cpVect of = cpvmult(cpvadd(n1, n2), 1.0/(cpvdot(n1, n2) + 1.0f)); |
| 265 | cpVect v = cpvadd(v0, cpvmult(of, inset)); |
| 266 | |
| 267 | vertexes[4*i + 0] = (Vertex){{(float)v.x, (float)v.y}, {0.0f, 0.0f}, 0.0f, fill, outline}; |
| 268 | vertexes[4*i + 1] = (Vertex){{(float)v.x, (float)v.y}, {(float)n1.x, (float)n1.y}, r, fill, outline}; |
| 269 | vertexes[4*i + 2] = (Vertex){{(float)v.x, (float)v.y}, {(float)of.x, (float)of.y}, r, fill, outline}; |
| 270 | vertexes[4*i + 3] = (Vertex){{(float)v.x, (float)v.y}, {(float)n2.x, (float)n2.y}, r, fill, outline}; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void ChipmunkDebugDrawBB(cpBB bb, cpSpaceDebugColor color) |
| 275 | { |
| 276 | cpVect verts[] = { |
| 277 | cpv(bb.r, bb.b), |
| 278 | cpv(bb.r, bb.t), |
| 279 | cpv(bb.l, bb.t), |
| 280 | cpv(bb.l, bb.b), |
| 281 | }; |
| 282 | ChipmunkDebugDrawPolygon(4, verts, 0.0f, color, LAColor(0, 0)); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | ChipmunkDebugDrawFlushRenderer(void) |
| 287 | { |
| 288 | cpTransform t = ChipmunkDebugDrawVPMatrix; |
| 289 | Uniforms uniforms = { |
| 290 | .U_vp_matrix = {(float)t.a , (float)t.b , 0.0f, 0.0f, (float)t.c , (float)t.d , 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, (float)t.tx, (float)t.ty, 0.0f, 1.0f}, |
| 291 | }; |
| 292 | |
| 293 | sg_update_buffer(VertexBuffer, Vertexes, VertexCount*sizeof(Vertex)); |
| 294 | sg_update_buffer(IndexBuffer, Indexes, IndexCount*sizeof(Index)); |
| 295 | |
| 296 | sg_apply_pipeline(pipeline); |
| 297 | sg_apply_bindings(&bindings); |
| 298 | sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &uniforms, sizeof(Uniforms)); |
| 299 | sg_draw(0, IndexCount, 1); |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | ChipmunkDebugDrawClearRenderer(void) |
| 304 | { |
| 305 | VertexCount = IndexCount = 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static size_t PushedVertexCount, PushedIndexCount; |
| 310 | |
| 311 | void |
| 312 | ChipmunkDebugDrawPushRenderer(void) |
| 313 | { |
| 314 | PushedVertexCount = VertexCount; |
| 315 | PushedIndexCount = IndexCount; |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | ChipmunkDebugDrawPopRenderer(void) |
| 320 | { |
| 321 | VertexCount = PushedVertexCount; |
| 322 | IndexCount = PushedIndexCount; |
| 323 | } |