summaryrefslogtreecommitdiff
path: root/c2.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-02-13 23:32:28 +1100
committerquou <quou@disroot.org>2025-02-13 23:33:54 +1100
commit2e4ecca19aadc09d5c3d927724f8004b6a0ff0b0 (patch)
tree156c747452788f5221158f6e2a9151eaeb61b672 /c2.cpp
parent42cd6a572a35c354dc7d7fd390e86e7cff191617 (diff)
refactoring; prep for shadows
Diffstat (limited to 'c2.cpp')
-rw-r--r--c2.cpp104
1 files changed, 57 insertions, 47 deletions
diff --git a/c2.cpp b/c2.cpp
index 06c31c5..2832131 100644
--- a/c2.cpp
+++ b/c2.cpp
@@ -5,6 +5,7 @@
#include "lighting.hpp"
#include "model.hpp"
#include "physics.hpp"
+#include "renderer.hpp"
#include "scene.hpp"
#include "ui.hpp"
#include "video.hpp"
@@ -20,7 +21,7 @@ extern "C" {
#define video_arena_size (1024 * 1024 * 16)
#define asset_arena_size (1024 * 1024 * 4)
#define ui_arena_size (1024 * 1024)
-#define scene_arena_size (1024)
+#define scene_arena_size (1024 * 4)
#define per_frame_memory_size (1024 * 1024)
static float verts[] = {
@@ -94,7 +95,8 @@ static Sampler_Id create_clamped_linear(Device* dev) {
return dev->create_sampler("repeated linear", s);
}
-struct Orbit_Cam : public Camera {
+struct Orbit_Cam {
+ Camera* camera;
bool first_frame;
v3f target;
int px, py;
@@ -103,15 +105,16 @@ struct Orbit_Cam : public Camera {
static constexpr float sense = 5.0f;
static constexpr float min_dist = 0.3f;
- void init() {
+ void init(Camera* cam) {
pscroll = 0;
first_frame = true;
target = v3f(0.0f);
- position = v3f(0.0f, 0.0f, -5.0f);
- Camera::init(
+ camera = cam;
+ camera->position = v3f(0.0f, 0.0f, -5.0f);
+ camera->init(
90.0f,
- v3f::normalised(target - position),
- position
+ v3f::normalised(target - camera->position),
+ camera->position
);
}
@@ -122,36 +125,36 @@ struct Orbit_Cam : public Camera {
if (app.mjp(mbtn_middle) || app.mjp(mbtn_right))
dx = dy = 0.0f;
if (app.mp(mbtn_middle)) {
- v3f left = v3f::cross(forward, v3f(0.0f, 1.0f, 0.0f));
- v3f up = v3f::cross(forward, left);
- float dist = v3f::mag(position - target);
- position += left * v3f(dx) * dist;
- position += up * v3f(dy) * dist;
- target = position + forward * dist;
- forward = v3f::normalised(target - position);
+ v3f left = v3f::cross(camera->forward, v3f(0.0f, 1.0f, 0.0f));
+ v3f up = v3f::cross(camera->forward, left);
+ float dist = v3f::mag(camera->position - target);
+ camera->position += left * v3f(dx) * dist;
+ camera->position += up * v3f(dy) * dist;
+ target = camera->position + camera->forward * dist;
+ camera->forward = v3f::normalised(target - camera->position);
}
if (app.mp(mbtn_right)) {
m4f rotation = m4f::rotate(
m4f::identity(),
- forward.z < 0.0f? (float)dy: (float)-dy,
+ camera->forward.z < 0.0f? (float)dy: (float)-dy,
v3f(1.0f, 0.0f, 0.0f)
) * m4f::rotate(
m4f::identity(),
(float)dx,
v3f(0.0f, 1.0f, 0.0f)
);
- v4f pos = v4f(position - target, 0.0f);
+ v4f pos = v4f(camera->position - target, 0.0f);
yrot += dy;
pos = rotation * pos;
- position = target + v3f(pos.x, pos.y, pos.z);
- forward = v3f::normalised(target - position);
+ camera->position = target + v3f(pos.x, pos.y, pos.z);
+ camera->forward = v3f::normalised(target - camera->position);
}
if (dscroll) {
- v3f zp = position - forward * (float)dscroll;
+ v3f zp = camera->position - camera->forward * (float)dscroll;
float dist = v3f::mag(zp - target);
- if (dist < min_dist || v3f::dot(forward, zp - target) > 0.0f)
- zp = target - forward * min_dist;
- position = zp;
+ if (dist < min_dist || v3f::dot(camera->forward, zp - target) > 0.0f)
+ zp = target - camera->forward * min_dist;
+ camera->position = zp;
}
px = app.mx;
py = app.my;
@@ -640,8 +643,10 @@ struct C2 : public App {
Texture_Id default_texture;
Entity_Id monkey, monkey2, box, floor;
Model_Scene scene;
+ Renderer renderer;
Lighting lighting;
- Orbit_Cam camera;
+ Camera_Id camera;
+ Orbit_Cam orbit_cam;
Fullscreen_Quad quad;
Sky sky;
Env_Probe eprobe;
@@ -710,6 +715,7 @@ struct C2 : public App {
per_frame_memory_size
);
clamped_linear = create_clamped_linear(dev);
+ renderer.init(arena, dev);
world = (World*)arena_alloc(arena, sizeof *world);
world->init(arena);
ui = UI::create(this, &ui_arena);
@@ -724,7 +730,8 @@ struct C2 : public App {
sky.init(dev, &assets);
eprobe.init(dev, &assets, 256);
tonemap.init(dev, &assets);
- camera.init();
+ camera = renderer.create_camera();
+ orbit_cam.init(&renderer.get_camera(camera));
{
monkey = world->create_entity();
auto [_, mod] = world->add<Transform, C_Model>(monkey);
@@ -732,6 +739,7 @@ struct C2 : public App {
dev,
(Model*)assets.load("monkey.mdl")
);
+ renderer.default_model(mod.i);
}
{
monkey2 = world->create_entity();
@@ -740,6 +748,7 @@ struct C2 : public App {
dev,
(Model*)assets.load("monkey.mdl")
);
+ renderer.default_model(mod.i);
}
{
auto sun = world->create_entity();
@@ -747,6 +756,7 @@ struct C2 : public App {
light.colour = v3f(1.0f, 0.95f, 0.80f);
light.dir = v3f(0.57f, 0.57f, 0.57f);
light.brightness = 1.0f;
+ light.caster = true;
}
{
box_col = make_box(&asset_arena, v3f(1.0f, 1.0f, 1.0f));
@@ -760,6 +770,7 @@ struct C2 : public App {
dev,
(Model*)assets.load("cube.mdl")
);
+ renderer.default_model(mod.i);
floor = world->create_entity();
auto [transf, rbf, modf] = world->add<Transform, Rigidbody, C_Model>(floor);
@@ -769,6 +780,7 @@ struct C2 : public App {
dev,
(Model*)assets.load("scene.mdl")
);
+ renderer.default_model(modf.i);
}
world->get<Transform>(monkey).mat = m4f::translate(
m4f::identity(),
@@ -792,10 +804,11 @@ struct C2 : public App {
void on_update() override {
float phys_dt = dt;
Editor_Settings& es = editor_settings();
+ Camera& pcam = renderer.get_camera(camera);
Arena frame_arena;
init_arena(&frame_arena, per_frame, per_frame_memory_size);
- editor_update(*this, camera);
+ editor_update(*this, pcam);
if (es.pause_physics)
phys_dt = 0.0f;
@@ -866,15 +879,6 @@ struct C2 : public App {
dev->get_ctx().submit(pb.build_rp());
pb.begin_rp();
- pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore);
- Render_Pass& depth_prepass = pb.build_rp();
-
- pb.begin_rp();
- pb.rp_target(hdr_target, Clear_Mode::restore);
- pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore);
- Render_Pass& pass2 = pb.build_rp();
-
- pb.begin_rp();
pb.rp_target(hdr_target, Clear_Mode::restore);
pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore);
Render_Pass& sky_pass = pb.build_rp();
@@ -887,19 +891,24 @@ struct C2 : public App {
eprobe.render(dev, &frame_arena, quad, sky);
ctx.debug_pop();
+ renderer.env_cubemap = eprobe.get_cubemap();
+ renderer.clamped_linear = clamped_linear;
+ renderer.drawlists[FORWARD].camera = camera;
+ renderer.drawlists[SHADOW_MAP_START].camera = camera;
+
Texture& bb = dev->get_texture(hdr_target);
- camera.asp = (float)bb.w / (float)bb.h;
- camera.update_orbit(*this);
- camera.update();
+ pcam.asp = (float)bb.w / (float)bb.h;
+ orbit_cam.update_orbit(*this);
+ pcam.update();
lighting.update(dev, ctx, *world);
- update_scene(scene, dev, lighting, camera, *world);
+ update_scene(scene, dev, lighting, pcam, *world);
ctx.debug_push("scene");
- ctx.debug_push("depth prepass");
- scene.render(dev, &frame_arena, depth_prepass, &lighting, 0, clamped_linear);
- ctx.debug_pop();
- ctx.debug_push("forward");
- scene.render(dev, &frame_arena, pass2, &lighting, eprobe.get_cubemap(), clamped_linear);
- ctx.debug_pop();
+ renderer.render(
+ dev,
+ &frame_arena,
+ hdr_target,
+ &lighting
+ );
ctx.debug_pop();
ctx.debug_push("sky");
@@ -909,7 +918,7 @@ struct C2 : public App {
quad,
sky_pass,
clamped_linear,
- camera,
+ pcam,
hdr_target
);
ctx.debug_pop();
@@ -966,7 +975,7 @@ struct C2 : public App {
if (mjp(mbtn_left)) {
auto [a, b] = scene_pick(
*world,
- camera,
+ pcam,
w,
h,
mx,
@@ -986,7 +995,7 @@ struct C2 : public App {
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());
+ lr.flush(pcam, dev, &frame_arena, pb.build_rp());
ctx.debug_pop();
r += 10;
@@ -1003,6 +1012,7 @@ struct C2 : public App {
lr.destroy(dev);
eprobe.destroy(dev);
tonemap.destroy(dev);
+ renderer.destroy(dev);
ui->destroy();
deinit_editor();
assets.destroy();