一重山,两重山。山远天高烟水寒,相思枫叶丹。
菊花开,菊花残。塞雁高飞人未还,一帘风月闲。
.pro中添加
QT += core gui opengl
win32:LIBS += -lOpengl32 \
-lglu32 \
-lglut
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWindow>
#include <QOpenGLFunctions_4_5_Core>
class MainWindow : public QWindow,QOpenGLFunctions_4_5_Core
{
Q_OBJECT
public:
MainWindow(QWindow *parent = nullptr);
~MainWindow();
virtual void render();
virtual void initialize();
public slots:
void renderNow();
protected:
void exposeEvent(QExposeEvent *);
void resizeEvent(QResizeEvent *) ;
private:
void myPerspective(GLdouble fov, GLdouble aspectRatio, GLdouble zNear, GLdouble zFar );
void loadGLTexture();
private:
QOpenGLContext *m_context;
GLfloat xrot;//x旋转量
GLfloat yrot;//y旋转量
GLfloat zrot;//z旋转量
GLuint texture[1];
QTimer* m_pTimer;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QGuiApplication>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QOpenGLContext>
#include<GL/gl.h>
#include <QTimer>
MainWindow::MainWindow(QWindow *parent)
: QWindow(parent)
,m_context(0)
{ setSurfaceType(QWindow::OpenGLSurface);
xrot=45.0f;
yrot=45.0f;
zrot=45.0f;
m_pTimer=new QTimer(this);
connect(m_pTimer,SIGNAL( timeout()),this,SLOT(renderNow()));
}
MainWindow::~MainWindow()
{
glDeleteTextures(1,&texture[0]);
if(m_pTimer)
{ delete m_pTimer;
m_pTimer = 0;}
}
void MainWindow::myPerspective(GLdouble fov,GLdouble aspectRatio,GLdouble zNear,GLdouble zFar)
{
GLdouble rFov=fov*3.1415926/180.0;
glFrustum(-zNear*tan(rFov/2.0)*aspectRatio,
zNear*tan(rFov/2.0)*aspectRatio,
-zNear*tan(rFov/2.0),
zNear*tan(rFov/2.0),
zNear,zFar);
}
void MainWindow::initialize()//(初始化)
{ m_pTimer->start(100);
loadGLTexture();
glEnable(GL_TEXTURE_2D);//启用纹理映射
glShadeModel(GL_SMOOTH);//启用平滑着色
glClearColor(0.0f,0.0f,0.0f,0.0f);//黑色背景
glClearDepth(1.0f);//设置深度缓存
glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//深度测试类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//透视修正
}
void MainWindow::loadGLTexture()
{
QImage image("./data/717.jpg");
image =image.convertToFormat(QImage::Format_RGB888).mirrored();
glGenTextures(1,texture);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,
image.width(),image.height(),0,GL_RGB,GL_UNSIGNED_BYTE,
image.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void MainWindow::render()
{ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除颜色和深度缓存
glViewport(0,0,(GLint)width(),(GLint)height());//重置当前视口
glMatrixMode(GL_PROJECTION);//投影矩阵
glLoadIdentity();//重置
myPerspective(45.0,(GLfloat)width()/(GLfloat)height(),0.1,100.0);
//设置相机参数
glMatrixMode(GL_MODELVIEW);//选择模型视图矩阵
glLoadIdentity();//重置为单位矩阵
glTranslatef(0.0,0.0,-10.0f);//移入屏幕6.0平移
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glBegin(GL_QUADS);
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(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,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(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,0.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(1.0f,-1.0f,-1.0f);
glEnd();
xrot+=5.0f;
yrot+=5.0f;
zrot+=5.0f;//动态效果5度
}
void MainWindow::renderNow()
{ if(!isExposed())return;
bool needsInitialize = false;
if(!m_context){
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true; }
m_context->makeCurrent(this);
if(needsInitialize){
initializeOpenGLFunctions();
initialize(); }
render();
m_context->swapBuffers(this);
}
void MainWindow::exposeEvent(QExposeEvent *)
{ if(!isExposed())return;
bool needsInitialize = false;
if(!m_context){
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true;
}
m_context->makeCurrent(this);
if(needsInitialize){
initializeOpenGLFunctions();
initialize();}
renderNow();
m_context->swapBuffers(this);
}
void MainWindow::resizeEvent(QResizeEvent *)
{ if(!isExposed())return;
bool needsInitialize = false;
if(!m_context){
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true;}
m_context->makeCurrent(this);
if(needsInitialize){
initializeOpenGLFunctions();
initialize();}
renderNow();
m_context->swapBuffers(this);
}