Qt OpenGL gltexture
-
Attention:you must add glu.h or glut.h manually in you QtOpenGL file
I think one must understand though only core code related to texture is showed.@#include <QtGui>
#include <QtOpenGL>
#include <glut.h>void GLwidget::loadTextures()
{
glEnable(GL_TEXTURE_2D);
QImage text[3],buff[3];
if(!buff[0].load("./sun.bmp")
||!buff[1].load("./earth.bmp")
||!buff[2].load("./moon.bmp")){
printf("Load Image failed!\n");
}
text[0] = QGLWidget::convertToGLFormat(buff[0]);
text[1] = QGLWidget::convertToGLFormat(buff[1]);
text[2] = QGLWidget::convertToGLFormat(buff[2]);glGenTextures(3,&texture[0]);
glBindTexture( GL_TEXTURE_2D, texture[0] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, text[0].width(), text[0].height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, text[0].bits() );glBindTexture( GL_TEXTURE_2D, texture[1] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, text[1].width(), text[1].height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, text[1].bits() );glBindTexture( GL_TEXTURE_2D, texture[2] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, text[2].width(), text[2].height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, text[2].bits() );
}@
@
void GLwidget::paintGL()
{GLUquadricObj *quadratic = gluNewQuadric();
glBindTexture(GL_TEXTURE_2D, texture[0]); //the same with texture[1],[2]
glPushMatrix();
gluSphere(quadratic,sunRadius,100,100);
glPopMatrix();
}@
@
void GLwidget::initializeGL(){
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
}@