AABB rendering

This commit is contained in:
2025-03-23 00:42:56 +01:00
parent 2208a7200f
commit 74268015a0
3 changed files with 156 additions and 22 deletions

View File

@@ -51,6 +51,7 @@ Collider :: struct {
override_aabb: bool;
render_aabb: bool;
aabb_color: Vector4 = .{0,1,0,1};
aabb: AABB;
union {
@@ -231,33 +232,41 @@ physics_step :: (scene: *Scene, timestep: float) {
if !can_collide(e, other_e) continue;
if other_e.flags & .COLLISION {
point := gjk(e.collider, other_e.collider);
if other_e.collider.type == .AABB {
inv_matrix := inverse(other_e.transform.model_matrix);
aabb := other_e.collider.aabb;
if point_inside_aabb(aabb, transform_position(e.transform.position, inv_matrix)) {
//add_trigger_overlap_if_new(other_e, e);
}
} else {
point := gjk(e.collider, other_e.collider);
if point.has_collision {
if other_e.flags & .TRIGGER {
// TRIGGER CALLBACK
add_trigger_overlap_if_new(other_e, e);
} else {
n := -point.normal;
speed_along_normal := dot(e.body.velocity, n);
if point.has_collision {
if other_e.flags & .TRIGGER {
// TRIGGER CALLBACK
add_trigger_overlap_if_new(other_e, e);
} else {
n := -point.normal;
speed_along_normal := dot(e.body.velocity, n);
restitution := e.body.bounciness;
impulse := n * (-(1.0 + restitution) * speed_along_normal);
e.body.velocity += impulse;
restitution := e.body.bounciness;
impulse := n * (-(1.0 + restitution) * speed_along_normal);
e.body.velocity += impulse;
percent := 0.1;
slop := 0.005;
correction := n * max(point.penetration_depth - slop, 0.0) / (1.0 / percent);
set_position(*e.transform, e.transform.position + correction);
percent := 0.1;
slop := 0.005;
correction := n * max(point.penetration_depth - slop, 0.0) / (1.0 / percent);
set_position(*e.transform, e.transform.position + correction);
if e.body.check_for_grounded {
e.body.grounded = dot(n, WORLD_UP) > 0.6; // @Incomplete: Add allowed angle variable at some point?
if e.body.check_for_grounded {
e.body.grounded = dot(n, WORLD_UP) > 0.6; // @Incomplete: Add allowed angle variable at some point?
}
// @Incomplete: This shouldn't be in here
//if e.type == Diamond && length(impulse) > 2.0 {
// play_audio_event(sfx_diamond_hit);
//}
}
// @Incomplete: This shouldn't be in here
//if e.type == Diamond && length(impulse) > 2.0 {
// play_audio_event(sfx_diamond_hit);
//}
}
}
}

View File

@@ -678,6 +678,7 @@ create_renderer :: (window: *Window) -> *Renderer {
init_freetype();
init_default_meshes();
init_trigger_line_rendering();
array_reserve(*engine.renderer.command_buffer.commands, 4096);
@@ -1639,6 +1640,7 @@ render :: () {
engine.renderer.used_text_buffers_count = 0;
}
#load "trigger_rendering.jai";
#load "render_graph.jai";
#load "font.jai";
#load "mesh.jai";

View File

@@ -0,0 +1,123 @@
TRIGGER_VERTEX_SHADER :: #string END
cbuffer CameraData : register(b0)
{
float4x4 projection;
float4x4 view;
float4 camera_position;
};
struct PS_INPUT
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
PS_INPUT main_vs(float3 pos : POSITION, float4 color : COLOR)
{
PS_INPUT output;
output.position = float4(pos.x, pos.y, pos.z, 1.0);
output.position = mul(output.position, view);
output.position = mul(output.position, projection);
output.color = color;
return output;
}
END
TRIGGER_PIXEL_SHADER :: #string END
struct PS_INPUT
{
float4 position : SV_POSITION;
float4 color : COLOR0;
};
float4 main_ps(PS_INPUT input) : SV_Target
{
return input.color;
}
END
MAX_TRIGGER_LINES :: 1024;
trigger_line_buffer : Buffer_Handle;
trigger_pipeline : Pipeline_State_Handle;
Trigger_Line_Vertex :: struct {
position: Vector3;
color: Vector4;
}
init_trigger_line_rendering :: () {
trigger_line_buffer = create_vertex_buffer(engine.renderer, null, size_of(Trigger_Line_Vertex) * MAX_TRIGGER_LINES * 2, stride=size_of(Trigger_Line_Vertex), mappable=true);
{
vs := create_vertex_shader_from_source(engine.renderer, "trigger_vertex_shader", TRIGGER_VERTEX_SHADER, "main_vs", mesh_data_types = .[.POSITION, .NORMAL, .TEXCOORD, .BONE_INDICES, .BONE_WEIGHTS], defines = string.["SKINNING"]);
ps := create_pixel_shader_from_source(engine.renderer, "trigger_pixel_shader", TRIGGER_PIXEL_SHADER, "main_ps");
trigger_pipeline = create_pipeline_state2(engine.renderer, vs, ps, blend_type=.TRANSPARENT);
}
}
render_trigger_lines :: () {
vertices: [..] Trigger_Line_Vertex;
vertices.allocator = temp;
for e: engine.current_scene.entities {
if e.collider.render_aabb {
color := e.collider.aabb_color;
scale := e.transform.scale;
aabb := e.collider.aabb;
min := transform_position(aabb.min, e.transform.model_matrix);
max := transform_position(aabb.max, e.transform.model_matrix);
array_add(*vertices, .{.{min.x, min.y, min.z}, color});
array_add(*vertices, .{.{min.x, min.y, max.z}, color});
array_add(*vertices, .{.{max.x, min.y, min.z}, color});
array_add(*vertices, .{.{max.x, min.y, max.z}, color});
array_add(*vertices, .{.{min.x, max.y, min.z}, color});
array_add(*vertices, .{.{min.x, max.y, max.z}, color});
array_add(*vertices, .{.{max.x, max.y, min.z}, color});
array_add(*vertices, .{.{max.x, max.y, max.z}, color});
array_add(*vertices, .{.{min.x, min.y, min.z}, color});
array_add(*vertices, .{.{min.x, max.y, min.z}, color});
array_add(*vertices, .{.{max.x, min.y, min.z}, color});
array_add(*vertices, .{.{max.x, max.y, min.z}, color});
array_add(*vertices, .{.{max.x, min.y, max.z}, color});
array_add(*vertices, .{.{max.x, max.y, max.z}, color});
array_add(*vertices, .{.{min.x, min.y, max.z}, color});
array_add(*vertices, .{.{min.x, max.y, max.z}, color});
array_add(*vertices, .{.{min.x, min.y, min.z}, color});
array_add(*vertices, .{.{max.x, min.y, min.z}, color});
array_add(*vertices, .{.{min.x, max.y, min.z}, color});
array_add(*vertices, .{.{max.x, max.y, min.z}, color});
array_add(*vertices, .{.{min.x, min.y, max.z}, color});
array_add(*vertices, .{.{max.x, min.y, max.z}, color});
array_add(*vertices, .{.{min.x, max.y, max.z}, color});
array_add(*vertices, .{.{max.x, max.y, max.z}, color});
}
}
upload_data_to_buffer(engine.renderer, trigger_line_buffer, vertices.data, vertices.count * size_of(Trigger_Line_Vertex));
push_cmd_set_draw_mode(engine.renderer, .WIREFRAME);
push_cmd_set_depth_write(engine.renderer, true);
push_cmd_set_cull_face(engine.renderer, .NONE);
push_cmd_set_pipeline_state(engine.renderer, trigger_pipeline);
push_cmd_set_constant_buffer(engine.renderer, 0, engine.camera_buffer, .VERTEX);
push_cmd_set_vertex_buffer(engine.renderer, trigger_line_buffer);
push_cmd_draw(engine.renderer, xx vertices.count, 0, topology=.LINE_LIST);
}