273 lines
11 KiB
C++
273 lines
11 KiB
C++
#include "render/concrete.hpp"
|
|
#include "game/floor.hpp"
|
|
#include "render/layout.hpp"
|
|
#include "render/profiler.hpp"
|
|
#include "render/graphics_settings.hpp"
|
|
|
|
#include "raymath.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
std::string resolve(const char* rel) {
|
|
if (FileExists(rel)) return rel;
|
|
char path[512];
|
|
std::snprintf(path, sizeof(path), "%s../%s", GetApplicationDirectory(), rel);
|
|
return FileExists(path) ? path : std::string();
|
|
}
|
|
|
|
int raylib_filter(gfx::TextureFiltering f) {
|
|
switch (f) {
|
|
case gfx::TextureFiltering::Anisotropic4X: return TEXTURE_FILTER_ANISOTROPIC_4X;
|
|
case gfx::TextureFiltering::Anisotropic8X: return TEXTURE_FILTER_ANISOTROPIC_8X;
|
|
default: return TEXTURE_FILTER_TRILINEAR;
|
|
}
|
|
}
|
|
|
|
void apply_filter(Texture2D& tex) {
|
|
if (tex.id == 0) return;
|
|
SetTextureFilter(tex, raylib_filter(gfx::graphics_settings().concrete_filter));
|
|
}
|
|
|
|
Texture2D load_map(const char* name) {
|
|
const std::string path = resolve(name);
|
|
if (path.empty()) {
|
|
TraceLog(LOG_WARNING, "CONCRETE: %s missing", name);
|
|
return Texture2D{};
|
|
}
|
|
Texture2D tex = LoadTexture(path.c_str());
|
|
if (tex.mipmaps <= 1) GenTextureMipmaps(&tex);
|
|
apply_filter(tex);
|
|
SetTextureWrap(tex, TEXTURE_WRAP_REPEAT);
|
|
return tex;
|
|
}
|
|
|
|
// The old blockout used these as opaque floor plates. They now only supply a
|
|
// hue for a low-strength tint in concrete.fs, so the PBR detail stays visible.
|
|
Color room_hue(RoomKind kind) {
|
|
switch (kind) {
|
|
case RoomKind::Gate: return {94, 143, 190, 255};
|
|
case RoomKind::Plaza: return {58, 58, 64, 255};
|
|
case RoomKind::Projector: return {198, 157, 36, 255};
|
|
case RoomKind::OfficeSuite: return {70, 84, 92, 255};
|
|
case RoomKind::Silos: return {24, 142, 172, 255};
|
|
case RoomKind::Stairwell: return {150, 30, 40, 255};
|
|
default: return {68, 126, 116, 255};
|
|
}
|
|
}
|
|
|
|
void write_box(std::vector<float>& v, std::vector<float>& n,
|
|
std::vector<float>& uv, std::vector<unsigned char>& c,
|
|
std::vector<unsigned short>& idx,
|
|
Vector3 center, Vector3 size, float rough_mult) {
|
|
const float hx = size.x * 0.5f, hy = size.y * 0.5f, hz = size.z * 0.5f;
|
|
struct Face {
|
|
Vector3 normal;
|
|
Vector3 corner[4];
|
|
};
|
|
const Face faces[6] = {
|
|
{{ 1, 0, 0}, {{hx, -hy, -hz}, {hx, hy, -hz}, {hx, hy, hz}, {hx, -hy, hz}}},
|
|
{{-1, 0, 0}, {{-hx, -hy, -hz}, {-hx, -hy, hz}, {-hx, hy, hz}, {-hx, hy, -hz}}},
|
|
{{ 0, 1, 0}, {{-hx, hy, -hz}, {-hx, hy, hz}, {hx, hy, hz}, {hx, hy, -hz}}},
|
|
{{ 0,-1, 0}, {{-hx, -hy, -hz}, {hx, -hy, -hz}, {hx, -hy, hz}, {-hx, -hy, hz}}},
|
|
{{ 0, 0, 1}, {{-hx, -hy, hz}, {hx, -hy, hz}, {hx, hy, hz}, {-hx, hy, hz}}},
|
|
{{ 0, 0,-1}, {{hx, -hy, -hz}, {-hx, -hy, -hz}, {-hx, hy, -hz}, {hx, hy, -hz}}},
|
|
};
|
|
for (const Face& f : faces) {
|
|
const unsigned short base = static_cast<unsigned short>(v.size() / 3);
|
|
const bool x_face = std::fabs(f.normal.x) > 0.5f;
|
|
const bool y_face = std::fabs(f.normal.y) > 0.5f;
|
|
for (int i = 0; i < 4; ++i) {
|
|
const float wx = center.x + f.corner[i].x;
|
|
const float wy = center.y + f.corner[i].y;
|
|
const float wz = center.z + f.corner[i].z;
|
|
v.push_back(wx);
|
|
v.push_back(wy);
|
|
v.push_back(wz);
|
|
n.push_back(f.normal.x);
|
|
n.push_back(f.normal.y);
|
|
n.push_back(f.normal.z);
|
|
// World-aligned planar UVs: one projection per axis-aligned face,
|
|
// continuous across adjacent boxes. uScale is applied in the shader.
|
|
if (x_face) { uv.push_back(wz); uv.push_back(wy); }
|
|
else if (y_face) { uv.push_back(wx); uv.push_back(wz); }
|
|
else { uv.push_back(wx); uv.push_back(wy); }
|
|
c.push_back(255);
|
|
c.push_back(255);
|
|
c.push_back(255);
|
|
c.push_back(static_cast<unsigned char>(rough_mult * 255.0f));
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void ConcreteMaterial::add_box(Vector3 center, Vector3 size, float rough_mult) {
|
|
write_box(vertices_, normals_, uvs_, colors_, indices_, center, size, rough_mult);
|
|
}
|
|
|
|
void ConcreteMaterial::load_room_tints(const Floor& floor) {
|
|
room_count_ = 0;
|
|
for (const FloorRoom& r : floor.rooms()) {
|
|
if (room_count_ >= kMaxRoomTints) break;
|
|
const int i = room_count_++;
|
|
const FloorRect& q = r.rect;
|
|
room_rect_[i * 4 + 0] = q.x0;
|
|
room_rect_[i * 4 + 1] = q.z0;
|
|
room_rect_[i * 4 + 2] = q.x1;
|
|
room_rect_[i * 4 + 3] = q.z1;
|
|
|
|
const Color c = room_hue(r.kind);
|
|
const float rgb[3] = {c.r / 255.0f, c.g / 255.0f, c.b / 255.0f};
|
|
const float peak = std::max(rgb[0], std::max(rgb[1], rgb[2]));
|
|
const float strength = r.kind == RoomKind::Side ? 0.14f : 0.22f;
|
|
for (int k = 0; k < 3; ++k) {
|
|
room_tint_[i * 3 + k] = 1.0f + (rgb[k] / peak - 1.0f) * strength;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ConcreteMaterial::init(float arena, const Floor* floor) {
|
|
const std::string vs = resolve("assets/shaders/concrete.vs");
|
|
const std::string fs = resolve("assets/shaders/concrete.fs");
|
|
albedo_ = load_map("assets/textures/concrete/concrete_albedo.png");
|
|
normal_ = load_map("assets/textures/concrete/concrete_normal.png");
|
|
roughness_ = load_map("assets/textures/concrete/concrete_roughness.png");
|
|
|
|
if (vs.empty() || fs.empty() || albedo_.id == 0 || normal_.id == 0
|
|
|| roughness_.id == 0) {
|
|
TraceLog(LOG_WARNING, "CONCRETE: assets missing, falling back to flat colours");
|
|
return;
|
|
}
|
|
shader_ = LoadShader(vs.c_str(), fs.c_str());
|
|
if (shader_.id == 0) {
|
|
TraceLog(LOG_WARNING, "CONCRETE: shader compile failed");
|
|
return;
|
|
}
|
|
shader_ok_ = true;
|
|
loc_corruption_ = GetShaderLocation(shader_, "uCorruption");
|
|
loc_camera_ = GetShaderLocation(shader_, "uCameraPos");
|
|
loc_room_rect_ = GetShaderLocation(shader_, "uRoomRect[0]");
|
|
loc_room_tint_ = GetShaderLocation(shader_, "uRoomTint[0]");
|
|
loc_room_count_ = GetShaderLocation(shader_, "uRoomCount");
|
|
if (floor != nullptr) load_room_tints(*floor);
|
|
|
|
// The floor is sealed and glossier than the raw walls/pillars; vertex alpha
|
|
// carries that difference so everything still shares one material + mesh.
|
|
if (floor != nullptr) {
|
|
// Floor 01: the whole play field is the walled 160 x 120 m floor plate.
|
|
add_box({0.0f, -0.5f, 0.0f},
|
|
{Floor::kWidth + 8.0f, 1.0f, Floor::kHeight + 8.0f}, 0.55f);
|
|
for (const FloorWall& w : floor->walls()) {
|
|
const FloorRect& r = w.rect;
|
|
add_box({(r.x0 + r.x1) * 0.5f, w.height * 0.5f, (r.z0 + r.z1) * 0.5f},
|
|
{r.x1 - r.x0, w.height, r.z1 - r.z0}, 1.0f);
|
|
}
|
|
} else {
|
|
add_box({0.0f, -0.5f, 0.0f},
|
|
{arena * 2.0f + 40.0f, 1.0f, arena * 2.0f + 40.0f}, 0.55f);
|
|
add_box({0.0f, 0.6f, -(arena + 1.0f)}, {arena * 2.0f + 4.0f, 1.2f, 1.0f}, 1.0f);
|
|
add_box({0.0f, 0.6f, arena + 1.0f}, {arena * 2.0f + 4.0f, 1.2f, 1.0f}, 1.0f);
|
|
add_box({-(arena + 1.0f), 0.6f, 0.0f}, {1.0f, 1.2f, arena * 2.0f + 4.0f}, 1.0f);
|
|
add_box({arena + 1.0f, 0.6f, 0.0f}, {1.0f, 1.2f, arena * 2.0f + 4.0f}, 1.0f);
|
|
|
|
for (const layout::Anchor& a : layout::kCentralPillars) {
|
|
const Vector3 p{a.x, a.y, a.z};
|
|
add_box(p, {2.0f, 4.0f, 2.0f}, 1.0f);
|
|
add_box({p.x, 0.22f, p.z}, {2.5f, 0.44f, 2.5f}, 1.0f);
|
|
}
|
|
for (const layout::Anchor& a : layout::kMidPillars) {
|
|
const Vector3 p{a.x, a.y, a.z};
|
|
add_box(p, {2.2f, 4.3f, 2.2f}, 1.0f);
|
|
add_box({p.x, 0.22f, p.z}, {2.7f, 0.44f, 2.7f}, 1.0f);
|
|
}
|
|
for (const layout::Anchor& a : layout::kOuterPillars) {
|
|
const Vector3 p{a.x, a.y, a.z};
|
|
add_box(p, {2.4f, 4.6f, 2.4f}, 1.0f);
|
|
add_box({p.x, 0.22f, p.z}, {3.0f, 0.44f, 3.0f}, 1.0f);
|
|
}
|
|
add_box({0.0f, 2.9f, arena + 0.45f}, {5.6f, 5.8f, 0.34f}, 1.0f);
|
|
}
|
|
|
|
mesh_.vertexCount = static_cast<int>(vertices_.size() / 3);
|
|
mesh_.triangleCount = static_cast<int>(indices_.size() / 3);
|
|
mesh_.vertices = static_cast<float*>(MemAlloc(vertices_.size() * sizeof(float)));
|
|
mesh_.normals = static_cast<float*>(MemAlloc(normals_.size() * sizeof(float)));
|
|
mesh_.texcoords = static_cast<float*>(MemAlloc(uvs_.size() * sizeof(float)));
|
|
mesh_.colors = static_cast<unsigned char*>(MemAlloc(colors_.size()));
|
|
mesh_.indices = static_cast<unsigned short*>(MemAlloc(indices_.size() * sizeof(unsigned short)));
|
|
std::memcpy(mesh_.vertices, vertices_.data(), vertices_.size() * sizeof(float));
|
|
std::memcpy(mesh_.normals, normals_.data(), normals_.size() * sizeof(float));
|
|
std::memcpy(mesh_.texcoords, uvs_.data(), uvs_.size() * sizeof(float));
|
|
std::memcpy(mesh_.colors, colors_.data(), colors_.size());
|
|
std::memcpy(mesh_.indices, indices_.data(), indices_.size() * sizeof(unsigned short));
|
|
UploadMesh(&mesh_, false);
|
|
mesh_ok_ = true;
|
|
vertices_.clear();
|
|
normals_.clear();
|
|
uvs_.clear();
|
|
colors_.clear();
|
|
indices_.clear();
|
|
|
|
material_ = LoadMaterialDefault();
|
|
material_.shader = shader_;
|
|
// raylib binds map slots 0/1/2 to texture0/texture1/texture2, so roughness
|
|
// rides the SPECULAR slot and the normal map the NORMAL slot.
|
|
SetMaterialTexture(&material_, MATERIAL_MAP_ALBEDO, albedo_);
|
|
SetMaterialTexture(&material_, MATERIAL_MAP_SPECULAR, roughness_);
|
|
SetMaterialTexture(&material_, MATERIAL_MAP_NORMAL, normal_);
|
|
|
|
ready_ = shader_ok_ && mesh_ok_;
|
|
TraceLog(LOG_INFO, "CONCRETE: %d verts / %d tris in one draw call",
|
|
mesh_.vertexCount, mesh_.triangleCount);
|
|
}
|
|
|
|
void ConcreteMaterial::apply_texture_filter() {
|
|
if (!ready_) return;
|
|
apply_filter(albedo_);
|
|
apply_filter(normal_);
|
|
apply_filter(roughness_);
|
|
}
|
|
|
|
void ConcreteMaterial::shutdown() {
|
|
if (mesh_ok_) UnloadMesh(mesh_);
|
|
if (shader_ok_) UnloadMaterial(material_); // also releases shader_
|
|
if (albedo_.id != 0) UnloadTexture(albedo_);
|
|
if (normal_.id != 0) UnloadTexture(normal_);
|
|
if (roughness_.id != 0) UnloadTexture(roughness_);
|
|
mesh_ok_ = false;
|
|
shader_ok_ = false;
|
|
ready_ = false;
|
|
}
|
|
|
|
void ConcreteMaterial::draw(float corruption, Vector3 camera_pos) {
|
|
if (!ready_) return;
|
|
SetShaderValue(shader_, loc_corruption_, &corruption, SHADER_UNIFORM_FLOAT);
|
|
if (loc_camera_ >= 0) {
|
|
const float cam[3] = {camera_pos.x, camera_pos.y, camera_pos.z};
|
|
SetShaderValue(shader_, loc_camera_, cam, SHADER_UNIFORM_VEC3);
|
|
}
|
|
if (room_count_ > 0) {
|
|
if (loc_room_rect_ >= 0)
|
|
SetShaderValueV(shader_, loc_room_rect_, room_rect_, SHADER_UNIFORM_VEC4,
|
|
room_count_);
|
|
if (loc_room_tint_ >= 0)
|
|
SetShaderValueV(shader_, loc_room_tint_, room_tint_, SHADER_UNIFORM_VEC3,
|
|
room_count_);
|
|
if (loc_room_count_ >= 0)
|
|
SetShaderValue(shader_, loc_room_count_, &room_count_, SHADER_UNIFORM_INT);
|
|
}
|
|
PROF_DRAW_CALL();
|
|
DrawMesh(mesh_, material_, MatrixIdentity());
|
|
}
|