Skip to content

Commit

Permalink
#5767: Unit tests for flipping face and patch textures
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 1, 2021
1 parent d850f7c commit 9e9a81f
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 94 deletions.
85 changes: 85 additions & 0 deletions test/TextureManipulation.cpp
Expand Up @@ -2,6 +2,7 @@

#include "algorithm/Scene.h"
#include "algorithm/Primitives.h"
#include "scenelib.h"

namespace test
{
Expand Down Expand Up @@ -82,4 +83,88 @@ TEST_F(TextureManipulationTest, FaceRotateTexDef)
EXPECT_TRUE(algorithm::faceHasVertex(face, Vector3(-64, -64, -288), Vector2(0.146447, 65.1124)));
}

namespace
{

void performFaceFlipTest(int axis)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto brush = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), "textures/numbers/1");
scene::addNodeToContainer(brush, worldspawn);
Node_setSelected(brush, true);

auto faceUp = algorithm::findBrushFaceWithNormal(Node_getIBrush(brush), Vector3(0, 0, 1));

std::vector<Vector2> oldTexCoords;
for (const auto& vertex : faceUp->getWinding())
{
oldTexCoords.push_back(vertex.texcoord);
}

auto cmd = axis == 0 ? "FlipTextureX" : "FlipTextureY";
GlobalCommandSystem().executeCommand(cmd);

auto faceUvBounds = algorithm::getTextureSpaceBounds(*faceUp);

// Every face vertex should have been flipped about the bounds origin
auto old = oldTexCoords.begin();
for (const auto& vertex : faceUp->getWinding())
{
// Calculate the mirrored coordinate
auto expectedTexcoord = *(old++);
expectedTexcoord[axis] = 2 * faceUvBounds.origin[axis] - expectedTexcoord[axis];

EXPECT_EQ(vertex.texcoord.x(), expectedTexcoord.x()) << "Mirrored vertex should be at " << expectedTexcoord;
EXPECT_EQ(vertex.texcoord.y(), expectedTexcoord.y()) << "Mirrored vertex should be at " << expectedTexcoord;
}
}

void performPatchFlipTest(int axis)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB(Vector3(4, 50, 60), Vector3(64, 128, 256)), "textures/numbers/1");

auto patch = Node_getIPatch(patchNode);
patch->scaleTextureNaturally();
patch->controlPointsChanged();

Node_setSelected(patchNode, true);

// Get the texture space bounds of this patch
auto patchBounds = algorithm::getTextureSpaceBounds(*patch);
auto bounds = patchBounds;
bounds.extents *= 1.2f;

std::vector<Vector2> oldTexCoords;
algorithm::foreachPatchVertex(*patch, [&](const PatchControl& ctrl) { oldTexCoords.push_back(ctrl.texcoord); });

auto cmd = axis == 0 ? "FlipTextureX" : "FlipTextureY";
GlobalCommandSystem().executeCommand(cmd);

// Every changed vertex should have been flipped about the bounds origin
algorithm::expectVerticesHaveBeenFlipped(axis, *patch, oldTexCoords, { patchBounds.origin.x(), patchBounds.origin.y() });
}

}

TEST_F(TextureManipulationTest, FaceFlipS)
{
performFaceFlipTest(0);
}

TEST_F(TextureManipulationTest, FaceFlipT)
{
performFaceFlipTest(1);
}

TEST_F(TextureManipulationTest, PatchFlipS)
{
performPatchFlipTest(0);
}

TEST_F(TextureManipulationTest, PatchFlipT)
{
performPatchFlipTest(1);
}

}

0 comments on commit 9e9a81f

Please sign in to comment.