prosperon/source/engine/debug/debugdraw.c

629 lines
15 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "debugdraw.h"
2023-05-22 00:08:08 -05:00
#include "render.h"
2023-05-24 20:45:50 -05:00
#include "yugine.h"
#include "log.h"
2023-01-25 21:32:58 -06:00
#include <assert.h>
#include "debug.h"
2023-02-13 08:30:35 -06:00
#include "window.h"
2023-03-03 13:07:59 -06:00
#include "2dphysics.h"
2023-01-25 21:32:58 -06:00
#include "stb_ds.h"
2023-05-04 17:07:00 -05:00
#include "sokol/sokol_gfx.h"
2021-11-30 21:29:18 -06:00
#include "point.sglsl.h"
#include "poly.sglsl.h"
#include "circle.sglsl.h"
#include "line.sglsl.h"
#include "grid.sglsl.h"
2023-05-12 22:21:11 -05:00
#define PAR_STREAMLINES_IMPLEMENTATION
#include "par/par_streamlines.h"
2023-05-12 13:22:05 -05:00
#include "font.h"
2021-11-30 21:29:18 -06:00
2023-06-07 08:41:09 -05:00
#define v_amt 5000
2023-05-24 20:45:50 -05:00
2023-12-15 12:45:09 -06:00
struct flush {
sg_shader shader;
sg_pipeline pipe;
sg_bindings bind;
void *verts;
int c;
int v;
int sc;
int sv;
};
typedef struct flush flush;
static flush fpoint;
static flush circle;
void flushview(flush *f, HMM_Mat4 *view)
{
sg_apply_pipeline(f->pipe);
sg_apply_bindings(&f->bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*view));
sg_draw(f->sc, f->c, 1);
}
void flushpass(flush *f)
{
f->sc = f->c;
f->c = 0;
f->sv = f->v;
f->v = 0;
}
2023-05-16 01:31:13 -05:00
static sg_shader point_shader;
static sg_pipeline point_pipe;
static sg_bindings point_bind;
struct point_vertex {
struct draw_p pos;
2023-05-16 01:31:13 -05:00
struct rgba color;
float radius;
};
static int point_c = 0;
2023-06-05 10:32:45 -05:00
static int point_sc = 0;
2023-05-16 01:31:13 -05:00
2023-05-16 13:31:19 -05:00
static sg_shader line_shader;
static sg_pipeline line_pipe;
static sg_bindings line_bind;
struct line_vert {
struct draw_p pos;
2023-05-16 13:31:19 -05:00
float dist;
struct rgba color;
float seg_len;
2023-05-25 21:55:55 -05:00
float seg_speed;
2023-05-16 13:31:19 -05:00
};
static int line_c = 0;
static int line_v = 0;
2023-06-05 10:32:45 -05:00
static int line_sc = 0;
static int line_sv = 0;
2023-05-12 13:22:05 -05:00
static sg_pipeline grid_pipe;
static sg_bindings grid_bind;
static sg_shader grid_shader;
static int grid_c = 0;
2021-11-30 21:29:18 -06:00
2023-05-13 22:01:53 -05:00
static sg_pipeline poly_pipe;
static sg_bindings poly_bind;
static sg_shader poly_shader;
static int poly_c = 0;
static int poly_v = 0;
2023-06-05 10:32:45 -05:00
static int poly_sc = 0;
static int poly_sv = 0;
2023-05-16 01:31:13 -05:00
struct poly_vertex {
struct draw_p pos;
2023-05-16 01:31:13 -05:00
float uv[2];
struct rgba color;
};
2023-05-13 22:01:53 -05:00
2023-05-12 13:22:05 -05:00
static sg_pipeline circle_pipe;
static sg_bindings circle_bind;
static sg_shader csg;
static int circle_count = 0;
2023-06-05 10:32:45 -05:00
static int circle_sc = 0;
2023-05-16 01:31:13 -05:00
struct circle_vertex {
struct draw_p pos;
2023-05-16 01:31:13 -05:00
float radius;
struct rgba color;
2023-05-24 20:45:50 -05:00
float segsize;
float fill;
2023-05-16 01:31:13 -05:00
};
2023-05-24 20:45:50 -05:00
/* Writes debug data to buffers, and draws */
2023-06-05 10:32:45 -05:00
void debug_flush(HMM_Mat4 *view)
2023-05-07 19:47:49 -05:00
{
2023-05-22 00:08:08 -05:00
if (poly_c != 0) {
2023-06-05 10:32:45 -05:00
sg_apply_pipeline(poly_pipe);
sg_apply_bindings(&poly_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view));
sg_draw(poly_sc,poly_c,1);
2023-05-22 00:08:08 -05:00
}
2023-05-22 00:08:08 -05:00
if (point_c != 0) {
2023-06-05 10:32:45 -05:00
sg_apply_pipeline(point_pipe);
sg_apply_bindings(&point_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view));
sg_draw(point_sc,point_c,1);
2023-05-22 00:08:08 -05:00
}
2023-05-22 00:08:08 -05:00
if (line_c != 0) {
2023-06-05 10:32:45 -05:00
sg_apply_pipeline(line_pipe);
sg_apply_bindings(&line_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view));
lfs_params_t lt;
lt.time = apptime();
sg_apply_uniforms(SG_SHADERSTAGE_FS,0,SG_RANGE_REF(lt));
2023-06-05 10:32:45 -05:00
sg_draw(line_sc,line_c,1);
2023-10-12 17:05:49 -05:00
}
2023-05-27 07:01:17 -05:00
if (circle_count != 0) {
2023-06-05 10:32:45 -05:00
sg_apply_pipeline(circle_pipe);
sg_apply_bindings(&circle_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*view));
sg_draw(circle_sc,4,circle_count);
2023-05-27 07:01:17 -05:00
}
2023-05-07 19:47:49 -05:00
}
2023-06-05 10:32:45 -05:00
void debug_nextpass()
{
point_sc = point_c;
point_c = 0;
circle_sc = circle_count;
circle_count = 0;
line_sv = line_v;
line_v = 0;
line_sc = line_c;
line_c = 0;
poly_sc = poly_c;
poly_c = 0;
poly_sv = poly_v;
poly_v = 0;
}
void debug_newframe()
{
point_sc = 0;
point_c = 0;
circle_sc = circle_count = line_sv = line_v = line_sc = line_c = poly_sc = poly_c = 0;
poly_sv = poly_v = 0;
2023-06-05 10:32:45 -05:00
}
2023-05-12 13:22:05 -05:00
static sg_shader_uniform_block_desc projection_ubo = {
.size = sizeof(projection),
.uniforms = {
[0] = { .name = "proj", .type = SG_UNIFORMTYPE_MAT4 },
}
};
2023-05-24 20:45:50 -05:00
static sg_shader_uniform_block_desc time_ubo = {
.size = sizeof(float),
.uniforms = {
[0] = { .name = "time", .type = SG_UNIFORMTYPE_FLOAT },
}
};
2021-11-30 21:29:18 -06:00
void debugdraw_init()
{
point_shader = sg_make_shader(point_shader_desc(sg_query_backend()));
2023-05-16 01:31:13 -05:00
point_pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = point_shader,
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
[2].format = SG_VERTEXFORMAT_FLOAT /* radius */
}
},
.primitive_type = SG_PRIMITIVETYPE_POINTS,
.colors[0].blend = blend_trans,
.label = "dbg point",
2023-05-16 01:31:13 -05:00
});
point_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
2023-06-05 10:32:45 -05:00
.size = sizeof(struct point_vertex)*v_amt,
2023-05-16 01:31:13 -05:00
.usage = SG_USAGE_STREAM
});
line_shader = sg_make_shader(line_shader_desc(sg_query_backend()));
2023-05-16 13:31:19 -05:00
line_pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = line_shader,
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_FLOAT, /* dist */
[2].format = SG_VERTEXFORMAT_UBYTE4N, /* color */
2023-05-25 21:55:55 -05:00
[3].format = SG_VERTEXFORMAT_FLOAT, /* seg length */
[4].format = SG_VERTEXFORMAT_FLOAT /* dashed line speed */
2023-05-16 13:31:19 -05:00
}
},
.primitive_type = SG_PRIMITIVETYPE_LINES,
.index_type = SG_INDEXTYPE_UINT16,
.colors[0].blend = blend_trans,
.label = "dbg line",
2023-05-16 13:31:19 -05:00
});
line_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
2023-06-05 10:32:45 -05:00
.size = sizeof(struct line_vert)*v_amt,
2023-05-16 13:31:19 -05:00
.usage = SG_USAGE_STREAM
});
line_bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){
2023-06-05 10:32:45 -05:00
.size = sizeof(uint16_t)*v_amt,
2023-05-16 13:31:19 -05:00
.usage = SG_USAGE_STREAM,
.type = SG_BUFFERTYPE_INDEXBUFFER
});
csg = sg_make_shader(circle_shader_desc(sg_query_backend()));
2023-05-12 13:22:05 -05:00
circle_pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = csg,
.layout = {
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT2,
[0].buffer_index = 1,
2023-05-16 01:31:13 -05:00
[1].format = SG_VERTEXFORMAT_FLOAT2,
[2].format = SG_VERTEXFORMAT_FLOAT,
2023-05-24 20:45:50 -05:00
[3].format = SG_VERTEXFORMAT_UBYTE4N,
[4].format = SG_VERTEXFORMAT_FLOAT,
[5].format = SG_VERTEXFORMAT_FLOAT
2023-05-12 13:22:05 -05:00
},
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE,
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.cull_mode = SG_CULLMODE_BACK,
2023-05-16 01:31:13 -05:00
.colors[0].blend = blend_trans,
2023-05-12 13:22:05 -05:00
.label = "circle pipeline"
});
circle_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
2023-05-24 20:45:50 -05:00
.size = sizeof(struct circle_vertex)*v_amt,
2023-05-12 13:22:05 -05:00
.usage = SG_USAGE_STREAM,
});
float circleverts[8] = {
-1,-1,
-1,1,
1,-1,
1,1
};
circle_bind.vertex_buffers[1] = sg_make_buffer(&(sg_buffer_desc){
.data = (sg_range){.ptr = circleverts, .size = sizeof(float)*8},
2023-05-12 13:22:05 -05:00
.usage = SG_USAGE_IMMUTABLE,
});
grid_shader = sg_make_shader(grid_shader_desc(sg_query_backend()));
2023-05-12 13:22:05 -05:00
grid_pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = grid_shader,
.layout = {
.attrs = {
2023-05-16 01:31:13 -05:00
[0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
2023-05-12 13:22:05 -05:00
}
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
2023-10-12 17:05:49 -05:00
.cull_mode = SG_CULLMODE_BACK,
2023-05-12 13:22:05 -05:00
.label = "grid pipeline",
2023-05-16 01:31:13 -05:00
.colors[0].blend = blend_trans,
.label = "dbg grid",
2023-05-12 13:22:05 -05:00
});
2023-05-16 01:31:13 -05:00
2023-05-12 13:22:05 -05:00
grid_bind.vertex_buffers[0] = circle_bind.vertex_buffers[1];
poly_shader = sg_make_shader(poly_shader_desc(sg_query_backend()));
2023-05-13 22:01:53 -05:00
poly_pipe = sg_make_pipeline(&(sg_pipeline_desc){
.shader = poly_shader,
.layout = {
2023-05-16 01:31:13 -05:00
.attrs = { [0].format = SG_VERTEXFORMAT_FLOAT2, /* pos */
[1].format = SG_VERTEXFORMAT_FLOAT2, /* uv */
[2].format = SG_VERTEXFORMAT_UBYTE4N /* color rgba */
2023-05-13 22:01:53 -05:00
}
},
2023-05-16 01:31:13 -05:00
.index_type = SG_INDEXTYPE_UINT32,
.colors[0].blend = blend_trans,
2023-10-12 17:05:49 -05:00
// .cull_mode = SG_CULLMODE_FRONT,
.label = "dbg poly",
2023-05-13 22:01:53 -05:00
});
poly_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
2023-05-24 20:45:50 -05:00
.size = sizeof(struct poly_vertex)*v_amt,
2023-05-13 22:01:53 -05:00
.usage = SG_USAGE_STREAM,
.type = SG_BUFFERTYPE_VERTEXBUFFER,
});
poly_bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){
2023-05-24 20:45:50 -05:00
.size = sizeof(uint32_t)*6*v_amt,
2023-05-13 22:01:53 -05:00
.usage = SG_USAGE_STREAM,
.type = SG_BUFFERTYPE_INDEXBUFFER
});
2021-11-30 21:29:18 -06:00
}
2023-12-19 17:28:45 -06:00
void draw_line(HMM_Vec2 *points, int n, struct rgba color, float seg_len, float seg_speed)
2021-11-30 21:29:18 -06:00
{
2023-10-12 17:05:49 -05:00
if (n < 2) return;
2023-05-16 13:31:19 -05:00
2023-12-19 17:28:45 -06:00
seg_speed = 1;
2023-05-16 13:31:19 -05:00
struct line_vert v[n];
float dist = 0;
2023-10-12 17:05:49 -05:00
for (int i = 0; i < n; i++) {
v[i].pos.x = points[i].x;
v[i].pos.y = points[i].y;
2023-05-16 13:31:19 -05:00
v[i].color = color;
v[i].seg_len = seg_len;
2023-05-25 21:55:55 -05:00
v[i].seg_speed = seg_speed;
2023-05-16 13:31:19 -05:00
}
2023-10-12 17:05:49 -05:00
v[0].dist = 0;
for (int i = 1; i < n; i++) {
2023-11-14 09:20:09 -06:00
dist += HMM_DistV2(points[i-1], points[i]);
2023-10-12 17:05:49 -05:00
v[i].dist = dist;
}
2023-05-16 13:31:19 -05:00
int i_c = (n-1)*2;
uint16_t idxs[i_c];
for (int i = 0, d = 0; i < n-1; i++, d+=2) {
2023-10-12 17:05:49 -05:00
idxs[d] = i + line_v;
idxs[d+1] = idxs[d]+1;
2023-05-16 13:31:19 -05:00
}
sg_range vr = {
.ptr = v,
.size = sizeof(struct line_vert)*n
};
sg_range ir = {
.ptr = idxs,
.size = sizeof(uint16_t)*i_c
};
2023-10-12 17:05:49 -05:00
sg_append_buffer(line_bind.vertex_buffers[0], &vr);
sg_append_buffer(line_bind.index_buffer, &ir);
2023-12-19 17:28:45 -06:00
YughWarn("Drew %d line segments with %d verts and %d indexes, starting at %d and %d.", n-1, n, i_c, line_v, line_c);
2023-05-16 13:31:19 -05:00
2023-12-19 17:28:45 -06:00
line_c += i_c+1;
2023-05-16 13:31:19 -05:00
line_v += n;
2021-11-30 21:29:18 -06:00
}
2023-11-14 09:20:09 -06:00
HMM_Vec2 center_of_vects(HMM_Vec2 *v, int n)
2023-02-15 17:54:05 -06:00
{
2023-11-14 09:20:09 -06:00
HMM_Vec2 c;
2023-02-15 17:54:05 -06:00
for (int i = 0; i < n; i++) {
c.x += v[i].x;
c.y += v[i].y;
}
c.x /= n;
c.y /= n;
return c;
}
2023-12-19 17:28:45 -06:00
/* Given a series of points p, computes a new series with them expanded on either side by d */
2023-11-14 09:20:09 -06:00
HMM_Vec2 *inflatepoints(HMM_Vec2 *p, float d, int n)
2023-03-24 14:01:01 -05:00
{
if (d == 0) {
2023-11-14 09:20:09 -06:00
HMM_Vec2 *ret = NULL;
arraddn(ret,n);
2023-02-16 01:56:22 -06:00
for (int i = 0; i < n; i++)
ret[i] = p[i];
2023-02-16 01:56:22 -06:00
return ret;
2023-02-16 01:56:22 -06:00
}
2023-03-24 14:01:01 -05:00
parsl_position par_v[n];
uint16_t spine_lens[] = {n};
for (int i = 0; i < n; i++) {
par_v[i].x = p[i].x;
par_v[i].y = p[i].y;
};
parsl_context *par_ctx = parsl_create_context((parsl_config){
.thickness = d,
.flags= PARSL_FLAG_ANNOTATIONS,
.u_mode = PAR_U_MODE_DISTANCE
});
parsl_mesh *mesh = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){
.num_vertices = n,
.num_spines = 1,
.vertices = par_v,
.spine_lengths = spine_lens,
.closed = 0,
});
2023-02-16 01:56:22 -06:00
2023-11-14 09:20:09 -06:00
HMM_Vec2 *ret = NULL;
arraddn(ret,mesh->num_vertices);
for (int i = 0; i < mesh->num_vertices; i++) {
ret[i].x = mesh->positions[i].x;
ret[i].y = mesh->positions[i].y;
};
return ret;
2023-02-16 01:56:22 -06:00
}
2023-12-19 17:28:45 -06:00
/* Given a strip of points, draws them as segments. So 5 points is 4 segments, and ultimately 8 vertices */
void draw_edge(HMM_Vec2 *points, int n, struct rgba color, int thickness, int flags, struct rgba line_color, float line_seg)
2021-11-30 21:29:18 -06:00
{
2023-12-19 17:28:45 -06:00
int closed = 0;
if (thickness <= 1) {
draw_line(points,n,color,0,0);
return;
}
return;
2023-05-24 20:45:50 -05:00
2023-05-16 01:31:13 -05:00
/* todo: should be dashed, and filled. use a texture. */
/* draw polygon outline */
2023-11-14 09:20:09 -06:00
if (HMM_EqV2(points[0], points[n-1])) {
2023-05-24 20:45:50 -05:00
closed = true;
n--;
}
2023-05-13 22:01:53 -05:00
parsl_position par_v[n];
for (int i = 0; i < n; i++) {
par_v[i].x = points[i].x;
par_v[i].y = points[i].y;
}
2023-02-16 01:56:22 -06:00
2023-05-13 22:01:53 -05:00
uint16_t spine_lens[] = {n};
parsl_context *par_ctx = parsl_create_context((parsl_config){
2023-05-24 20:45:50 -05:00
.thickness = thickness,
2023-05-16 13:31:19 -05:00
.flags = PARSL_FLAG_ANNOTATIONS,
2023-05-13 22:01:53 -05:00
});
parsl_mesh *mesh = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){
.num_vertices = n,
.num_spines = 1,
2023-05-16 01:31:13 -05:00
.vertices = par_v,
2023-05-13 22:01:53 -05:00
.spine_lengths = spine_lens,
2023-05-16 01:31:13 -05:00
.closed = closed
2023-05-13 22:01:53 -05:00
});
for (int i = 0; i < mesh->num_triangles*3; i++)
2023-06-05 10:32:45 -05:00
mesh->triangle_indices[i] += (poly_v+poly_sv);
2023-05-13 22:01:53 -05:00
2023-05-16 01:31:13 -05:00
struct poly_vertex vertices[mesh->num_vertices];
2023-05-13 22:01:53 -05:00
2023-05-16 01:31:13 -05:00
for (int i = 0; i < mesh->num_vertices; i++) {
vertices[i].pos = (struct draw_p){ .x = mesh->positions[i].x, .y = mesh->positions[i].y };
2023-05-16 01:31:13 -05:00
vertices[i].uv[0] = mesh->annotations[i].u_along_curve;
vertices[i].uv[1] = mesh->annotations[i].v_across_curve;
vertices[i].color = color;
}
2023-10-12 17:05:49 -05:00
sg_append_buffer(poly_bind.vertex_buffers[0], &(sg_range){.ptr = vertices, .size = sizeof(struct poly_vertex)*mesh->num_vertices});
sg_append_buffer(poly_bind.index_buffer, &(sg_range){.ptr = mesh->triangle_indices, sizeof(uint32_t)*mesh->num_triangles*3});
2023-05-13 22:01:53 -05:00
poly_c += mesh->num_triangles*3;
2023-05-16 01:31:13 -05:00
poly_v += mesh->num_vertices;
2023-05-13 22:01:53 -05:00
parsl_destroy_context(par_ctx);
2023-05-24 20:45:50 -05:00
/* Now drawing the line outlines */
if (thickness == 1) {
2023-12-19 17:28:45 -06:00
draw_line(points,n,line_color,line_seg, 0);
2023-05-24 20:45:50 -05:00
} else {
2023-11-14 09:20:09 -06:00
HMM_Vec2 in_p[n];
HMM_Vec2 out_p[n];
2023-05-24 20:45:50 -05:00
2023-12-19 15:34:36 -06:00
for (int i = 1, v = 0; i < n*2+1; i+=2, v++) {
2023-10-12 17:05:49 -05:00
in_p[v].x = vertices[i].pos.x;
in_p[v].y = vertices[i].pos.y;
}
2023-12-19 15:34:36 -06:00
for (int i = 0, v = 0; i < n*2; i+=2,v++) {
2023-10-12 17:05:49 -05:00
out_p[v].x = vertices[i].pos.x;
out_p[v].y = vertices[i].pos.y;
}
if (!closed) {
2023-11-14 09:20:09 -06:00
HMM_Vec2 p[n*2];
2023-10-12 17:05:49 -05:00
for (int i = 0; i < n; i++)
p[i] = in_p[i];
for (int i = n-1, v = n; i >= 0; i--,v++)
p[v] = out_p[i];
2023-12-19 17:28:45 -06:00
draw_line(p,n*2,line_color,line_seg,0);
2023-10-12 17:05:49 -05:00
return;
}
2023-05-24 20:45:50 -05:00
2023-12-19 17:28:45 -06:00
draw_line(in_p,n,line_color,line_seg,0);
draw_line(out_p,n,line_color,line_seg,0);
2023-05-24 20:45:50 -05:00
}
2021-11-30 21:29:18 -06:00
}
2023-11-14 09:20:09 -06:00
void draw_circle(HMM_Vec2 pos, float radius, float pixels, struct rgba color, float seg)
2021-11-30 21:29:18 -06:00
{
2023-05-16 01:31:13 -05:00
struct circle_vertex cv;
cv.pos.x = pos.x;
cv.pos.y = pos.y;
2023-05-16 01:31:13 -05:00
cv.radius = radius;
cv.color = color;
2023-05-25 21:55:55 -05:00
cv.segsize = seg/radius;
2023-05-24 20:45:50 -05:00
cv.fill = pixels/radius;
2023-10-12 17:05:49 -05:00
sg_append_buffer(circle_bind.vertex_buffers[0], &(sg_range){.ptr = &cv, .size = sizeof(struct circle_vertex)});
2023-05-12 13:22:05 -05:00
circle_count++;
2021-11-30 21:29:18 -06:00
}
2023-11-14 09:20:09 -06:00
void draw_box(HMM_Vec2 c, HMM_Vec2 wh, struct rgba color)
2021-11-30 21:29:18 -06:00
{
2023-05-25 21:55:55 -05:00
float hw = wh.x / 2.f;
float hh = wh.y / 2.f;
2021-11-30 21:29:18 -06:00
2023-11-14 09:20:09 -06:00
HMM_Vec2 verts[4] = {
2023-05-25 21:55:55 -05:00
{ .x = c.x-hw, .y = c.y-hh },
{ .x = c.x+hw, .y = c.y-hh },
{ .x = c.x+hw, .y = c.y+hh },
{ .x = c.x-hw, .y = c.y+hh }
2021-11-30 21:29:18 -06:00
};
2023-05-25 21:55:55 -05:00
draw_poly(verts, 4, color);
2023-02-05 17:42:36 -06:00
}
void draw_grid(float width, float span, struct rgba color)
2021-11-30 21:29:18 -06:00
{
2023-11-14 09:20:09 -06:00
HMM_Vec2 offset = (HMM_Vec2)cam_pos();
offset = HMM_MulV2F(offset, 1/cam_zoom());
2023-02-20 16:28:07 -06:00
float ubo[4];
ubo[0] = offset.x;
ubo[1] = offset.y;
ubo[2] = mainwin.rwidth;
ubo[3] = mainwin.rheight;
2023-05-12 13:22:05 -05:00
sg_apply_pipeline(grid_pipe);
sg_apply_bindings(&grid_bind);
float col[4];
rgba2floats(col,color);
fs_params_t pt;
pt.thickness = (float)width;
pt.span = span/cam_zoom();
memcpy(&pt.color, col, sizeof(float)*4);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(ubo));
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(pt));
2023-05-12 13:22:05 -05:00
sg_draw(0,4,1);
2021-11-30 21:29:18 -06:00
}
2023-11-14 09:20:09 -06:00
void draw_cppoint(HMM_Vec2 point, float r, struct rgba color)
2021-11-30 21:29:18 -06:00
{
2023-05-16 13:31:19 -05:00
struct point_vertex p = {
.color = color,
.radius = r
2023-05-16 01:31:13 -05:00
};
2023-11-14 09:20:09 -06:00
p.pos.x = point.X;
p.pos.y = point.Y;
2023-10-12 17:05:49 -05:00
sg_append_buffer(point_bind.vertex_buffers[0], &(sg_range){.ptr = &p, .size = sizeof(struct point_vertex)});
2023-05-16 01:31:13 -05:00
point_c++;
2021-11-30 21:29:18 -06:00
}
2023-11-14 09:20:09 -06:00
void draw_points(HMM_Vec2 *points, int n, float size, struct rgba color)
2023-01-25 21:32:58 -06:00
{
for (int i = 0; i < n; i++)
draw_cppoint(points[i], size, color);
2023-01-25 21:32:58 -06:00
}
2023-11-14 09:20:09 -06:00
void draw_poly(HMM_Vec2 *points, int n, struct rgba color)
2021-11-30 21:29:18 -06:00
{
2023-05-16 01:31:13 -05:00
/* Find polygon mesh */
int tric = n - 2;
2023-05-13 22:01:53 -05:00
2023-05-29 10:47:30 -05:00
if (tric < 1) return;
2023-05-16 01:31:13 -05:00
uint32_t tridxs[tric*3];
2023-05-13 22:01:53 -05:00
2023-05-16 01:31:13 -05:00
for (int i = 2, ti = 0; i < n; i++, ti+=3) {
tridxs[ti] = 0;
tridxs[ti+1] = i-1;
tridxs[ti+2] = i;
}
for (int i = 0; i < tric*3; i++)
2023-06-05 10:32:45 -05:00
tridxs[i] += poly_v+poly_sv;
2023-05-16 01:31:13 -05:00
struct poly_vertex polyverts[n];
2023-05-13 22:01:53 -05:00
2023-05-16 01:31:13 -05:00
for (int i = 0; i < n; i++) {
polyverts[i].pos = (struct draw_p) { .x = points[i].x, .y = points[i].y};
2023-05-16 01:31:13 -05:00
polyverts[i].uv[0] = 0.0;
polyverts[i].uv[1] = 0.0;
polyverts[i].color = color;
2023-05-13 22:01:53 -05:00
}
2023-10-12 17:05:49 -05:00
sg_append_buffer(poly_bind.vertex_buffers[0], &(sg_range){.ptr = polyverts, .size = sizeof(struct poly_vertex)*n});
sg_append_buffer(poly_bind.index_buffer, &(sg_range){.ptr = tridxs, sizeof(uint32_t)*3*tric});
2023-05-13 22:01:53 -05:00
2023-05-16 01:31:13 -05:00
poly_c += tric*3;
poly_v += n;
2021-11-30 21:29:18 -06:00
}