3D魔方QT

3D魔方QT

关键词:qt、opengl、3D、魔方

此 Demo 是利用 QOpenGLWidget 实现的3D魔方效果,可实现自动宣传及鼠标拖动,可根据自己喜好贴不同的背景图及编写内容,整体效果如下:

在这里插入图片描述

简介

  1. 此Demo使用QT Creator 4.11.0, Based on Qt 5.14.0 编写,大部分qt版本均适用。
  2. Demo源码链接在本文最后提供。

核心代码

此Demo的核心代码如下,或下载源码修改调试:

vshader.glsl文件
    #ifdef GL_ES
    // Set default precision to medium
    precision mediump int;
    precision mediump float;
    #endif
    uniform mat4 mvp_matrix;
    attribute vec4 a_position;
    attribute vec2 a_texcoord;
    varying vec2 v_texcoord;
    //! [0]
    void main()
    {
        // Calculate vertex position in screen space
        gl_Position = mvp_matrix * a_position;
        // Pass texture coordinate to fragment shader
        // Value will be automatically interpolated to fragments inside polygon faces
        v_texcoord = a_texcoord;
    }
fshader.glsl
    #ifdef GL_ES
    // Set default precision to medium
    precision mediump int;
    precision mediump float;
    #endif
    uniform sampler2D texture;
    varying vec2 v_texcoord;
    //! [0]
    void main()
    {
        // Set fragment color from texture
        gl_FragColor = texture2D(texture, v_texcoord);
    }
    //! [0]


//主程序
void DlgCube::initializeGL()
{
    initializeOpenGLFunctions();
    //球 后 widget 的背景色
//    glClearColor(1, 1, 1, 0);
    initShaders();
    initTextures();
    // Enable depth buffer
    glEnable(GL_DEPTH_TEST);
    // Enable back face culling
    glEnable(GL_CULL_FACE);
    geometries = new GeometryEngine;
    // Use QBasicTimer because its faster than QTimer
    timer.start(12, this);
}

void DlgCube::initShaders()
{
    // Compile vertex shader
    if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl"))
        close();
    // Compile fragment shader
    if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl"))
        close();
    // Link shader pipeline
    if (!program.link())
        close();
    // Bind shader pipeline for use
    if (!program.bind())
        close();
}

void DlgCube::initTextures()
{
    // Load cube.png image
    texture = new QOpenGLTexture(myImage.mirrored());
    // Set nearest filtering mode for texture minification
    texture->setMinificationFilter(QOpenGLTexture::Nearest);
    // Set bilinear filtering mode for texture magnification
    texture->setMagnificationFilter(QOpenGLTexture::Linear);
    // Wrap texture coordinates by repeating
    // f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
    texture->setWrapMode(QOpenGLTexture::Repeat);
}

void DlgCube::resizeGL(int w, int h)
{
    // Calculate aspect ratio
    qreal aspect = qreal(w) / qreal(h ? h : 1);
    // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
    //fov 可以调节大小
    const qreal zNear = 2.5, zFar = 7.0, fov = 45.0;
    // Reset projection
    projection.setToIdentity();
    // Set perspective projection
    projection.perspective(fov, aspect, zNear, zFar);
}

void DlgCube::paintGL()
{
    // Clear color and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    texture->bind();
    // Calculate model view transformation
    QMatrix4x4 matrix;
    matrix.translate(0.0, 0.0, -5.0);
    matrix.rotate(rotation);
    // Set modelview-projection matrix
    program.setUniformValue("mvp_matrix", projection * matrix);
    // Use texture unit 0 which contains cube.png
    program.setUniformValue("texture", 0);
    // Draw cube geometry
    geometries->drawCubeGeometry(&program);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值