/* * SourceGLCanvas.cpp * * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com * (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "SourceGLCanvas.h" #include "MainWindow.h" SourceGLCanvas::SourceGLCanvas(QWidget* parent) : MapperGLCanvas(parent) { } Shape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) { if (mappingId == NULL_UID) return NULL; else { Mapping::ptr mapping = MainWindow::getInstance().getMappingManager().getMappingById(mappingId); // TODO: this is real shit... : we should at the very least use some dynamic casting or (better) implement a correct // class architecture to suit our needs if (mapping->getType().endsWith("_texture")) { std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(mapping); Q_CHECK_PTR(textureMapping); return textureMapping->getInputShape().get(); } else return NULL; } } //Quad& SourceGLCanvas::getQuad() //{ // std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); // Q_CHECK_PTR(textureMapping); // // std::tr1::shared_ptr inputQuad = std::tr1::static_pointer_cast(textureMapping->getInputShape()); // Q_CHECK_PTR(inputQuad); // // return (*inputQuad); //} void SourceGLCanvas::doDraw(QPainter* painter) { if (!MainWindow::getInstance().hasCurrentPaint()) return; uint paintId = MainWindow::getInstance().getCurrentPaintId(); // Retrieve paint (as texture) and draw it. Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaintById(paintId); Q_CHECK_PTR(paint); // Retrieve all mappings associated to paint. std::map mappings = MainWindow::getInstance().getMappingManager().getPaintMappingsById(paintId); if (paint->getType() == "color") _drawColor(painter, paint, mappings); else _drawTexture(painter, paint, mappings); } void SourceGLCanvas::_drawColor(QPainter* painter, Paint::ptr paint, std::map mappings) { // Not doing anything for now. Q_UNUSED(painter); Q_UNUSED(paint); Q_UNUSED(mappings); } void SourceGLCanvas::_drawTexture(QPainter* painter, Paint::ptr paint, std::map mappings) { Q_UNUSED(painter); std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(paint); Q_CHECK_PTR(texture); // Load texture if needed. if (texture->getTextureId() == 0) { texture->loadTexture(); } // Draw the texture. glPushMatrix(); // Enable blending mode (for alphas). glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable (GL_LIGHTING); glEnable (GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->getWidth(), texture->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->getBits()); //std::cout << texture->getX() << "x" << texture->getY() << " : " << texture->getWidth() << "x" << texture->getHeight() << " " << texture->getTextureId() << std::endl; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // TODO: Exact projection of texture // see http://stackoverflow.com/questions/15242507/perspective-correct-texturing-of-trapezoid-in-opengl-es-2-0 // Draw source texture (not moving) in the center of the area. // float centerX = (float) width() / 2.0f; // float centerY = (float) height() / 2.0f; // float textureHalfWidth = (float) texture->getWidth() / 2.0f; // float textureHalfHeight = (float) texture->getHeight() / 2.0f; glColor4f (1.0f, 1.0f, 1.0f, 1.0f); // FIXME: Does this draw the quad counterclockwise? glBegin (GL_QUADS); { Util::correctGlTexCoord(0, 0); glVertex3f (texture->getX(), texture->getY(), 0); Util::correctGlTexCoord(1, 0); glVertex3f (texture->getX()+texture->getWidth(), texture->getY(), 0); Util::correctGlTexCoord(1, 1); glVertex3f (texture->getX()+texture->getWidth(), texture->getY() + texture->getHeight(), 0); Util::correctGlTexCoord(0, 1); glVertex3f (texture->getX(), texture->getY() + texture->getHeight(), 0); } glEnd (); glDisable(GL_TEXTURE_2D); for (std::map::iterator it = mappings.begin(); it != mappings.end(); ++it) { // TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture. // Il faut changer ca. std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(it->second); Q_CHECK_PTR(textureMapping); std::tr1::shared_ptr inputShape = std::tr1::static_pointer_cast(textureMapping->getInputShape()); Q_CHECK_PTR(inputShape); if (it->first == MainWindow::getInstance().getCurrentMappingId()) { Mapper::ptr mapper = MainWindow::getInstance().getMapperByMappingId(it->first); Q_CHECK_PTR(mapper); std::tr1::shared_ptr textureMapper = std::tr1::static_pointer_cast(mapper); Q_CHECK_PTR(textureMapper); textureMapper->drawInputControls(painter); } } glPopMatrix(); }