diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index 053a843..598c98a 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -91,20 +91,43 @@ void DestinationGLCanvas::doDraw() { glColor4f(0.0f, 0.0f, 0.7f, 1.0f); - // Destination quad. - // Source quad. - glLineWidth(5); - glBegin (GL_LINE_STRIP); + if (dynamic_cast(shape)) { - for (int i = 0; i < shape->nVertices()+1; i++) + Mesh* mesh = (Mesh*)shape; + + std::vector quads = mesh->getQuads(); + for (std::vector::const_iterator it = quads.begin(); it != quads.end(); ++it) { - glVertex2f( - shape->getVertex(i % shape->nVertices()).x, - shape->getVertex(i % shape->nVertices()).y - ); + glLineWidth(1); + glBegin (GL_LINE_STRIP); + for (int i = 0; i < it->nVertices()+1; i++) + { + glVertex2f( + it->getVertex(i % it->nVertices()).x, + it->getVertex(i % it->nVertices()).y + ); + } + glEnd (); } + + } + else + { + // Destination quad. + // Source quad. + glLineWidth(5); + glBegin (GL_LINE_STRIP); + { + for (int i = 0; i < shape->nVertices()+1; i++) + { + glVertex2f( + shape->getVertex(i % shape->nVertices()).x, + shape->getVertex(i % shape->nVertices()).y + ); + } + } + glEnd (); } - glEnd (); } glPopMatrix(); diff --git a/Mapper.cpp b/Mapper.cpp index 0f5435a..4b4487b 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -134,30 +134,61 @@ void TextureMapper::draw() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - if (inputShape->nVertices() == 4) - glBegin(GL_QUADS); - else if (inputShape->nVertices() == 3) - glBegin(GL_TRIANGLES); - else - // TODO: untested - glBegin(GL_POLYGON); - + if (std::tr1::dynamic_pointer_cast(outputShape)) { - for (int i = 0; i < inputShape->nVertices(); i++) + std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); + std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); + std::vector > outputQuads = outputMesh->getQuads2d(); + std::vector > inputQuads = inputMesh->getQuads2d(); + for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) { - Util::correctGlTexCoord( - (inputShape->getVertex(i).x - texture->getX()) / (GLfloat) texture->getWidth(), - (inputShape->getVertex(i).y - texture->getY()) / (GLfloat) texture->getHeight()); - glVertex2f( - outputShape->getVertex(i).x, - outputShape->getVertex(i).y - ); + for (int y = 0; y < outputMesh->nVerticalQuads(); y++) + { + Quad& outputQuad = outputQuads[x][y]; + Quad& inputQuad = inputQuads[x][y]; + glBegin(GL_QUADS); + for (int i = 0; i < 4; i++) + { + Util::correctGlTexCoord( + (inputQuad.getVertex(i).x - texture->getX()) / (GLfloat) texture->getWidth(), + (inputQuad.getVertex(i).y - texture->getY()) / (GLfloat) texture->getHeight()); + glVertex2f( + outputQuad.getVertex(i).x, + outputQuad.getVertex(i).y + ); + } + glEnd(); + } } + + } + else + { + if (std::tr1::dynamic_pointer_cast(outputShape)) + glBegin(GL_QUADS); + else if (std::tr1::dynamic_pointer_cast(outputShape)) + glBegin(GL_TRIANGLES); + else + // TODO: untested + glBegin(GL_POLYGON); + { + for (int i = 0; i < inputShape->nVertices(); i++) + { + Util::correctGlTexCoord( + (inputShape->getVertex(i).x - texture->getX()) / (GLfloat) texture->getWidth(), + (inputShape->getVertex(i).y - texture->getY()) / (GLfloat) texture->getHeight()); + glVertex2f( + outputShape->getVertex(i).x, + outputShape->getVertex(i).y + ); + } + } + glEnd(); } - glEnd(); glDisable(GL_TEXTURE_2D); } + void TextureMapper::_buildShapeProperty(QtProperty* shapeItem, Shape* shape) { for (int i=0; inVertices(); i++) diff --git a/Shape.h b/Shape.h index 6f63384..355f0bf 100644 --- a/Shape.h +++ b/Shape.h @@ -20,7 +20,11 @@ #ifndef SHAPE_H_ #define SHAPE_H_ +#include +#include #include +#include + #include #include @@ -32,6 +36,12 @@ struct Point double x; double y; Point(double x_, double y_) : x(x_), y(y_) {} + Point(const QPointF& p) : x(p.x()), y(p.y()) {} + + QPointF toQPointF() const + { + return QPointF(x, y); + } }; /** @@ -41,7 +51,6 @@ class Shape { public: typedef std::tr1::shared_ptr ptr; - std::vector vertices; Shape() {} Shape(std::vector vertices_) : vertices(vertices_) @@ -52,7 +61,7 @@ public: int nVertices() const { return vertices.size(); } - const Point& getVertex(int i) + Point getVertex(int i) const { return vertices[i]; } @@ -122,6 +131,9 @@ public: { return vertices.size(); } + +protected: + std::vector vertices; }; /** @@ -157,4 +169,223 @@ public: virtual ~Triangle() {} }; +class Mesh : public Quad { +public: + Mesh() : _nColumns(0), _nRows(0) { + init(1, 1); + } + Mesh(Point p1, Point p2, Point p3, Point p4, int nColumns=2, int nRows=2) + : Quad(p1, p2, p3, p4), _nColumns(0), _nRows(0) + { + Q_ASSERT(nColumns >= 2 && nRows >= 2); + init(nColumns, nRows); + } + virtual ~Mesh() {} + + void resizeVertices2d(std::vector< std::vector >& vertices2d, int nColumns, int nRows) + { + vertices2d.resize(nColumns); + for (int i=0; i > newVertices2d; + resizeVertices2d(newVertices2d, nColumns()+1, nRows()); + + // Left displacement of points already there. + float leftMoveProp = 1.0f/(nColumns()-1) - 1.0f/nColumns(); + + // Add a point at each row. + int k = nVertices(); + for (int y=0; y > newVertices2d; + resizeVertices2d(newVertices2d, nColumns(), nRows()+1); + + // Top displacement of points already there. + float topMoveProp = 1.0f/(nRows()-1) - 1.0f/nRows(); + + // Add a point at each row. + int k = nVertices(); + for (int x=0; x= 1 && columnId < nColumns()); +// +// std::vector< std::vector > newVertices2d; +// resizeVertices2d(newVertices2d, nHorizontalVertices()-1, nVerticalVertices()); +// for (int y=0; y getQuads() const + { + std::vector quads; + for (int i=0; i > getQuads2d() const + { + std::vector< std::vector > quads2d; + for (int i=0; i column; + for (int j=0; j > _vertices2d; + // Maps a vertex id to the pair of vertex ids it "splits". + std::map > _splitVertices; +}; + + #endif /* SHAPE_H_ */ diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index d0989ab..110b0c9 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -142,19 +142,44 @@ void SourceGLCanvas::doDraw() else glColor4f(1.0f, 1.0f, 1.0f, 0.5f); - // Source shape. - glLineWidth(5); - glBegin (GL_LINE_STRIP); + Shape* shape = inputShape.get(); + if (dynamic_cast(shape)) { - for (int i = 0; i < inputShape->nVertices()+1; i++) + Mesh* mesh = (Mesh*)shape; + + std::vector quads = mesh->getQuads(); + for (std::vector::const_iterator it = quads.begin(); it != quads.end(); ++it) { - glVertex2f( - inputShape->getVertex(i % inputShape->nVertices()).x, - inputShape->getVertex(i % inputShape->nVertices()).y - ); + glLineWidth(1); + glBegin (GL_LINE_STRIP); + for (int i = 0; i < it->nVertices()+1; i++) + { + glVertex2f( + it->getVertex(i % it->nVertices()).x, + it->getVertex(i % it->nVertices()).y + ); + } + glEnd (); } + + } + else + { + // Destination quad. + // Source quad. + glLineWidth(5); + glBegin (GL_LINE_STRIP); + { + for (int i = 0; i < shape->nVertices()+1; i++) + { + glVertex2f( + shape->getVertex(i % shape->nVertices()).x, + shape->getVertex(i % shape->nVertices()).y + ); + } + } + glEnd (); } - glEnd (); } glPopMatrix(); diff --git a/Util.cpp b/Util.cpp index 0e84cd2..37429da 100644 --- a/Util.cpp +++ b/Util.cpp @@ -56,9 +56,9 @@ int map_int(int value, int istart, int istop, int ostart, int ostop) return std::max(std::min(int(ret), ostop), ostart); } -Quad* createQuadForTexture(Texture* texture, int frameWidth, int frameHeight) +Mesh* createQuadForTexture(Texture* texture, int frameWidth, int frameHeight) { - return new Quad( + return new Mesh( Point(texture->getX(), texture->getY()), Point(texture->getX() + texture->getWidth(), texture->getY()), Point(texture->getX() + texture->getWidth(), texture->getY() + texture->getHeight()), diff --git a/Util.h b/Util.h index 4dbdd93..842c809 100644 --- a/Util.h +++ b/Util.h @@ -39,7 +39,7 @@ float map_float(float value, float istart, float istop, float ostart, float osto int map_int(int value, int istart, int istop, int ostart, int ostop); -Quad* createQuadForTexture(Texture* texture, int frameWidth, int frameHeight); +Mesh* createQuadForTexture(Texture* texture, int frameWidth, int frameHeight); Triangle* createTriangleForTexture(Texture* texture, int frameWidth, int frameHeight); } // end of namespace