#include "util.hpp" #include #include #include <3ds.h> #include #include #include constexpr bool thorough_tests = false; constexpr float rand_case_multiplier = 0.1; struct C3DFixture { C3DFixture() { C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); } ~C3DFixture() { C3D_Fini(); } }; // TODO: Consider rewriting this with LUTs => Can check negative alignment too! TEST_CASE_METHOD(C3DFixture, "Quaternion space transformation (normal)", "") { auto light_axis = GENERATE(-1, 1, -2, 2, -3, 3); // -x/+x, -y/+y, -z/+z auto quat_x = GENERATE(-1.f, 0.f, 1.f); auto quat_y = GENERATE(-1.f, 0.f, 1.f); auto quat_z = GENERATE(-1.f, 0.f, 1.f); auto quat_w = GENERATE(-1.f, 0.f, 1.f); // Normalize the quaternion to compute reliable reference results { auto length = sqrt(quat_x * quat_x + quat_y * quat_y + quat_z * quat_z + quat_w * quat_w); if (length == 0) { length = std::numeric_limits::infinity(); } quat_x /= length; quat_y /= length; quat_z /= length; quat_w /= length; } auto size = GENERATE(64); auto config = TestQuadBuilder{}; config.Constant(RESULT_NORMALQUAT, quat_x, quat_y, quat_z, quat_w); auto quad = config.Build(size, size); quad.Begin(); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvInit(env); C3D_TexEnvSrc(env, C3D_Both, GPU_FRAGMENT_PRIMARY_COLOR, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); // TODO: Just set up a default material in TestQuad. citro3d uses this internally to multiply each light, but we want to use the raw light config anyway... C3D_Material material = { { 0.f, 0.f, 0.f }, //ambient { 1.0f, 1.0f, 1.0f }, //diffuse { 0.8f, 0.8f, 0.8f }, //specular0 { 0.0f, 0.0f, 0.0f }, //specular1 { 0.0f, 0.0f, 0.0f }, //emission }; C3D_LightEnv light_env; C3D_LightEnvInit(&light_env); C3D_LightEnvBind(&light_env); C3D_LightEnvMaterial(&light_env, &material); C3D_Light light; C3D_LightInit(&light, &light_env); const int light_sign = light_axis / abs(light_axis); C3D_FVec light_dir = FVec4_New( (abs(light_axis) == 1) * light_sign, (abs(light_axis) == 2) * light_sign, (abs(light_axis) == 3) * light_sign, 0.0f); // w=0 indicates a directional light C3D_LightColor(&light, 1.0, 1.0, 1.0); C3D_LightDiffuse(&light, 1.0, 1.0, 1.0); C3D_LightPosition(&light, &light_dir); quad.DrawAndFinish(); float expected_normal[3] = { 2 * (quat_y * quat_w + quat_x * quat_z), 2 * (quat_y * quat_z - quat_x * quat_w), 1 - 2 * (quat_x * quat_x + quat_y * quat_y), }; auto tochar = [](float a) { return (a < 0) ? '-' : (a > 0) ? '+' : '0'; }; INFO(std::string { "+-"[std::signbit(light_axis)] } + "_xyz"[abs(light_axis)] + ": " + tochar(quat_x) + tochar(quat_y) + tochar(quat_z) + tochar(quat_w)); if (quat_x == 0 && quat_y == 0 && quat_z == 0 && quat_w == 0) { // null quaternions always produce zero as the dot product CHECK(quad.GetResult(0, 0).r() == 0); } else if (quad.GetResult(0, 0).r() > 0xc0) { CHECK_THAT(expected_normal[abs(light_axis) - 1] * light_sign, Catch::Matchers::WithinAbs(1.0, 0.0001)); } else if (quad.GetResult(0, 0).r() > 0x0) { CHECK(expected_normal[abs(light_axis) - 1] * light_sign < 0.80); } else { // TODO: Check behavior for two-sided diffuse, too CHECK(expected_normal[abs(light_axis) - 1] * light_sign <= 0.0001); } }