diff options
author | quou <quou@disroot.org> | 2025-01-14 00:04:55 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2025-01-14 00:04:55 +1100 |
commit | fd488f9603f22db0312eadcdb93b7880922dc9a7 (patch) | |
tree | 15b7986e2c06eea57575f4c97811cbc65eaa120a /c2.cpp | |
parent | dfa0b6de5a070d1be63d04574c3b8ce469518250 (diff) |
misc refactoring
Diffstat (limited to 'c2.cpp')
-rw-r--r-- | c2.cpp | 147 |
1 files changed, 12 insertions, 135 deletions
@@ -1,4 +1,7 @@ #include "app.hpp" +#include "camera.hpp" +#include "debugdraw.hpp" +#include "editor.hpp" #include "model.hpp" #include "ui.hpp" #include "video.hpp" @@ -108,7 +111,7 @@ struct Orbit_Cam : public Camera { ); } - void update(const App& app) { + void update_orbit(const App& app) { float dx = ((float)(px - app.mx) / (float)app.w) * sense; float dy = ((float)(py - app.my) / (float)app.h) * sense; int dscroll = pscroll - app.scrolly; @@ -534,134 +537,6 @@ struct Env_Probe { } }; -struct Line_Renderer { - static constexpr int max_lines = 1024; - Staged_Buffer vb, cb; - Shader* shader; - int vert_binding, cbuffer_binding; - int cur; - int w, h; - v3f cur_col; - - struct Vertex { - float x, y, z; - float r, g, b; - }* verts; - - struct CBuffer { - m4f vp; - }; - - void init(Device* dev, Asset_Arena* assets) { - vb.init( - dev, - "Line Renderer mesh", - max_lines * sizeof(Vertex) * 2, - Buffer_Flags::vertex_buffer - ); - cb.init( - dev, - "Line Renderer cbuffer", - sizeof(CBuffer), - Buffer_Flags::constant_buffer - ); - shader = (Shader*)assets->load("debug.csh"); - vert_binding = shader->binding_index("verts"); - cbuffer_binding = shader->descriptor_binding("cbuf"); - assert(vert_binding >= 0); - assert(cbuffer_binding >= 0); - verts = (Vertex*)vb.map(dev); - cur = 0; - } - - void destroy(Device* dev) { - vb.unmap(dev); - vb.destroy(dev); - cb.destroy(dev); - } - - void begin(int wi, int he) { - cur = 0; - w = wi; - h = he; - cur_col = v3f(1.0f, 1.0f, 1.0f); - } - - void colour(const v3f& c) { - cur_col = c; - } - - void add_line(const v3f& s, const v3f& e) { - if (cur >= max_lines) { - assert(0); - return; - } - Vertex& a = verts[cur * 2]; - Vertex& b = verts[cur * 2 + 1]; - a.x = s.x; - a.y = s.y; - a.z = s.z; - b.x = e.x; - b.y = e.y; - b.z = e.z; - a.r = cur_col.x; - a.g = cur_col.y; - a.b = cur_col.z; - b.r = cur_col.x; - b.g = cur_col.y; - b.b = cur_col.z; - cur++; - } - - void add_box(const AABB& b) { - add_line(v3f(b.max.x, b.max.y, b.max.z), v3f(b.max.x, b.max.y, b.min.z)); - add_line(v3f(b.max.x, b.max.y, b.max.z), v3f(b.max.x, b.min.y, b.max.z)); - add_line(v3f(b.max.x, b.max.y, b.max.z), v3f(b.min.x, b.max.y, b.max.z)); - add_line(v3f(b.max.x, b.min.y, b.min.z), v3f(b.max.x, b.max.y, b.min.z)); - add_line(v3f(b.max.x, b.min.y, b.min.z), v3f(b.max.x, b.min.y, b.max.z)); - add_line(v3f(b.min.x, b.max.y, b.max.z), v3f(b.min.x, b.min.y, b.max.z)); - add_line(v3f(b.min.x, b.max.y, b.min.z), v3f(b.max.x, b.max.y, b.min.z)); - add_line(v3f(b.min.x, b.max.y, b.min.z), v3f(b.min.x, b.max.y, b.max.z)); - add_line(v3f(b.min.x, b.min.y, b.max.z), v3f(b.max.x, b.min.y, b.max.z)); - add_line(v3f(b.min.x, b.min.y, b.min.z), v3f(b.max.x, b.min.y, b.min.z)); - add_line(v3f(b.min.x, b.min.y, b.min.z), v3f(b.min.x, b.max.y, b.min.z)); - add_line(v3f(b.min.x, b.min.y, b.min.z), v3f(b.min.x, b.min.y, b.max.z)); - } - - void flush( - const Camera& cam, - Device* dev, - Arena* a, - Render_Pass& rp - ) { - if (!cur) return; - CBuffer* c = (CBuffer*)cb.map(dev); - c->vp = cam.get_proj() * cam.get_view(); - cb.unmap(dev); - Pipeline_Builder pb(a, dev); - Context& ctx = dev->get_ctx(); - cb.update(ctx); - vb.update(ctx); - pb.begin(); - pb.shader(shader->id); - pb.cbuffer(cbuffer_binding, cb.gpuonly); - pb.vertex_format(shader->vf); - pb.geo(Geo_Type::lines); - Pipeline& pip = pb.build(); - Vertex_Buffer_Binding binding[] = {{ - .id = vb.gpuonly, - .offset = 0, - .target = vert_binding - }, {}}; - Draw d{}; - d.verts = binding; - d.vertex_count = cur * 2; - d.instance_count = 1; - ctx.submit(d, pip, rp); - cur = 0; - } -}; - struct Config_Buffer { float offset[2]; }; @@ -744,6 +619,7 @@ struct C2 : public App { ); clamped_linear = create_clamped_linear(dev); ui = UI::create(dev, this, &ui_arena, ui_shader->id); + init_editor(ui); lr.init(dev, &assets); assert(per_frame != 0); vbo = upload_verts(dev); @@ -862,7 +738,8 @@ struct C2 : public App { v3f(1.0f, 0.0f, 0.0f) ); camera.asp = (float)bb.w / (float)bb.h; - camera.update(*this); + camera.update_orbit(*this); + camera.update(); scene.update(camera, dev); ctx.debug_push("scene"); ctx.debug_push("depth prepass"); @@ -888,19 +765,18 @@ struct C2 : public App { ctx.debug_pop(); if (mjp(mbtn_left)) { - std::tie(selected_model, selected_mesh) = - scene.pick(camera, w, h, mx, my); + auto [a, b] = scene.pick(camera, w, h, mx, my); + editor_on_select(a, b); } lr.colour(v3f(1.0f, 0.0f, 0.0f)); - if (selected_model) { - lr.add_box(selected_model->bounds[selected_mesh]); - } + editor_draw(lr); ctx.debug_push("debug"); pb.begin_rp(); pb.rp_target(dev->get_backbuffer(), Clear_Mode::restore); + pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore); lr.flush(camera, dev, &frame_arena, pb.build_rp()); ctx.debug_pop(); @@ -916,6 +792,7 @@ struct C2 : public App { lr.destroy(dev); eprobe.destroy(dev); ui->destroy(); + deinit_editor(); assets.destroy(); dev->destroy_texture(default_texture); dev->destroy_sampler(clamped_linear); |