<>纹理类型的创建
#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渲染顺序为先进后出的栈顺序。 }

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信