当前位置:网站首页>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
边栏推荐
- R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
- 为什么我的mysql容器启动不了呢
- previous permutation lintcode51
- ArcGIS application (XXI) ArcMap method of deleting layer specified features
- Groovy测试类 和 Junit测试
- Interview experience in summer camp of Central South University in 2022
- Vulnhub pyexp
- Introduction to the implementation principle of rxjs observable filter operator
- Qt OpenGL 纹理贴图
- 牛牛的组队竞赛
猜你喜欢
随机推荐
Hongmeng fourth training
Shardingsphere sub database and sub table < 3 >
vulnhub之GeminiInc v2
【mysql官方文档】死锁
Interview experience in summer camp of Central South University in 2022
Notes on 32-96 questions of sword finger offer
安装electron失败的解决办法
外插散点数据
Redis notes 01: Introduction
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Socket TCP for network communication (I)
Qt OpenGL 纹理贴图
Why can't my MySQL container start
OpenGL 绘制彩色的三角形
Qt OpenGL 旋转、平移、缩放
Unity3d learning notes 5 - create sub mesh
Kubernetes three dozen probes and probe mode
php 获取文件夹下面的文件列表和文件夹列表
Raven2 of vulnhub
vulnhub之Ripper