当前位置:网站首页>OpenGL shader use
OpenGL shader use
2022-07-03 12:00:00 【wb175208】
The shader is OpenGL Important concepts in , Is running on the GPU The applet on
1. Vertex shader
#version 330 core
layout (location=0) in vec3 aPos;
out vec4 vertextColor;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
vertextColor = vec4(1.0, 1.0, 0.0, 1.0);
}
#version 330 core
Current version and openGL The version number of corresponds to
layout (location=0) in vec3 aPos;
location Set the attribute position value of the position variable of external input data , The external data is shown above aPos The location variable attribute value of is 0.in Indicates input , out Indicative output
aPos Define a variable input position coordinates
gl_Position yes GLSL Internal keywords , be used for GPU Position display of
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); Receive coordinates from external input
out vec4 vertextColor; Define colors in vertex shaders , You can wear it all the way to the clip shader . Just define the same variable reception in the fragment shader
2. Fragment Shader
#version 330 core
out vec4 fragColor;
in vec4 vertextColor;// Color passed from vertex shader
void main(){
//fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
fragColor = vertextColor;
}
Be careful : Each vertex shader can only have one output variable !!!
3. from CPU towards GPU Transmit data
Use keywords in shaders uniform
Defined in shaders
uniform vec4 randColor;
GLuint randColor = _shaderProgram.uniformLocation("randColor");
_shaderProgram.setUniformValue(randColor, QVector4D(0.4, qAbs(qSin((float)time.elapsed()/1000.0)),0.3 ,1.0));
4. Code example
#pragma once
#include <QOpenGLWindow>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QTime>
class QOpenGLFunctions_3_3_Core;
class HelloShader : public QOpenGLWindow {
Q_OBJECT
public:
HelloShader();
~HelloShader();
private:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
QOpenGLFunctions_3_3_Core* _openGLCore;
GLuint _VBO;
GLuint _VAO;
QOpenGLShaderProgram _shaderProgram;// Shader program , All shaders in the system
QTime time = QTime::currentTime();
};
#include "HelloShader.h"
#include <QOpenGLFunctions_3_3_Core>
#include <QVector4D>
#include <QMath.h>
HelloShader::HelloShader() {
}
HelloShader::~HelloShader() {
}
void HelloShader::initializeGL() {
_openGLCore = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
GLfloat ver[] = {
// If there are two triangles , Conventional drawing method , Define the position points of two triangles
// The first triangle
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
_openGLCore->glGenBuffers(1, &_VAO);
_openGLCore->glBindVertexArray(_VAO);
_openGLCore->glGenBuffers(1, &_VBO);
_openGLCore->glBindBuffer(GL_ARRAY_BUFFER, _VBO);
_openGLCore->glBufferData(GL_ARRAY_BUFFER, sizeof(ver), ver, GL_STATIC_DRAW);
_openGLCore->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
_openGLCore->glEnableVertexAttribArray(0);
_openGLCore->glBindVertexArray(0);
QOpenGLShader vertexShager(QOpenGLShader::Vertex);// Vertex shader
vertexShager.compileSourceFile("E:/Projects/QtGuiTest/OPenGLApp/shader/HelloShader.vert");
QOpenGLShader fragmentShager(QOpenGLShader::Fragment);// Fragment Shader
fragmentShager.compileSourceFile("E:/Projects/QtGuiTest/OPenGLApp/shader/HelloShader.frag");
_shaderProgram.addShader(&vertexShager);
_shaderProgram.addShader(&fragmentShager);
_shaderProgram.link();
}
void HelloShader::resizeGL(int w, int h) {
}
void HelloShader::paintGL() {
_openGLCore->glClearColor(0.6f, 0.6f, 0.6f, 1.0);
_openGLCore->glClear(GL_COLOR_BUFFER_BIT);
_shaderProgram.bind();
GLuint randColor = _shaderProgram.uniformLocation("randColor");
_shaderProgram.setUniformValue(randColor, QVector4D(0.4, qAbs(qSin((float)time.elapsed()/1000.0)),0.3 ,1.0));
_openGLCore->glBindVertexArray(_VAO);
_openGLCore->glDrawArrays(GL_TRIANGLES, 0, 3);
update();
}
Vertex shader :
#version 330 core
layout (location=0) in vec3 aPos;
out vec4 vertextColor;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
vertextColor = vec4(1.0, 1.0, 0.0, 1.0);
}
Fragment Shader
#version 330 core
out vec4 fragColor;
in vec4 vertextColor;// Color passed from vertex shader
uniform vec4 randColor;
void main(){
//fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
//fragColor = vertextColor;
fragColor = randColor;
}
Running results :
above !
aaa
边栏推荐
- Visual Studio 2022下载及配置OpenCV4.5.5
- Colleagues wrote a responsibility chain model, with countless bugs
- previous permutation lintcode51
- 在CoreOS下部署WordPress实例教程
- vulnhub之GeminiInc
- vulnhub之narak
- R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
- 【学习笔记】dp 状态与转移
- rxjs Observable filter Operator 的实现原理介绍
- Laravel time zone timezone
猜你喜欢

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

Duplicate numbers in the array of sword finger offer 03

"Jianzhi offer 04" two-dimensional array search

Niuniu's team competition

rxjs Observable filter Operator 的实现原理介绍

ArcGIS应用(二十一)Arcmap删除图层指定要素的方法

Visual Studio 2022下载及配置OpenCV4.5.5

为什么我的mysql容器启动不了呢

Raven2 of vulnhub
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer
随机推荐
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
836. Merge sets (day 63) and search sets
网络通讯之Socket-Tcp(一)
vulnhub之Ripper
Visual Studio 2022下载及配置OpenCV4.5.5
During FTP login, the error "530 login incorrect.login failed" is reported
网络通讯之Socket-Tcp(一)
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Keepalived中Master和Backup角色选举策略
牛牛的组队竞赛
ftp登录时,报错“530 Login incorrect.Login failed”
在CoreOS下部署WordPress实例教程
DNS multi-point deployment IP anycast+bgp actual combat analysis
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Vulnhub's Tomato (tomato)
Vulnhub's cereal
Ripper of vulnhub
vulnhub之Nagini
STL教程8-map