Files
metin2-SDK/speedtree/tests/legacy_runtime_test.cpp
T
shenandshen 66992b3024 Implement cross-platform SpeedTree runtime and recovery tests
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.
2026-10-12 18:52:02 -07:00

87 lines
5.6 KiB
C++

#include "speedtree/speedtree.h"
#include <cmath>
#include <bit>
#include <iostream>
#include <stdexcept>
using namespace speedtree;
void require(bool value,const char* message) { if(!value) throw std::runtime_error(message); }
bool near(float a,float b,float eps=0.0001f) { return std::abs(a-b)<eps; }
int main() {
try {
BranchSpline curve;
require(BranchSpline::Parse("BezierSpline 0 1 0 { 2 0 0 1 1 0.2 1 1 1 1 0.2 }",curve),"curve parse");
require(curve.samples_ready && near(curve.samples[250],0.5f),"500-table resampling");
require(near(curve.Evaluate(0.5f),0.499f),"legacy 500/499 lookup behavior");
require(near(curve.Evaluate(0),0) && near(curve.Evaluate(1),1),"curve endpoint handling");
LeafDefinition leaf;leaf.dimensions={8,12,0};leaf.pivot={0.25f,0.75f,0};
auto corners=BuildLeafCorners(leaf,false),mirrored=BuildLeafCorners(leaf,true);
require(near(corners[0][2],2) && near(corners[1][2],-6)
&& near(corners[0][1],3) && near(corners[2][1],-9),"authored leaf dimensions/pivot");
require(near(mirrored[0][2],6) && near(mirrored[1][2],-2),"mirrored leaf pivot");
auto offset=OrientLeafOffset({0,2,3},90,0,0);
require(near(offset[0],-3) && near(offset[1],2) && near(offset[2],0),"leaf camera azimuth");
offset=OrientLeafOffset({0,2,3},0,90,0);
require(near(offset[0],2) && near(offset[1],0) && near(offset[2],3),"leaf camera pitch");
offset=OrientLeafOffset({0,2,3},0,0,90);
require(near(offset[0],0) && near(offset[1],3) && near(offset[2],-2),"leaf local rocking axis");
// Values from an independent integer Park-Miller/shuffle reference,
// with the DLL's float table storage and indexed child reseeding.
require(near(SampleBranchAttachment(17,0,0.3f,1),std::bit_cast<float>(0x3f6c5c7fu),0.000001f)
&& near(SampleBranchAttachment(17,1,0.3f,1),std::bit_cast<float>(0x3f38b5d0u),0.000001f)
&& near(SampleBranchAttachment(17,2,0.3f,1),std::bit_cast<float>(0x3eb03402u),0.000001f),"indexed random reference values");
LegacyWindController controller;
controller.Update(0,0.2f);
require(controller.GetMatrices().size()==4,"missing wind groups");
auto matrix=controller.GetMatrices()[0];
float angle=7.0f/57.2957795f;
require(near(matrix[0],std::cos(angle)) && near(matrix[4],std::sin(angle)),"original time-zero compound rotation");
controller.Update(1,0.2f);
float oldPhase=controller.GetLeafPhase();
float oldX=std::asin(-controller.GetMatrices()[0][6]);
controller.Update(1,0.4f);
require(near(oldPhase,controller.GetLeafPhase()),"leaf phase jumped on strength change");
require(near(std::asin(-controller.GetMatrices()[0][6]),2*oldX),"matrix phase jumped on strength change");
for(const auto& m:controller.GetMatrices()) {
for(int column=0;column<3;++column) {
float length=0;for(int row=0;row<3;++row) length+=m[column*4+row]*m[column*4+row];
require(near(length,1),"non-orthogonal wind matrix");
}
}
LegacyWindController independent;independent.Update(1,0.2f);
controller.Update(2,0);
for(const auto& m:controller.GetMatrices()) for(int c=0;c<4;++c) for(int r=0;r<4;++r)
require(near(m[c*4+r],c==r?1.0f:0.0f),"zero-strength controller not identity");
require(near(independent.GetLeafPhase(),1.6f),"controller phase shared globally");
require(SelectDiscreteLod(1,4)==0 && SelectDiscreteLod(0.5f,4)==2
&& SelectDiscreteLod(0,4)==3 && SelectDiscreteLod(0,0)==-1,"original discrete LOD boundaries");
auto transition=SelectLodTransition(0.5f,2,0.1f,0.5f,2,84);
require(transition.first==0 && transition.second==1 && near(transition.first_alpha,84)
&& near(transition.second_alpha,84),"LOD transition center");
transition=SelectLodTransition(0.59f,2,0.1f,0.5f,2,84);
require(transition.first==0 && transition.second==1 && near(transition.first_alpha,84)
&& near(transition.second_alpha,222.51f,0.01f),"LOD threshold exponent");
transition=SelectLodTransition(0,2,0.1f,0.5f,2);
require(transition.first==1 && transition.second==-1,"LOD last-level clamp");
require(SelectLodTransition(0.5f,0,0.1f,0.5f,2).first==-1,"empty LOD set");
SpeedTreeSptInfo atlas;atlas.billboard_360=true;atlas.billboard_uv_quads.resize(8);
for(int angle=-720;angle<=720;++angle) {
auto views=SelectBillboardViews(atlas,float(angle),150);
require(!views.empty() && views.size()<=2,"360 billboard view count");
for(const auto& view:views) require(view.texture_index>=0 && view.texture_index<8
&& view.alpha_ref>=150 && view.alpha_ref<255,"360 billboard texture/alpha range");
}
auto a=SelectBillboardViews(atlas,0,84),b=SelectBillboardViews(atlas,360,84);
require(a.size()==b.size() && a[0].texture_index==b[0].texture_index
&& near(a[0].alpha_ref,b[0].alpha_ref),"billboard wrap discontinuity");
require(SelectBillboardViews(atlas,45,255).empty(),"invisible atlas views retained");
atlas.billboard_360=false;
require(SelectBillboardViews(atlas,90,120)[0].texture_index==0,"simple billboard atlas index");
SpeedTreeForest forest;forest.Update(0.25f);
require(forest.GetWindParams().matrices.size()==4 && forest.GetWindParams().legacy_leaf_rocking,
"native controller not integrated into forest");
std::cout<<"Legacy LUT, wind phase and LOD regressions passed\n";
}catch(const std::exception& e) { std::cerr<<e.what()<<'\n';return 1; }
}