Files
EVEngine/test/softbody.cpp
T
西风逍遥游 34d1fdb63b refactor(modules): clear remaining back-edges and split the largest TUs (#127)
* chore(ci): add clang-format gate, CODEOWNERS and agent collaboration rules

* refactor(modules): clear remaining back-edges and split the largest TUs

Three known dependency back-edges are gone (module_depgraph.py KNOWN_BACK_EDGES
is now empty; --check passes with 0 violations, 0 cycles):

- window -> graphics: window queries the new IWindowSurfaceHost capability
  (common/WindowSurfaceHost.h) instead of holding a Graphics pointer;
  Window::setGraphics was removed and all call sites updated.
- window -> image: Window::setIcon was replaced by setIconRGBA(const uint8_t*,
  w, h); the window no longer decodes ImageData.
- scene -> graphics: pickScreenAt / collectFrustumIdsAt implementations moved
  to graphics/ScenePicking.cpp (graphics -> scene is a legal downward edge);
  scene now depends only on spatial.

TU splits (pure moves, verified identical method sets and brace balance):

- vulkan/Graphics.cpp (5118 lines) -> Graphics.cpp (lifecycle), GraphicsPipeline,
  Graphics2D, Graphics3D, GraphicsMesh + shared GraphicsInternal.h.
- scene/Scene.cpp -> Scene.cpp (script layer / host lifecycle / serialization)
  + SceneNodes.cpp + shared SceneInternal.h.

Verified with a real build (MSVC 19.51, prebuilt third-party): full build green;
window.* and Scene.* suites pass, including scene picking/culling and GPU cases.

* refactor(graphics): narrow Graphics interfaces and clear public-header leaks

Step 4 — interface segregation:
- New lean interfaces: IGraphics2D, IGraphics3D, IResourceFactory, IPostFX
  (pure virtuals, forward-declared types only; graphics/Color.h extracted from
  Canvas.h so modules that only need the color type stop pulling Canvas.h).
- Graphics now implements all four facets; consumers can depend on the narrow
  interface they use instead of the 129-method god class.
- Migrated voxel (VoxelWorld::drawVisible -> IGraphics3D*) and model3d
  (ModelRenderer::buildRenderable(s) -> IResourceFactory&).
- Graphics.h slims from 24 sub-header includes to a handful of small type
  headers + forward declarations; the destructor moved out-of-line so
  unique_ptr effect members are destroyed where types are complete.

Step 5 — header leaks (module_depgraph.py '*' marks: 14 -> 7):
- map/TileLayer.h, particles/ParticleEmitter.h, avatar/AvatarInstance.h,
  animation/SpineAnim.h, map/TileSystem.h, model3d/ModelData.h,
  model3d/ModelRenderer.h, audio/Audio.h + Source.h, image/ImageData.h +
  Image.h, dialogue/Dialogue.h, procgen Palette/OutputSpec/TextureRecipe/
  PbrMaterial -> forward declarations, includes moved to .cpp.
- voxel/VoxelWorld.h: terrain sampler Pimpl'd (unique_ptr + out-of-line
  destructor), removing the procgen header dependency.
- gpgpu/vulkan backend headers stop pulling graphics/vulkan/Graphics.h.
- Remaining '*' marks are deliberate: tiny POD/enum headers (GridConfig,
  MapObject, BlendMode, Color), an internal backend header
  (GraphicsInternal.h -> filesystem), and lean interface references.

Verified with a full clean build (MSVC) and tests: window/Scene/voxel/
RenderSystem/Quad/shader/TextureSampler/resourceLifetime pass; broad suite
only shows the pre-existing PHYSFS-init filesystem-environment failures.

* feat(resources): revive ResourceManager as unified IAssetReloader-backed cache

* fix(resources): leak ResourceManager singleton to avoid exit-time teardown crashes

Cached CPU resources may own third-party handles (FreeType faces, Assimp
scenes, image decode handlers) whose libraries are torn down at process
exit in an unspecified TU order; destroying cached entries from the
singleton destructor can segfault at exit (seen on CI Windows in
model3d.materialApi.objMtl). Keep the singleton alive until OS reclaim;
explicit unload()/clear() still release entries during the run.

* refactor(devtools): consume business modules through capability interfaces

DevTools / cmdline no longer include business-module headers, so trimming
scene / physics / procgen / particles / audio / ui / graphics no longer
requires disabling devtools, and devtools can be switched off with
-DEVENGINE_MODULE_DEVTOOLS=OFF.

New capability interfaces in common/:
- ISceneQuery (scene) - host/node inspection + mutation + transform sync
- IRenderCapture (graphics) - render status, PNG capture, camera control,
  visible-entity snapshot, entity-ID mask, G-buffer PNGs
- IPhysicsQuery (physics) - 2D world lifecycle + ray casts
- IProcgenQuery (procgen) - recipe listing, map/mesh generation
- IParticlesQuery (particles) - emitter status + one-shot emit
- IAudioQuery (audio) - volume / stop-all
- IEditorHost (ui) - declarative editor host (used by cmdline too)

Providers register in their module constructors. DevTools rewires McpServer
(render/scene/procgen/physics/particles/audio/host tools), SceneInspect
(now delegates to ISceneQuery + IRenderCapture) and RenderVision (uses
IRenderCapture::capturePngDataUrl); cmdline/Mcp.cpp uses IEditorHost.

manifest: devtools DEPS graphics scene physics procgen particles audio ui
image event filesystem -> event; cmdline DEPS filesystem ui -> filesystem.

Verified: full build green; devtools.mcp.* tests pass; broad suite shows
only the pre-existing PHYSFS-init filesystem-environment failures.

* style: apply clang-format to changed lines after dev merge

* fix(webgpu): include RenderControl/TextureSampler explicitly after Graphics.h slimming

* fix(webgpu): provide newFont/print stubs to satisfy IGraphics2D/IResourceFactory vtables

* fix(common): make ProcgenQuery.h self-sufficient (cstdint/utility for GCC)
2026-08-20 05:53:33 +08:00

206 lines
6.6 KiB
C++

#include "zeroerr/assert.h"
#include "zeroerr/unittest.h"
#include "graphics/AmbientOcclusion.h"
#include "graphics/AntiAliasing.h"
#include "graphics/Canvas.h"
#include "graphics/DrawItem2D.h"
#include "graphics/Font.h"
#include "graphics/GBuffer.h"
#include "graphics/GlobalIllumination.h"
#include "graphics/Graphics.h"
#include "graphics/Grass.h"
#include "graphics/Light.h"
#include "graphics/Material.h"
#include "graphics/Mesh.h"
#include "graphics/Outline.h"
#include "graphics/Quad.h"
#include "graphics/RenderControl.h"
#include "graphics/ScreenSpaceReflection.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Volumetric.h"
#include "graphics/Water.h"
#include "graphics/Waterfall.h"
#include "physics/Cloth.h"
#include "physics/Fluid.h"
#include "physics/Physics.h"
#include "window/Window.h"
#include <SDL2/SDL.h>
#include <cmath>
#include <memory>
using namespace eve::physics;
using namespace eve::graphics;
TEST_CASE("softbody.cloth.fallsAndPinsHold") {
auto *mod = Physics::create();
std::unique_ptr<Cloth> cloth(mod->newCloth(8, 6, 10.f, 100.f, 40.f));
REQUIRE(cloth.get() != nullptr);
CHECK_EQ(cloth->getCols(), 8);
CHECK_EQ(cloth->getRows(), 6);
CHECK_EQ(cloth->getParticleCount(), 48);
CHECK(cloth->isPinned(0));
CHECK(cloth->isPinned(7));
// Pinned top row holds under gravity.
const float pinY0 = cloth->getParticleY(0);
const float pinX0 = cloth->getParticleX(0);
cloth->setBounds(0.f, 0.f, 800.f, 600.f);
for (int i = 0; i < 60; ++i)
cloth->update(1.f / 60.f);
CHECK(std::fabs(cloth->getParticleY(0) - pinY0) < 0.5f);
CHECK(std::fabs(cloth->getParticleX(0) - pinX0) < 0.5f);
// Free-fall: unpin everything and verify gravity moves particles down.
for (int i = 0; i < cloth->getParticleCount(); ++i)
cloth->unpin(i);
const float freeY0 = cloth->getParticleY(20);
for (int i = 0; i < 45; ++i)
cloth->update(1.f / 60.f);
CHECK_GT(cloth->getParticleY(20), freeY0 + 20.f);
}
TEST_CASE("softbody.cloth.grabMovesParticle") {
auto *mod = Physics::create();
std::unique_ptr<Cloth> cloth(mod->newCloth(6, 6, 12.f, 50.f, 50.f));
// Unpin a mid particle so it can be grabbed.
const int idx = 3 * 6 + 3;
cloth->unpin(idx);
const float x = cloth->getParticleX(idx);
const float y = cloth->getParticleY(idx);
int grabbed = cloth->grabAt(x, y, 20.f);
CHECK_EQ(grabbed, idx);
CHECK(cloth->isGrabbing());
cloth->moveGrab(x + 40.f, y + 30.f);
for (int i = 0; i < 10; ++i)
cloth->update(1.f / 60.f);
CHECK(std::fabs(cloth->getParticleX(idx) - (x + 40.f)) < 1.f);
CHECK(std::fabs(cloth->getParticleY(idx) - (y + 30.f)) < 1.f);
cloth->releaseGrab();
CHECK(!cloth->isGrabbing());
}
TEST_CASE("softbody.fluid.emitAndSettle") {
auto *mod = Physics::create();
std::unique_ptr<Fluid> fluid(mod->newFluid(256));
REQUIRE(fluid.get() != nullptr);
fluid->setBounds(0.f, 0.f, 400.f, 400.f);
fluid->setGravity(0.f, 900.f);
int added = fluid->emit(200.f, 80.f, 40, 0.f, 0.f);
CHECK_EQ(added, 40);
CHECK_EQ(fluid->getParticleCount(), 40);
float y0 = 0.f;
for (int i = 0; i < fluid->getParticleCount(); ++i)
y0 += fluid->getParticleY(i);
y0 /= float(fluid->getParticleCount());
for (int i = 0; i < 120; ++i)
fluid->update(1.f / 60.f);
float y1 = 0.f;
for (int i = 0; i < fluid->getParticleCount(); ++i)
y1 += fluid->getParticleY(i);
y1 /= float(fluid->getParticleCount());
CHECK_GT(y1, y0 + 10.f);
// Stay inside container.
for (int i = 0; i < fluid->getParticleCount(); ++i) {
CHECK_GE(fluid->getParticleX(i), -1.f);
CHECK_LE(fluid->getParticleX(i), 401.f);
CHECK_GE(fluid->getParticleY(i), -1.f);
CHECK_LE(fluid->getParticleY(i), 401.f);
}
}
TEST_CASE("softbody.fluid.interactRepels") {
auto *mod = Physics::create();
std::unique_ptr<Fluid> fluid(mod->newFluid(128));
fluid->setBounds(0.f, 0.f, 300.f, 300.f);
fluid->setGravity(0.f, 0.f);
fluid->emit(150.f, 150.f, 25, 0.f, 0.f);
float avgDist0 = 0.f;
for (int i = 0; i < fluid->getParticleCount(); ++i) {
const float dx = fluid->getParticleX(i) - 150.f;
const float dy = fluid->getParticleY(i) - 150.f;
avgDist0 += std::sqrt(dx * dx + dy * dy);
}
avgDist0 /= float(fluid->getParticleCount());
for (int i = 0; i < 45; ++i) {
fluid->interactAt(150.f, 150.f, 80.f, -4000.f);
fluid->update(1.f / 60.f);
}
float avgDist1 = 0.f;
for (int i = 0; i < fluid->getParticleCount(); ++i) {
const float dx = fluid->getParticleX(i) - 150.f;
const float dy = fluid->getParticleY(i) - 150.f;
avgDist1 += std::sqrt(dx * dx + dy * dy);
}
avgDist1 /= float(fluid->getParticleCount());
CHECK_GT(avgDist1, avgDist0 + 2.f);
}
TEST_CASE("softbody.render.clothAndFluidPreview") {
auto *win = eve::window::Window::create();
auto *gfx = Graphics::create();
REQUIRE(win != nullptr);
REQUIRE(gfx != nullptr);
eve::window::WindowSettings s;
s.width = 720;
s.height = 420;
s.centered = true;
REQUIRE(win->setWindowSettings(s));
auto *mod = Physics::create();
std::unique_ptr<Cloth> cloth(mod->newCloth(14, 10, 12.f, 40.f, 30.f));
cloth->setBounds(0.f, 0.f, 720.f, 420.f);
cloth->setColor(0.78f, 0.84f, 0.98f, 1.f);
std::unique_ptr<Fluid> fluid(mod->newFluid(400));
fluid->setBounds(360.f, 40.f, 320.f, 340.f);
fluid->setGravity(0.f, 980.f);
fluid->setColor(0.2f, 0.55f, 0.95f, 0.9f);
fluid->emit(520.f, 90.f, 120, 0.f, 40.f);
gfx->setBackgroundColorRGBA(0.07f, 0.08f, 0.11f, 1.f);
for (int frame = 0; frame < 90; ++frame) {
if (frame == 30)
cloth->grabAt(cloth->getParticleX(5 * 14 + 7), cloth->getParticleY(5 * 14 + 7), 30.f);
if (frame >= 30 && frame < 60)
cloth->moveGrab(180.f + float(frame - 30) * 2.f, 220.f);
if (frame == 60)
cloth->releaseGrab();
if ((frame % 15) == 0)
fluid->emit(500.f, 70.f, 8, 20.f, 60.f);
fluid->interactAt(520.f, 200.f, 70.f, -2500.f);
cloth->update(1.f / 60.f);
fluid->update(1.f / 60.f);
gfx->clearScreen();
// Fluid tank outline.
gfx->drawSolidRectRGBA(360.f, 40.f, 320.f, 340.f, 0.12f, 0.14f, 0.18f, 1.f);
cloth->draw(gfx);
fluid->draw(gfx);
gfx->present();
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) break;
}
SDL_Delay(16);
}
CHECK_GT(cloth->getParticleY(5 * 14 + 7), 40.f);
CHECK_GT(fluid->getParticleCount(), 100);
win->close();
}