当前位置:网站首页>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
边栏推荐
- 简单工厂和工厂方法模式
- Dynamically monitor disk i/o with ZABBIX
- Duplicate numbers in the array of sword finger offer 03
- Simple factory and factory method mode
- XML (DTD, XML parsing, XML modeling)
- Notes on 32-96 questions of sword finger offer
- OPenGL 基本知识(根据自己理解整理)
- STL教程8-map
- R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
- Socket TCP for network communication (I)
猜你喜欢

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

Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022

Kubernetes three dozen probes and probe mode

VS2015的下载地址和安装教程

cgroup简介

Vulnhub's presidential

Web security summary

836. Merge sets (day 63) and search sets

vulnhub之tomato(西红柿)

vulnhub之presidential
随机推荐
AOSP ~ NTP (Network Time Protocol)
Solution à la défaillance de l'installation d'Electron
shardingSphere分库分表<3>
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
cgroup简介
剑指offer专项32-96题做题笔记
2022年中南大学夏令营面试经验
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
pragma-pack语法与使用
Qt+vtk+occt reading iges/step model
Pragma pack syntax and usage
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
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Vulnhub's cereal
Cacti监控Redis实现过程
How to convert a numeric string to an integer
Simple factory and factory method mode
【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
为什么我的mysql容器启动不了呢