#include "src/eter_render/SpeedTreeRT.h" #include "src/eter_render/SpeedTreeForest.h" #include "src/eter_render/MapObjectManager.h" #include #include #include #include #include #include #include #include #include #include "src/eter_render/ActorInstance.h" using namespace ava2; std::vector SyntheticSpt(float size) { std::vector 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(magic.size())); bytes.insert(bytes.end(), magic.begin(), magic.end()); push(1002); push(2006); push(std::bit_cast(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(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(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 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;iBuildBillboardMeshes(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;i0.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(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 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{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{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;imodel;std::array 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;iwindStrength==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::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(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(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(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();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; }