QT openglwidget实现简单的3D绘制显示和纹理贴图

本文介绍如何使用OpenGL在Qt环境中创建3D渲染应用,包括设置OpenGL上下文、加载纹理、编译着色器、设置顶点缓冲区、绘制几何形状以及响应键盘输入进行视图控制。

UI如图`

头文件

#ifndef OPENWIDGET_H
#define OPENWIDGET_H

#include <QWidget>
#include"qopenglwidget.h"
#include<qopenglfunctions.h>
#include<qopenglshaderprogram.h>
#include<QOpenGLBuffer>
class QOpenGLShaderProgram;
class QOpenGLTexture;
class OpenWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit OpenWidget(QWidget *parent = nullptr);

//signals:

//public slots:
public:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void keyPressEvent(QKeyEvent*event);
private:
  QOpenGLShaderProgram *program;
  QOpenGLBuffer vbo;
  QOpenGLTexture* texture[2];
  GLfloat translate,xRot,yRot,zRot;
};

#endif // OPENWIDGET_H`

cpp

#include "openwidget.h"
#include<qopenglshaderprogram.h>
#include<QOpenGLTexture>
#include<qimage.h>
#include<QKeyEvent>
OpenWidget::OpenWidget(QWidget *parent) : QOpenGLWidget(parent)
{
   
   
translate=-6.0;
xRot=zRot=0.0;
yRot=-30.0;
}

void OpenWidget::initializeGL()
{
   
   
	initializeOpenGLFunctions();
    glEnable(GL_DEPTH_TEST);
     texture[0]=new QOpenGLTexture(QImage(QString(":/myopengl/side%1.bmp")));
     texture[1]=new QOpenGLTexture(QImage(QString(":/myopengl/side%2.bmp")));
   QOpenGLShader *vshader=new QOpenGLShader(QOpenGLShader::Vertex,this);
   const char *vsrc=
           "#version 150                               \n"
           "in vec4 vPosition;                         \n"
           "in vec4 vTexCoord;                         \n"
           "out vec4 texCoord;                         \n"
           "uniform mat4 matrix;                       \n"
           "void main(){                               \n"
           "texCoord=vTexCoord;                        \n"
           "gl_Position=matrix*vPosition;              \n"
           "}                                          \n";
   if (!vshader->compileSourceCode<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值