From 0257dfd84f12010ea324f9a3b19fa2fe4c3c114e Mon Sep 17 00:00:00 2001 From: dandovlatov Date: Mon, 23 Nov 2026 23:02:31 +0400 Subject: [PATCH] FBC: NIGHT SHIFT - polished batching and added raw-path bypass, frustum culling and shard LOD --- src/main.cpp | 30 +++--- src/render/presenter.cpp | 218 +++++++++++++++++++++++++++++---------- src/render/presenter.hpp | 1 - 3 files changed, 179 insertions(+), 70 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ad26f38..ed653dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -303,11 +303,14 @@ int main(int argc, char** argv) { } // Only the scene renders into the post-FX target: bloom, corruption and - // the damage flash must never tint the HUD. - BeginTextureMode(post_target); - ClearBackground(Color{24, 24, 27, 255}); - presenter.draw_scene(game); - EndTextureMode(); + // the damage flash must never tint the HUD. The default raw palette + // draws straight to the backbuffer — no full-res render target or blit. + if (use_post) { + BeginTextureMode(post_target); + ClearBackground(Color{24, 24, 27, 255}); + presenter.draw_scene(game); + EndTextureMode(); + } if (use_bloom) { const Rectangle full = { @@ -349,8 +352,8 @@ int main(int argc, char** argv) { } BeginDrawing(); - if (use_post) BeginShaderMode(post_shader); if (use_post) { + BeginShaderMode(post_shader); if (bloom_ok && loc_bloom >= 0) { SetShaderValueTexture(post_shader, loc_bloom, bloom_a.texture); @@ -362,13 +365,16 @@ int main(int argc, char** argv) { SetShaderValue(post_shader, loc_corruption, &corruption, SHADER_UNIFORM_FLOAT); SetShaderValue(post_shader, loc_damage, &damage, SHADER_UNIFORM_FLOAT); + DrawTextureRec(post_target.texture, + Rectangle{0.0f, 0.0f, + static_cast(post_target.texture.width), + -static_cast(post_target.texture.height)}, + Vector2{0.0f, 0.0f}, WHITE); + EndShaderMode(); + } else { + ClearBackground(Color{24, 24, 27, 255}); + presenter.draw_scene(game); } - DrawTextureRec(post_target.texture, - Rectangle{0.0f, 0.0f, - static_cast(post_target.texture.width), - -static_cast(post_target.texture.height)}, - Vector2{0.0f, 0.0f}, WHITE); - if (use_post) EndShaderMode(); const Vector2 mouse = GetMousePosition(); clay_ui::begin_frame(mouse.x, mouse.y, IsMouseButtonDown(MOUSE_LEFT_BUTTON), GetFrameTime()); diff --git a/src/render/presenter.cpp b/src/render/presenter.cpp index 4feff70..cb6eb0e 100644 --- a/src/render/presenter.cpp +++ b/src/render/presenter.cpp @@ -1,6 +1,7 @@ #include "render/presenter.hpp" #include "render/layout.hpp" +#include "raymath.h" #include "rlgl.h" #include @@ -42,6 +43,38 @@ float actor_flash(float timer, float scale) { return timer <= 0.0f ? 0.0f : std::min(1.0f, timer * scale); } +class ViewCull { +public: + void init(const Camera3D& camera, float aspect) { + cam_ = camera.position; + forward_ = Vector3Normalize( + Vector3Subtract(camera.target, camera.position)); + right_ = Vector3CrossProduct(forward_, Vector3{0.0f, 1.0f, 0.0f}); + if (Vector3LengthSqr(right_) < 1e-6f) right_ = Vector3{1.0f, 0.0f, 0.0f}; + right_ = Vector3Normalize(right_); + up_ = Vector3Normalize(Vector3CrossProduct(right_, forward_)); + const float half = std::tan(camera.fovy * DEG2RAD * 0.5f); + tan_y_ = half; + tan_x_ = half * aspect; + } + + bool visible(float x, float y, float z, float radius) const { + const Vector3 d{x - cam_.x, y - cam_.y, z - cam_.z}; + const float depth = Vector3DotProduct(d, forward_); + if (depth < -radius || depth > 180.0f + radius) return false; + return std::abs(Vector3DotProduct(d, right_)) <= depth * tan_x_ + radius + && std::abs(Vector3DotProduct(d, up_)) <= depth * tan_y_ + radius; + } + +private: + Vector3 cam_{}; + Vector3 forward_{}; + Vector3 right_{}; + Vector3 up_{}; + float tan_x_ = 1.0f; + float tan_y_ = 1.0f; +}; + float enemy_hiss(const Enemy& e) { const EnemyMods& m = e.mods; float h = 0.0f; @@ -67,33 +100,54 @@ Color world_object_color(const WorldObject& obj) { struct StaticMeshBuilder { std::vector pos; + std::vector nrm; std::vector col; std::vector idx; - void add_vertex(float x, float y, float z, Color c) { + + void add_vertex(float x, float y, float z, Color c, + float nx, float ny, float nz) { pos.push_back(x); pos.push_back(y); pos.push_back(z); + nrm.push_back(nx); nrm.push_back(ny); nrm.push_back(nz); col.push_back(c.r); col.push_back(c.g); col.push_back(c.b); col.push_back(c.a); } + + void add_triangle(float ax, float ay, float az, float bx, float by, float bz, + float cx, float cy, float cz, Color c) { + const float ux = bx - ax, uy = by - ay, uz = bz - az; + const float vx = cx - ax, vy = cy - ay, vz = cz - az; + float nx = uy * vz - uz * vy; + float ny = uz * vx - ux * vz; + float nz = ux * vy - uy * vx; + const float len = std::sqrt(nx * nx + ny * ny + nz * nz); + if (len > 1e-6f) { nx /= len; ny /= len; nz /= len; } + else { nx = 0.0f; ny = 1.0f; nz = 0.0f; } + const unsigned int base = static_cast(pos.size() / 3); + add_vertex(ax, ay, az, c, nx, ny, nz); + add_vertex(bx, by, bz, c, nx, ny, nz); + add_vertex(cx, cy, cz, c, nx, ny, nz); + idx.push_back(base); idx.push_back(base + 1); idx.push_back(base + 2); + } + + void add_face(float ax, float ay, float az, float bx, float by, float bz, + float cx, float cy, float cz, float dx, float dy, float dz, + Color c) { + add_triangle(ax, ay, az, bx, by, bz, cx, cy, cz, c); + add_triangle(ax, ay, az, cx, cy, cz, dx, dy, dz, c); + } + void add_box(Vector3 center, Vector3 size, Color c) { const float hx = size.x * 0.5f, hy = size.y * 0.5f, hz = size.z * 0.5f; const float x0 = center.x - hx, x1 = center.x + hx; const float y0 = center.y - hy, y1 = center.y + hy; const float z0 = center.z - hz, z1 = center.z + hz; - // 6 faces, 4 verts each - auto face = [&](float ax, float ay, float az, float bx, float by, float bz, - float cx, float cy, float cz, float dx, float dy, float dz) { - const unsigned int base = static_cast(pos.size() / 3); - add_vertex(ax, ay, az, c); add_vertex(bx, by, bz, c); - add_vertex(cx, cy, cz, c); add_vertex(dx, dy, dz, c); - idx.push_back(base); idx.push_back(base + 1); idx.push_back(base + 2); - idx.push_back(base); idx.push_back(base + 2); idx.push_back(base + 3); - }; - face(x0,y0,z0, x1,y0,z0, x1,y1,z0, x0,y1,z0); // -Z? - face(x1,y0,z1, x0,y0,z1, x0,y1,z1, x1,y1,z1); // +Z - face(x0,y0,z1, x0,y0,z0, x0,y1,z0, x0,y1,z1); // -X - face(x1,y0,z0, x1,y0,z1, x1,y1,z1, x1,y1,z0); // +X - face(x0,y1,z0, x1,y1,z0, x1,y1,z1, x0,y1,z1); // +Y - face(x0,y0,z1, x1,y0,z1, x1,y0,z0, x0,y0,z0); // -Y + add_face(x1,y0,z0, x0,y0,z0, x0,y1,z0, x1,y1,z0, c); + add_face(x0,y0,z1, x1,y0,z1, x1,y1,z1, x0,y1,z1, c); + add_face(x0,y0,z0, x0,y0,z1, x0,y1,z1, x0,y1,z0, c); + add_face(x1,y0,z1, x1,y0,z0, x1,y1,z0, x1,y1,z1, c); + add_face(x0,y1,z1, x1,y1,z1, x1,y1,z0, x0,y1,z0, c); + add_face(x0,y0,z0, x1,y0,z0, x1,y0,z1, x0,y0,z1, c); } + void add_cylinder(Vector3 center, float r1, float r2, float h, int segs, Color c) { const float y0 = center.y, y1 = center.y + h; const float cx = center.x, cz = center.z; @@ -102,45 +156,44 @@ struct StaticMeshBuilder { const float a0 = i * step, a1 = (i + 1) * step; const float c0 = std::cos(a0), s0 = std::sin(a0); const float c1 = std::cos(a1), s1 = std::sin(a1); - // side quad - const unsigned int base = static_cast(pos.size() / 3); - add_vertex(cx + r1*c0, y0, cz + r1*s0, c); - add_vertex(cx + r2*c0, y1, cz + r2*s0, c); - add_vertex(cx + r2*c1, y1, cz + r2*s1, c); - add_vertex(cx + r1*c1, y0, cz + r1*s1, c); - idx.push_back(base); idx.push_back(base+1); idx.push_back(base+2); - idx.push_back(base); idx.push_back(base+2); idx.push_back(base+3); + add_face(cx + r1*c0, y0, cz + r1*s0, + cx + r2*c0, y1, cz + r2*s0, + cx + r2*c1, y1, cz + r2*s1, + cx + r1*c1, y0, cz + r1*s1, c); } - // bottom cap (fan) - optional; skip for thin decals if (h > 0.05f) { for (int i = 0; i < segs; ++i) { const float a0 = i * step, a1 = (i + 1) * step; - const unsigned int base = static_cast(pos.size() / 3); - add_vertex(cx, y0, cz, c); - add_vertex(cx + r1*std::cos(a0), y0, cz + r1*std::sin(a0), c); - add_vertex(cx + r1*std::cos(a1), y0, cz + r1*std::sin(a1), c); - idx.push_back(base); idx.push_back(base+1); idx.push_back(base+2); - } - for (int i = 0; i < segs; ++i) { - const float a0 = i * step, a1 = (i + 1) * step; - const unsigned int base = static_cast(pos.size() / 3); - add_vertex(cx, y1, cz, c); - add_vertex(cx + r2*std::cos(a1), y1, cz + r2*std::sin(a1), c); - add_vertex(cx + r2*std::cos(a0), y1, cz + r2*std::sin(a0), c); - idx.push_back(base); idx.push_back(base+1); idx.push_back(base+2); + add_triangle(cx, y0, cz, + cx + r1*std::cos(a0), y0, cz + r1*std::sin(a0), + cx + r1*std::cos(a1), y0, cz + r1*std::sin(a1), c); + add_triangle(cx, y1, cz, + cx + r2*std::cos(a1), y1, cz + r2*std::sin(a1), + cx + r2*std::cos(a0), y1, cz + r2*std::sin(a0), c); } } } + Mesh to_mesh() { Mesh m{}; - m.vertexCount = static_cast(pos.size() / 3); + const std::size_t verts = pos.size() / 3; + if (verts == 0 || verts > 65535u || idx.size() > 65535u) { + TraceLog(LOG_WARNING, "STATIC ENV: batch overflow (%zu verts), skipping", + verts); + return m; + } + m.vertexCount = static_cast(verts); m.triangleCount = static_cast(idx.size() / 3); - m.vertices = (float*)MemAlloc(pos.size() * sizeof(float)); + m.vertices = static_cast(MemAlloc(pos.size() * sizeof(float))); std::memcpy(m.vertices, pos.data(), pos.size() * sizeof(float)); - m.colors = (unsigned char*)MemAlloc(col.size()); + m.normals = static_cast(MemAlloc(nrm.size() * sizeof(float))); + std::memcpy(m.normals, nrm.data(), nrm.size() * sizeof(float)); + m.colors = static_cast(MemAlloc(col.size())); std::memcpy(m.colors, col.data(), col.size()); - m.indices = (unsigned short*)MemAlloc(idx.size() * sizeof(unsigned short)); - for (size_t i = 0; i < idx.size(); ++i) m.indices[i] = static_cast(idx[i]); + m.indices = static_cast(MemAlloc(idx.size() * sizeof(unsigned short))); + for (std::size_t i = 0; i < idx.size(); ++i) { + m.indices[i] = static_cast(idx[i]); + } return m; } }; @@ -221,7 +274,7 @@ void Presenter::init(float arena_half) { {-19.0f, -8.0f, 1.5f, 0.6f}, {3.0f, -20.0f, 2.1f, 0.7f}, }; for (const auto& st : stains) { - b.add_cylinder({st[0], 0.016f, st[1]}, st[2], st[2], 0.012f, 8, + b.add_cylinder({st[0], 0.016f, st[1]}, st[2], st[2], 0.012f, 10, Color{28, 12, 14, static_cast(55 * st[3])}); } // Scraps. @@ -244,7 +297,7 @@ void Presenter::init(float arena_half) { {83.0f, 74.0f, 2.7f, 0.7f}, {4.0f, -38.0f, 2.0f, 0.8f}, }; for (const auto& st : far_stains) { - b.add_cylinder({st[0], 0.016f, st[1]}, st[2], st[2] * 1.3f, 0.012f, 8, + b.add_cylinder({st[0], 0.016f, st[1]}, st[2], st[2] * 1.3f, 0.012f, 11, Color{26, 12, 14, static_cast(48 * st[3])}); } // Far scraps. @@ -268,10 +321,47 @@ void Presenter::init(float arena_half) { b.add_box({x, 0.03f, 0.0f}, {0.09f, 0.03f, 34.0f}, Color{20, 20, 24, 255}); } - static_env_mesh_ = b.to_mesh(); - UploadMesh(&static_env_mesh_, false); - static_env_model_ = LoadModelFromMesh(static_env_mesh_); - static_env_ready_ = true; + // Room partitions and entrance signage (previously immediate-mode). + const layout::Room& room = layout::kOpenRoom; + const float rx0 = room.cx - room.hx, rx1 = room.cx + room.hx; + const float rz0 = room.cz - room.hz, rz1 = room.cz + room.hz; + const Color partition{58, 60, 64, 255}; + b.add_box({room.cx, 0.85f, rz1}, {room.hx * 2.0f, 1.7f, 0.45f}, partition); + b.add_box({rx0, 0.75f, room.cz}, {0.45f, 1.5f, room.hz * 2.0f}, partition); + b.add_box({rx1, 0.75f, room.cz}, {0.45f, 1.5f, room.hz * 2.0f}, partition); + const float south_span = room.hx - 5.0f; + b.add_box({room.cx - (room.hx + 5.0f) * 0.5f, 0.65f, rz0}, + {south_span, 1.3f, 0.45f}, partition); + b.add_box({room.cx + (room.hx + 5.0f) * 0.5f, 0.65f, rz0}, + {south_span, 1.3f, 0.45f}, partition); + for (float x = rx0 + 4.0f; x < rx1; x += 8.0f) { + b.add_box({x, 1.46f, rz1}, {1.1f, 0.12f, 0.5f}, + Color{198, 192, 176, 255}); + } + b.add_box({room.cx, 1.02f, rz0 + 0.26f}, {4.6f, 0.32f, 0.08f}, + Color{245, 197, 24, 255}); + + // Threshold door hazard stripes and green exit lamps. + for (int i = 0; i < 10; ++i) { + const float x0 = -2.31f + i * 0.462f; + b.add_box({x0, 0.045f, arena - 0.6f}, {0.23f, 0.055f, 1.1f}, + (i % 2 == 0) ? Color{198, 153, 13, 255} + : Color{20, 20, 22, 255}); + } + const float door_z = arena + 0.45f; + b.add_box({-10.0f, 2.4f, door_z - 0.2f}, {1.7f, 0.52f, 0.12f}, + Color{46, 170, 76, 255}); + b.add_box({10.0f, 2.4f, door_z - 0.2f}, {1.7f, 0.52f, 0.12f}, + Color{46, 170, 76, 255}); + + Mesh batch = b.to_mesh(); + if (batch.vertexCount > 0) { + UploadMesh(&batch, false); + static_env_model_ = LoadModelFromMesh(batch); + static_env_ready_ = true; + TraceLog(LOG_INFO, "STATIC ENV: %d verts / %d tris in one draw call", + batch.vertexCount, batch.triangleCount); + } } aura_.init(); dressing_.init(); @@ -298,7 +388,6 @@ void Presenter::shutdown() { models_.shutdown(); if (static_env_ready_) { UnloadModel(static_env_model_); - UnloadMesh(static_env_mesh_); static_env_ready_ = false; } if (spr_glow_.id != 0) UnloadTexture(spr_glow_); @@ -425,9 +514,8 @@ void Presenter::draw_environment(const Game& game) { rlEnableBackfaceCulling(); } - // Low partitions mark the new room without blocking the overhead camera: - // a north wall, two side walls and a south wall split by a wide entrance. - + // Low partitions, entrance signage and door stripes are baked into the + // static mesh above; only the animated dressing stays per-frame here. // Fluorescent strips live at curb height — the tactical camera sits above // the room, so a real ceiling would only occlude the play field. The z=-8 @@ -459,7 +547,7 @@ void Presenter::draw_environment(const Game& game) { 0.0f, 1.0f, lights_revived ? Color{240, 230, 180, 255} : WHITE); - // Threshold door + hazard stripes (the run's exit). + // Threshold door frame (stripes and exit lamps are in the static batch). const float door_z = arena + 0.45f; if (concrete_fallback) { DrawCube({0.0f, 2.9f, door_z}, 5.6f, 5.8f, 0.34f, Color{28, 28, 32, 255}); @@ -724,6 +812,9 @@ void Presenter::draw_scene(Game& game) { camera_.target = {cx + sx * 0.3f, 0.0f, cz}; } camera_.fovy = tuning_.fov; + ViewCull cull; + cull.init(camera_, static_cast(GetScreenWidth()) + / static_cast(std::max(1, GetScreenHeight()))); if (tier_wave_pending_) { // Reusable scene wave: also callable for boss entrances / events. aura_.spawn_ground({p.x, 0.09f, p.y}, 11.0f, 2.6f, anim_time_); @@ -742,6 +833,7 @@ void Presenter::draw_scene(Game& game) { // Blob shadows sit under every actor. draw_blob_shadow(p, 0.8f); for (const Enemy& e : game.enemy_system.enemies) { + if (!cull.visible(e.pos.x, 0.1f, e.pos.y, e.radius)) continue; draw_blob_shadow(e.pos, e.radius * 0.85f); } @@ -771,6 +863,7 @@ void Presenter::draw_scene(Game& game) { // Enemies face the player; Hiss units add a nervous jitter. for (const Enemy& e : game.enemy_system.enemies) { + if (!cull.visible(e.pos.x, 1.0f, e.pos.y, e.radius + 0.6f)) continue; const Vec2 dir = p - e.pos; float yaw = std::atan2(dir.x, dir.y); const bool mold = e.type == EnemyType::MoldSpore; @@ -789,6 +882,10 @@ void Presenter::draw_scene(Game& game) { // Enemy drops: small cyan Resonance crystals, spun by the crystal rig. const Texture2D glow = spr_glow_.id != 0 ? spr_glow_ : soft_disc_; for (const XpShard& s : game.pickup_system.shards) { + if (!cull.visible(s.pos.x, 0.25f, s.pos.y, 0.6f)) continue; + const float dx = s.pos.x - cx, dz = s.pos.y - cz; + const float dist_sq = dx * dx + dz * dz; + if (dist_sq > 62.0f * 62.0f) continue; const float spin = anim_time_ * 2.6f + s.pos.x * 0.7f; const float bob = std::sin(spin * 1.3f) * 0.035f; DrawBillboardRec(camera_, glow, @@ -796,15 +893,18 @@ void Presenter::draw_scene(Game& game) { static_cast(glow.height)}, {s.pos.x, 0.22f + bob, s.pos.y}, Vector2{0.62f, 0.62f}, Color{70, 225, 255, 110}); - models_.draw(ActorModel::Shard, {s.pos.x, 0.18f + bob, s.pos.y}, spin, - 0.7f, WHITE, 0.0f, false, 0.0f, 0.0f, - anim_time_ + s.pos.x * 0.7f); + if (dist_sq <= 20.0f * 20.0f) { + models_.draw(ActorModel::Shard, {s.pos.x, 0.18f + bob, s.pos.y}, + spin, 0.7f, WHITE, 0.0f, false, 0.0f, 0.0f, + anim_time_ + s.pos.x * 0.7f); + } } // World objects of power: small camera-facing glows (§4.6). const Texture2D object_glow = spr_glow_.id != 0 ? spr_glow_ : soft_disc_; for (int i = 0; i < game.buff_system.world_object_count(); ++i) { const WorldObject& obj = game.buff_system.world_object(i); + if (!cull.visible(obj.pos.x, 0.75f, obj.pos.y, 0.8f)) continue; const Color c = world_object_color(obj); DrawBillboardRec(camera_, object_glow, Rectangle{0.0f, 0.0f, static_cast(object_glow.width), @@ -814,6 +914,7 @@ void Presenter::draw_scene(Game& game) { // Projectile glow remains camera-facing — it is an FX, not a card. for (const Projectile& pr : game.weapon_system.projectiles) { + if (!cull.visible(pr.pos.x, 1.1f, pr.pos.y, 0.8f)) continue; const float s = projectile_size(pr.source); DrawBillboardRec(camera_, glow, Rectangle{0.0f, 0.0f, static_cast(glow.width), @@ -824,6 +925,7 @@ void Presenter::draw_scene(Game& game) { // Light particles from hits, kills and tier-ups — camera-facing glows. for (const Spark& s : sparks_) { + if (!cull.visible(s.pos.x, s.pos.y, s.pos.z, s.size)) continue; const float fade = 1.0f - s.age / s.life; Color tint = s.color; tint.a = static_cast(255.0f * fade * fade); @@ -850,6 +952,7 @@ void Presenter::draw_scene(Game& game) { // World-anchored health bars for damaged enemies. for (const Enemy& e : game.enemy_system.enemies) { if (e.hp >= e.max_hp) continue; + if (!cull.visible(e.pos.x, 1.2f, e.pos.y, e.radius + 0.8f)) continue; const ActorModel model = e.type == EnemyType::MoldSpore ? ActorModel::MoldSpore : (e.type == EnemyType::HissBrute ? ActorModel::HissBrute : (e.type == EnemyType::HissGhost ? ActorModel::HissGhost @@ -864,6 +967,7 @@ void Presenter::draw_scene(Game& game) { // Floating damage numbers. for (const DamageNumber& n : damage_numbers_) { + if (!cull.visible(n.pos.x, 1.6f, n.pos.y, 1.0f)) continue; const float t = n.age / 0.8f; const Vector2 screen = GetWorldToScreen( {n.pos.x, 1.6f + n.age * 1.8f, n.pos.y}, camera_); diff --git a/src/render/presenter.hpp b/src/render/presenter.hpp index 6b8395a..62c8323 100644 --- a/src/render/presenter.hpp +++ b/src/render/presenter.hpp @@ -64,7 +64,6 @@ private: PaperDebris debris_; ConcreteDebris concrete_debris_; ScorchDecals scorch_; - Mesh static_env_mesh_{}; Model static_env_model_{}; bool static_env_ready_ = false; float shake_ = 0.0f;