166 lines
6.7 KiB
C++
166 lines
6.7 KiB
C++
#include "render/concrete.hpp"
|
|
|
|
#include "raymath.h"
|
|
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
namespace {
|
|
|
|
const char* resolve(const char* rel) {
|
|
if (FileExists(rel)) return rel;
|
|
static char path[512];
|
|
std::snprintf(path, sizeof(path), "%s../%s", GetApplicationDirectory(), rel);
|
|
return FileExists(path) ? path : nullptr;
|
|
}
|
|
|
|
Texture2D load_map(const char* name) {
|
|
const char* path = resolve(name);
|
|
if (path == nullptr) {
|
|
TraceLog(LOG_WARNING, "CONCRETE: %s missing", name);
|
|
return Texture2D{};
|
|
}
|
|
Texture2D tex = LoadTexture(path);
|
|
SetTextureFilter(tex, TEXTURE_FILTER_ANISOTROPIC_8X);
|
|
SetTextureWrap(tex, TEXTURE_WRAP_REPEAT);
|
|
return tex;
|
|
}
|
|
|
|
void write_box(std::vector<float>& v, std::vector<float>& n,
|
|
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);
|
|
for (int i = 0; i < 4; ++i) {
|
|
v.push_back(center.x + f.corner[i].x);
|
|
v.push_back(center.y + f.corner[i].y);
|
|
v.push_back(center.z + f.corner[i].z);
|
|
n.push_back(f.normal.x);
|
|
n.push_back(f.normal.y);
|
|
n.push_back(f.normal.z);
|
|
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_, colors_, indices_, center, size, rough_mult);
|
|
}
|
|
|
|
void ConcreteMaterial::init(float arena) {
|
|
const char* vs = resolve("assets/shaders/concrete.vs");
|
|
const char* 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 == nullptr || fs == nullptr || albedo_.id == 0 || normal_.id == 0
|
|
|| roughness_.id == 0) {
|
|
TraceLog(LOG_WARNING, "CONCRETE: assets missing, falling back to flat colours");
|
|
return;
|
|
}
|
|
shader_ = LoadShader(vs, fs);
|
|
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");
|
|
|
|
// The floor is sealed and glossier than the raw walls/pillars; vertex alpha
|
|
// carries that difference so everything still shares one material + mesh.
|
|
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);
|
|
|
|
const Vector3 pillars[] = {
|
|
{-18.0f, 2.0f, -18.0f}, {18.0f, 2.0f, -18.0f},
|
|
{-18.0f, 2.0f, 18.0f}, {18.0f, 2.0f, 18.0f},
|
|
{0.0f, 2.0f, -21.0f}, {0.0f, 2.0f, 21.0f},
|
|
{-21.0f, 2.0f, 0.0f}, {21.0f, 2.0f, 0.0f},
|
|
};
|
|
for (const Vector3& p : pillars) {
|
|
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);
|
|
}
|
|
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_.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_.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();
|
|
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::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);
|
|
}
|
|
DrawMesh(mesh_, material_, MatrixIdentity());
|
|
}
|