Add bounded SPT parsing, authored branch frames and foliage generation, independent LOD meshes, billboards, collision metadata, and legacy CPU/shader wind support. Restore seeded sampling and special leaf selection with generation diagnostics and reproducible baseline tools. Include regression coverage and static recovery documentation describing remaining differences from the original runtime. SDK tests pass (4/4); full corpus and client validation are recorded in the recovery notes.
133 lines
4.7 KiB
C++
133 lines
4.7 KiB
C++
#include "speedtree/speedtree.h"
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
void TestProceduralTree() {
|
|
std::cout << "[Test] Procedural Tree Generation..." << std::endl;
|
|
auto model = speedtree::SpeedTreeModel::CreateProcedural("TestOak", 1200.0f, "bark.dds", "leaf.dds");
|
|
assert(model != nullptr);
|
|
assert(model->GetHeight() == 1200.0f);
|
|
assert(model->GetBarkTexture() == "bark.dds");
|
|
assert(model->GetLeafTexture() == "leaf.dds");
|
|
|
|
const auto& trunk = model->GetTrunkMesh();
|
|
assert(!trunk.vertices.empty());
|
|
assert(!trunk.indices.empty());
|
|
assert(trunk.indices.size() % 3 == 0);
|
|
|
|
const auto& leaf = model->GetLeafMesh();
|
|
assert(!leaf.vertices.empty());
|
|
assert(!leaf.indices.empty());
|
|
assert(leaf.indices.size() % 6 == 0); // Quads = 2 triangles
|
|
|
|
std::cout << " Trunk vertices: " << trunk.vertices.size()
|
|
<< ", indices: " << trunk.indices.size() << std::endl;
|
|
std::cout << " Leaf vertices: " << leaf.vertices.size()
|
|
<< ", indices: " << leaf.indices.size() << std::endl;
|
|
|
|
// Verify Spherical Normals and Leaf Pivot Offsets
|
|
for (size_t i = 0; i < leaf.vertices.size(); ++i) {
|
|
const auto& v = leaf.vertices[i];
|
|
// Normal must be normalized
|
|
float len = std::sqrt(v.normal[0]*v.normal[0] + v.normal[1]*v.normal[1] + v.normal[2]*v.normal[2]);
|
|
assert(std::fabs(len - 1.0f) < 0.05f);
|
|
|
|
// Wind weights must be valid [0, 1]
|
|
assert(v.wind_weights[0] >= 0.0f && v.wind_weights[0] <= 1.5f);
|
|
assert(v.wind_weights[1] >= 0.0f && v.wind_weights[1] <= 1.5f);
|
|
assert(v.wind_weights[2] >= 0.0f && v.wind_weights[2] <= 1.5f);
|
|
assert(v.wind_weights[3] >= 0.0f && v.wind_weights[3] <= 1.0f);
|
|
|
|
// Leaf pivot offset must be bounded by quad size
|
|
float pivot_dist = std::sqrt(v.leaf_pivot_offset[0]*v.leaf_pivot_offset[0] +
|
|
v.leaf_pivot_offset[1]*v.leaf_pivot_offset[1] +
|
|
v.leaf_pivot_offset[2]*v.leaf_pivot_offset[2]);
|
|
assert(pivot_dist < 1200.0f * 0.5f);
|
|
}
|
|
std::cout << " [PASS] Procedural Tree verified!" << std::endl;
|
|
}
|
|
|
|
void TestSptTokenSniffing() {
|
|
std::cout << "[Test] SPT Binary Token Sniffer..." << std::endl;
|
|
// Build synthetic .spt token stream
|
|
std::vector<uint8_t> buffer;
|
|
auto push_i32 = [&](int32_t val) {
|
|
uint8_t b[4];
|
|
std::memcpy(b, &val, 4);
|
|
buffer.insert(buffer.end(), b, b + 4);
|
|
};
|
|
auto push_f32 = [&](float val) {
|
|
uint8_t b[4];
|
|
std::memcpy(b, &val, 4);
|
|
buffer.insert(buffer.end(), b, b + 4);
|
|
};
|
|
|
|
push_i32(1000);
|
|
std::string signature = "__IdvSpt_02_";
|
|
push_i32(static_cast<int32_t>(signature.size()));
|
|
buffer.insert(buffer.end(), signature.begin(), signature.end());
|
|
push_i32(1002);
|
|
|
|
// Token 2000: bark name "custom_bark.dds"
|
|
std::string bark_str = "custom_bark.dds";
|
|
push_i32(2000);
|
|
push_i32(static_cast<int32_t>(bark_str.size()));
|
|
buffer.insert(buffer.end(), bark_str.begin(), bark_str.end());
|
|
|
|
// Token 2006: size 850.0f
|
|
push_i32(2006);
|
|
push_f32(850.0f);
|
|
|
|
// Token 2007: variance 50.0f
|
|
push_i32(2007);
|
|
push_f32(50.0f);
|
|
|
|
push_i32(1003);
|
|
push_i32(1001);
|
|
|
|
speedtree::SpeedTreeSptInfo info;
|
|
bool ok = speedtree::SpeedTreeSptInfo::SniffMemory(buffer.data(), buffer.size(), info);
|
|
assert(ok);
|
|
assert(info.bark_texture == "custom_bark.dds");
|
|
assert(info.size == 850.0f);
|
|
assert(info.variance == 50.0f);
|
|
|
|
std::cout << " [PASS] SPT Sniff verified!" << std::endl;
|
|
}
|
|
|
|
void TestForestManagement() {
|
|
std::cout << "[Test] SpeedTree Forest Management..." << std::endl;
|
|
speedtree::SpeedTreeForest forest;
|
|
assert(forest.GetInstanceCount() == 0);
|
|
|
|
assert(!forest.RegisterPropertyData(speedtree::SpeedTreeProperty{1, "Pine", "missing_test_pine.spt", 1000.0f, 0.0f}));
|
|
assert(!forest.HasMainTree(1));
|
|
|
|
auto* inst1 = forest.CreateInstance(100.0f, 0.0f, 200.0f, 1, 45.0f, 1.2f, "ForestPine1");
|
|
auto* inst2 = forest.CreateInstance(300.0f, 0.0f, 400.0f, 1, 90.0f, 0.9f, "ForestPine2");
|
|
assert(inst1 != nullptr && inst2 != nullptr);
|
|
assert(forest.GetInstanceCount() == 2);
|
|
|
|
forest.Update(0.016f);
|
|
assert(forest.GetWindParams().time > 0.01f);
|
|
|
|
forest.DeleteInstance(inst1);
|
|
assert(forest.GetInstanceCount() == 1);
|
|
|
|
forest.Clear();
|
|
assert(forest.GetInstanceCount() == 0);
|
|
|
|
std::cout << " [PASS] Forest Management verified!" << std::endl;
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "=== Running SpeedTree SDK Unit Tests ===" << std::endl;
|
|
TestProceduralTree();
|
|
TestSptTokenSniffing();
|
|
TestForestManagement();
|
|
std::cout << "=== All SpeedTree SDK Tests Passed! ===" << std::endl;
|
|
return 0;
|
|
}
|