当前位置:网站首页>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
边栏推荐
- OpenGL 绘制彩色的三角形
- [learning notes] DP status and transfer
- XML (DTD, XML parsing, XML modeling)
- Dynamically monitor disk i/o with ZABBIX
- 在CoreOS下部署WordPress实例教程
- vulnhub之Ripper
- Notes on 32-96 questions of sword finger offer
- 小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- 错排问题 (抽奖,发邮件)
猜你喜欢

Hongmeng third training (project training)

Visual Studio 2022下载及配置OpenCV4.5.5

Vulnhub's Nagini

STL tutorial 10 container commonalities and usage scenarios

错排问题 (抽奖,发邮件)

Groovy测试类 和 Junit测试

The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation

Socket TCP for network communication (I)

Unity3D学习笔记5——创建子Mesh

vulnhub之GeminiInc v2
随机推荐
OpenGL 索引缓存对象EBO和线宽模式
Dynamically monitor disk i/o with ZABBIX
解决msvcp120d.dll和msvcr120d.dll缺失
mysql使用update联表更新的方法
pragma-pack语法与使用
并发编程-单例
Systemverilog-- OOP--对象的拷贝
Notes on 32-96 questions of sword finger offer
【mysql官方文档】死锁
为什么我的mysql容器启动不了呢
网络通讯之Socket-Tcp(一)
Mysql根据时间搜索常用方法整理
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
2022年湖南工学院ACM集训第二次周测题解
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
Web security summary
Vulnhub geminiinc
PHP導出word方法(一mht)
vulnhub之pyexp
libvirt 中体验容器