Added width to trigger line rendering, abstracted PhysX away a bit

This commit is contained in:
2025-07-19 13:02:38 +02:00
parent 2f6ca9d15b
commit 9353bc3e07
5 changed files with 297 additions and 91 deletions

View File

@@ -0,0 +1,89 @@
MAX_LINES :: 4096;
line_pipeline: Pipeline_State_Handle;
Line_Point :: struct {
position: Vector3;
width: float;
}
Line :: struct {
points: Static_Array(Line_Point, 1024);
color: Color;
}
lines: [..] Line;
init_line_rendering :: () {
line_vertex_buffer = create_vertex_buffer(engine.renderer, null, size_of(Line_Vertex) * MAX_LINES * 6, stride=size_of(Line_Vertex), mappable=true);
{
vs := create_vertex_shader(engine.renderer, "../assets/shaders/line.hlsl", "VS", mesh_data_types = .[.POSITION, .COLOR]);
ps := create_pixel_shader(engine.renderer, "../assets/shaders/line.hlsl", "PS");
line_pipeline = create_pipeline_state(engine.renderer, vs, ps, blend_type=.TRANSPARENT);
}
array_reserve(*lines, 4096);
}
begin_line :: (color: Color) {
current_line = .{};
current_line.color = color;
}
end_line :: () {
array_add(*lines, current_line);
}
add_line_point :: (position: Vector3, width: float) {
array_add(*current_line.points, .{position, width});
}
add_line :: (from: Vector3, to: Vector3, color: Color = .{1,1,1,1}, line_width: float = 0.1) {
hw := line_width*0.5; // @Incomplete
up := -engine.current_scene.camera.forward;
forward := normalize(to - from);
right := cross(up, forward);
line_dir := normalize(to - from);
view_dir := normalize(engine.current_scene.camera.position - (from + to) * 0.5);
offset_dir := normalize(cross(line_dir, view_dir)) * hw;
array_add(*line_vertices, .{from + offset_dir, color});
array_add(*line_vertices, .{to + offset_dir, color});
array_add(*line_vertices, .{from - offset_dir, color});
array_add(*line_vertices, .{to + offset_dir, color});
array_add(*line_vertices, .{to - offset_dir, color});
array_add(*line_vertices, .{from - offset_dir, color});
}
render_lines :: () {
if line_vertices.count > 0 {
upload_data_to_buffer(engine.renderer, line_vertex_buffer, line_vertices.data, size_of(Line_Vertex) * line_vertices.count);
push_cmd_set_draw_mode(engine.renderer, .FILL);
push_cmd_set_depth_write(engine.renderer, true);
push_cmd_set_cull_face(engine.renderer, .NONE);
push_cmd_set_pipeline_state(engine.renderer, line_pipeline);
push_cmd_set_constant_buffer(engine.renderer, 0, engine.camera_buffer, .VERTEX);
push_cmd_set_vertex_buffer(engine.renderer, line_vertex_buffer);
push_cmd_draw(engine.renderer, xx line_vertices.count);
}
line_vertices.count = 0;
}
#scope_file
Line_Vertex :: struct {
position: Vector3;
color: Color;
}
line_vertex_buffer: Buffer_Handle;
line_vertices: [..] Line_Vertex;

View File

@@ -1,3 +1,5 @@
#load "line_rendering.jai";
TRIGGER_VERTEX_SHADER :: #string END
cbuffer CameraData : register(b0)
{
@@ -59,65 +61,28 @@ init_trigger_line_rendering :: () {
}
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;
if e.physics.type == .BOX {
color := Color.{0,0,1,1};//e.collider.aabb_color;
aabb := AABB.{-e.physics.box.half_extent, e.physics.box.half_extent};
min := transform_position(aabb.min, e.transform.model_matrix);
max := transform_position(aabb.max, e.transform.model_matrix);
min := aabb.min;
max := aabb.max;
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});
add_line(transform_position(.{min.x, min.y, min.z}, e.transform.model_matrix), transform_position(.{min.x, min.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{max.x, min.y, min.z}, e.transform.model_matrix), transform_position(.{max.x, min.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, max.y, min.z}, e.transform.model_matrix), transform_position(.{min.x, max.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{max.x, max.y, min.z}, e.transform.model_matrix), transform_position(.{max.x, max.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, min.y, min.z}, e.transform.model_matrix), transform_position(.{min.x, max.y, min.z}, e.transform.model_matrix), color);
add_line(transform_position(.{max.x, min.y, min.z}, e.transform.model_matrix), transform_position(.{max.x, max.y, min.z}, e.transform.model_matrix), color);
add_line(transform_position(.{max.x, min.y, max.z}, e.transform.model_matrix), transform_position(.{max.x, max.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, min.y, max.z}, e.transform.model_matrix), transform_position(.{min.x, max.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, min.y, min.z}, e.transform.model_matrix), transform_position(.{max.x, min.y, min.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, max.y, min.z}, e.transform.model_matrix), transform_position(.{max.x, max.y, min.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, min.y, max.z}, e.transform.model_matrix), transform_position(.{max.x, min.y, max.z}, e.transform.model_matrix), color);
add_line(transform_position(.{min.x, max.y, max.z}, e.transform.model_matrix), transform_position(.{max.x, max.y, max.z}, e.transform.model_matrix), 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);
render_lines();
}