dashed lines

This commit is contained in:
John Alanbrook 2023-05-16 18:31:19 +00:00
parent d662d9e2fa
commit 630d823591
10 changed files with 3480 additions and 58 deletions

View file

@ -26,6 +26,18 @@ struct point_vertex {
}; };
static int point_c = 0; static int point_c = 0;
static sg_shader line_shader;
static sg_pipeline line_pipe;
static sg_bindings line_bind;
struct line_vert {
cpVect pos;
float dist;
struct rgba color;
float seg_len;
};
static int line_c = 0;
static int line_v = 0;
static sg_pipeline grid_pipe; static sg_pipeline grid_pipe;
static sg_bindings grid_bind; static sg_bindings grid_bind;
static sg_shader grid_shader; static sg_shader grid_shader;
@ -74,6 +86,13 @@ void debug_flush()
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
sg_draw(0,point_c,1); sg_draw(0,point_c,1);
point_c = 0; point_c = 0;
sg_apply_pipeline(line_pipe);
sg_apply_bindings(&line_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
sg_draw(0,line_c,1);
line_c = 0;
line_v = 0;
} }
static sg_shader_uniform_block_desc projection_ubo = { static sg_shader_uniform_block_desc projection_ubo = {
@ -118,6 +137,38 @@ void debugdraw_init()
.usage = SG_USAGE_STREAM .usage = SG_USAGE_STREAM
}); });
line_shader = sg_make_shader(&(sg_shader_desc){
.vs.source = slurp_text("shaders/linevert.glsl"),
.fs.source = slurp_text("shaders/linefrag.glsl"),
.vs.uniform_blocks[0] = projection_ubo
});
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 */
[3].format = SG_VERTEXFORMAT_FLOAT /* seg length */
}
},
.primitive_type = SG_PRIMITIVETYPE_LINES,
.index_type = SG_INDEXTYPE_UINT16,
.colors[0].blend = blend_trans
});
line_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(struct line_vert)*5000,
.usage = SG_USAGE_STREAM
});
line_bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(uint16_t)*5000,
.usage = SG_USAGE_STREAM,
.type = SG_BUFFERTYPE_INDEXBUFFER
});
csg = sg_make_shader(&(sg_shader_desc){ csg = sg_make_shader(&(sg_shader_desc){
.vs.source = slurp_text("shaders/circlevert.glsl"), .vs.source = slurp_text("shaders/circlevert.glsl"),
.fs.source = slurp_text("shaders/circlefrag.glsl"), .fs.source = slurp_text("shaders/circlefrag.glsl"),
@ -223,10 +274,57 @@ void debugdraw_init()
}); });
} }
void draw_line(cpVect s, cpVect e, struct rgba color) void draw_line(cpVect *a_points, int a_n, struct rgba color, float seg_len)
{ {
cpVect verts[2] = {s, e}; if (a_n < 2) return;
draw_poly(verts, 2, color); int n = a_n+1;
cpVect points[n];
for (int i = 0; i < n; i++)
points[i] = a_points[i];
points[n-1] = a_points[0];
struct line_vert v[n];
float dist = 0;
for (int i = 0; i < n-1; i++) {
v[i].pos = points[i];
v[i].dist = dist;
v[i].color = color;
v[i].seg_len = seg_len;
dist += cpvdist(points[i], points[i+1]);
}
v[n-1].pos = points[n-1];
v[n-1].dist = dist;
v[n-1].color = color;
v[n-1].seg_len = seg_len;
int i_c = (n-1)*2;
uint16_t idxs[i_c];
for (int i = 0, d = 0; i < n-1; i++, d+=2) {
idxs[d] = i + line_v;
idxs[d+1] = i+1 + line_v;
}
sg_range vr = {
.ptr = v,
.size = sizeof(struct line_vert)*n
};
sg_range ir = {
.ptr = idxs,
.size = sizeof(uint16_t)*i_c
};
sg_append_buffer(line_bind.vertex_buffers[0], &vr);
sg_append_buffer(line_bind.index_buffer, &ir);
line_c += i_c;
line_v += n;
} }
cpVect center_of_vects(cpVect *v, int n) cpVect center_of_vects(cpVect *v, int n)
@ -310,7 +408,8 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos
parsl_context *par_ctx = parsl_create_context((parsl_config){ parsl_context *par_ctx = parsl_create_context((parsl_config){
.thickness = 1, .thickness = 1,
.flags = PARSL_FLAG_ANNOTATIONS .flags = PARSL_FLAG_ANNOTATIONS,
.u_mode = PAR_U_MODE_DISTANCE,
}); });
parsl_mesh *mesh = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){ parsl_mesh *mesh = parsl_mesh_from_lines(par_ctx, (parsl_spine_list){
@ -385,8 +484,9 @@ void draw_box(struct cpVect c, struct cpVect wh, struct rgba color)
} }
void draw_arrow(struct cpVect start, struct cpVect end, struct rgba color, int capsize) void draw_arrow(struct cpVect start, struct cpVect end, struct rgba color, int capsize)
{ {
draw_line(start, end, color); cpVect points[2] = {start, end};
draw_line(points, 2, color, 0);
draw_cppoint(end, capsize, color); draw_cppoint(end, capsize, color);
} }
@ -412,37 +512,27 @@ void draw_grid(int width, int span, struct rgba color)
sg_draw(0,4,1); sg_draw(0,4,1);
} }
void draw_point(int x, int y, float r, struct rgba color)
{
struct point_vertex p;
p.pos.x = x;
p.pos.y = y;
p.color = color;
p.radius = r;
sg_range pt = {
.ptr = &p,
.size = sizeof(p)
};
sg_append_buffer(point_bind.vertex_buffers[0], &pt);
point_c++;
}
void draw_cppoint(struct cpVect point, float r, struct rgba color) void draw_cppoint(struct cpVect point, float r, struct rgba color)
{ {
draw_point(point.x, point.y, r, color); struct point_vertex p = {
.pos = point,
.color = color,
.radius = r
};
sg_append_buffer(point_bind.vertex_buffers[0], SG_RANGE_REF(p));
point_c++;
} }
void draw_points(struct cpVect *points, int n, float size, struct rgba color) void draw_points(struct cpVect *points, int n, float size, struct rgba color)
{ {
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
draw_point(points[i].x, points[i].y, size, color); draw_cppoint(points[i], size, color);
} }
void draw_poly(cpVect *points, int n, struct rgba color) void draw_poly(cpVect *points, int n, struct rgba color)
{ {
draw_edge(points,n,color,1,1,0); draw_line(points, n, color, 10);
color.a = 40; color.a = 40;

View file

@ -5,20 +5,22 @@
struct rgba; struct rgba;
void debugdraw_init(); void debugdraw_init();
void draw_line(cpVect s, cpVect e, struct rgba color); void draw_cppoint(struct cpVect point, float r, struct rgba color);
void draw_edge(struct cpVect *points, int n, struct rgba color, int thickness, int closed, int flags);
void draw_points(struct cpVect *points, int n, float size, struct rgba color); void draw_points(struct cpVect *points, int n, float size, struct rgba color);
void draw_line(cpVect *points, int n, struct rgba color, float seg_len);
void draw_arrow(struct cpVect start, struct cpVect end, struct rgba, int capsize); void draw_arrow(struct cpVect start, struct cpVect end, struct rgba, int capsize);
void draw_edge(struct cpVect *points, int n, struct rgba color, int thickness, int closed, int flags);
void draw_circle(int x, int y, float radius, int pixels, struct rgba color, int fill); void draw_circle(int x, int y, float radius, int pixels, struct rgba color, int fill);
void draw_grid(int width, int span, struct rgba color);
void draw_rect(int x, int y, int w, int h, struct rgba color); void draw_rect(int x, int y, int w, int h, struct rgba color);
void draw_box(struct cpVect c, struct cpVect wh, struct rgba color); void draw_box(struct cpVect c, struct cpVect wh, struct rgba color);
void draw_point(int x, int y, float r, struct rgba color);
void draw_cppoint(struct cpVect point, float r, struct rgba color);
void draw_poly(cpVect *points, int n, struct rgba color); void draw_poly(cpVect *points, int n, struct rgba color);
void debug_flush(); void draw_grid(int width, int span, struct rgba color);
void debug_flush();
void debugdraw_flush(); /* This is called once per frame to draw all queued elements */ void debugdraw_flush(); /* This is called once per frame to draw all queued elements */

View file

@ -119,10 +119,10 @@ int js_arrlen(JSValue v) {
struct rgba js2color(JSValue v) { struct rgba js2color(JSValue v) {
struct rgba color = { struct rgba color = {
.r = js2int(js_arridx(v, 0)), .r = js2int(js_arridx(v,0)),
.g = js2int(js_arridx(v, 1)), .g = js2int(js_arridx(v,1)),
.b = js2int(js_arridx(v, 2)), .b = js2int(js_arridx(v,2)),
.a = 255 .a = js2int(js_arridx(v,3)),
}; };
return color; return color;
} }
@ -719,7 +719,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
return JS_NULL; return JS_NULL;
case 47: case 47:
draw_grid(js2int(argv[1]), js2int(argv[2]), color_white); draw_grid(js2int(argv[1]), js2int(argv[2]), js2color(argv[3]));
return JS_NULL; return JS_NULL;
case 48: case 48:

View file

@ -249,10 +249,7 @@ void gameobject_setpos(struct gameobject *go, cpVect vec) {
void object_gui(struct gameobject *go) { void object_gui(struct gameobject *go) {
/* /*
float temp_pos[2]; float temp_pos[2];
temp_pos[0] = cpBodyGetPosition(go->body).x; draw_cppoint(cpBodyGetPosition(go->body), 3);
temp_pos[1] = cpBodyGetPosition(go->body).y;
draw_point(temp_pos[0], temp_pos[1], 3);
nuke_property_float2("Position", -1000000.f, temp_pos, 1000000.f, 1.f, 0.5f); nuke_property_float2("Position", -1000000.f, temp_pos, 1000000.f, 1.f, 0.5f);
@ -327,7 +324,7 @@ void gameobject_draw_debug(int go) {
.g = 255, .g = 255,
.a = 255 .a = 255
}; };
draw_point(pos.x, pos.y, 3.f, color); draw_cppoint(pos, 3.f, color);
cpBodyEachShape(g->body, body_draw_shapes_dbg, NULL); cpBodyEachShape(g->body, body_draw_shapes_dbg, NULL);
} }

View file

@ -291,8 +291,6 @@ void openglRender(struct window *window) {
if (debugDrawPhysics) if (debugDrawPhysics)
gameobject_draw_debugs(); gameobject_draw_debugs();
struct rgba c = {100,130,200,255};
draw_circle(100,100,40,2,c,0);
call_debugs(); call_debugs();
debug_flush(); debug_flush();

File diff suppressed because it is too large Load diff

View file

@ -8,8 +8,9 @@ var Gizmos = {
var Debug = { var Debug = {
draw_grid(width, span) { draw_grid(width, span, color) {
cmd(47, width, span); color = color ? color : Color.green;
cmd(47, width, span, color);
}, },
point(pos, size, color) { point(pos, size, color) {

View file

@ -116,13 +116,13 @@ var Window = {
}; };
var Color = { var Color = {
white: [255,255,255], white: [255,255,255,255],
blue: [84,110,255], blue: [84,110,255,255],
green: [120,255,10], green: [120,255,10,255],
yellow: [251,255,43], yellow: [251,255,43,255],
red: [255,36,20], red: [255,36,20,255],
teal: [96, 252, 237], teal: [96, 252, 237,255],
gray: [181, 181,181], gray: [181, 181,181,255],
}; };
var GUI = { var GUI = {

View file

@ -1,10 +1,31 @@
#version 330 #version 330
out vec4 color; out vec4 color;
uniform vec3 linecolor; in float dist;
uniform float alpha; in vec4 fcolor;
in float seg_len;
float pat = 0.5;
int pp = 0x0C24;
void main() void main()
{ {
color = vec4(1.f,1.f,1.f,1.f); color = fcolor;
if (mod(dist,seg_len)/seg_len < 0.5)
discard;
/*
int d = int(dist);
if (pp * mod((d / 20), 16) == 0)
discard;
*/
/*
float patrn = 16 * mod(dist,seg_len)/seg_len;
if (patrn < 8)
discard;
*/
} }

View file

@ -1,9 +1,19 @@
#version 330 #version 330
layout (location = 0) in vec2 pos; layout (location = 0) in vec2 apos;
layout (location = 1) in float adist;
layout (location = 2) in vec4 acolor;
layout (location = 3) in float aseglen;
out float dist;
out vec4 fcolor;
out float seg_len;
uniform mat4 proj; uniform mat4 proj;
void main() void main()
{ {
gl_Position = proj * vec4(pos, 0.f, 1.f); gl_Position = proj * vec4(apos, 0.f, 1.f);
fcolor = acolor;
dist = adist;
seg_len = aseglen;
} }