qt中QOpenGLTexture纹理使用

纹理类型的创建

#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渲染顺序为先进后出的栈顺序。

}

Qt 动态修改纹理的定点坐标,需要使用 OpenGL 来实现。具体步骤如下: 1. 创建一个自定义的 OpenGL 窗口类,继承自 QOpenGLWidget,并重写其 initializeGL()、resizeGL()、paintGL() 函数。 2. 在 initializeGL() 函数,初始化 OpenGL 相关设置,如启用深度测试、纹理等。 3. 在 paintGL() 函数,绑定需要渲染的纹理,然后使用 OpenGL 绘制图形。 4. 在需要修改纹理的定点坐标时,可以使用 OpenGL 的 glTexCoordPointer() 函数来指定纹理坐标的位置和格式,并使用 glVertexPointer() 函数来指定顶点坐标的位置和格式。 5. 在绘制图形前,使用 glDrawArrays() 函数来绘制图形。 以下是一个简单的示例代码: ```cpp void MyGLWidget::initializeGL() { // 初始化 OpenGL 相关设置 glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); QImage img(":/texture.png"); m_texture = new QOpenGLTexture(img.mirrored()); } void MyGLWidget::paintGL() { // 绑定纹理 m_texture->bind(); // 指定顶点坐标和纹理坐标 GLfloat vertices[] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f}; GLfloat texCoords[] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}; glVertexPointer(2, GL_FLOAT, 0, vertices); glTexCoordPointer(2, GL_FLOAT, 0, texCoords); // 绘制图形 glDrawArrays(GL_QUADS, 0, 4); } void MyGLWidget::updateTextureCoords() { // 修改纹理坐标 GLfloat texCoords[] = {0.0f, 0.0f, 0.0f, 2.0f, 2.0f, 2.0f, 2.0f, 0.0f}; glTexCoordPointer(2, GL_FLOAT, 0, texCoords); // 更新窗口 update(); } ``` 在上面的代码,updateTextureCoords() 函数用于动态修改纹理的定点坐标。在该函数,我们重新指定了纹理坐标,并调用了 update() 函数来更新窗口。在下一帧的 paintGL() 函数OpenGL使用新的纹理坐标来绘制图形。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Elsa的迷弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值