Added width to trigger line rendering, abstracted PhysX away a bit
This commit is contained in:
89
renderer/line_rendering.jai
Normal file
89
renderer/line_rendering.jai
Normal 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;
|
||||
Reference in New Issue
Block a user