Qt三维图形添加纹理,并使图形旋转。立体图形添加纹理。

此篇博客展示了如何使用OpenGL在Qt应用中实现动态的三维场景,结合古诗《长相思》中的意境,以枫叶为元素,创造出具有艺术气息的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一重山,两重山。山远天高烟水寒,相思枫叶丹。

菊花开,菊花残。塞雁高飞人未还,一帘风月闲。

.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);
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Super毛毛穗

今天晚饭加什么?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值