首页 > 其他分享 >Qt+OpenGL混合两个纹理,并可以改变纹理透明度

Qt+OpenGL混合两个纹理,并可以改变纹理透明度

时间:2024-08-23 17:28:09浏览次数:10  
标签:1.0 Qt OpenGL 0.0 void 纹理 GLTextureMixWindow GL

一、概述

  需求:

    1.将两纹理进行融合,改变其中一个透明度让其渐渐显示或隐藏

    2.控制x轴像素互换,让笑脸达到向左看或向右看的目的。

  

   大部分的代码都是常规代码。还是那一套流程:

    1.定义顶点、纹理、顶点索引坐标。

    2.创建并绑定VAO、VBO、EBO。

    3.告知显卡顶点解析方式。

    4.定义顶点着色器及片元着色器,编译程序。

    5.创建纹理。

    6.开始绘制

    这里面最主要的是顶点即纹理的定义、着色器的传参、绘制代码

  步骤:

  1.定义顶点、纹理坐标

float vertices[] = {
    //     ---- 位置 ----          - 纹理坐标 -
         1.0f,  1.0f, 0.0f,   1.0f, 1.0f,   // 右上
         1.0f, -1.0f, 0.0f,   1.0f, 0.0f,   // 右下
        -1.0f, -1.0f, 0.0f,   0.0f, 0.0f,   // 左下
        -1.0f,  1.0f, 0.0f,    0.0f, 1.0f    // 左上
};
unsigned int indices[] = {
   0, 1, 3, // first triangle
   1, 2, 3  // second triangle
};

  2.着色器的定义

//顶点着色器
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 ourTexCoord;
void main(void)
{
    gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0f);
    ourTexCoord = aTexCoord;
}

//片元着色器
#version 330 core
out vec4 FragColor;
in vec2 ourTexCoord;
uniform float maxScale;
uniform float flip_x;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
void main()
{
    FragColor = mix(texture(ourTexture1,ourTexCoord),texture(ourTexture2,vec2(ourTexCoord.x*flip_x,ourTexCoord.y)),maxScale);
}

  3.给定义的着色器传参(绘制)

glUniform1f(glGetUniformLocation(programId, "flip_x"), flip_x);
glUniform1f(glGetUniformLocation(programId, "maxScale"), mixScale);
//指定纹理单元编号
GLint ourTextureLocation = glGetUniformLocation(programId, "ourTexture1");
glUniform1i(ourTextureLocation, 0);
GLint ourTexture1Location = glGetUniformLocation(programId, "ourTexture2");
glUniform1i(ourTexture1Location, 1);

 

二、完整代码示例

  1.GLTextureMixWindow.h

#pragma once

#include "BaseOpenGLWindow.h"
#ifndef STB_IMAGE_H
#define STB_IMAGE_H
#include "../../stb_image.h"
#endif // STB_IMAGE_H
#include <qmatrix4x4.h>
#include <qvector3d.h>
#include <QRectF>

class GLTextureMixWindow : public BaseOpenGLWindow
{
    Q_OBJECT

public:
    GLTextureMixWindow(QWidget *parent = nullptr);
    ~GLTextureMixWindow();

protected:
    virtual void initializeGL();//初始化OpenGL
    virtual void resizeGL(int w, int h);//ResizeWindow大小
    virtual void paintGL();//绘制OpenGL
public:
    GLuint buildTexture(QString textureFileName, GLint imageColorType);
    void setMixScale(float mix);
    void flipToX();
private:
    unsigned int VAO, VBO, EBO;
    unsigned int texture, texture1, texture2;
    GLuint programId;
    float mixScale = 0.5f;
    float flip_x = 1.0f;
};

 

  2.GLTextureMixWindow.cpp

#include "GLTextureMixWindow.h"

GLTextureMixWindow::GLTextureMixWindow(QWidget* parent)
    : BaseOpenGLWindow(parent)
{
    this->setWindowTitle("纹理融合");
    QHBoxLayout* hLayout = new QHBoxLayout(this);
    QPushButton* btnRed = new QPushButton(this);
    btnRed->setText("融合系数增大(+)");
    QPushButton* btnGreen = new QPushButton(this);
    btnGreen->setText("融合系数减小(-)");
    QPushButton* btnFlipX = new QPushButton(this);
    btnFlipX->setText("x轴像素位置互换");
    hLayout->addWidget(btnRed);
    hLayout->addWidget(btnGreen);
    hLayout->addWidget(btnFlipX);
    hLayout->setAlignment(Qt::AlignTop);

    connect(btnRed, &QPushButton::clicked, [this]() {
        this->mixScale += 0.1f;
        if (this->mixScale >= 1.0f) this->mixScale = 1.0f;
        setMixScale(mixScale);
        });

    connect(btnGreen, &QPushButton::clicked, [=]() {
        this->mixScale -= 0.1f;
        if (this->mixScale < 0.0f) this->mixScale = 0.0f;
        setMixScale(mixScale);
        });

    connect(btnFlipX, &QPushButton::clicked, [=]() {
        flipToX();
        });

}
void GLTextureMixWindow::initializeGL() {
    this->initializeOpenGLFunctions();
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    float vertices[] = {
        //     ---- 位置 ----          - 纹理坐标 -
             1.0f,  1.0f, 0.0f,   1.0f, 1.0f,   // 右上
             1.0f, -1.0f, 0.0f,   1.0f, 0.0f,   // 右下
            -1.0f, -1.0f, 0.0f,   0.0f, 0.0f,   // 左下
            -1.0f,  1.0f, 0.0f,    0.0f, 1.0f    // 左上
    };
    //float vertices[] = {
    //    //     ---- 位置 ----          - 纹理坐标 -
    //         1.0f,  1.0f, 0.0f,   4.0f, 4.0f,   // 右上
    //         1.0f, -1.0f, 0.0f,   4.0f, 0.0f,   // 右下
    //        -1.0f, -1.0f, 0.0f,   0.0f, 0.0f,   // 左下
    //        -1.0f,  1.0f, 0.0f,    0.0f, 4.0f    // 左上
    //};
    unsigned int indices[] = {
       0, 1, 3, // first triangle
       1, 2, 3  // second triangle
    };
    //创建VAO、VBO、EBO
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
    glBindVertexArray(VAO);

    ////顶点数据存入缓冲区
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    ////EBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    ////告知显卡顶点数据如何解析
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    ////告知显卡纹理坐标如何解析
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

    stbi_set_flip_vertically_on_load(true);
    texture = buildTexture("E:/tony/demo/visualstudio_workspace/QtForOpenCV4Tool/QtForOpenCV4Tool/bin/images/flowers.jpeg",
        GL_RGB);
    texture1 = buildTexture("E:/tony/demo/visualstudio_workspace/QtForOpenCV4Tool/QtForOpenCV4Tool/bin/images/similing_face.png",
        GL_RGBA);

    //小程序
    programId = buildAttachShaderAndReturnProgramId(":/QtForOpenCV4Tool/shader/texture_mix.vert",
        ":/QtForOpenCV4Tool/shader/texture_mix.frag");
}
GLuint GLTextureMixWindow::buildTexture(QString textureFileName, GLint imageColorType) {
    GLuint mTexture;
    ////创建纹理
    glGenTextures(1, &mTexture);
    glBindTexture(GL_TEXTURE_2D, mTexture);
    //设置纹理环绕方式
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    //设置纹理过滤方式
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //生成纹理
    int width, height, mChannels;
    //QString path = getTexturePath(textureFileName);
    //qDebug() << path.toStdString().c_str();
    unsigned char* data = stbi_load(textureFileName.toStdString().c_str(), &width, &height, &mChannels, 0);
    if (data) {
        qDebug() << "图片加载成功,width=" << width << ",height=" << height;
        glTexImage2D(GL_TEXTURE_2D, 0, imageColorType, width, height, 0, imageColorType, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else {
        qDebug() << "图像数据加载失败";
    }
    stbi_image_free(data);

    return mTexture;
}
void GLTextureMixWindow::resizeGL(int w, int h) {
    glViewport(0, 0, w, h);
}
void GLTextureMixWindow::paintGL() {
    glBindVertexArray(VAO);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glUseProgram(programId);
    glUniform1f(glGetUniformLocation(programId, "flip_x"), flip_x);
    glUniform1f(glGetUniformLocation(programId, "maxScale"), mixScale);
    //指定纹理单元编号
    GLint ourTextureLocation = glGetUniformLocation(programId, "ourTexture1");
    glUniform1i(ourTextureLocation, 0);
    GLint ourTexture1Location = glGetUniformLocation(programId, "ourTexture2");
    glUniform1i(ourTexture1Location, 1);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void GLTextureMixWindow::setMixScale(float mix) {
    this->mixScale = mix;
    update();
}
void GLTextureMixWindow::flipToX() {
    this->flip_x = -this->flip_x;
    update();
}
GLTextureMixWindow::~GLTextureMixWindow()
{
}

 

标签:1.0,Qt,OpenGL,0.0,void,纹理,GLTextureMixWindow,GL
From: https://www.cnblogs.com/tony-yang-flutter/p/18376647

相关文章

  • QT+OpenGL纹理与颜色混合
    一、概述使用OpenGL将纹理与颜色混合输出。ps:此处并无实际的意义只是单纯的为了好玩。步骤:1.定义顶点坐标、颜色、纹理及顶点索引floatvertices[]={//----位置---------颜色------纹理坐标-1.0f,1.0f,0.0f,1.0......
  • Qt实现画图板
    Qt实现画板基于QGraphicsView实现绘画曲线注意点_qgraphicsview绘制曲线图-CSDN博客Qt桌面白板工具其一(解决曲线不平滑的问题——贝塞尔曲线)_qt贝塞尔曲线-CSDN博客先实现一个最简单的不考虑任何优化的曲线画板我们知道鼠标移动时,会在其移动轨迹上留下一系列的点,我们可以将这......
  • 【Qt】Qt窗口 | QDialog 对话框
    文章目录一.对话框二.对话框的分类1.非模态对话框2.模态对话框3.混合属性对话框三.自定义对话框1.代码实现2.ui文件实现四.内置对话框1.QMessageBox消息对话框2.QColorDialog颜色对话框3.QFileDialog文件对话框4.QFontDialog字体对话框5.QInputDialo......
  • 八、OpenTK中的纹理操作(万字详细教程)
    文章目录一、纹理加载和创建(一)从图像文件加载纹理(如PNG、JPEG)(二)生成程序纹理(如噪声纹理、棋盘格纹理)二、纹理坐标(一)2D纹理坐标计算(二)3D纹理坐标映射(三)纹理坐标环绕和过滤模式三、纹理单元(一)、多纹理的使用(二)、纹理混合和叠加(三)、立方体贴图纹理......
  • Qt5.14.2 多边形与多边形求交集
    利用Qt5.14.2中自带的QPolygonF类的intersected()函数求两个多边形的交集多边形。inlineQList<QPolygonF>getPolygonIntersectedWithPolygon(QPolygonFpolygon1,QPolygonFpolygon2){QList<QPolygonF>outval;//此处求出的intersection可能包含多个子......
  • qt5.14.2 MSVC opencv(不结合vs)路径添加问题 测试通过
     不换行写法1win32:CONFIG(release,debug|release):LIBS+=-LG:/opencv4.4.0/opencv4.4_vs_msvc19_qt6.5/x64/vc17/lib/-lopencv_aruco440-lopencv_bgsegm440-lopencv_bioinspired440-lopencv_calib3d440-lopencv_ccalib440-lopencv_core440-lopencv_cvv440-lopen......
  • Qt5.14.2 MinGW7.3.0_64 opencv helloworld
    路径配置.proINCLUDEPATH+=E:\Opencv\includeLIBS+=E:\Opencv\x64\mingw\lib\libopencv_*.a 点击按钮打开一张图片1#include"mainwindow.h"2#include"ui_mainwindow.h"3#include<QFileDialog>4#include<QMessageBox>5......
  • PyQt5简介
    PyQt是Qt框架的Python语言实现,由RiverbankComputing开发,是最强大的GUI库之一。PyQt提供了一个设计良好的窗口控件集合,每一个PyQt控件都对应一个Qt控件,因此PyQt的API接口与Qt的API接口很接近,但PyQt不再使用QMake系统和Q_OBJECT宏。PyQt5提供GPL版和商业版证书,自由开发者可以使用......
  • Qt Line Edit焦点丢失|Checkbox转移焦点丢失
    在我设计的一个界面中,用事件过滤器获取键盘方向键,通过键盘方向键转移控件的焦点,获取焦点的控件显示高亮,在从一个Checkbox控件转移焦点到一个LineEdit控件的时候,该获得焦点的控件并没有显示高亮,并且根据后续操作推测焦点消失了,通过qDebug调试发现转移焦点后的那一瞬间,LineEdit控......
  • Qt:Qt背景
    目录1.Qt解释2.Windows下开发GUI的方案3.框架4.Qt历史4.Qt支持的平台5.Qt版本6.Qt案例1.Qt解释        前端开发,分为网页前端开发(Web)、桌面应用开发(Windows、Linux)、移动应用开发(Android)。Qt一般用于客户端开发,这里的客户端是狭义的,指桌面应用开发。  ......