Skip to content

Commit

Permalink
#5746: Implement a few PatchNode methods
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 18, 2021
1 parent eaea086 commit b31ce21
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/ipatch.h
Expand Up @@ -211,6 +211,12 @@ class IPatch

// Alligns the assigned texture at the given edge (if possible)
virtual void alignTexture(AlignEdge type) = 0;

// Reverts any transform that has been applied since the last time freezeTransform() was called
virtual void revertTransform() = 0;

// Promotes the current transformed state to the new base state
virtual void freezeTransform() = 0;
};

namespace patch
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/patch/Patch.h
Expand Up @@ -146,9 +146,9 @@ class Patch :
void evaluateTransform();

// Revert the changes, fall back to the saved state in <m_ctrl>
void revertTransform();
void revertTransform() override;
// Apply the transformed control array, save it into <m_ctrl> and overwrite the old values
void freezeTransform();
void freezeTransform() override;

// callback for changed control points
void controlPointsChanged() override;
Expand Down
75 changes: 65 additions & 10 deletions radiantcore/selection/textool/PatchNode.h
Expand Up @@ -20,45 +20,100 @@ class PatchNode :

void beginTransformation() override
{
_patch.undoSave();
}

void revertTransformation() override
{
_patch.revertTransform();
_patch.controlPointsChanged();
}

void applyTransformToSelected(const Matrix3& transform) override
{
// TODO
foreachVertex([&](PatchControl& vertex)
{
vertex.texcoord = transform * vertex.texcoord;
});

_patch.controlPointsChanged();
}

void commitTransformation() override
{
_patch.freezeTransform();
}

const AABB& localAABB() const
{
_bounds = AABB();

for (std::size_t col = 0; col < _patch.getWidth(); ++col)
foreachVertex([&](PatchControl& vertex)
{
for (std::size_t row = 0; row < _patch.getHeight(); ++row)
{
const auto& ctrl = _patch.ctrlAt(row, col);
_bounds.includePoint({ ctrl.texcoord.x(), ctrl.texcoord.y(), 0 });
}
}
_bounds.includePoint({ vertex.texcoord.x(), vertex.texcoord.y(), 0 });
});

return _bounds;
}

void testSelect(Selector& selector, SelectionTest& test) override
{
// TODO
test.BeginMesh(Matrix4::getIdentity(), true);

foreachVertex([&](PatchControl& vertex)
{
SelectionIntersection intersection;

test.TestPoint(Vector3(vertex.texcoord.x(), vertex.texcoord.y(), 0), intersection);

if (intersection.isValid())
{
Selector_add(selector, *this);
}
});
}

void render() override
{
// TODO
glEnable(GL_BLEND);
glBlendColor(0, 0, 0, 0.3f);
glBlendFunc(GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT);

glColor3f(1, 1, 1);

// Get the tesselation and the first
auto tess = _patch.getTesselatedPatchMesh();

auto renderInfo = _patch.getRenderIndices();
auto* strip_indices = &renderInfo.indices.front();

for (std::size_t i = 0; i < renderInfo.numStrips; i++, strip_indices += renderInfo.lenStrips)
{
glBegin(GL_QUAD_STRIP);

for (std::size_t offset = 0; offset < renderInfo.lenStrips; offset++)
{
// Retrieve the mesh vertex from the line strip
auto& meshVertex = tess.vertices[*(strip_indices + offset)];
glVertex2d(meshVertex.texcoord[0], meshVertex.texcoord[1]);
}

glEnd();
}

glDisable(GL_BLEND);
}

private:
void foreachVertex(const std::function<void(PatchControl&)>& functor) const
{
for (std::size_t col = 0; col < _patch.getWidth(); ++col)
{
for (std::size_t row = 0; row < _patch.getHeight(); ++row)
{
functor(_patch.ctrlAt(row, col));
}
}
}
};

Expand Down

0 comments on commit b31ce21

Please sign in to comment.