Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/ShipLightSprite11Tests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1// Included by ClientRenderer11Tests.cpp to exercise the production flare setup.
2void ShipLightSpriteTests(Window11& host)
3{
4 constexpr unsigned extent = 384;
5 HR(host.Resize(extent, extent), "ship flare viewport");
6 RenderDevice device(host.Draws());
7 HR(device.BeginScene(), "begin ship flare fixture");
8 ComPtr<RenderTexture> texture;
9 HR(LoadTexture(&device, L"Textures/pointsprite.dds", &texture), "shared ship flare texture");
10 HR(device.SetFVF(VertexFormat::XYZ | VertexFormat::DIFFUSE), "ship light vertex layout");
11 HR(device.SetRenderState(RenderState::ZENABLE, TRUE), "ship flare depth test");
12 HR(device.SetRenderState(RenderState::STENCILENABLE, FALSE), "fixture stencil");
13 HR(device.SetRenderState(RenderState::TEXTUREFACTOR, 0xffffffff), "white strobe");
14 struct Vertex { float x, y, z; DWORD color; };
15 const Vertex source{0, 0, 0, 0xffffffff};
16 RiseMath::Matrix projection;
17 RiseMath::MatrixPerspectiveFovRH(&projection, RiseMath::Pi / 3, 1, .1f, 10.f);
18 HR(device.SetTransform(TransformSlot::PROJECTION, &projection), "external camera projection");
19 const auto draw = [&](float size, float depth = 1.f) {
20 HR(device.Clear(0, nullptr, ClearFlags::TARGET | ClearFlags::ZBUFFER, 0xff000000, depth, 0), "clear ship flare frame");
21 HR(device.SetRenderState(RenderState::POINTSIZE, ShipLightSprites::SizeBits(size)), "ship flare diameter");
22 HR(device.DrawPrimitiveUP(Primitive::PointList, 1, &source, sizeof(source)), "shared point billboard draw");
23 return Pixels(host.Device());
24 };
25 const auto lit = [](const std::vector<uint32_t>& pixels) {
26 return std::count_if(pixels.begin(), pixels.end(), [](uint32_t p) { return (p & 0xffffff) != 0; });
27 };
28 std::filesystem::create_directories("outputs/ship-light-sprites");
29 const auto save = [&](const wchar_t* path) {
30 ComPtr<RenderSurface> surface;
31 HR(device.GetBackBuffer(0, 0, 0, &surface), "flare capture surface");
32 HR(SaveSurfaceToFile(path, ImageFile::PNG, surface.Get(), nullptr, nullptr), "save flare GPU image");
33 };
34 const auto bright = [](const std::vector<uint32_t>& pixels, unsigned threshold) {
35 return std::count_if(pixels.begin(), pixels.end(), [=](uint32_t p) { return (p & 255) >= threshold; });
36 };
37 for (float facing : {-1.f, 0.f, .01f, .3f, 1.f})
38 {
39 Check(ShipLightSprites::Size(128, .5f, facing) == 256,
40 "visible strobe retains its diameter from a grazing or tail camera");
41 Check(ShipLightSprites::Size(128, 1, facing) == 128,
42 "navigation bulbs are omnidirectional");
43 Check(ShipLightSprites::Size(128, 1.5f, facing) == 256,
44 "beacon visibility is not restricted to an emission cone");
45 }
46 Check(ShipLightSprites::Size(128, 2, -1) == 0 && ShipLightSprites::Size(128, 2, 0) == 0
47 && ShipLightSprites::Size(128, 2, 1) == 512 && ShipLightSprites::Size(128, 2, .5f) == 2,
48 "only beams retain their directional angular falloff");
49 std::vector<uint32_t> reference, glareReference;
50 for (float angle : {0.f, .7f, 1.8f})
51 {
52 const RiseMath::Vector3 eye(3 * std::cos(angle), 1, 3 * std::sin(angle)), target(0, 0, 0), up(0, 1, 0);
54 RiseMath::MatrixLookAtRH(&view, &eye, &target, &up);
55 HR(device.SetTransform(TransformSlot::VIEW, &view), "orbit external camera");
56 ShipLightSprites::Bind(device, texture.Get());
57 HR(device.SetRenderState(RenderState::POINTSIZE_MAX, ShipLightSprites::SizeBits(64)), "reproduce old native clamp");
58 const auto old = draw(256);
59 if (angle == 0) save(L"outputs/ship-light-sprites/old-clamp.png");
60 ShipLightSprites::Bind(device, texture.Get());
61 const auto flare = draw(256);
62 if (angle == 0) save(L"outputs/ship-light-sprites/corrected-flare.png");
63 Check(lit(flare) > 8 * lit(old), "near ship flare retains its authored extent instead of the 64-pixel clamp");
64 Check(lit(flare) < 256 * 256, "flare texture has transparent dark borders, not a solid billboard");
65 if (reference.empty()) reference = flare;
66 else Check(flare == reference, "flare stays camera-facing while orbiting the ship");
67 Check(draw(512) == flare, "ship flare remains bounded to 256 pixels");
68 Check(lit(draw(256, 0)) == 0, "intervening opaque hull or building occludes ship flare");
69
70 // Prior scene passes may leave unrelated texture/blend/point state.
71 HR(device.SetRenderState(RenderState::POINTSPRITEENABLE, FALSE), "dirty point UV state");
72 HR(device.SetRenderState(RenderState::ALPHABLENDENABLE, FALSE), "dirty blend state");
73 HR(device.SetRenderState(RenderState::ALPHATESTENABLE, TRUE), "dirty alpha test");
74 HR(device.SetRenderState(RenderState::ALPHAFUNC, Compare::NEVER), "reject stale alpha");
75 HR(device.SetTextureStageState(0, TextureState::COLORARG1, TextureArgument::DIFFUSE), "dirty flare texture argument");
76 HR(device.SetTextureStageState(1, TextureState::COLOROP, TextureOperation::SELECTARG1), "dirty second texture stage");
77 HR(device.SetTextureStageState(1, TextureState::COLORARG1, TextureArgument::DIFFUSE), "dirty second stage argument");
78 ShipLightSprites::Bind(device, texture.Get());
79 Check(draw(256) == flare, "ship flare explicitly establishes texture, billboard and blend state");
80 std::cout << "Ship flare camera=" << angle << ": old=" << lit(old) << " corrected=" << lit(flare) << " lit pixels\n";
81
82 const auto flash = [&](float transmission, float depth = 1.f, bool enabled = true) {
83 ShipLightSprites::StrobeGlare glare(device, RiseMath::Color(1, 1, 1, 1), transmission, enabled);
84 return draw(ShipLightSprites::Size(128, .5f, 0), depth);
85 };
86 const auto strobe = flash(1);
87 if (angle == 0) save(L"outputs/ship-light-sprites/bright-strobe.png");
88 Check(bright(strobe, 128) > 4 * bright(flare, 128), "strobe has a substantially broader bright core");
89 Check(bright(strobe, 16) > 2 * bright(flare, 16), "strobe retains a strong soft halo");
90 if (glareReference.empty()) glareReference = strobe;
91 else Check(strobe == glareReference, "exposed strobe remains bright across camera directions");
92 Check(draw(256) == flare, "strobe glare restores subsequent ordinary navigation lamp pixels");
93 Check(flash(1, 1, false) == flare, "ordinary bulbs retain baseline flare brightness");
94 Check(lit(flash(0)) == 0, "opaque cloud or fog completely extinguishes even a saturated strobe");
95 Check(lit(flash(1, 0)) == 0, "hull and buildings still occlude strong strobe glare");
96 for (float transmission : {.01f, .25f, .5f})
97 {
98 const auto attenuated = flash(transmission);
99 bool matches = true;
100 for (size_t i = 0; i < strobe.size(); ++i)
101 matches &= std::abs(float(attenuated[i] & 255) - float(strobe[i] & 255) * transmission) <= 1.f;
102 Check(matches, "cloud transmission applies once, including the saturated strobe core");
103 }
104 std::cout << "Strobe camera=" << angle << ": bright core=" << bright(flare, 128) << " -> " << bright(strobe, 128)
105 << ", halo=" << bright(flare, 16) << " -> " << bright(strobe, 16) << " pixels\n";
106 }
107 HR(device.SetRenderState(RenderState::TEXTUREFACTOR, 0xffff0000), "red navigation lamp");
108 const auto red = draw(256);
109 Check(lit(red) > 100, "navigation lamp also uses the flare texture");
110 Check(std::all_of(red.begin(), red.end(), [](uint32_t pixel) { return (pixel & 0xffff) == 0; }),
111 "navigation flare retains authored red color");
112 HR(device.EndScene(), "finish ship flare fixture");
113 DebugClean(host.Device());
114 HR(host.Resize(64, 48), "restore client test viewport");
115}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
HRESULT SaveSurfaceToFile(const wchar_t *path, ImageFile format, RenderSurface *surface, const void *, const RECT *crop)
matches(path, entry)
constexpr float Pi
Definition ClientMath.h:12
Matrix * MatrixLookAtRH(Matrix *out, const Vector3 *eye, const Vector3 *at, const Vector3 *up)
Definition ClientMath.h:141
Matrix * MatrixPerspectiveFovRH(Matrix *out, float fov, float aspect, float nearZ, float farZ)
Definition ClientMath.h:151
void Bind(Rendering::RenderDevice &device, Rendering::RenderTexture *texture)
float Size(float baseSize, float power, float facing)
DWORD SizeBits(float value)
path
Definition erin.py:153