make dd_line_width stick to the draw command

main
Dominik Madarász 2024-08-15 12:35:28 +02:00
parent 6398fd62ad
commit b6dd7f50d3
6 changed files with 131 additions and 36 deletions

View File

@ -1449,6 +1449,8 @@ typedef struct lightmap_t {
void* screenshot(int components);
void* screenshot_async(int components);
void ddraw_line_width(float width);
void ddraw_line_width_push(float scale);
void ddraw_line_width_pop();
void ddraw_color(unsigned rgb);
void ddraw_color_push(unsigned rgb);
void ddraw_color_pop();

View File

@ -17750,6 +17750,8 @@ API void* screenshot_async(int components); // 3 RGB, 4 RGBA, -3 BGR, -4 BGRA
// [ ] camera, light bulb, light probe,
API void ddraw_line_width(float width);
API void ddraw_line_width_push(float scale);
API void ddraw_line_width_pop();
API void ddraw_color(unsigned rgb);
API void ddraw_color_push(unsigned rgb);
API void ddraw_color_pop();
@ -386184,14 +386186,28 @@ typedef struct text2d_cmd {
static renderstate_t dd_rs;
static uint32_t dd_color = ~0u;
static float dd_line_width = 1.0f;
static GLuint dd_program = -1;
static int dd_u_color = -1;
static map(unsigned,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static map(uint64_t,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static bool dd_use_line = 0;
static bool dd_ontop = 0;
static array(text2d_cmd) dd_text2d;
static array(vec4) dd_matrix2d;
static inline
uint64_t convert_key_from_color_width(uint32_t color, float width) {
union { float f; uint32_t i; } u = { .f = width };
return ((uint64_t)color << 32) | u.i;
}
static inline
void convert_key_to_color_width(uint64_t key, uint32_t *color, float *width) {
*color = key >> 32;
union { float f; uint64_t i; } u = { .i = key };
*width = u.f;
}
void ddraw_push_2d() {
float width = window_width();
float height = window_height();
@ -386215,7 +386231,6 @@ void ddraw_flush() {
ddraw_flush_projview(camera_get_active()->proj, camera_get_active()->view);
}
static float dd_line_width = 1.0f;
void ddraw_flush_projview(mat44 proj, mat44 view) {
do_once dd_rs = renderstate();
dd_rs.depth_test_enabled = dd_ontop;
@ -386240,13 +386255,15 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -386274,12 +386291,14 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? 1 : 0.3); // 0.625);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -386314,9 +386333,18 @@ void ddraw_ontop_pop() {
if(pop) dd_ontop = *pop;
}
static array(float) dd_line_scales;
void ddraw_line_width(float width) {
dd_line_width = width;
}
void ddraw_line_width_push(float scale) {
array_push(dd_line_scales, dd_line_width);
dd_line_width = scale;
}
void ddraw_line_width_pop() {
float *pop = array_pop(dd_line_scales);
if(pop) dd_line_width = *pop;
}
static array(uint32_t) dd_colors;
void ddraw_color(unsigned rgb) {
@ -386332,16 +386360,19 @@ void ddraw_color_pop() {
}
void ddraw_point(vec3 from) {
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], key, 0);
array_push(*found, from);
}
void ddraw_line_thin(vec3 from, vec3 to) { // thin lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], key, 0);
array_push(*found, from);
array_push(*found, to);
}
void ddraw_line(vec3 from, vec3 to) { // thick lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], key, 0);
array_push(*found, from);
array_push(*found, to);
}
@ -386936,7 +386967,7 @@ void ddraw_position( vec3 position, float radius ) {
void ddraw_init() {
do_once {
for( int i = 0; i < 2; ++i )
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_int, hash_int);
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_64, hash_64);
dd_program = shader(dd_vs,dd_fs,"att_position","fragcolor", NULL);
dd_u_color = glGetUniformLocation(dd_program, "u_color");
ddraw_flush(); // alloc vao & vbo, also resets color

View File

@ -35,14 +35,28 @@ typedef struct text2d_cmd {
static renderstate_t dd_rs;
static uint32_t dd_color = ~0u;
static float dd_line_width = 1.0f;
static GLuint dd_program = -1;
static int dd_u_color = -1;
static map(unsigned,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static map(uint64_t,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static bool dd_use_line = 0;
static bool dd_ontop = 0;
static array(text2d_cmd) dd_text2d;
static array(vec4) dd_matrix2d;
static inline
uint64_t convert_key_from_color_width(uint32_t color, float width) {
union { float f; uint32_t i; } u = { .f = width };
return ((uint64_t)color << 32) | u.i;
}
static inline
void convert_key_to_color_width(uint64_t key, uint32_t *color, float *width) {
*color = key >> 32;
union { float f; uint64_t i; } u = { .i = key };
*width = u.f;
}
void ddraw_push_2d() {
float width = window_width();
float height = window_height();
@ -66,7 +80,6 @@ void ddraw_flush() {
ddraw_flush_projview(camera_get_active()->proj, camera_get_active()->view);
}
static float dd_line_width = 1.0f;
void ddraw_flush_projview(mat44 proj, mat44 view) {
do_once dd_rs = renderstate();
dd_rs.depth_test_enabled = dd_ontop;
@ -91,13 +104,15 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -125,12 +140,14 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? 1 : 0.3); // 0.625);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -165,9 +182,18 @@ void ddraw_ontop_pop() {
if(pop) dd_ontop = *pop;
}
static array(float) dd_line_scales;
void ddraw_line_width(float width) {
dd_line_width = width;
}
void ddraw_line_width_push(float scale) {
array_push(dd_line_scales, dd_line_width);
dd_line_width = scale;
}
void ddraw_line_width_pop() {
float *pop = array_pop(dd_line_scales);
if(pop) dd_line_width = *pop;
}
static array(uint32_t) dd_colors;
void ddraw_color(unsigned rgb) {
@ -183,16 +209,19 @@ void ddraw_color_pop() {
}
void ddraw_point(vec3 from) {
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], key, 0);
array_push(*found, from);
}
void ddraw_line_thin(vec3 from, vec3 to) { // thin lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], key, 0);
array_push(*found, from);
array_push(*found, to);
}
void ddraw_line(vec3 from, vec3 to) { // thick lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], key, 0);
array_push(*found, from);
array_push(*found, to);
}
@ -787,7 +816,7 @@ void ddraw_position( vec3 position, float radius ) {
void ddraw_init() {
do_once {
for( int i = 0; i < 2; ++i )
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_int, hash_int);
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_64, hash_64);
dd_program = shader(dd_vs,dd_fs,"att_position","fragcolor", NULL);
dd_u_color = glGetUniformLocation(dd_program, "u_color");
ddraw_flush(); // alloc vao & vbo, also resets color

View File

@ -11,6 +11,8 @@
// [ ] camera, light bulb, light probe,
API void ddraw_line_width(float width);
API void ddraw_line_width_push(float scale);
API void ddraw_line_width_pop();
API void ddraw_color(unsigned rgb);
API void ddraw_color_push(unsigned rgb);
API void ddraw_color_pop();

View File

@ -21374,14 +21374,28 @@ typedef struct text2d_cmd {
static renderstate_t dd_rs;
static uint32_t dd_color = ~0u;
static float dd_line_width = 1.0f;
static GLuint dd_program = -1;
static int dd_u_color = -1;
static map(unsigned,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static map(uint64_t,array(vec3)) dd_lists[2][3] = {0}; // [0/1 ontop][0/1/2 thin lines/thick lines/points]
static bool dd_use_line = 0;
static bool dd_ontop = 0;
static array(text2d_cmd) dd_text2d;
static array(vec4) dd_matrix2d;
static inline
uint64_t convert_key_from_color_width(uint32_t color, float width) {
union { float f; uint32_t i; } u = { .f = width };
return ((uint64_t)color << 32) | u.i;
}
static inline
void convert_key_to_color_width(uint64_t key, uint32_t *color, float *width) {
*color = key >> 32;
union { float f; uint64_t i; } u = { .i = key };
*width = u.f;
}
void ddraw_push_2d() {
float width = window_width();
float height = window_height();
@ -21405,7 +21419,6 @@ void ddraw_flush() {
ddraw_flush_projview(camera_get_active()->proj, camera_get_active()->view);
}
static float dd_line_width = 1.0f;
void ddraw_flush_projview(mat44 proj, mat44 view) {
do_once dd_rs = renderstate();
dd_rs.depth_test_enabled = dd_ontop;
@ -21430,13 +21443,15 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
dd_rs.line_width = (i == 1 ? dd_line_width : 0.3); // 0.625);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -21464,12 +21479,14 @@ void ddraw_flush_projview(mat44 proj, mat44 view) {
for( int i = 0; i < 3; ++i ) { // [0] thin, [1] thick, [2] points
GLenum mode = i < 2 ? GL_LINES : GL_POINTS;
dd_rs.line_width = (i == 1 ? 1 : 0.3); // 0.625);
for each_map(dd_lists[dd_ontop][i], unsigned, rgb, array(vec3), list) {
for each_map(dd_lists[dd_ontop][i], uint64_t, meta, array(vec3), list) {
int count = array_count(list);
if(!count) continue;
unsigned rgbi = 0;
convert_key_to_color_width(meta, &rgbi, &dd_line_width);
renderstate_apply(&dd_rs);
// color
vec3 rgbf = {((rgb>>0)&255)/255.f,((rgb>>8)&255)/255.f,((rgb>>16)&255)/255.f};
vec3 rgbf = {((rgbi>>0)&255)/255.f,((rgbi>>8)&255)/255.f,((rgbi>>16)&255)/255.f};
glUniform3fv(dd_u_color, GL_TRUE, &rgbf.x);
// config vertex data
glBufferData(GL_ARRAY_BUFFER, count * 3 * 4, list, GL_STATIC_DRAW);
@ -21504,9 +21521,18 @@ void ddraw_ontop_pop() {
if(pop) dd_ontop = *pop;
}
static array(float) dd_line_scales;
void ddraw_line_width(float width) {
dd_line_width = width;
}
void ddraw_line_width_push(float scale) {
array_push(dd_line_scales, dd_line_width);
dd_line_width = scale;
}
void ddraw_line_width_pop() {
float *pop = array_pop(dd_line_scales);
if(pop) dd_line_width = *pop;
}
static array(uint32_t) dd_colors;
void ddraw_color(unsigned rgb) {
@ -21522,16 +21548,19 @@ void ddraw_color_pop() {
}
void ddraw_point(vec3 from) {
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][2], key, 0);
array_push(*found, from);
}
void ddraw_line_thin(vec3 from, vec3 to) { // thin lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][0], key, 0);
array_push(*found, from);
array_push(*found, to);
}
void ddraw_line(vec3 from, vec3 to) { // thick lines
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], dd_color, 0);
uint64_t key = convert_key_from_color_width(dd_color, dd_line_width);
array(vec3) *found = map_find_or_add(dd_lists[dd_ontop][1], key, 0);
array_push(*found, from);
array_push(*found, to);
}
@ -22126,7 +22155,7 @@ void ddraw_position( vec3 position, float radius ) {
void ddraw_init() {
do_once {
for( int i = 0; i < 2; ++i )
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_int, hash_int);
for( int j = 0; j < 3; ++j ) map_init(dd_lists[i][j], less_64, hash_64);
dd_program = shader(dd_vs,dd_fs,"att_position","fragcolor", NULL);
dd_u_color = glGetUniformLocation(dd_program, "u_color");
ddraw_flush(); // alloc vao & vbo, also resets color

View File

@ -3817,6 +3817,8 @@ API void* screenshot_async(int components); // 3 RGB, 4 RGBA, -3 BGR, -4 BGRA
// [ ] camera, light bulb, light probe,
API void ddraw_line_width(float width);
API void ddraw_line_width_push(float scale);
API void ddraw_line_width_pop();
API void ddraw_color(unsigned rgb);
API void ddraw_color_push(unsigned rgb);
API void ddraw_color_pop();