399 lines
22 KiB
C++
399 lines
22 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 <limits>
|
|
#include <filesystem>
|
|
#include "src/eter_render/ActorInstance.h"
|
|
|
|
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");
|
|
assert(!trunk.lighting && !canopy.lighting && "Original static tree colors must not receive dynamic lighting twice");
|
|
|
|
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, nullptr, nullptr, true);
|
|
auto draws_t1 = tree->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.57f, 25.0f, nullptr, nullptr, true);
|
|
|
|
// 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 its entire bounding sphere behind the camera. An origin behind
|
|
// the camera is insufficient when the generated canopy crosses the plane.
|
|
const float behind=-(forest.GetMainTree(1001)->GetRadius()*2+100);
|
|
forest.CreateInstance(0.0f, 0.0f, behind, 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, std::abs(behind)+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);
|
|
camera={0,0,4000};
|
|
auto farDraws=model.BuildDrawCommands(0,0,0,0,1,0,15,nullptr,&camera);
|
|
// Windows CSpeedTreeWrapper::Advance forces SetLodLevel(1): near, overlap
|
|
// and far cameras all draw full geometry, not atlas billboard substitutes.
|
|
assert(transition.size()==nearDraws.size() && farDraws.size()==nearDraws.size());
|
|
for(size_t i=0;i<nearDraws.size();++i) {
|
|
assert(transition[i].indices==nearDraws[i].indices && farDraws[i].indices==nearDraws[i].indices);
|
|
assert(farDraws[i].positions.size()==nearDraws[i].positions.size());
|
|
assert(farDraws[i].uv0==nearDraws[i].uv0);
|
|
}
|
|
assert(farDraws[1].alpha_test && farDraws[1].alpha_ref==84);
|
|
auto windy=model.BuildDrawCommands(0,0,0,0,1,10,30,nullptr,&camera);
|
|
assert(farDraws[1].positions!=windy[1].positions); // Full foliage still receives wind at distance.
|
|
// The SDK's independent billboard builder remains available to other consumers.
|
|
auto billboards=impl->BuildBillboardMeshes(camera);
|
|
assert(billboards.size()==2 && billboards[0].vertices.size()==4 && billboards[1].vertices.size()==4);
|
|
|
|
}
|
|
|
|
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 && !draws[1].lighting);
|
|
auto later=model.BuildDrawCommands(0,0,0,0,1,2);
|
|
assert(later[0].positions==draws[0].positions && later[1].positions==draws[1].positions);
|
|
assert(later[0].normals==draws[0].normals && later[1].normals==draws[1].normals);
|
|
auto windy=model.BuildDrawCommands(0,0,0,0,1,2,15,nullptr,nullptr,true);
|
|
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);
|
|
speedtree::WindParameters frame;frame.strength=0.75f;frame.legacy_leaf_rocking=true;frame.legacy_leaf_phase=1;
|
|
frame.matrices={std::array<float,16>{1,0,0,0,0,1,0,0,0,0,1,0,10000,20000,30000,1}};
|
|
auto first=model.BuildDrawCommands(30,40,50,90,2,0,15,&frame,&camera);
|
|
frame.legacy_leaf_phase=3;
|
|
auto rocking=model.BuildDrawCommands(30,40,50,90,2,0,15,&frame,&camera);
|
|
assert(rocking[0].positions==first[0].positions && rocking[0].normals==first[0].normals);
|
|
assert(rocking[1].positions!=first[1].positions);
|
|
// Rotation is around the authored pivot, without translating it using wind matrices.
|
|
const auto& vertices=impl->GetLeafMesh().vertices;
|
|
for(size_t i=0;i<vertices.size();++i) {
|
|
const auto& v=vertices[i];
|
|
float localPivot[3]={v.position[0]-v.leaf_pivot_offset[0],v.position[1]-v.leaf_pivot_offset[1],v.position[2]-v.leaf_pivot_offset[2]};
|
|
float pivot[3]={30+2*localPivot[2],40+2*localPivot[1],50-2*localPivot[0]};
|
|
float expected=0,actual=0;
|
|
for(size_t axis=0;axis<3;++axis) {
|
|
expected+=4*v.leaf_pivot_offset[axis]*v.leaf_pivot_offset[axis];
|
|
float d=rocking[1].positions[i*3+axis]-pivot[axis];actual+=d*d;
|
|
}
|
|
assert(std::abs(actual-expected)<0.05f);
|
|
}
|
|
const auto identity=std::array<float,16>{1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
|
|
auto neutral=frame;neutral.matrices={identity};
|
|
auto neutralDraws=model.BuildDrawCommands(30,40,50,90,2,0,15,&neutral,&camera);
|
|
for(size_t i=0;i<rocking.size();++i)assert(rocking[i].positions==neutralDraws[i].positions);
|
|
auto cpu=model.BuildDrawCommands(30,40,50,90,2,0,15,&frame,&camera,true);
|
|
assert(cpu[0].positions!=rocking[0].positions && cpu[1].positions!=rocking[1].positions);
|
|
}
|
|
|
|
void TestBoundsSharingAndWindPropagation() {
|
|
SptFixtures fixtures;SpeedTreeForest forest;
|
|
forest.RegisterPropertyData({12345,"Wide",fixtures.write("wide.spt",1000),1000,0});
|
|
auto* first=forest.CreateInstance(300,0,500,12345,90,2);
|
|
auto model=first->model;std::array<float,3> center;float radius;
|
|
model->GetWorldBounds(first->x,first->y,first->z,first->yaw,first->scale,center,radius);
|
|
auto expanded=model->BuildDrawCommands(first->x,first->y,first->z,first->yaw,first->scale,1,1);
|
|
for(const auto& draw:expanded)for(size_t i=0;i<draw.positions.size();i+=3) {
|
|
float distance=0;for(int k=0;k<3;++k){float delta=draw.positions[i+k]-center[k];distance+=delta*delta;}
|
|
assert(distance<=radius*radius+1);
|
|
}
|
|
float view[16]={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
|
|
float proj[16]={0.0001f,0,0,0,0,0.0001f,0,0,0,0,0.00005f,0,0,0,0.5f,1};
|
|
auto visible=forest.BuildDrawCommands(view,proj,0,50,0,50);
|
|
assert(!visible.empty()); // Origin beyond 50, crown still intersects distance sphere.
|
|
assert(visible[0].geometry && visible[0].positions.empty());
|
|
assert(visible[0].alpha_test && visible[0].alpha_ref==84 && !visible[0].alpha_blend);
|
|
auto shared=visible[0].geometry;
|
|
auto again=forest.BuildDrawCommands(view,proj,0,50,0,50);
|
|
assert(again[0].geometry==shared);
|
|
const auto& local=visible[0].Mesh().positions;
|
|
for(size_t i=0;i<local.size();i+=3) {
|
|
const float* m=visible[0].world;
|
|
const float actual[3]={local[i]*m[0]+local[i+1]*m[4]+local[i+2]*m[8]+m[12],local[i]*m[1]+local[i+1]*m[5]+local[i+2]*m[9]+m[13],local[i]*m[2]+local[i+1]*m[6]+local[i+2]*m[10]+m[14]};
|
|
for(int k=0;k<3;++k)assert(std::abs(actual[k]-expanded[0].positions[i+k])<0.01f);
|
|
}
|
|
assert(first->windStrength==1 && forest.GetWindStrength()==0);
|
|
forest.UpdateSystem(1);const float phase=first->leafWind.GetLeafPhase();
|
|
forest.SetWindStrength(0.2f);assert(first->windStrength==0.2f && std::abs(first->leafWind.GetLeafPhase()-phase)<0.001f);
|
|
auto* later=forest.CreateInstance(-300,0,500,12345);assert(later->windStrength==1);
|
|
forest.SetWindStrength(0.2f);assert(later->windStrength==1); // Same-value setter doesn't update new instances, like source.
|
|
forest.SetWindStrength(0.4f);assert(first->windStrength==0.4f && later->windStrength==0.4f);
|
|
forest.SetWindStrength(std::numeric_limits<float>::quiet_NaN());assert(forest.GetWindStrength()==0.4f);
|
|
forest.SetCpuWindEnabled(true);auto cpu=forest.BuildDrawCommands(view,proj,0,50,0,0);assert(!cpu[0].geometry);
|
|
forest.SetCpuWindEnabled(false);again=forest.BuildDrawCommands(view,proj,0,50,0,0);assert(again[0].geometry==shared);
|
|
// Dense instances share one trunk stream instead of allocating a copy for each tree/frame.
|
|
for(int i=0;i<128;++i)forest.CreateInstance(float(i),0,500,12345);
|
|
auto dense=forest.BuildDrawCommands(view,proj,0,50,0,0);size_t trunks=0;
|
|
for(const auto& draw:dense)if(draw.geometry){assert(draw.geometry==shared);++trunks;}
|
|
assert(trunks==130);
|
|
}
|
|
void TestTreeCollisionPrimitives() {
|
|
SptFixtures fixtures;auto bytes=SyntheticSpt(300);
|
|
auto u=[&](uint32_t v){for(int k=0;k<4;++k)bytes.push_back(uint8_t(v>>(k*8)));};
|
|
auto f=[&](float v){u(std::bit_cast<uint32_t>(v));};
|
|
u(12000);u(12002);f(100);f(200);f(300);f(20);
|
|
u(12003);f(0);f(0);f(0);f(10);f(100);
|
|
u(12004);f(-100);f(0);f(0);f(50);f(50);f(50);u(12001);
|
|
auto path=fixtures.directory/"collision.spt";{std::ofstream file(path,std::ios::binary);file.write(reinterpret_cast<const char*>(bytes.data()),bytes.size());}
|
|
auto partial=SyntheticSpt(300);for(int k=0;k<4;++k)partial.push_back(uint8_t(22000>>(k*8)));partial.push_back(0);
|
|
auto partialPath=fixtures.directory/"partial.spt";{std::ofstream file(partialPath,std::ios::binary);file.write(reinterpret_cast<const char*>(partial.data()),partial.size());}
|
|
auto partialModel=SpeedTreeModel::Create(partialPath.string(),300);assert(partialModel && !partialModel->GetSptInfo().metadata_complete && partialModel->GetSptInfo().unsupported_token==22000);
|
|
SpeedTreeForest resourceTest;
|
|
auto* named=resourceTest.CreateInstance(0,0,0,7,0,1,path.string().c_str());assert(named && named->model->GetHeight()==300);
|
|
resourceTest.RegisterPropertyData({8,"Missing",(fixtures.directory/"missing.spt").string(),300,0});
|
|
assert(!resourceTest.CreateInstance(0,0,0,8) && !resourceTest.HasMainTree(9));
|
|
assert(!resourceTest.CreateInstance(0,0,0,9,0,1,(fixtures.directory/"missing.spt").string().c_str()));
|
|
auto forest=std::make_unique<SpeedTreeForest>();forest->RegisterPropertyData({4242,"Collision",path.string(),300,0});
|
|
auto* instance=forest->CreateInstance(10,20,30,4242,90,2);
|
|
assert(instance->model->GetSptInfo().collision_objects.size()==3);
|
|
MapObjectManager objects;objects.SetTreeForest(forest.get());
|
|
float start[3]={0,320,230},end[3]={220,320,230};assert(objects.CheckFlyingCollision(start,end,0));
|
|
assert(objects.CheckMovementCollision(start,end,0)); // Both endpoints outside: swept crossing still blocks.
|
|
start[1]=end[1]=340;assert(!objects.CheckFlyingCollision(start,end,0)); // Strict sphere tangency.
|
|
float cstart[3]={-50,50,30},cend[3]={0,50,30};assert(objects.CheckMovementCollision(cstart,cend,30));
|
|
float high[3]={0,200,30};assert(!objects.CheckMovementCollision(high,high,1));
|
|
float tangent[3]={20,50,30};assert(objects.CheckFlyingCollision(tangent,tangent,0)); // Cylinder inclusive.
|
|
float box[3]={-90,50,30};assert(!objects.CheckFlyingCollision(box,box,1));
|
|
float inside[3]={110,320,230},outside[3]={140,320,230};assert(!objects.CheckMovementCollision(inside,outside,0));
|
|
ActorInstance actor;actor.SetPosition(-50,20,30);actor.SetSpeed(100);actor.MoveInDirection(90,0.5f,nullptr,&objects);assert(actor.GetX()<-20);
|
|
forest->DeleteInstance(instance);assert(!objects.CheckFlyingCollision(cstart,cend,30));
|
|
forest->CreateInstance(10,20,30,4242);assert(objects.CheckFlyingCollision(cstart,cend,30));
|
|
forest.reset();assert(!objects.CheckFlyingCollision(cstart,cend,30)); // Borrowed forest lifetime protected.
|
|
}
|
|
int main() {
|
|
std::cout << "=== Running SpeedTree & Forest Engine Tests ===" << std::endl;
|
|
TestPrtParsing();
|
|
TestSptSniffing();
|
|
TestSpeedTreeModelGeneration();
|
|
TestWindMotionSimulation();
|
|
TestSpeedTreeForestSystem();
|
|
TestBillboardRendering();
|
|
TestFrondRendering();
|
|
TestAuthoredLeafRendering();
|
|
TestBoundsSharingAndWindPropagation();
|
|
TestTreeCollisionPrimitives();
|
|
std::cout << "=== ALL SpeedTree & Forest Tests PASSED (100%) ===" << std::endl;
|
|
return 0;
|
|
}
|