render update

This commit is contained in:
John Alanbrook 2023-05-22 05:08:08 +00:00
parent 49271b4a5d
commit c16a0332a5
10 changed files with 63 additions and 29 deletions

View file

@ -1,6 +1,7 @@
#include "debugdraw.h" #include "debugdraw.h"
#include "openglrender.h" #include "openglrender.h"
#include "render.h"
#include "shader.h" #include "shader.h"
#include "log.h" #include "log.h"
@ -25,6 +26,7 @@ struct point_vertex {
float radius; float radius;
}; };
static int point_c = 0; static int point_c = 0;
static struct point_vertex point_b[1000];
static sg_shader line_shader; static sg_shader line_shader;
static sg_pipeline line_pipe; static sg_pipeline line_pipe;
@ -37,6 +39,8 @@ struct line_vert {
}; };
static int line_c = 0; static int line_c = 0;
static int line_v = 0; static int line_v = 0;
static struct line_vert line_b[1000];
static uint16_t line_bi[1000];
static sg_pipeline grid_pipe; static sg_pipeline grid_pipe;
static sg_bindings grid_bind; static sg_bindings grid_bind;
@ -53,6 +57,8 @@ struct poly_vertex {
float uv[2]; float uv[2];
struct rgba color; struct rgba color;
}; };
static struct poly_vertex poly_b[1000];
static uint32_t poly_bi[1000];
static sg_pipeline circle_pipe; static sg_pipeline circle_pipe;
static sg_bindings circle_bind; static sg_bindings circle_bind;
@ -64,36 +70,59 @@ struct circle_vertex {
float radius; float radius;
struct rgba color; struct rgba color;
}; };
static struct circle_vertex circle_b[1000];
void debug_flush() void debug_flush()
{ {
if (circle_count != 0) {
sg_apply_pipeline(circle_pipe); sg_apply_pipeline(circle_pipe);
sg_apply_bindings(&circle_bind); sg_apply_bindings(&circle_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection));
sg_update_buffer(circle_bind.vertex_buffers[0], &(sg_range){
.ptr = circle_b,
.size = sizeof(struct circle_vertex)*circle_count
});
sg_draw(0,4,circle_count); sg_draw(0,4,circle_count);
circle_count = 0; circle_count = 0;
}
if (poly_c != 0) {
sg_apply_pipeline(poly_pipe); sg_apply_pipeline(poly_pipe);
sg_apply_bindings(&poly_bind); sg_apply_bindings(&poly_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
sg_update_buffer(poly_bind.vertex_buffers[0], &(sg_range){
.ptr = poly_b, .size = sizeof(struct poly_vertex)*poly_v});
sg_update_buffer(poly_bind.index_buffer, &(sg_range){
.ptr = poly_bi, .size = sizeof(uint32_t)*poly_c});
sg_draw(0,poly_c,1); sg_draw(0,poly_c,1);
poly_c = 0; poly_c = 0;
poly_v = 0; poly_v = 0;
}
if (point_c != 0) {
sg_apply_pipeline(point_pipe); sg_apply_pipeline(point_pipe);
sg_apply_bindings(&point_bind); sg_apply_bindings(&point_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
sg_update_buffer(point_bind.vertex_buffers[0], &(sg_range){
.ptr = point_b,
.size = sizeof(struct point_vertex)*point_c});
sg_draw(0,point_c,1); sg_draw(0,point_c,1);
point_c = 0; point_c = 0;
}
if (line_c != 0) {
sg_apply_pipeline(line_pipe); sg_apply_pipeline(line_pipe);
sg_apply_bindings(&line_bind); sg_apply_bindings(&line_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection));
sg_update_buffer(line_bind.vertex_buffers[0], &(sg_range){
.ptr = line_b, .size = sizeof(struct line_vert)*line_v});
sg_update_buffer(line_bind.index_buffer, &(sg_range){
.ptr = line_bi, .size = sizeof(uint16_t)*line_c});
sg_draw(0,line_c,1); sg_draw(0,line_c,1);
line_c = 0; line_c = 0;
line_v = 0; line_v = 0;
} }
}
static sg_shader_uniform_block_desc projection_ubo = { static sg_shader_uniform_block_desc projection_ubo = {
.size = sizeof(projection), .size = sizeof(projection),
@ -102,15 +131,6 @@ static sg_shader_uniform_block_desc projection_ubo = {
} }
}; };
sg_blend_state blend_trans = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.src_factor_alpha = SG_BLENDFACTOR_SRC_ALPHA,
.src_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
};
void debugdraw_init() void debugdraw_init()
{ {
point_shader = sg_make_shader(&(sg_shader_desc){ point_shader = sg_make_shader(&(sg_shader_desc){
@ -320,8 +340,8 @@ void draw_line(cpVect *a_points, int a_n, struct rgba color, float seg_len)
.size = sizeof(uint16_t)*i_c .size = sizeof(uint16_t)*i_c
}; };
sg_append_buffer(line_bind.vertex_buffers[0], &vr); memcpy(line_b+line_v, v, sizeof(struct line_vert)*n);
sg_append_buffer(line_bind.index_buffer, &ir); memcpy(line_bi+line_c, idxs, sizeof(uint16_t)*i_c);
line_c += i_c; line_c += i_c;
line_v += n; line_v += n;
@ -443,8 +463,8 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos
.size = sizeof(struct poly_vertex)*mesh->num_vertices .size = sizeof(struct poly_vertex)*mesh->num_vertices
}; };
sg_append_buffer(poly_bind.vertex_buffers[0], &vvt); memcpy(poly_b+poly_v, vertices, sizeof(struct poly_vertex)*mesh->num_vertices);
sg_append_buffer(poly_bind.index_buffer, &it); memcpy(poly_bi+poly_c, mesh->triangle_indices, sizeof(uint32_t)*mesh->num_triangles*3);
poly_c += mesh->num_triangles*3; poly_c += mesh->num_triangles*3;
poly_v += mesh->num_vertices; poly_v += mesh->num_vertices;
@ -459,7 +479,7 @@ void draw_circle(int x, int y, float radius, int pixels, struct rgba color, int
cv.pos[1] = y; cv.pos[1] = y;
cv.radius = radius; cv.radius = radius;
cv.color = color; cv.color = color;
sg_append_buffer(circle_bind.vertex_buffers[0], SG_RANGE_REF(cv)); memcpy(circle_b+circle_count, &cv, sizeof(struct circle_vertex));
circle_count++; circle_count++;
} }
@ -520,7 +540,7 @@ void draw_cppoint(struct cpVect point, float r, struct rgba color)
.radius = r .radius = r
}; };
sg_append_buffer(point_bind.vertex_buffers[0], SG_RANGE_REF(p)); memcpy(point_b+point_c, &p, sizeof(struct point_vertex));
point_c++; point_c++;
} }
@ -572,8 +592,8 @@ void draw_poly(cpVect *points, int n, struct rgba color)
.size = sizeof(struct poly_vertex)*n .size = sizeof(struct poly_vertex)*n
}; };
sg_append_buffer(poly_bind.vertex_buffers[0], &ppp); memcpy(poly_b+poly_v, polyverts, sizeof(struct poly_vertex)*n);
sg_append_buffer(poly_bind.index_buffer, &trip); memcpy(poly_bi+poly_c, tridxs, sizeof(uint32_t)*3*tric);
poly_c += tric*3; poly_c += tric*3;
poly_v += n; poly_v += n;

View file

@ -2,7 +2,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <vec.h>
#include "editor.h" #include "editor.h"
void editor_init_project(struct gameproject *gp) void editor_init_project(struct gameproject *gp)

View file

@ -118,11 +118,13 @@ int js_arrlen(JSValue v) {
} }
struct rgba js2color(JSValue v) { struct rgba js2color(JSValue v) {
JSValue ja = js_arridx(v,3);
unsigned char a = JS_IsUndefined(ja) ? 255 : js2int(ja);
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 = js2int(js_arridx(v,3)), .a = a,
}; };
return color; return color;
} }

View file

@ -108,6 +108,7 @@ void font_init(struct shader *textshader) {
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE
}, },
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.colors[0].blend = blend_trans,
}); });
float text_verts[8] = { float text_verts[8] = {

View file

@ -426,9 +426,7 @@ void input_poll(double wait) {
mouseWheelX = 0; mouseWheelX = 0;
mouseWheelY = 0; mouseWheelY = 0;
glfwPollEvents(); glfwWaitEventsTimeout(wait);
// glfwWaitEventsTimeout(wait);
for (int i = 0; i < arrlen(downkeys); i++) for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(&downkeys[i]); call_input_down(&downkeys[i]);

View file

@ -4,6 +4,7 @@
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "sokol/sokol_gfx.h"
struct uv_n { struct uv_n {
unsigned short u; unsigned short u;
@ -15,4 +16,13 @@ struct st_n {
struct uv_n t; struct uv_n t;
}; };
static sg_blend_state blend_trans = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.src_factor_alpha = SG_BLENDFACTOR_SRC_ALPHA,
.src_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
};
#endif #endif

View file

@ -211,8 +211,11 @@ int main(int argc, char **args) {
double elapsed = glfwGetTime() - lastTick; double elapsed = glfwGetTime() - lastTick;
deltaT = elapsed; deltaT = elapsed;
lastTick = glfwGetTime(); lastTick = glfwGetTime();
double wait = fmax(0, renderMS - elapsed); //double wait = fmax(0, renderMS - elapsed);
input_poll(wait); if (sim_playing())
input_poll(fmax(0, renderMS-elapsed));
else
input_poll(1000);
window_all_handle_events(); window_all_handle_events();
framems[framei++] = elapsed; framems[framei++] = elapsed;

View file

@ -1312,8 +1312,6 @@ var editor = {
"PAUSED" : "PAUSED" :
"STOPPED", [0, 0], 1); "STOPPED", [0, 0], 1);
gui_text("FPS " + this.fps, [0, 540], 1);
var clvl = this.edit_level; var clvl = this.edit_level;
var ypos = 200; var ypos = 200;
var lvlcolor = Color.white; var lvlcolor = Color.white;

View file

@ -128,7 +128,7 @@ var Color = {
var GUI = { var GUI = {
text(str, pos, size, color, wrap) { text(str, pos, size, color, wrap) {
size = size ? size : 1; size = size ? size : 1;
color = color ? color : [255,255,255]; color = color ? color : [255,255,255,255];
wrap = wrap ? wrap : 500; wrap = wrap ? wrap : 500;
var h = ui_text(str, pos, size, color, wrap); var h = ui_text(str, pos, size, color, wrap);

View file

@ -6,10 +6,13 @@ out vec4 color;
uniform sampler2D text; uniform sampler2D text;
float osize = 1.0;
void main() void main()
{ {
float lettera = texture(text,TexCoords).r; float lettera = texture(text,TexCoords).r;
// color = vec4(1.f, 1.f, 1.f, texture(text, TexCoords).r); // color = vec4(1.f, 1.f, 1.f, texture(text, TexCoords).r);
if (lettera <= 0.1f) if (lettera <= 0.1f)
discard; discard;