Files
AVA2/tests/speedtree_forest_test.cpp
T

293 lines
13 KiB
C++

#include "src/eter_render/SpeedTreeRT.h"
#include "src/eter_render/SpeedTreeForest.h"
#include "src/eter_render/MapObjectManager.h"
#include <cassert>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <bit>
#include <chrono>
#include <filesystem>
using namespace ava2;
std::vector<uint8_t> SyntheticSpt(float size) {
std::vector<uint8_t> bytes;
auto push = [&](uint32_t value) {
for (int i = 0; i < 4; ++i) bytes.push_back(uint8_t(value >> (i * 8)));
};
std::string magic = "__IdvSpt_02_";
push(1000); push(static_cast<uint32_t>(magic.size()));
bytes.insert(bytes.end(), magic.begin(), magic.end());
push(1002); push(2006); push(std::bit_cast<uint32_t>(size));
push(2007); push(0); push(1003); push(1001);
return bytes;
}
struct SptFixtures {
std::filesystem::path directory = std::filesystem::temp_directory_path()
/ ("ava2-spt-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
SptFixtures() { std::filesystem::create_directory(directory); }
~SptFixtures() { std::filesystem::remove_all(directory); }
std::string write(const char* name, float size) {
auto bytes = SyntheticSpt(size);
auto path = directory / name;
std::ofstream file(path, std::ios::binary);
file.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
assert(file.good());
return path.string();
}
};
void TestPrtParsing() {
std::cout << "[Test] SpeedTree Property (.prt) Parsing..." << std::endl;
// Test synthetic PRT
std::string test_prt = "/tmp/test_tree.prt";
{
std::ofstream out(test_prt);
out << "YPRT\n"
<< "4240221483\n"
<< "propertyname\t\t\"treewillowoakfreesu\"\n"
<< "propertytype\t\t\"Tree\"\n"
<< "treefile\t\t\"d:/ymir work/palnts_2/all/ot/treewillowoakfreesu.spt\"\n"
<< "treesize\t\t\"1200.000000\"\n"
<< "treevariance\t\t\"50.000000\"\n";
}
SpeedTreeProperty prop;
bool ok = SpeedTreeProperty::LoadFromFile(test_prt, prop);
assert(ok && "Failed to parse synthetic .prt file");
assert(prop.crc == 4240221483 && "Property CRC mismatch");
assert(prop.property_name == "treewillowoakfreesu" && "Property name mismatch");
assert(prop.tree_size == 1200.0f && "Tree size mismatch");
assert(prop.tree_variance == 50.0f && "Tree variance mismatch");
std::cout << " -> PRT parsed successfully! (CRC: " << prop.crc << ", size: " << prop.tree_size << ")" << std::endl;
}
void TestSptSniffing() {
std::cout << "[Test] SpeedTree Binary (.spt) Sniffing..." << std::endl;
std::string test_spt = "build/dev/ymir work/plants/grass.spt";
SpeedTreeSptInfo info;
bool ok = SpeedTreeSptInfo::SniffSpt(test_spt, info);
if (ok) {
std::cout << " -> Sniffed grass.spt: ok=" << info.ok
<< ", bark='" << info.bark_texture
<< "', leaf='" << info.leaf_texture << "'" << std::endl;
assert(!info.leaf_texture.empty() && "Leaf texture must not be empty");
} else {
// Fallback test with synthetic memory buffer
auto syn = SyntheticSpt(1000.0f);
bool syn_ok = SpeedTreeSptInfo::SniffMemory(syn.data(), syn.size(), info);
assert(syn_ok && "Synthetic SPT sniffing must succeed");
std::cout << " -> Synthetic SPT sniffed successfully!" << std::endl;
}
}
void TestSpeedTreeModelGeneration() {
std::cout << "[Test] SpeedTree Model Geometry Generation..." << std::endl;
// 1. Procedural Oak Tree
auto oak = SpeedTreeModel::CreateProcedural("WillowOak", 1000.0f, "bark.dds", "leaves.dds");
assert(oak != nullptr && "Failed to create procedural tree model");
assert(!oak->IsGrassOrShrub() && "Oak should be identified as tree");
assert(oak->GetHeight() == 1000.0f && "Height mismatch");
auto draws = oak->BuildDrawCommands(100.0f, 50.0f, 200.0f, 45.0f, 1.0f, 0.0f);
assert(draws.size() == 2 && "Tree model must generate trunk and canopy draws");
const auto& trunk = draws[0];
const auto& canopy = draws[1];
assert(!trunk.positions.empty() && !trunk.indices.empty() && "Trunk must have geometry");
assert(!canopy.positions.empty() && !canopy.indices.empty() && "Canopy must have geometry");
assert(canopy.alpha_test && canopy.alpha_ref == 84 && "Canopy must have alpha test 84");
assert(canopy.cull_mode == 1 && "Canopy must be double sided");
std::cout << " -> Oak Tree generated: Trunk verts=" << trunk.positions.size() / 3
<< ", tris=" << trunk.indices.size() / 3
<< " | Canopy verts=" << canopy.positions.size() / 3
<< ", tris=" << canopy.indices.size() / 3 << std::endl;
// 2. Procedural Grass / Bush
auto grass = SpeedTreeModel::CreateProcedural("WildGrass", 120.0f, "", "plant_grass.dds");
assert(grass->IsGrassOrShrub() && "WildGrass should be identified as shrub/grass");
auto gdraws = grass->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
assert(gdraws.size() == 1 && "Grass model should only generate foliage draw");
assert(gdraws[0].cull_mode == 1 && gdraws[0].alpha_test && "Grass must be double-sided alpha tested");
std::cout << " -> Grass cluster generated: verts=" << gdraws[0].positions.size() / 3
<< ", tris=" << gdraws[0].indices.size() / 3 << std::endl;
}
void TestWindMotionSimulation() {
std::cout << "[Test] Wind Motion Simulation & Anchoring..." << std::endl;
auto tree = SpeedTreeModel::CreateProcedural("BlowingPine", 1000.0f, "bark.dds", "leaves.dds");
auto draws_t0 = tree->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 25.0f);
auto draws_t1 = tree->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.57f, 25.0f);
// Check base vertex (y ~ 0) stays anchored
float base_x0 = draws_t0[0].positions[0];
float base_x1 = draws_t1[0].positions[0];
assert(std::abs(base_x0 - base_x1) < 0.01f && "Tree trunk base must be firmly anchored at ground");
// Check top canopy vertex sways with wind (flex grows with height², so pick the
// highest canopy vertex; vertex 0 is the bottom edge of the lowest cluster).
const auto& canopy0 = draws_t0[1].positions;
size_t top = 0;
for (size_t i = 0; i < canopy0.size() / 3; ++i) {
if (canopy0[i * 3 + 1] > canopy0[top * 3 + 1]) top = i;
}
float top_x0 = canopy0[top * 3 + 0];
float top_x1 = draws_t1[1].positions[top * 3 + 0];
float delta_sway = std::abs(top_x0 - top_x1);
assert(delta_sway > 0.5f && "Tree canopy must visibly sway with wind");
std::cout << " -> Wind displacement at canopy tip: " << delta_sway << " units (sway verified!)" << std::endl;
}
void TestSpeedTreeForestSystem() {
std::cout << "[Test] SpeedTreeForest Management & Frustum Culling..." << std::endl;
SpeedTreeForest forest;
forest.ClearMainTree();
SptFixtures fixtures;
SpeedTreeProperty prop_oak{1001, "Oak", fixtures.write("oak.spt", 1200.0f), 1200.0f, 0.0f};
SpeedTreeProperty prop_grass{1002, "Grass", fixtures.write("grass.spt", 100.0f), 100.0f, 0.0f};
forest.RegisterPropertyData(prop_oak);
forest.RegisterPropertyData(prop_grass);
// Place trees in front of camera
forest.CreateInstance(0.0f, 0.0f, 300.0f, 1001, 0.0f, 1.0f);
forest.CreateInstance(50.0f, 0.0f, 350.0f, 1002, 30.0f, 1.0f);
// Place one tree behind camera (Z = -500)
forest.CreateInstance(0.0f, 0.0f, -500.0f, 1001, 0.0f, 1.0f);
assert(forest.GetInstanceCount() == 3 && "Instance count mismatch");
forest.UpdateSystem(0.033f);
// Standard camera looking down +Z
float view[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
float proj[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, -1.0f, 0.0f
};
auto culled_draws = forest.BuildDrawCommands(view, proj, 0.0f, 50.0f, 0.0f, 2000.0f);
assert(!culled_draws.empty() && "Visible trees must produce draw commands");
// Front trees (1 tree = 2 draws, 1 grass = 1 draw -> 3 draws total)
assert(culled_draws.size() == 3 && "Behind-camera tree must be culled (3 draws expected)");
std::cout << " -> Forest generated " << culled_draws.size()
<< " culled draw commands for 2 visible instances (1 culled behind camera)!" << std::endl;
}
void TestBillboardRendering() {
auto bytes=SyntheticSpt(1000);
auto push=[&](uint32_t value) {
for(int i=0;i<4;++i) bytes.push_back(uint8_t(value>>(i*8)));
};
auto f=[&](float value) { push(std::bit_cast<uint32_t>(value)); };
// Add LOD distances inside the main general block.
bytes.resize(bytes.size()-8);
push(2003);f(1000);push(2001);f(3000);push(1003);push(1001);
push(10000);push(10003);push(1);
for(float uv:{0.f,0.f,1.f,0.f,1.f,1.f,0.f,1.f}) f(uv);
push(10006);bytes.push_back(1);push(10001);
push(9000);push(9003);f(0.1f);push(9001);
auto impl=speedtree::SpeedTreeModel::CreateFromMemory(bytes.data(),bytes.size());
assert(impl);
SpeedTreeModel model(impl);
std::array<float,3> camera{0,50,500};
auto nearDraws=model.BuildDrawCommands(0,0,0,0,1,0,15,nullptr,&camera);
assert(nearDraws.size()==2 && nearDraws[0].positions.size()>12);
camera={0,0,2000};
auto transition=model.BuildDrawCommands(0,0,0,0,1,0,15,nullptr,&camera);
assert(transition.size()==4 && transition[2].positions.size()==12);
camera={0,0,4000};
auto farDraws=model.BuildDrawCommands(0,0,0,0,1,0,15,nullptr,&camera);
assert(farDraws.size()==2 && farDraws[0].positions.size()==12 && farDraws[1].positions.size()==12);
assert(farDraws[0].alpha_test && farDraws[0].alpha_ref==84);
auto windy=model.BuildDrawCommands(0,0,0,0,1,10,30,nullptr,&camera);
assert(farDraws[0].positions==windy[0].positions && farDraws[1].positions==windy[1].positions);
assert(model.BuildDrawCommands(0,0,0,0,1,0).size()==2);
// A different camera heading, tree yaw and scale must still face the camera.
camera={4000,0,0};
auto rotated=model.BuildDrawCommands(0,0,0,90,2,0,15,nullptr,&camera);
float centerX=0,centerZ=0;
for(size_t i=0;i<4;++i) {
centerX+=rotated[0].positions[i*3]*0.25f;
centerZ+=rotated[0].positions[i*3+2]*0.25f;
}
float dx=camera[0]-centerX,dz=camera[2]-centerZ,length=std::hypot(dx,dz);
for(size_t i=0;i<4;++i) {
assert(std::abs(rotated[0].normals[i*3]-dx/length)<0.001f);
assert(std::abs(rotated[0].normals[i*3+2]-dz/length)<0.001f);
}
}
void TestFrondRendering() {
auto bytes=SyntheticSpt(1000);bytes.resize(bytes.size()-8);
auto push=[&](uint32_t value) { for(int i=0;i<4;++i) bytes.push_back(uint8_t(value>>(i*8))); };
push(1014);push(2);
for(int level=0;level<2;++level) { push(1016);push(6008);push(6);push(6009);push(4);push(1017); }
push(1015);push(1003);push(1001);
push(13000);push(13002);push(1);push(13003);push(0);push(13004);push(2);
push(13007);bytes.push_back(1);push(13001);
auto impl=speedtree::SpeedTreeModel::CreateFromMemory(bytes.data(),bytes.size());
assert(impl && !impl->GetFrondMeshes().empty());
SpeedTreeModel model(impl);
auto draws=model.BuildDrawCommands(0,0,0,0,1,0);
assert(draws.size()==2 && impl->GetLeafMesh().vertices.empty());
assert(draws[1].alpha_test && draws[1].alpha_ref==84 && draws[1].cull_mode==1);
auto windy=model.BuildDrawCommands(0,0,0,0,1,2);
bool moved=false;
for(size_t i=0;i<draws[1].positions.size();++i) {
assert(std::isfinite(windy[1].positions[i]));
moved|=std::abs(windy[1].positions[i]-draws[1].positions[i])>0.01f;
}
assert(moved);
}
void TestAuthoredLeafRendering() {
auto bytes=SyntheticSpt(1000);bytes.resize(bytes.size()-8);
auto push=[&](uint32_t value) { for(int i=0;i<4;++i) bytes.push_back(uint8_t(value>>(i*8))); };
auto f=[&](float value) { push(std::bit_cast<uint32_t>(value)); };
push(1014);push(2);
for(int level=0;level<2;++level) { push(1016);push(6008);push(6);push(6009);push(4);push(1017); }
push(1015);push(1003);
push(1004);push(1009);push(1006);push(1);push(1007);
push(4004);f(0.25f);f(0.75f);f(0);push(4006);f(80);f(120);f(0);
push(1008);push(1010);push(1005);push(1001);
auto impl=speedtree::SpeedTreeModel::CreateFromMemory(bytes.data(),bytes.size());
assert(impl && impl->GetLeafMesh().vertices[0].authored_leaf_card);
SpeedTreeModel model(impl);std::array<float,3> camera{0,0,4000};
auto draws=model.BuildDrawCommands(0,0,0,90,2,0,0,nullptr,&camera);
assert(draws.size()==2);
for(size_t i=0;i<draws[1].normals.size()/3;++i) {
assert(std::abs(draws[1].normals[i*3])<0.001f);
assert(std::abs(draws[1].normals[i*3+2]-1)<0.001f);
}
camera={4000,0,0};
auto changed=model.BuildDrawCommands(0,0,0,90,2,0,0,nullptr,&camera);
assert(changed[1].positions!=draws[1].positions);
assert(changed[1].uv0==draws[1].uv0 && changed[1].indices==draws[1].indices);
}
int main() {
std::cout << "=== Running SpeedTree & Forest Engine Tests ===" << std::endl;
TestPrtParsing();
TestSptSniffing();
TestSpeedTreeModelGeneration();
TestWindMotionSimulation();
TestSpeedTreeForestSystem();
TestBillboardRendering();
TestFrondRendering();
TestAuthoredLeafRendering();
std::cout << "=== ALL SpeedTree & Forest Tests PASSED (100%) ===" << std::endl;
return 0;
}