110 lines
3.8 KiB
C++
110 lines
3.8 KiB
C++
#include "render/aura.hpp"
|
|
|
|
#include "render/profiler.hpp"
|
|
#include "raymath.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#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();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void CorruptionAura::init() {
|
|
const std::string vs = resolve("assets/shaders/aura.vs");
|
|
const std::string fs = resolve("assets/shaders/aura.fs");
|
|
if (vs.empty() || fs.empty()) {
|
|
TraceLog(LOG_WARNING, "AURA: wave shader missing, ground waves disabled");
|
|
return;
|
|
}
|
|
shader_ = LoadShader(vs.c_str(), fs.c_str());
|
|
if (shader_.id == 0) {
|
|
TraceLog(LOG_WARNING, "AURA: wave shader failed to compile");
|
|
return;
|
|
}
|
|
loc_time_ = GetShaderLocation(shader_, "uTime");
|
|
loc_phase_ = GetShaderLocation(shader_, "uPhase");
|
|
loc_corruption_ = GetShaderLocation(shader_, "uCorruption");
|
|
loc_intensity_ = GetShaderLocation(shader_, "uIntensity");
|
|
|
|
quad_.vertexCount = 4;
|
|
quad_.triangleCount = 2;
|
|
quad_.vertices = static_cast<float*>(MemAlloc(4 * 3 * sizeof(float)));
|
|
quad_.texcoords = static_cast<float*>(MemAlloc(4 * 2 * sizeof(float)));
|
|
quad_.indices = static_cast<unsigned short*>(MemAlloc(6 * sizeof(unsigned short)));
|
|
const float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f,
|
|
0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.0f};
|
|
const float uvs[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
|
|
const unsigned short indices[] = {0, 1, 2, 0, 2, 3};
|
|
for (int i = 0; i < 12; ++i) quad_.vertices[i] = vertices[i];
|
|
for (int i = 0; i < 8; ++i) quad_.texcoords[i] = uvs[i];
|
|
for (int i = 0; i < 6; ++i) quad_.indices[i] = indices[i];
|
|
UploadMesh(&quad_, false);
|
|
|
|
material_ = LoadMaterialDefault();
|
|
material_.shader = shader_;
|
|
ready_ = true;
|
|
waves_.reserve(16);
|
|
}
|
|
|
|
void CorruptionAura::shutdown() {
|
|
if (ready_) {
|
|
UnloadMesh(quad_);
|
|
UnloadMaterial(material_); // also releases shader_
|
|
}
|
|
waves_.clear();
|
|
ready_ = false;
|
|
}
|
|
|
|
void CorruptionAura::begin_frame(float time, float corruption) {
|
|
if (!ready_) return;
|
|
SetShaderValue(shader_, loc_time_, &time, SHADER_UNIFORM_FLOAT);
|
|
SetShaderValue(shader_, loc_corruption_, &corruption, SHADER_UNIFORM_FLOAT);
|
|
}
|
|
|
|
void CorruptionAura::update(float dt) {
|
|
for (GroundWave& w : waves_) w.age += dt;
|
|
waves_.erase(std::remove_if(waves_.begin(), waves_.end(),
|
|
[](const GroundWave& w) { return w.age >= w.life; }),
|
|
waves_.end());
|
|
}
|
|
|
|
void CorruptionAura::spawn_ground(Vector3 pos, float radius, float life,
|
|
float phase) {
|
|
if (!ready_) return;
|
|
if (waves_.size() >= 16) waves_.erase(waves_.begin());
|
|
waves_.push_back({pos, radius, 0.0f, life, phase});
|
|
}
|
|
|
|
Matrix CorruptionAura::ground_transform(const GroundWave& wave) const {
|
|
// Flat XZ decal: local X -> world X, local Y -> world Z, normal -> +Y.
|
|
Matrix m = MatrixIdentity();
|
|
const float r = wave.radius * (0.7f + 0.5f * (wave.age / wave.life));
|
|
m.m0 = r; m.m4 = 0.0f; m.m8 = 0.0f;
|
|
m.m1 = 0.0f; m.m5 = 0.0f; m.m9 = r;
|
|
m.m2 = 0.0f; m.m6 = 1.0f; m.m10 = 0.0f;
|
|
m.m3 = wave.pos.x;
|
|
m.m7 = wave.pos.y;
|
|
m.m11 = wave.pos.z;
|
|
return m;
|
|
}
|
|
|
|
void CorruptionAura::draw_ground_waves() {
|
|
if (!ready_ || waves_.empty()) return;
|
|
for (const GroundWave& w : waves_) {
|
|
const float t = w.age / w.life;
|
|
const float fade = (1.0f - t) * std::min(1.0f, t * 6.0f);
|
|
SetShaderValue(shader_, loc_phase_, &w.phase, SHADER_UNIFORM_FLOAT);
|
|
SetShaderValue(shader_, loc_intensity_, &fade, SHADER_UNIFORM_FLOAT);
|
|
PROF_DRAW_CALL();
|
|
DrawMesh(quad_, material_, ground_transform(w));
|
|
}
|
|
}
|