diff options
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | c2.cpp | 49 | ||||
-rw-r--r-- | configure.lua | 1 | ||||
-rw-r--r-- | editor.cpp | 16 | ||||
-rw-r--r-- | editor.hpp | 4 | ||||
-rw-r--r-- | model.cpp | 51 | ||||
-rw-r--r-- | model.hpp | 7 | ||||
-rw-r--r-- | scene.cpp | 67 | ||||
-rw-r--r-- | scene.hpp | 39 |
9 files changed, 163 insertions, 81 deletions
@@ -58,14 +58,16 @@ model.o: model.cpp g++ -std=c++20 $(opt_com) $(cflags) -c model.cpp -o model.o pipeline.o: pipeline.cpp g++ -std=c++20 $(opt_com) $(cflags) -c pipeline.cpp -o pipeline.o +scene.o: scene.cpp + g++ -std=c++20 $(opt_com) $(cflags) -c scene.cpp -o scene.o ui.o: ui.cpp g++ -std=c++20 $(opt_com) $(cflags) -c ui.cpp -o ui.o video.o: video.cpp g++ -std=c++20 $(opt_com) $(cflags) -c video.cpp -o video.o world.o: world.cpp g++ -std=c++20 $(opt_com) $(cflags) -c world.cpp -o world.o -c2: app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o ui.o video.o world.o libqstd.a - g++ $(opt_lnk) $(lflags) -o c2 app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o ui.o video.o world.o libqstd.a -lX11 -lm +c2: app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o scene.o ui.o video.o world.o libqstd.a + g++ $(opt_lnk) $(lflags) -o c2 app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o scene.o ui.o video.o world.o libqstd.a -lX11 -lm c2: pack @@ -131,10 +133,10 @@ pack: packer data/debug.csh data/mip_spec.csh data/sky.csh data/surface.csh data data: mkdir -p data --include qstd/memory.d qstd/plat.d qstd/str.d qstd/pack.d cfg/cfgparse.d sc/sc.d sc/includer.d app.d asset.d c2.d camera.d debugdraw.d editor.d maths.d model.d pipeline.d ui.d video.d world.d convtexture.d convmodel.d convmaterial.d packer.d +-include qstd/memory.d qstd/plat.d qstd/str.d qstd/pack.d cfg/cfgparse.d sc/sc.d sc/includer.d app.d asset.d c2.d camera.d debugdraw.d editor.d maths.d model.d pipeline.d scene.d ui.d video.d world.d convtexture.d convmodel.d convmaterial.d packer.d clean: - rm -f qstd/memory.o qstd/plat.o qstd/str.o qstd/pack.o cfg/cfgparse.o sc/sc.o sc/includer.o app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o ui.o video.o world.o convtexture.o convmodel.o convmaterial.o packer.o qstd/memory.d qstd/plat.d qstd/str.d qstd/pack.d cfg/cfgparse.d sc/sc.d sc/includer.d app.d asset.d c2.d camera.d debugdraw.d editor.d maths.d model.d pipeline.d ui.d video.d world.d convtexture.d convmodel.d convmaterial.d packer.d data/debug.csh data/mip_spec.csh data/sky.csh data/surface.csh data/surface_depthonly.csh data/triangle.csh data/ui.csh data/monkey.mdl data/22.tex data/kita.tex data/brick_albedo.tex data/brick_ao.tex data/brick_normal.tex data/sky.tex data/bricks.mat data/plastic.mat + rm -f qstd/memory.o qstd/plat.o qstd/str.o qstd/pack.o cfg/cfgparse.o sc/sc.o sc/includer.o app.o asset.o c2.o camera.o debugdraw.o editor.o maths.o model.o pipeline.o scene.o ui.o video.o world.o convtexture.o convmodel.o convmaterial.o packer.o qstd/memory.d qstd/plat.d qstd/str.d qstd/pack.d cfg/cfgparse.d sc/sc.d sc/includer.d app.d asset.d c2.d camera.d debugdraw.d editor.d maths.d model.d pipeline.d scene.d ui.d video.d world.d convtexture.d convmodel.d convmaterial.d packer.d data/debug.csh data/mip_spec.csh data/sky.csh data/surface.csh data/surface_depthonly.csh data/triangle.csh data/ui.csh data/monkey.mdl data/22.tex data/kita.tex data/brick_albedo.tex data/brick_ao.tex data/brick_normal.tex data/sky.tex data/bricks.mat data/plastic.mat rm -f shadercompiler rmdir data rm -f c2 @@ -3,8 +3,10 @@ #include "debugdraw.hpp" #include "editor.hpp" #include "model.hpp" +#include "scene.hpp" #include "ui.hpp" #include "video.hpp" +#include "world.hpp" extern "C" { #include "plat.h" #include "memory.h" @@ -571,7 +573,7 @@ struct C2 : public App { Texture* texture; Texture* texture2; Texture_Id default_texture; - Model_Instance* monkey, * monkey2; + Entity_Id monkey, monkey2; Model_Scene scene; Orbit_Cam camera; Fullscreen_Quad quad; @@ -580,6 +582,7 @@ struct C2 : public App { Buffer_Id vbo, cbuf; Sampler_Id clamped_linear; Line_Renderer lr; + World* world; UI* ui; UI::Label* fps_label; void* per_frame; @@ -643,17 +646,27 @@ struct C2 : public App { ui->layout(w, h); fps_label = ui->create_element<UI::Label>(ui->root, ""); scene.init(&scene_arena, 32, clamped_linear); - monkey = scene.instantiate( - dev, - (Model*)assets.load("monkey.mdl") - ); - monkey2 = scene.instantiate( - dev, - (Model*)assets.load("monkey.mdl") - ); sky.init(dev, &assets); eprobe.init(dev, &assets, 256); camera.init(); + world = (World*)arena_alloc(arena, sizeof *world); + world->init(arena); + { + monkey = world->create_entity(); + auto [_, mod] = world->add<Transform, C_Model>(monkey); + mod.i = scene.instantiate( + dev, + (Model*)assets.load("monkey.mdl") + ); + } + { + monkey2 = world->create_entity(); + auto [_, mod] = world->add<Transform, C_Model>(monkey2); + mod.i = scene.instantiate( + dev, + (Model*)assets.load("monkey.mdl") + ); + } } void on_update() override { @@ -738,7 +751,7 @@ struct C2 : public App { ctx.debug_pop(); Texture& bb = dev->get_texture(dev->get_backbuffer()); - monkey->transform = m4f::translate( + world->get<Transform>(monkey).mat = m4f::translate( m4f::identity(), v3f(0.0f, 0.0f, 0.0f) ) * m4f::rotate( @@ -746,7 +759,7 @@ struct C2 : public App { rot, raxis ); - monkey2->transform = m4f::translate( + world->get<Transform>(monkey2).mat = m4f::translate( m4f::identity(), v3f(2.0f, 0.0f, 2.0f) ) * m4f::rotate( @@ -757,7 +770,7 @@ struct C2 : public App { camera.asp = (float)bb.w / (float)bb.h; camera.update_orbit(*this); camera.update(); - scene.update(camera, dev); + update_scene(scene, dev, camera, *world); ctx.debug_push("scene"); ctx.debug_push("depth prepass"); scene.render(dev, &frame_arena, depth_prepass, 0, clamped_linear); @@ -783,8 +796,15 @@ struct C2 : public App { ctx.debug_pop(); if (mjp(mbtn_left)) { - auto [a, b] = scene.pick(camera, w, h, mx, my); - editor_on_select(a, b); + auto [a, b] = scene_pick( + *world, + camera, + w, + h, + mx, + my + ); + editor_on_select(*world, a, b); } lr.colour(v3f(1.0f, 0.0f, 0.0f)); @@ -830,7 +850,6 @@ struct C2 : public App { } }; - extern "C" App* entrypoint(Arena* mem) { C2* a = (C2*)arena_alloc(mem, sizeof *a); new(a) C2(); diff --git a/configure.lua b/configure.lua index 7e1d5e1..ac95b4d 100644 --- a/configure.lua +++ b/configure.lua @@ -9,6 +9,7 @@ config = { "maths", "model", "pipeline", + "scene", "ui", "video", "world", @@ -1,7 +1,9 @@ #include "debugdraw.hpp" #include "editor.hpp" #include "model.hpp" +#include "scene.hpp" #include "ui.hpp" +#include "world.hpp" static struct { UI* ui; @@ -12,6 +14,7 @@ static struct { UI::Slider* metalness_input; UI::Slider* roughness_input; UI::Slider* ao_input; + Entity_Id selected; Model_Instance* selected_inst; int selected_mesh; } editor; @@ -126,10 +129,17 @@ void deinit_editor() { } -void editor_on_select(Model_Instance* instance, int mesh) { +void editor_on_select(World& w, Entity_Id e, int m) { if (!editor.ui->hovered) { - editor.selected_inst = instance; - editor.selected_mesh = mesh; + editor.selected = e; + if (e && w.has<C_Model>(e)) { + auto& cm = w.get<C_Model>(e); + editor.selected_inst = cm.i; + editor.selected_mesh = m; + } else { + editor.selected_inst = 0; + editor.selected_mesh = -1; + } if (editor.mat_win) { setup_mat_edit(); } @@ -1,13 +1,15 @@ #ifndef editor_hpp #define editor_hpp +#include "world.hpp" + struct UI; struct Model_Instance; struct Line_Renderer; void init_editor(UI* ui); void deinit_editor(); -void editor_on_select(Model_Instance* instance, int mesh); +void editor_on_select(World& w, Entity_Id e, int m); void editor_draw(Line_Renderer& lr); #endif @@ -464,54 +464,3 @@ void Model_Scene::destroy(Device* dev) { for (i = 0; i < count; i++, instance++) instance->destroy(dev, h); } - -std::pair<Model_Instance*, int> Model_Scene::pick( - const Camera& cam, - int w, - int h, - int mx, - int my -) { - int i, c = count; - v2f uv = (2.0f * v2f(mx, my) - v2f(w, h)) / (float)h; - v4f e = - cam.get_proj().inverse() * - v4f(uv.x, uv.y, -1.0f, 1.0f); - e.z = -1.0f; e.w = 0.0f; - v4f d4 = cam.get_view().inverse() * e; - v3f d = v3f::normalised(v3f(d4.x, d4.y, d4.z)); - const v3f& o = cam.position; - float t = INFINITY; - Model_Instance* r1 = 0; - int r2 = -1; - for (i = 0; i < c; i++) { - Model_Instance& inst = instances[i]; - Model* m = inst.m; - int j; - for (j = 0; j < m->mesh_count; j++) { - AABB& b = inst.bounds[j]; - v3f id = 1.0f / d; - v3f t1 = (b.min - o) * id; - v3f t2 = (b.max - o) * id; - float tmin = std::max(std::max( - std::min(t1.x, t2.x), - std::min(t1.y, t2.y)), - std::min(t1.z, t2.z) - ); - float tmax = std::min(std::min( - std::max(t1.x, t2.x), - std::max(t1.y, t2.y)), - std::max(t1.z, t2.z) - ); - if (tmax < 0.0f || tmin > tmax) - continue; - if (tmin < t) { - t = tmin; - r1 = &inst; - r2 = j; - } - } - } - return { r1, r2 }; -} - @@ -123,13 +123,6 @@ struct Model_Scene { Texture_Id env_cubemap, Sampler_Id sampler ); - std::pair<Model_Instance*, int> pick( - const Camera& cam, - int w, - int h, - int mx, - int my - ); }; #endif diff --git a/scene.cpp b/scene.cpp new file mode 100644 index 0000000..89f6f75 --- /dev/null +++ b/scene.cpp @@ -0,0 +1,67 @@ +#include "camera.hpp" +#include "model.hpp" +#include "scene.hpp" + +void update_scene( + Model_Scene& ms, + Device* dev, + const Camera& cam, + World& w +) { + for (auto v : w.view<Transform, C_Model>()) { + auto& t = v.get<Transform>(); + auto& m = v.get<C_Model>(); + m.i->transform = t.mat; + } + ms.update(cam, dev); +} + +std::pair<Entity_Id, int> scene_pick( + World& world, + const Camera& cam, + int w, + int h, + int mx, + int my +) { + v2f uv = (2.0f * v2f(mx, my) - v2f(w, h)) / (float)h; + v4f e = + cam.get_proj().inverse() * + v4f(uv.x, uv.y, -1.0f, 1.0f); + e.z = -1.0f; e.w = 0.0f; + v4f d4 = cam.get_view().inverse() * e; + v3f d = v3f::normalised(v3f(d4.x, d4.y, d4.z)); + const v3f& o = cam.position; + float t = INFINITY; + Entity_Id r1 = 0; + int r2 = -1; + for (auto v : world.view<C_Model>()) { + Model_Instance& inst = *v.get<C_Model>().i; + Model* m = inst.m; + int j; + for (j = 0; j < m->mesh_count; j++) { + AABB& b = inst.bounds[j]; + v3f id = 1.0f / d; + v3f t1 = (b.min - o) * id; + v3f t2 = (b.max - o) * id; + float tmin = std::max(std::max( + std::min(t1.x, t2.x), + std::min(t1.y, t2.y)), + std::min(t1.z, t2.z) + ); + float tmax = std::min(std::min( + std::max(t1.x, t2.x), + std::max(t1.y, t2.y)), + std::max(t1.z, t2.z) + ); + if (tmax < 0.0f || tmin > tmax) + continue; + if (tmin < t) { + t = tmin; + r1 = v.entity(); + r2 = j; + } + } + } + return { r1, r2 }; +} diff --git a/scene.hpp b/scene.hpp new file mode 100644 index 0000000..31f1cab --- /dev/null +++ b/scene.hpp @@ -0,0 +1,39 @@ +#ifndef scene_hpp +#define scene_hpp + +#include "maths.hpp" +#include "video.hpp" +#include "world.hpp" + +#include <tuple> + +struct Arena; +struct Camera; +struct Model_Instance; +struct Model_Scene; +struct World; + +struct Transform { + m4f mat; +}; + +struct C_Model { + Model_Instance* i; +}; + +void update_scene( + Model_Scene& ms, + Device* dev, + const Camera& cam, + World& w +); +std::pair<Entity_Id, int> scene_pick( + World& world, + const Camera& cam, + int w, + int h, + int mx, + int my +); + +#endif |