前言:
环境是 Qt 5.9.2 Mingw 32 bit
Qt 提供了 opengl 的窗口 是 QGLWidget
所以不需要 glut
这个纹理贴图是我随便找的一个 请忽略
Qt opengl 的环境搭建也很简单
pro 文件内 加入
QT += opengl
LIBS += -lopengl32 \
-lglu32
这样就ok
继承 QGLWidget 然后实现一下那几个虚函数就行
这里我就不叙述了 直接贴代码
openglwidget.h
#include <QWidget>
#include <QtOpenGL/qgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <QTimer>
namespace Ui {
class openglWidget;
}
class openglWidget : public QGLWidget
{
Q_OBJECT
public:
explicit openglWidget(QWidget *parent = 0);
~openglWidget();
void loadTextures(QString path);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
Ui::openglWidget *ui;
GLfloat triangleRotateAngle;
GLfloat quadrangleRotateAngle;
GLuint m_texture; //纹理
QTimer* m_timer;
};
openglwidget.cpp
openglWidget::openglWidget(QWidget *parent) :
QGLWidget(parent),
ui(new Ui::openglWidget)
{
ui->setupUi(this);
triangleRotateAngle = 0.0f;
quadrangleRotateAngle = 0.0f;
m_timer = new QTimer();
//定时器每0.1秒 更新一下
connect(m_timer,&QTimer::timeout,this,&openglWidget::updateGL);
m_timer->setInterval(100);
m_timer->start();
initializeGL();
}
openglWidget::~openglWidget()
{
delete ui;
}
void openglWidget::loadTextures(QString path)
{
QImage tex, buffer;
glGenTextures(1, &m_texture);
buffer.load(path);
tex = QGLWidget::convertToGLFormat( buffer );
glBindTexture( GL_TEXTURE_2D, m_texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
void openglWidget::initializeGL()
{
//绑定位图转换为纹理
loadTextures("D://1.bmp");
//纹理映射
glEnable(GL_TEXTURE_2D);
glShadeModel( GL_SMOOTH );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void openglWidget::resizeGL(int w, int h)
{
if (0 >= w || 0 >= h )
return;
glLoadIdentity();
glViewport(0, 0, w, h);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0 );
glMatrixMode( GL_MODELVIEW );
updateGL();
}
void openglWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
/*重置当前的模型观察矩阵
*
* 当您调用glLoadIdentity()之后,您实际上将当前点移到了屏幕中心,
* X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。OpenGL屏幕中心的坐标值是X和Y轴上的0.0点。
* 中心左面的坐标值是负值,右面是正值。移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值
*
* */
glLoadIdentity();
glTranslatef( 0.0, 0.0, -5.0 );
glRotatef( triangleRotateAngle, 0.0, 1.0, 0.0 );
glRotatef( triangleRotateAngle, 1.0, 0.0, 0.0 );
glBindTexture(GL_TEXTURE_2D,m_texture);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f); //右上(顶面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); //左上(顶面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); //左下(顶面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f); //右下(顶面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f); //右上(底面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); //左上(底面)
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); //左下(底面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f); //右下(底面)
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f); //右上(前面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); //左上(前面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); //左下(前面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f); //右下(前面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f); //右上(后面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); //左上(后面)
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); //左下(后面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f); //右下(后面)
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); //右上(左面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); //左上(左面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); //左下(左面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); //右下(左面)
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f); //右上(右面)
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f); //左上(右面)
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f); //左下(右面)
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
triangleRotateAngle += 2;
}