From f23ce800fc1c27b3c4e2ee25a234427ea22228ab Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 16:58:29 -0600 Subject: [PATCH] Support for mesh textures. --- Mapper.cpp | 70 +++++++++++++++++++++++++++++++++++++++++------------- Mapper.h | 18 ++++++++++---- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 52225b4..e9cbbae 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -171,6 +171,10 @@ void ShapeGraphicsItem::_syncShape() QGraphicsItem* child = children.at(i); _shape->setVertex(i, child->scenePos()); } + + // The shape object is the model: it contains the logic to make sure the vertices are ok. + // So here we need to re-sync the vertices (view side) according to the model. + _syncVertices(); } void ShapeGraphicsItem::_syncVertices() @@ -420,29 +424,53 @@ void TriangleTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected glEnd(); } } + +void MeshTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected) +{ + Q_UNUSED(painter); + Q_UNUSED(selected); + if (isOutput()) { - if (isMappingCurrent()) + std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(_shape); + std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(_inputShape); + QVector > outputQuads = outputMesh->getQuads2d(); + QVector > inputQuads = inputMesh->getQuads2d(); + for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) { - // FIXME: Does this draw the quad counterclockwise? - glBegin (GL_QUADS); + for (int y = 0; y < outputMesh->nVerticalQuads(); y++) { - QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); - - Util::correctGlTexCoord(0, 0); - glVertex3f (rect.x(), rect.y(), 0); - - Util::correctGlTexCoord(1, 0); - glVertex3f (rect.x() + rect.width(), rect.y(), 0); - - Util::correctGlTexCoord(1, 1); - glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); - - Util::correctGlTexCoord(0, 1); - glVertex3f (rect.x(), rect.y()+rect.height(), 0); + Quad& outputQuad = outputQuads[x][y]; + Quad& inputQuad = inputQuads[x][y]; + glBegin(GL_QUADS); + for (int i = 0; i < outputQuad.nVertices(); i++) + { + Util::setGlTexPoint(*_texture, inputQuad.getVertex(i), mapFromScene(outputQuad.getVertex(i))); + } + glEnd(); } - glEnd (); } } + +} + +void MeshTextureGraphicsItem::_doDrawControls(QPainter* painter) +{ + Mesh* mesh = static_cast(_shape.get()); + Q_ASSERT(mesh); + + // Init colors and stroke. + painter->setPen(MM::SHAPE_INNER_STROKE); + + // Draw inner quads. + QVector quads = mesh->getQuads(); + for (QVector::const_iterator it = quads.begin(); it != quads.end(); ++it) + { + painter->drawPolygon(mapFromScene(it->toPolygon())); + } + + // Draw outer quad. + painter->setPen(MM::SHAPE_STROKE); + painter->drawPolygon(mapFromScene(mesh->toPolygon())); } Mapper::Mapper(Mapping::ptr mapping) @@ -753,6 +781,9 @@ TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) { + _graphicsItem = new MeshTextureGraphicsItem(_mapping, true); + _inputGraphicsItem = new MeshTextureGraphicsItem(_mapping, false); + // Add mesh sub property. Mesh* mesh = (Mesh*)textureMapping->getShape().get(); _meshItem = _variantManager->addProperty(QVariant::Size, QObject::tr("Dimensions")); @@ -776,6 +807,11 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) outputMesh->resize(size.width(), size.height()); inputMesh->resize(size.width(), size.height()); + _graphicsItem->resetVertices(); + _inputGraphicsItem->resetVertices(); + + // TODO: here we need to create the graphicsitems + emit valueChanged(); } } diff --git a/Mapper.h b/Mapper.h index 0904fb6..f0608c7 100644 --- a/Mapper.h +++ b/Mapper.h @@ -202,6 +202,16 @@ public: virtual void _doDrawOutput(QPainter* painter, bool selected); }; + +/// Graphics item for textured mesh. +class MeshTextureGraphicsItem : public PolygonTextureGraphicsItem +{ +public: + MeshTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} + virtual ~MeshTextureGraphicsItem(){} + + virtual void _doDrawOutput(QPainter* painter, bool selected); + virtual void _doDrawControls(QPainter* painter); }; /** @@ -230,10 +240,10 @@ public: /// Returns a pointer to the properties editor for that mapper. virtual QWidget* getPropertiesEditor(); - virtual QGraphicsItem* getGraphicsItem() { + virtual ShapeGraphicsItem* getGraphicsItem() { return _graphicsItem; } - virtual QGraphicsItem* getInputGraphicsItem() { + virtual ShapeGraphicsItem* getInputGraphicsItem() { return _inputGraphicsItem; } @@ -255,8 +265,8 @@ protected: std::map > _propertyToVertex; - QGraphicsItem* _graphicsItem; - QGraphicsItem* _inputGraphicsItem; + ShapeGraphicsItem* _graphicsItem; + ShapeGraphicsItem* _inputGraphicsItem; // FIXME: use typedefs, member of the class for type names that are too long to type: MShape::ptr outputShape;