Ich hab gestern mal wieder was kleines mit OpenGL versucht zu machen, ich war aber irgendwie nicht mal mehr fähig, eine Textur auf ein Rechteck zu kleben. Ich glaub, ich steh ziemlich auf dem Schlauch.
Code:
// g++ -I$QTDIR/include -L$QTDIR/lib -o gltest textest.cpp -lqt-mt
#include <qgl.h>
#include <qapplication.h>
#include <qimage.h>
class GLTest : public QGLWidget
{
public:
GLTest(QWidget* parent = 0);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
QImage tex;
GLuint texid[1];
};
GLTest::GLTest(QWidget* parent) : QGLWidget(parent)
{
tex.load(QString("/home/axel/gfx/qt2.png"));
}
void GLTest::GLTest::initializeGL()
{
glClearColor(0.5f,0.5f,0.5f,1.0f);
glGenTextures(1, &texid[0]);
glBindTexture( GL_TEXTURE_2D, texid[0] );
glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glEnable(GL_TEXTURE_2D);
}
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);
}
void GLTest::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,texid[0]);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2i(0,0);
glTexCoord2f(0.0f,1.0f);
glVertex2i(0,tex.height());
glTexCoord2f(1.0f,1.0f);
glVertex2i(tex.width(),tex.height());
glTexCoord2f(1.0f,0.0f);
glVertex2i(tex.width(),0);
glEnd();
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
GLTest gltest;
app.setMainWidget(&gltest);
gltest.show();
return app.exec();
}
Lesezeichen