qt中QOpenGLTexture纹理使用

该博客详细介绍了如何在OpenGL中创建和配置纹理,包括加载图像、设置纹理参数、激活顶点属性、绑定纹理到uniform变量,并展示了使用混合纹理的着色器代码。通过示例代码,展示了从创建QOpenGLTexture对象到绘制纹理三角形的过程。

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

纹理类型的创建

#include <QOpenGLTexture>
QOpenGLTexture *m_texture, *v_texture;

添加纹理图像

	m_texture = new QOpenGLTexture(QImage("://container.jpg").mirrored());
	
    v_texture = new QOpenGLTexture(QImage("://awesomeface.png").mirrored());
    

配置纹理参数

	m_texture->setMinificationFilter(QOpenGLTexture::Nearest);//缩小
	m_texture->setMagnificationFilter(QOpenGLTexture::Linear);//放大
	m_texture->setWrapMode(QOpenGLTexture::Repeat);//环绕模式
	v_texture->setMinificationFilter(QOpenGLTexture::Nearest);
    v_texture->setMagnificationFilter(QOpenGLTexture::Linear);
    v_texture->setWrapMode(QOpenGLTexture::Repeat);

激活顶点属性

设置图形顶点对应的纹理顶点

program->setAttributeBuffer(2, GL_FLOAT, 6*sizeof (GL_FLOAT), 2, 8*sizeof(GL_FLOAT));
program->enableAttributeArray(2);

绑定到uniform全局变量

program->setUniformValue("texturewall", 0);
m_texture->bind(0);
program->setUniformValue("textureface", 1);
v_texture->bind(1);

管线

vertex

#version 450 core

layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexColor;
layout(location = 2) in vec2 vTexcoord;

out vec3 Color;
out vec2 Texcoord;

void main(void)
{
    gl_Position =  vec4(vertexPosition,1.0);
    Color = vertexColor;
    Texcoord = vTexcoord;
}

fragment

#version 450 core

in vec3 Color;
in vec2 Texcoord;
out vec4 FragColor;

uniform sampler2D texturewall;
uniform sampler2D textureface;

void main(void)
{
    FragColor = mix(texture(texturewall, Texcoord),texture(textureface,Texcoord),0.3)
            * vec4(Color,1);
}

主要代码
openglwidget

#include "openglwidget.h"
#include <iostream>
#include <QDebug>

OpenGLWidget::OpenGLWidget(QWidget* parent)
    :QOpenGLWidget(parent)
{

}

void OpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
//-------------------------------------------------------------------------------------

    // vertex shader
    QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
    //创建一个shader,类型为QOpenGLShader::Vertex,与当前OpenGLWidget(this)关联
    vshader->compileSourceFile("://coloredTriangle.vert");

    // fragment shader
    QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
    fshader->compileSourceFile("://coloredTriangle.frag");

    // shader program
    program = new QOpenGLShaderProgram;
    program->addShader(vshader);
    program->addShader(fshader);
    //program->addShaderFromSourceFile(QOpenGLShader::Vertex, "://coloredTriangle.vert");
    //也可以用上述函数添加
    program->link();

    GLfloat vertices1[] = {
        //location(顶点坐标)     color         texture coords(纹理坐标)
        -1.0f,  0.0f, 0.0f,    1.0f, 0.0f, 0.0f,  0.0f, 1.0f,  //左中--左上
        -1.0f, -1.0f, 0.0f,    0.0f, 0.0f, 1.0f,  0.0f, 0.0f,  //左下--左下
        0.0f, -1.0f, 0.0f,    0.0f, 1.0f, 0.0f,  1.0f, 0.0f,  //中下--右下
        0.0f,  0.0f, 0.0f,    0.3f, 0.3f, 0.3f,  1.0f, 1.0f,  //居中--右上
        -1.0f,  0.0f, 0.0f,    1.0f, 0.0f, 0.0f,  0.0f, 1.0f,  //左中--左上
        0.0f, -1.0f, 0.0f,    0.0f, 1.0f, 0.0f,  1.0f, 0.0f,  //中下--右下
    };

    //set up buffers and configure vertex attributes
    //设置缓存区配置顶点属性
    QOpenGLBuffer VBO(QOpenGLBuffer::VertexBuffer);
    VBO.create();
    if(!VBO.isCreated()){
        qDebug()<<"vbo is not created.";
    }
    VBO.bind();
    VAO.create();
    if(!VAO.isCreated()){
        qDebug()<<"vao is not created.";
    }
    VAO.bind();
    m_texture = new QOpenGLTexture(QImage("://container.jpg").mirrored());
    m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
    m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
    m_texture->setWrapMode(QOpenGLTexture::Repeat);
    v_texture = new QOpenGLTexture(QImage("://awesomeface.png").mirrored());
    v_texture->setMinificationFilter(QOpenGLTexture::Nearest);
    v_texture->setMagnificationFilter(QOpenGLTexture::Linear);
    v_texture->setWrapMode(QOpenGLTexture::Repeat);

    VBO.allocate(vertices1,sizeof (vertices1));
    program->setAttributeBuffer(0, GL_FLOAT, 0, 3, 8*sizeof(GL_FLOAT));
    program->enableAttributeArray(0);
    program->setAttributeBuffer(1, GL_FLOAT, 3*sizeof (GL_FLOAT), 3, 8*sizeof(GL_FLOAT));
    program->enableAttributeArray(1);
    program->setAttributeBuffer(2, GL_FLOAT, 6*sizeof (GL_FLOAT), 2, 8*sizeof(GL_FLOAT));
    program->enableAttributeArray(2);

    program->release();
    VAO.release();

    glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
    glEnable(GL_DEPTH_TEST);


}

void OpenGLWidget::resizeGL(int w, int h)
{

}

void OpenGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    //glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    program->bind();
    VAO.bind();
    program->setUniformValue("texturewall", 0);
    m_texture->bind(0);
    program->setUniformValue("textureface", 1);
    v_texture->bind(1);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    program->release();

    //  glDrawArrays渲染顺序为先进后出的栈顺序。

}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Elsa的迷弟

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值