当前位置:网站首页>QT OpenGL texture map
QT OpenGL texture map
2022-07-03 12:00:00 【wb175208】
Two pictures are pasted on a square .

Effect after mixing :
#pragma once
#include <QOpenGLWindow>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
class QOpenGLFunctions_3_3_Core;
class TextureWnd : public QOpenGLWindow {
Q_OBJECT
public:
TextureWnd();
~TextureWnd();
protected:
virtual void initializeGL();
virtual void paintGL();
private:
QOpenGLFunctions_3_3_Core* _openGLCore;
GLuint _VBO;// Vertex Position VBO
GLuint _VAO;
GLuint _EBO;
QOpenGLShaderProgram _shaderProgram;// Shader program , All shaders in the system
class QOpenGLTexture* _texture;
class QOpenGLTexture* _texture2;
};
#include <QOpenGLFunctions_3_3_Core>
#include <QVector4D>
#include <QOpenGLTexture>
#include "TextureWnd.h"
TextureWnd::TextureWnd() {
}
TextureWnd::~TextureWnd() {
}
void TextureWnd::initializeGL() {
_openGLCore = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
// Defining data - Location : Color : Texture coordinates
GLfloat vert[] = {
// Location Color Texture coordinates
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
// Define index data
unsigned int indices[] ={
0, 1, 3,
1, 2, 3
};
_openGLCore->glGenVertexArrays(1, &_VAO);
_openGLCore->glGenBuffers(1, &_VBO);
// Use VAO Start recording data attribute operation
_openGLCore->glBindVertexArray(_VAO);
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, _VBO);
_openGLCore->glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_STATIC_DRAW);
// Define data properties - Location properties ( The biggest take 8 A floating point number ( The first 5 Parameters ), from 0 Position start ( The first 6 Parameters ), take 3 Floating point data ( The first 2 Parameters ))
_openGLCore->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
_openGLCore->glEnableVertexAttribArray(0);
// Define data properties - color property ( The biggest take 8 A floating point number ( The first 5 Parameters ), from 3 Start with floating-point numbers ( The first 6 Parameters ), take 3 Floating point data ( The first 2 Parameters ))
_openGLCore->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
_openGLCore->glEnableVertexAttribArray(1);
// Define data properties - Texture coordinate attribute ( The biggest take 8 A floating point number ( The first 5 Parameters ), from 6 Start with floating-point numbers ( The first 6 Parameters ), take 2 Floating point data ( The first 2 Parameters ))
_openGLCore->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
_openGLCore->glEnableVertexAttribArray(2);
// Unbind cache
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, 0);
// Index cache objects
_openGLCore->glGenBuffers(1, &_EBO);
_openGLCore->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
_openGLCore->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// End the operation of recording data attributes
_openGLCore->glBindVertexArray(0);
_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, "E:/Projects/QtGuiTest/OPenGLApp/shader/Texture.vert");
_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, "E:/Projects/QtGuiTest/OPenGLApp/shader/Texture.frag");
_shaderProgram.link();
_texture = new QOpenGLTexture(QImage("E:/Projects/QtGuiTest/OPenGLApp/shader/1.jpg").mirrored());
_texture2 = new QOpenGLTexture(QImage("E:/Projects/QtGuiTest/OPenGLApp/shader/2.png").mirrored());
// If it's an image, you don't need to bind , The default binding . If it is more than 1 Zhang needs to actively bind ,
_shaderProgram.bind();
/* ** textureImg: Of the corresponding slice shader uniform sampler2D textureImg; ** textureCpp: Of the corresponding slice shader uniform sampler2D textureCpp; */
_shaderProgram.setUniformValue("textureImg", 0);
_shaderProgram.setUniformValue("textureCpp", 1);
}
void TextureWnd::paintGL() {
_openGLCore->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
_openGLCore->glClear(GL_COLOR_BUFFER_BIT);
_shaderProgram.bind();
_openGLCore->glBindVertexArray(_VAO);
_texture->bind(0);
_texture2->bind(1);
_openGLCore->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}
Vertex shader :
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTextureCoord;
out vec3 outColor;
out vec2 textureCoord;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
outColor = aColor;
textureCoord = aTextureCoord;
}
Fragment Shader :
#version 330 core
out vec4 fragColor;
in vec3 outColor;// Color passed from vertex shader
in vec2 textureCoord;
uniform sampler2D textureImg;
uniform sampler2D textureCpp;
void main(){
//fragColor = texture(textureImg, textureCoord);
fragColor = mix(texture(textureImg,textureCoord), texture(textureCpp,textureCoord), 0.5);
}
aaa
边栏推荐
猜你喜欢

Visual Studio 2022下载及配置OpenCV4.5.5

Colleagues wrote a responsibility chain model, with countless bugs

(database authorization - redis) summary of unauthorized access vulnerabilities in redis

Php Export word method (One MHT)

vulnhub之presidential
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

Unity3d learning notes 5 - create sub mesh

rxjs Observable filter Operator 的实现原理介绍

Vulnhub geminiinc V2

CGroup introduction
随机推荐
Socket TCP for network communication (I)
网络通讯之Socket-Tcp(一)
STL Tutorial 9 deep copy and shallow copy of container elements
MySQL searches and sorts out common methods according to time
Talk about the state management mechanism in Flink framework
STL tutorial 10 container commonalities and usage scenarios
Visual Studio 2022下载及配置OpenCV4.5.5
(数据库提权——Redis)Redis未授权访问漏洞总结
Redis 笔记 01:入门篇
Niuniu's team competition
Ripper of vulnhub
量化计算调研
Go语言实现静态服务器
安裝electron失敗的解决辦法
vulnhub之presidential
Hongmeng fourth training
vulnhub之cereal
Optimize interface performance
STL教程9-容器元素深拷贝和浅拷贝问题
XML (DTD, XML parsing, XML modeling)