Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trail overlap #6274

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions code/render/batching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ubyte>(center_r / 4);
vertex_center.g = static_cast<ubyte>(center_g / 4);
vertex_center.b = static_cast<ubyte>(center_b / 4);
vertex_center.a = static_cast<ubyte>(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);
Expand Down Expand Up @@ -909,6 +956,17 @@ 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_tri(int texture, vertex *verts)
{
Assertion((texture >= 0), "batching_add_tri() attempted for invalid texture");
Expand Down Expand Up @@ -957,6 +1015,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);
}
Expand Down
2 changes: 2 additions & 0 deletions code/render/batching.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct batch_vertex {
struct batch_info {
enum material_type {
FLAT_EMISSIVE,
FLAT_EMISSIVE_WITH_BACKFACES,
VOLUME_EMISSIVE,
DISTORTION,
FLAT_OPAQUE,
Expand Down Expand Up @@ -129,6 +130,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);
Expand Down
10 changes: 7 additions & 3 deletions code/weapon/trails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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(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);
}

}
}

Expand Down
Loading