PDA

View Full Version : Tiling with OpenGL and textures



soxs060389
25th September 2009, 17:32
I try to make a wall of pictures (no overlapping).
For this case I made simple proof of concept example code, but I encounterd one major drawback:
This solution renders ONLY the last texture and replaces all previuosly loaded ons with that one.
(Code compiles just fine)



#include <QtCore>
#include <QImage>
#include <QApplication>
#include <QtOpenGL>
#include <QtDebug>

class Tile {
public:
Tile(GLfloat x, GLfloat y, QString path) : gl_x(x), gl_y(y) {
QImage t;
QImage b;
b.load(path);
t = QGLWidget::convertToGLFormat( b );
glGenTextures( 1, &texid );
qDebug() << "texid tile = " << texid;
glBindTexture( GL_TEXTURE_2D, texid );
glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
virtual ~Tile(){
glDeleteTextures(1,&texid);
}
void draw(){
glPushMatrix();
glTranslatef(64.0f * gl_x, 64.0f * gl_y, 0.0f); // just to avoid total overlapping
glBindTexture(1,texid);
qDebug() << "draw texid" << texid;
glBegin(GL_QUADS);
glTexCoord2f(1.0f,1.0f);
glVertex2f(0.0f,0.0f);

glTexCoord2f(1.0f,0.0f);
glVertex2f(0.0f,128.0f);

glTexCoord2f(0.0f,0.0f);
glVertex2f(128.0f,128.0f);

glTexCoord2f(0.0f,1.0f);
glVertex2f(128.0f,0.0f);
glEnd();
glPopMatrix();
}
private:
GLfloat gl_x;
GLfloat gl_y;
GLuint texid;

};


// THE GL WIDGET CLASS
class GLTest : public QGLWidget
{
public:
GLTest(QWidget* parent = 0);
virtual ~GLTest();
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();

QList<Tile*> tiles;
};

GLTest::GLTest(QWidget* parent) : QGLWidget(parent)
{

}
GLTest::~GLTest()
{
for (QList<Tile*>::iterator i = tiles.begin(); i!=tiles.end(); ++i)
{
delete (*i);
}
}

void GLTest::initializeGL()
{
glClearColor(0.5f,0.5f,0.5f,1.0f);
glEnable(GL_TEXTURE_2D);
qDebug() << "init GL ";
tiles.append(new Tile(0.0f, 2.0f, QString("./images/side1.png")));
tiles.append(new Tile(1.0f, 3.0f, QString("./images/side2.png")));
tiles.append(new Tile(3.0f, 4.0f, QString("./images/side4.png")));

}

void GLTest::resizeGL(int w, int h)
{

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, w, h, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w, h);
glLoadIdentity();
}

void GLTest::paintGL()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0f,1.0f,1.0f);

for (QList<Tile*>::iterator i = tiles.begin(); i!=tiles.end(); ++i)
{
(*i)->draw();
}

}

int main(int argc, char** argv)
{
QApplication app(argc, argv);
GLTest gltest;
gltest.show();
return app.exec();
}


Thx for any help and suggestions.

soxs060389
25th September 2009, 21:06
Replace line 27 with
glBindTexture(GL_TEXTURE_"D,texid); and it works.. this nasty thingy made me hicup 2 days -.-:mad::crying: