Files
AVA2/tests/game_scene_test.cpp
shenandshen 281418f351 Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
2026-10-11 17:55:53 -07:00

110 lines
5.9 KiB
C++

#include "eter_render/Gr2ModelRenderer.h"
#include "eter_render/TerrainHeightmap.h"
#include "eter_effect/EffectInstance.h"
#include <SDL3/SDL.h>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <set>
#include <array>
#include <cmath>
int main() {
namespace fs = std::filesystem;
const auto old = fs::current_path();
const auto dir = fs::temp_directory_path() / "ava2-game-scene-test";
fs::create_directories(dir); fs::current_path(dir);
// Match the GM marker's asymmetric half sizes and scale. A looping
// emitter must remain one rectangular camera-facing quad at every yaw.
{
std::ofstream script("marker.mse");
script << "Group Particle { List TimeEventPosition { 0 MOVING_TYPE_DIRECT 0 0 110 } Group EmitterProperty { MaxEmissionCount 1 CycleLength 0.5 CycleLoopEnable 1 "
"List TimeEventEmissionCountPerSecond { 0 20 } List TimeEventLifeTime { 0 2.2 } "
"List TimeEventEmittingDirectionZ { 0 15 } List TimeEventEmittingVelocity { 0 3 } List TimeEventSizeX { 0 64 } List TimeEventSizeY { 0 64 } } "
"Group ParticleProperty { BillboardType 1 AttachEnable 1 List TimeEventGravity { 0 25 } List TimeEventScaleX { 0 0.2 } "
"List TimeEventScaleY { 0 0.4 } List TimeEventAlpha { 0 1 } } }";
}
auto markerData = std::make_shared<ava2::EffectData>();
assert(markerData->LoadScript("marker.mse"));
ava2::EffectInstance marker(markerData);
for (int i=0;i<20;++i) marker.Update(0.05f);
for (const auto& eye : {std::array<float,3>{0,100,-200}, std::array<float,3>{200,100,0}}) {
ava2::Gr2SceneRenderer camera(64,64);
camera.SetCameraLookAtRH(eye[0],eye[1],eye[2],0,0,0,0,1,0);
camera.SetPerspective(45,1,1,1000);
std::vector<ava2::Render3DDraw> commands;
marker.BuildDrawCommands(camera.GetViewMatrix(),camera.GetProjMatrix(),eye.data(),commands);
assert(commands.size()==1 && commands[0].positions.size()==12);
const auto& p=commands[0].positions;
auto edge=[&](int a,int b) { float s=0; for(int k=0;k<3;++k) s+=(p[a*3+k]-p[b*3+k])*(p[a*3+k]-p[b*3+k]); return std::sqrt(s); };
assert(std::abs(edge(0,1)-51.2f)<0.01f && std::abs(edge(1,2)-25.6f)<0.01f);
for(int k=0;k<3;++k) assert(std::abs(p[k]+p[6+k]-p[3+k]-p[9+k])<0.01f);
}
// Attached particles follow their owner, without multiplying script gravity by 9.8.
{
ava2::Gr2SceneRenderer camera(64,64);
camera.SetCameraLookAtRH(0,100,-200,0,0,0,0,1,0);
std::vector<ava2::Render3DDraw> before, after;
const float eye[3]={0,100,-200};
marker.BuildDrawCommands(camera.GetViewMatrix(),camera.GetProjMatrix(),eye,before);
float centerY=0; for(int i=0;i<4;++i) centerY+=before[0].positions[3*i+1]/4;
assert(centerY > 140 && centerY < 160);
marker.SetPosition(10,20,30);
marker.BuildDrawCommands(camera.GetViewMatrix(),camera.GetProjMatrix(),eye,after);
for(size_t i=0;i<before[0].positions.size();++i)
assert(std::abs(after[0].positions[i]-before[0].positions[i]-(i%3==0?10:i%3==1?20:30))<0.01f);
}
// DDS armor alpha is a specular mask: opaque rendering must retain its RGB.
unsigned char header[18]{};
header[2] = 2; header[12] = 2; header[14] = 2; header[16] = 32; header[17] = 0x28;
{ std::ofstream file("opaque.tga", std::ios::binary);
file.write(reinterpret_cast<const char*>(header), 18);
const unsigned char red[4] = {0, 0, 255, 0};
for (int i = 0; i < 4; ++i) file.write(reinterpret_cast<const char*>(red), 4); }
SDL_Surface* surface = SDL_CreateSurface(64, 64, SDL_PIXELFORMAT_ARGB8888);
SDL_Renderer* sdl = SDL_CreateSoftwareRenderer(surface);
assert(surface && sdl);
{
ava2::Gr2SceneRenderer scene(64, 64);
scene.SetCameraLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0);
scene.SetPerspective(45, 1, 1, 100);
scene.SetFog(false);
auto quad = [](float z, uint32_t color) {
ava2::Render3DDraw d;
d.positions = {-3,-3,z, 3,-3,z, 3,3,z, -3,3,z};
d.uv0 = {0,0, 1,0, 1,1, 0,1};
d.indices = {0,1,2, 0,2,3}; d.diffuse.assign(4, color);
d.cull_mode = 1; d.lighting = false; return d;
};
auto near = quad(0, 0xffffffff), far = quad(5, 0xff00ff00);
near.texture0 = "opaque.tga";
for (bool reverse : {false, true}) {
SDL_SetRenderDrawColor(sdl, 0, 0, 0, 255); SDL_RenderClear(sdl);
scene.RenderSceneGPU(sdl, reverse ? std::vector<ava2::Render3DDraw>{far, near} : std::vector<ava2::Render3DDraw>{near, far});
SDL_RenderPresent(sdl);
const auto pixel = static_cast<uint32_t*>(surface->pixels)[32 * (surface->pitch / 4) + 32];
assert(((pixel >> 16) & 255) > 240 && ((pixel >> 8) & 255) < 10);
}
}
SDL_DestroyRenderer(sdl); SDL_DestroySurface(surface);
ava2::TerrainHeightmap terrain;
std::vector<uint16_t> heights(131 * 131, 200);
std::vector<uint8_t> tiles(258 * 258, 1);
for (int y = 0; y < 258; ++y) for (int x = 16; x < 258; ++x) tiles[y * 258 + x] = 2;
assert(terrain.LoadFromBuffer(heights.data(), heights.size()));
assert(terrain.LoadTileFromBuffer(tiles.data(), tiles.size()));
assert(terrain.LoadTextureSetFromString("Start Texture001\n\"grass.dds\"\nEnd Texture001\nStart Texture002\n\"path.dds\"\nEnd Texture002\n"));
auto draws = terrain.BuildPatchDraws(0, 0);
std::set<std::string> textures;
bool blendedEdge = false;
for (const auto& draw : draws) {
textures.insert(draw.texture0);
if (draw.alpha_blend) for (auto color : draw.diffuse) {
const auto alpha = color >> 24;
if (alpha > 0 && alpha < 255) blendedEdge = true;
}
}
assert(textures.count("grass.dds") && textures.count("path.dds") && blendedEdge);
fs::current_path(old); fs::remove_all(dir);
}