当前位置:网站首页>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
边栏推荐
- mysql使用update联表更新的方法
- vulnhub之GeminiInc v2
- Vulnhub's presidential
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Wrong arrangement (lottery, email)
- PHP导出word方法(一phpword)
- Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
- MCDF实验1
- vulnhub之tomato(西红柿)
- STL教程8-map
猜你喜欢
Introduction to the implementation principle of rxjs observable filter operator
XML (DTD, XML parsing, XML modeling)
ftp登录时,报错“530 Login incorrect.Login failed”
《剑指offer 04》二维数组查找
Php Export word method (One MHT)
Vulnhub geminiinc V2
Vulnhub's cereal
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
During FTP login, the error "530 login incorrect.login failed" is reported
Slam mapping and autonomous navigation simulation based on turnlebot3
随机推荐
R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
VS2015的下载地址和安装教程
cgroup简介
Redis notes 01: Introduction
Cacti monitors redis implementation process
MySQL uses the method of updating linked tables with update
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
Visual Studio 2022下载及配置OpenCV4.5.5
vulnhub之Nagini
CGroup introduction
外插散点数据
Xml的(DTD,xml解析,xml建模)
[learning notes] DP status and transfer
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
mysql使用update联表更新的方法
Keepalived中Master和Backup角色选举策略
The excel table is transferred to word, and the table does not exceed the edge paper range
vulnhub之narak
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Uniapp implementation Click to load more