From b6e9b165db7cf93df4fdaf05dfe41c114047c022 Mon Sep 17 00:00:00 2001 From: Kestrellius <63537900+Kestrellius@users.noreply.github.com> Date: Thu, 4 Jul 2024 02:01:18 -0700 Subject: [PATCH 1/5] Adds function for twisted quads; uses for trails. --- code/render/batching.cpp | 60 ++++++++++++++++++++++++++++++++++++++++ code/render/batching.h | 1 + code/weapon/trails.cpp | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/code/render/batching.cpp b/code/render/batching.cpp index 720d974ad29..647dabbe891 100644 --- a/code/render/batching.cpp +++ b/code/render/batching.cpp @@ -439,6 +439,53 @@ void batching_add_quad_internal(primitive_batch* batch, int texture, vertex* ver batch->add_triangle(&v[0], &v[2], &v[3]); } +void batching_add_quad_twisted_internal(primitive_batch* batch, int texture, vertex* verts) +{ + Assert(batch->get_render_info().prim_type == PRIM_TYPE_TRIS); + + const int NUM_VERTICES = 5; + batch_vertex v[NUM_VERTICES]; + + auto array_index = texture - batch->get_render_info().texture; + + auto& vertex_center = v[4]; + vertex_center.tex_coord = vm_vec4_new(0, 0, (float)array_index, 1.0f); + vertex_center.position = ZERO_VECTOR; + uint16_t center_a = 0, center_r = 0, center_g = 0, center_b = 0; + + for ( int i = 0; i < NUM_VERTICES - 1; i++ ) { + v[i].tex_coord = vm_vec4_new(verts[i].texture_position.u, verts[i].texture_position.v, (float)array_index, 1.0f); + + v[i].position = verts[i].world; + + v[i].r = verts[i].r; + v[i].g = verts[i].g; + v[i].b = verts[i].b; + v[i].a = verts[i].a; + + vertex_center.tex_coord.xyzw.x += verts[i].texture_position.u; + vertex_center.tex_coord.xyzw.y += verts[i].texture_position.v; + vertex_center.position += verts[i].world; + center_r += verts[i].r; + center_g += verts[i].g; + center_b += verts[i].b; + center_a += verts[i].a; + } + + vertex_center.tex_coord.xyzw.x /= 4.0f; + vertex_center.tex_coord.xyzw.y /= 4.0f; + vertex_center.position *= 1.0f / 4.0f; + vertex_center.r = static_cast(center_r / 4); + vertex_center.g = static_cast(center_g / 4); + vertex_center.b = static_cast(center_b / 4); + vertex_center.a = static_cast(center_a / 4); + + batch->add_triangle(&v[0], &v[1], &v[4]); + batch->add_triangle(&v[1], &v[2], &v[4]); + batch->add_triangle(&v[2], &v[3], &v[4]); + batch->add_triangle(&v[0], &v[4], &v[3]); +} + void batching_add_tri_internal(primitive_batch *batch, int texture, vertex *verts) { Assert(batch->get_render_info().prim_type == PRIM_TYPE_TRIS); @@ -909,6 +956,19 @@ void batching_add_quad(int texture, vertex *verts, primitive_batch *batch, float batching_add_quad_internal(batch, texture, verts, trapezoidal_correction); } + +void batching_add_quad_twisted(int texture, vertex *verts, primitive_batch *batch) +{ + Assertion((texture >= 0), "batching_add_quad() attempted for invalid texture"); + if ( texture < 0 ) { + return; + } + + batching_add_quad_twisted_internal(batch, texture, verts); +} + +void batching_add_quad_twisted(int texture, vertex *verts, primitive_batch* batch); + void batching_add_tri(int texture, vertex *verts) { Assertion((texture >= 0), "batching_add_tri() attempted for invalid texture"); diff --git a/code/render/batching.h b/code/render/batching.h index 955907d8520..b0727ffbca1 100644 --- a/code/render/batching.h +++ b/code/render/batching.h @@ -129,6 +129,7 @@ void batching_add_tri(int texture, vertex *verts); //these require some blurring the lines between things, but finding the batch in every call gets expensive in some cases such as trails. void batching_add_quad(int texture, vertex *verts, primitive_batch* batch, float trapezoidal_correction = 1.0f); +void batching_add_quad_twisted(int texture, vertex *verts, primitive_batch* batch); void batching_add_tri(int texture, vertex *verts, primitive_batch* batch); void batching_render_all(bool render_distortions = false); diff --git a/code/weapon/trails.cpp b/code/weapon/trails.cpp index d8c3f43b92e..b52a7b64fa6 100644 --- a/code/weapon/trails.cpp +++ b/code/weapon/trails.cpp @@ -333,7 +333,7 @@ void trail_render( trail * trailp ) verts[0].texture_position.v = verts[3].texture_position.v = 0.0f; verts[1].texture_position.v = verts[2].texture_position.v = 1.0f; - batching_add_quad(ti->texture.bitmap_id, verts, batchp); + batching_add_quad_twisted(ti->texture.bitmap_id, verts, batchp); } } From ff5448a16102d07332aa58472c1536203a8d5531 Mon Sep 17 00:00:00 2001 From: Kestrellius <63537900+Kestrellius@users.noreply.github.com> Date: Sun, 7 Jul 2024 13:33:59 -0700 Subject: [PATCH 2/5] Fixes backface issue. --- code/render/batching.cpp | 1 + code/render/batching.h | 1 + code/weapon/trails.cpp | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/code/render/batching.cpp b/code/render/batching.cpp index 647dabbe891..158dcb81ba1 100644 --- a/code/render/batching.cpp +++ b/code/render/batching.cpp @@ -1017,6 +1017,7 @@ void batching_render_batch_item(primitive_batch_item* item, } else { batched_bitmap_material material_def; + material_def.set_cull_mode(item->batch_item_info.mat_type != batch_info::FLAT_EMISSIVE_WITH_BACKFACES); material_set_batched_bitmap(&material_def, item->batch_item_info.texture, 1.0f, 2.0f); gr_render_primitives_batched(&material_def, PRIM_TYPE_TRIS, layout, (int)item->offset, (int)item->n_verts, buffer_num); } diff --git a/code/render/batching.h b/code/render/batching.h index b0727ffbca1..fde7aa39c25 100644 --- a/code/render/batching.h +++ b/code/render/batching.h @@ -22,6 +22,7 @@ struct batch_vertex { struct batch_info { enum material_type { FLAT_EMISSIVE, + FLAT_EMISSIVE_WITH_BACKFACES, VOLUME_EMISSIVE, DISTORTION, FLAT_OPAQUE, diff --git a/code/weapon/trails.cpp b/code/weapon/trails.cpp index b52a7b64fa6..f580c4618c6 100644 --- a/code/weapon/trails.cpp +++ b/code/weapon/trails.cpp @@ -149,7 +149,7 @@ void trail_render( trail * trailp ) return; } - auto batchp = batching_find_batch(ti->texture.bitmap_id, batch_info::FLAT_EMISSIVE); + auto batchp = batching_find_batch(ti->texture.bitmap_id, batch_info::FLAT_EMISSIVE_WITH_BACKFACES); if (trailp->single_segment) { Assertion(trailp->tail == 2, "Single segment trail with more than two values!"); From 8ca31df702067d0bc06c577b32dd27f65fb2aa9e Mon Sep 17 00:00:00 2001 From: Kestrellius <63537900+Kestrellius@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:26:42 -0700 Subject: [PATCH 3/5] Ties change to max detail level. --- code/weapon/trails.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code/weapon/trails.cpp b/code/weapon/trails.cpp index f580c4618c6..5081613a66c 100644 --- a/code/weapon/trails.cpp +++ b/code/weapon/trails.cpp @@ -332,8 +332,12 @@ void trail_render( trail * trailp ) verts[0].texture_position.v = verts[3].texture_position.v = 0.0f; verts[1].texture_position.v = verts[2].texture_position.v = 1.0f; - - batching_add_quad_twisted(ti->texture.bitmap_id, verts, batchp); + if (Detail.num_particles = NUM_DEFAULT_DETAIL_LEVELS) { + batching_add_quad_twisted(ti->texture.bitmap_id, verts, batchp); + } else { + batching_add_quad(ti->texture.bitmap_id, verts, batchp); + } + } } From 2c0e97e38e70a6dc64820dd0eba5691b186ec53a Mon Sep 17 00:00:00 2001 From: Kestrellius <63537900+Kestrellius@users.noreply.github.com> Date: Sat, 27 Jul 2024 16:05:27 -0700 Subject: [PATCH 4/5] fixes syntax error --- code/weapon/trails.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/weapon/trails.cpp b/code/weapon/trails.cpp index 5081613a66c..cd1ae51848b 100644 --- a/code/weapon/trails.cpp +++ b/code/weapon/trails.cpp @@ -332,7 +332,7 @@ void trail_render( trail * trailp ) verts[0].texture_position.v = verts[3].texture_position.v = 0.0f; verts[1].texture_position.v = verts[2].texture_position.v = 1.0f; - if (Detail.num_particles = NUM_DEFAULT_DETAIL_LEVELS) { + if (Detail.num_particles == NUM_DEFAULT_DETAIL_LEVELS) { batching_add_quad_twisted(ti->texture.bitmap_id, verts, batchp); } else { batching_add_quad(ti->texture.bitmap_id, verts, batchp); From add3c761d6cc0339652b25b80e31b2511fd8dc9c Mon Sep 17 00:00:00 2001 From: Kestrellius <63537900+Kestrellius@users.noreply.github.com> Date: Sat, 27 Jul 2024 20:05:38 -0700 Subject: [PATCH 5/5] Removes redundant declaration. --- code/render/batching.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/render/batching.cpp b/code/render/batching.cpp index 158dcb81ba1..34b6975a86b 100644 --- a/code/render/batching.cpp +++ b/code/render/batching.cpp @@ -967,8 +967,6 @@ void batching_add_quad_twisted(int texture, vertex *verts, primitive_batch *batc batching_add_quad_twisted_internal(batch, texture, verts); } -void batching_add_quad_twisted(int texture, vertex *verts, primitive_batch* batch); - void batching_add_tri(int texture, vertex *verts) { Assertion((texture >= 0), "batching_add_tri() attempted for invalid texture");