Files

260 lines
12 KiB
C++
Raw Permalink Normal View History

2026-11-30 22:31:51 +02:00
#include "assets/level.hpp"
#include "assets/model.hpp"
#include "assets/palette.hpp"
#include "assets/sprites.hpp"
#include "core/vfs.hpp"
#include "render/world_geometry.hpp"
#include "sim/vehicle_defs.hpp"
2026-11-30 22:31:51 +02:00
#include <algorithm>
2026-11-30 22:31:51 +02:00
#include <cmath>
#include <iostream>
using namespace top_octane;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << "\n"; \
return 1; \
} \
} while (0)
int main() {
Vfs vfs;
CHECK(vfs.init(HIOCTANE_DEFAULT_DATA_DIR));
assets::Palette pal;
CHECK(pal.load(vfs));
// Verify CPU geometry generation for all 9 tracks
for (int l = 1; l <= 9; ++l) {
2026-11-30 22:31:51 +02:00
assets::LevelData lvl;
CHECK(lvl.load(vfs, l));
render::WorldGeometry geo = render::build_level_geometry(lvl);
CHECK(geo.terrain_verts.size() == (assets::kMapWidth - 1) * (assets::kMapHeight - 1) * 4u);
CHECK(geo.terrain_indices.size() == (assets::kMapWidth - 1) * (assets::kMapHeight - 1) * 6u);
CHECK(!geo.column_verts.empty());
CHECK(!geo.column_indices.empty());
CHECK(geo.column_indices.size() % 6 == 0);
bool has_open_ao = false;
bool has_crevice_ao = false;
2026-11-30 22:31:51 +02:00
for (const auto& v : geo.terrain_verts) {
CHECK(std::isfinite(v.pos.x) && std::isfinite(v.pos.y) && std::isfinite(v.pos.z));
CHECK(v.tile >= 0.0f && v.tile < 256.0f);
CHECK(v.light >= 0.25f && v.light <= 1.5f);
CHECK(std::isfinite(v.ao) && v.ao >= 0.65f && v.ao <= 1.01f);
2026-11-30 22:31:51 +02:00
CHECK(v.u >= -1e-4f && v.u <= 1.0001f);
CHECK(v.v >= -1e-4f && v.v <= 1.0001f);
if (v.ao > 0.99f) has_open_ao = true;
if (v.ao < 0.95f) has_crevice_ao = true;
2026-11-30 22:31:51 +02:00
}
CHECK(has_open_ao && has_crevice_ao);
2026-11-30 22:31:51 +02:00
for (const auto& v : geo.column_verts) {
CHECK(std::isfinite(v.pos.x) && std::isfinite(v.pos.y) && std::isfinite(v.pos.z));
CHECK(v.tile >= 0.0f && v.tile < 256.0f);
CHECK(std::isfinite(v.ao) && v.ao >= 0.65f && v.ao <= 1.01f);
2026-11-30 22:31:51 +02:00
CHECK(v.u >= -1e-4f && v.u <= 1.0001f);
CHECK(v.v >= -1e-4f && v.v <= 1.0001f);
}
// Verify strict outward counter-clockwise triangle winding on terrain (+Y) & 3D columns
auto check_winding = [&](const std::vector<render::WorldVertex>& verts,
const std::vector<uint32_t>& indices,
bool heightmap_up) -> bool {
for (size_t i = 0; i + 2 < indices.size(); i += 3) {
const auto& v0 = verts[indices[i + 0]];
const auto& v1 = verts[indices[i + 1]];
const auto& v2 = verts[indices[i + 2]];
Vec3 tri_n = (v1.pos - v0.pos).cross(v2.pos - v0.pos);
if (tri_n.len_sq() <= 1e-8f) {
return false;
}
if (heightmap_up) {
if (tri_n.y <= 0.0f) {
return false;
}
} else {
Vec3 avg_n = v0.normal + v1.normal + v2.normal;
if (tri_n.dot(avg_n) <= 0.0f) {
return false;
}
}
}
return true;
};
CHECK(check_winding(geo.terrain_verts, geo.terrain_indices, true));
CHECK(check_winding(geo.column_verts, geo.column_indices, false));
// Verify partial morph region update stays valid
render::update_terrain_region(lvl, 10, 80, 15, 85, geo.terrain_verts);
if (l == 6) {
// Remove 39 solid columns at (44..57, 50..55) using morph id=140 source cells
assets::LevelData morphed = lvl;
for (int oz = 0; oz <= 5; ++oz) {
for (int ox = 0; ox <= 13; ++ox) {
morphed.cell_mut(44 + ox, 50 + oz) = lvl.cell(31 + ox, 41 + oz);
}
}
std::vector<render::WorldVertex> mverts;
std::vector<uint32_t> mindices;
render::rebuild_column_geometry(morphed, mverts, mindices);
CHECK(mindices.size() < geo.column_indices.size());
CHECK(!mverts.empty() && mindices.size() % 6 == 0);
for (const auto& v : mverts) {
CHECK(std::isfinite(v.pos.x) && std::isfinite(v.pos.y) && std::isfinite(v.pos.z));
CHECK(v.tile >= 0.0f && v.tile < 256.0f);
CHECK(v.u >= -1e-4f && v.u <= 1.0001f);
CHECK(v.v >= -1e-4f && v.v <= 1.0001f);
}
CHECK(check_winding(mverts, mindices, false));
// Also verify bidirectional swap relocates column vertices cleanly with valid winding
assets::LevelData swapped = lvl;
for (int oz = 0; oz <= 5; ++oz) {
for (int ox = 0; ox <= 13; ++ox) {
auto dc = lvl.cell(44 + ox, 50 + oz);
auto sc = lvl.cell(31 + ox, 41 + oz);
swapped.cell_mut(44 + ox, 50 + oz) = sc;
swapped.cell_mut(31 + ox, 41 + oz) = dc;
}
}
render::rebuild_column_geometry(swapped, mverts, mindices);
CHECK(!mverts.empty() && mindices.size() == geo.column_indices.size());
CHECK(check_winding(mverts, mindices, false));
}
2026-11-30 22:31:51 +02:00
std::cout << " Level " << l << " geo: terrain_tris=" << geo.terrain_indices.size() / 3
<< " column_tris=" << geo.column_indices.size() / 3 << "\n";
}
// Verify TRACK0-1 full track silhouette preview sprites for all 9 tracks
2026-11-30 22:31:51 +02:00
{
assets::SpriteBank track_bank;
CHECK(track_bank.load(vfs, "DATA/TRACK0-1.DAT", "DATA/TRACK0-1.TAB", pal));
static constexpr int kPreviewIndices[9] = {1, 3, 6, 7, 9, 11, 12, 14, 15};
2026-11-30 22:31:51 +02:00
for (int idx : kPreviewIndices) {
CHECK(static_cast<size_t>(idx) < track_bank.sprites.size());
const auto& sp = track_bank.sprites[idx];
CHECK(sp.width >= 24 && sp.height >= 90);
2026-11-30 22:31:51 +02:00
CHECK(!sp.rgba.empty());
}
}
// Verify PANEL0-1 authentic HUD sprites referenced in GameApp::render_hud_viewport
{
assets::SpriteBank panel_bank;
CHECK(panel_bank.load(vfs, "DATA/PANEL0-1.DAT", "DATA/PANEL0-1.TAB", pal));
auto check_range = [&](int a, int b) -> bool {
for (int idx = a; idx <= b; ++idx) {
if (idx < 0 || static_cast<size_t>(idx) >= panel_bank.sprites.size()) return false;
const auto& sp = panel_bank.sprites[idx];
if (sp.width <= 2 || sp.height <= 2 || sp.rgba.empty()) return false;
}
return true;
};
CHECK(check_range(1, 77));
CHECK(check_range(86, 162));
CHECK(check_range(198, 199));
CHECK(check_range(227, 228));
CHECK(check_range(258, 268));
CHECK(check_range(270, 271));
CHECK(check_range(273, 276));
CHECK(check_range(281, 286));
}
// Verify 3D model GPU geometry conversion & TEX0-0 half-texel UV inset
2026-11-30 22:31:51 +02:00
assets::ObjectTextureAtlas atlas;
CHECK(atlas.load(vfs, pal));
const char* keys[] = {"CAR0-0", "TANK0-0", "JUGGA0-0", "JET0-0", "BIKE0-0", "SKIM0-0", "RECOV0-0"};
for (const char* k : keys) {
assets::ModelMesh mesh;
CHECK(mesh.load(vfs, std::string("OBJECTS/DATA/") + k + ".DAT", &atlas));
for (const auto& tri : mesh.triangles) {
if (!tri.textured) continue;
for (int c = 0; c < 3; ++c) {
CHECK(tri.u[c] > 0.0f && tri.u[c] < 1.0f);
CHECK(tri.v_coord[c] > 0.0f && tri.v_coord[c] < 1.0f);
}
}
2026-11-30 22:31:51 +02:00
render::ModelGeometry mg = render::build_model_geometry(mesh, pal);
CHECK(!mg.verts.empty());
CHECK(mg.indices.size() == mesh.triangles.size() * 3u);
if (std::string(k) == "RECOV0-0") {
Vec3 scaled = (mesh.bounds_max - mesh.bounds_min) * sim::kRecoveryVisualScale;
CHECK(scaled.x >= 0.5f && scaled.x <= 2.5f);
CHECK(scaled.y >= 0.5f && scaled.y <= 2.5f);
CHECK(scaled.z >= 0.5f && scaled.z <= 2.5f);
}
2026-11-30 22:31:51 +02:00
}
// Verify Bottom (-Y) overpass diagonal split, TerrainChunk AABBs & update bounds
{
assets::LevelData lvl8;
CHECK(lvl8.load(vfs, 8));
render::WorldGeometry geo8 = render::build_level_geometry(lvl8);
// 1. 16x16 TerrainChunk AABBs cover every vertex and sum to terrain_indices
const int gw = assets::kMapWidth - 1;
const int gh = assets::kMapHeight - 1;
const int cs = render::WorldGeometry::kTerrainChunkSize;
const int chunks_x = (gw + cs - 1) / cs;
const int chunks_z = (gh + cs - 1) / cs;
CHECK(geo8.terrain_chunks.size() == static_cast<size_t>(chunks_x) * static_cast<size_t>(chunks_z));
size_t total_chunk_idx = 0;
for (const auto& ch : geo8.terrain_chunks) {
CHECK(ch.index_count > 0);
CHECK(ch.first_index + ch.index_count <= geo8.terrain_indices.size());
total_chunk_idx += ch.index_count;
for (uint32_t i = 0; i < ch.index_count; ++i) {
uint32_t vi = geo8.terrain_indices[ch.first_index + i];
const Vec3& p = geo8.terrain_verts[vi].pos;
CHECK(p.x >= ch.min_b.x - 1e-4f && p.x <= ch.max_b.x + 1e-4f);
CHECK(p.y >= ch.min_b.y - 1e-4f && p.y <= ch.max_b.y + 1e-4f);
CHECK(p.z >= ch.min_b.z - 1e-4f && p.z <= ch.max_b.z + 1e-4f);
}
}
CHECK(total_chunk_idx == geo8.terrain_indices.size());
// 2. Refresh bounds reflects terrain height changes cleanly
lvl8.cell_mut(75, 70).height = 4096;
render::update_terrain_region(lvl8, 74, 69, 76, 71, geo8.terrain_verts);
render::refresh_terrain_chunk_bounds(geo8, 74, 69, 76, 71);
const auto& updated_chunk = geo8.terrain_chunks[(70 / cs) * chunks_x + (75 / cs)];
CHECK(updated_chunk.max_b.y >= 2.9f);
// 3. All Bottom (-Y) column quads split along (00)-(11) diagonal matching Top/Terrain
int bottom_quads = 0;
for (size_t i = 0; i + 5 < geo8.column_indices.size(); i += 6) {
const auto& v0 = geo8.column_verts[geo8.column_indices[i + 0]];
if (v0.normal.y > -0.9f) continue;
// For our emitted bottom indices {base+3, base+2, base+1, base+3, base+1, base+0},
// index+0 is p3=(00) and index+2 is p1=(11): shared diagonal has dx*dz > 0.5
const auto& d0 = geo8.column_verts[geo8.column_indices[i + 0]];
const auto& d1 = geo8.column_verts[geo8.column_indices[i + 2]];
CHECK((d1.pos.x - d0.pos.x) * (d1.pos.z - d0.pos.z) > 0.5f);
++bottom_quads;
}
CHECK(bottom_quads > 0);
// 4. Synthetic 2x2 sm=0x02 overpass treats center vertex as overpass (ao=0.88), not solid base (0.72)
assets::LevelData syn;
syn.map.resize(assets::kMapWidth * assets::kMapHeight);
syn.columns[1].shape_mask = 0x02u;
syn.columns[1].blocks[1] = 0;
for (int z = 10; z <= 11; ++z) {
for (int x = 10; x <= 11; ++x) {
syn.cell_mut(x, z).tex_or_col = -1;
}
}
render::WorldGeometry geo_syn = render::build_level_geometry(syn);
const size_t v_base = (static_cast<size_t>(11) * (assets::kMapWidth - 1) + 11) * 4u;
CHECK(std::abs(geo_syn.terrain_verts[v_base].ao - 0.88f) < 1e-4f);
}
std::cout << "[PASS] All 9 Levels & Vehicle 3D Render Geometry validated\n";
2026-11-30 22:31:51 +02:00
return 0;
}