Fix: textures were upside down

This commit is contained in:
Tats
2013-11-24 11:02:11 -05:00
parent 3addaf0728
commit 3964f7b7d1
8 changed files with 139 additions and 52 deletions
+66
View File
@@ -0,0 +1,66 @@
/*
* Mapper.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "Mapper.h"
void QuadTextureMapper::draw()
{
std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(_mapping);
Q_CHECK_PTR(textureMapping);
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(textureMapping->getPaint());
Q_CHECK_PTR(texture);
std::tr1::shared_ptr<Quad> quad = std::tr1::static_pointer_cast<Quad>(textureMapping->getShape());
Q_CHECK_PTR(quad);
std::tr1::shared_ptr<Quad> inputQuad = std::tr1::static_pointer_cast<Quad>(textureMapping->getInputShape());
Q_CHECK_PTR(inputQuad);
printf("Texid: %d\n", texture->getTextureId());
// Project source texture and sent it to destination.
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());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor4f(1, 1, 1, 1.0f);
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());
glVertex3f( quad->getVertex(i).x,
quad->getVertex(i).y,
0);
}
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
+3 -43
View File
@@ -26,6 +26,8 @@
#include "Shape.h"
#include "Paint.h"
#include "Util.h"
#include <tr1/memory>
#include <GL/gl.h>
@@ -129,49 +131,7 @@ public:
QuadTextureMapper(TextureMapping* mapping) : Mapper(mapping) {}
virtual ~QuadTextureMapper() {}
virtual void draw() {
std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(_mapping);
Q_CHECK_PTR(textureMapping);
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(textureMapping->getPaint());
Q_CHECK_PTR(texture);
std::tr1::shared_ptr<Quad> quad = std::tr1::static_pointer_cast<Quad>(textureMapping->getShape());
Q_CHECK_PTR(quad);
std::tr1::shared_ptr<Quad> inputQuad = std::tr1::static_pointer_cast<Quad>(textureMapping->getInputShape());
Q_CHECK_PTR(inputQuad);
printf("Texid: %d\n", texture->getTextureId());
// Project source texture and sent it to destination.
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());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor4f(1, 1, 1, 1.0f);
glBegin(GL_QUADS);
{
for (int i=0; i<4; i++)
{
glTexCoord2f( ( inputQuad->getVertex(i).x - texture->getX() ) / (GLfloat) texture->getWidth(),
( inputQuad->getVertex(i).y - texture->getY() ) / (GLfloat) texture->getHeight());
glVertex3f( quad->getVertex(i).x,
quad->getVertex(i).y,
0);
}
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
virtual void draw();
};
+4 -4
View File
@@ -86,16 +86,16 @@ void SourceGLCanvas::doDraw() {
glColor4f (1, 1, 1, 1.0f);
glBegin (GL_QUADS);
{
glTexCoord2f (0, 0);
Util::correctGlTexCoord(0, 0);
glVertex3f (texture->getX(), texture->getY(), 0);
glTexCoord2f (1, 0);
Util::correctGlTexCoord(1, 0);
glVertex3f (texture->getX()+texture->getWidth(), texture->getY(), 0);
glTexCoord2f (1, 1);
Util::correctGlTexCoord(1, 1);
glVertex3f (texture->getX()+texture->getWidth(), texture->getY()+texture->getHeight(), 0);
glTexCoord2f (0, 1);
Util::correctGlTexCoord(0, 1);
glVertex3f (texture->getX(), texture->getY()+texture->getHeight(), 0);
}
glEnd ();
+2
View File
@@ -25,6 +25,8 @@
#include "MapperGLCanvas.h"
#include "DestinationGLCanvas.h"
#include "Util.h"
class SourceGLCanvas: public MapperGLCanvas
{
Q_OBJECT
+25
View File
@@ -0,0 +1,25 @@
/*
* Util.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "Util.h"
void Util::correctGlTexCoord(GLfloat x, GLfloat y)
{
glTexCoord2f (x, 1.0f - y);
}
+31
View File
@@ -0,0 +1,31 @@
/*
* Util.h
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef UTIL_H_
#define UTIL_H_
#include <GL/gl.h>
class Util {
public:
static void correctGlTexCoord(GLfloat x, GLfloat y);
};
#endif /* UTIL_H_ */
+2 -2
View File
@@ -1,7 +1,7 @@
CONFIG += qt debug
TEMPLATE = app
HEADERS = Common.h SourceGLCanvas.h DestinationGLCanvas.h MapperGLCanvas.h
SOURCES = main.cpp Common.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MapperGLCanvas.cpp
HEADERS = Common.h Util.h SourceGLCanvas.h DestinationGLCanvas.h MapperGLCanvas.h Mapper.h Shape.h Paint.h
SOURCES = main.cpp Common.cpp Util.cpp Mapper.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MapperGLCanvas.cpp
QT += gui opengl
LIBS += -lSOIL -lglut
+6 -3
View File
@@ -11,6 +11,9 @@
#include <QtGui>
#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
@@ -30,8 +33,6 @@ int main(int argc, char *argv[])
QObject::connect(&destinationCanvas, SIGNAL(quadSwitched()),
&sourceCanvas, SLOT(updateCanvas()));
Common::initializeLibremapper(320, 480);
QSplitter splitter(Qt::Horizontal);
splitter.addWidget(&sourceCanvas);
splitter.addWidget(&destinationCanvas);
@@ -39,10 +40,12 @@ int main(int argc, char *argv[])
sourceCanvas.setFocusPolicy(Qt::ClickFocus);
destinationCanvas.setFocusPolicy(Qt::ClickFocus);
splitter.setWindowTitle(QObject::tr("Libremapping"));
splitter.resize(650, 480);
splitter.resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
splitter.show();
Common::initializeLibremapper(sourceCanvas.width(), sourceCanvas.height());
return app.exec();
}