当前位置:网站首页>OpenGL 着色器使用
OpenGL 着色器使用
2022-07-03 11:06:00 【wb175208】
着色器是OpenGL中很重要的概念,是运行在GPU上的小程序
1.顶点着色器
#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
当前使用版本和openGL的版本号对应
layout (location=0) in vec3 aPos;
location 设置外部输入数据的位置变量的属性位置值,上面展示了外部数据aPos的位置变量属性值为0。in 表示输入, out 表示输出
aPos 定义一个变量输入位置坐标
gl_Position 是GLSL内部的关键字,用来GPU的位置显示
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);接收外部输入的坐标
out vec4 vertextColor;在顶点着色器定义颜色,可以一直穿到片段着色器。片段着色器中只要定义一相同的变量接收就可以了
2.片段着色器
#version 330 core
out vec4 fragColor;
in vec4 vertextColor;//从顶点着色器中传过来的颜色
void main(){
//fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
fragColor = vertextColor;
}
注意:每个顶点着色器只能有一个输出变量!!!
3.从CPU向GPU传送数据
在着色器中使用关键字uniform
着色器中定义
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.代码示例
#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;//着色器程序,所里系统所有的着色器
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[] = {
//如果话两个三角形的话,常规画法,定义两个三角形的位置点
//第一个三角形
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);//顶点着色器
vertexShager.compileSourceFile("E:/Projects/QtGuiTest/OPenGLApp/shader/HelloShader.vert");
QOpenGLShader fragmentShager(QOpenGLShader::Fragment);//片段着色器
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();
}
顶点着色器:
#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
out vec4 fragColor;
in vec4 vertextColor;//从顶点着色器中传过来的颜色
uniform vec4 randColor;
void main(){
//fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
//fragColor = vertextColor;
fragColor = randColor;
}
运行结果:
以上!
aaa
边栏推荐
- vulnhub之narak
- Keepalived中Master和Backup角色选举策略
- Machine learning 3.2 decision tree model learning notes (to be supplemented)
- How should intermediate software designers prepare for the soft test
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
- The excel table is transferred to word, and the table does not exceed the edge paper range
- 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
- Raven2 of vulnhub
- vulnhub之pyexp
猜你喜欢

DS90UB949

836. 合并集合(DAY 63)并查集

Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)

Multi dimensional monitoring: the data base of intelligent monitoring

Numpy np.max和np.maximum实现relu函数

vulnhub之GeminiInc

Hongmeng fourth training

vulnhub之presidential

Cadence background color setting

Modular programming of single chip microcomputer
随机推荐
VS2015的下载地址和安装教程
The excel table is transferred to word, and the table does not exceed the edge paper range
vulnhub之GeminiInc v2
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
DS90UB949
XML (DTD, XML parsing, XML modeling)
Ripper of vulnhub
Deploying WordPress instance tutorial under coreos
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
Phpcms prompt message page Jump showmessage
Uniapp implementation Click to load more
2022年湖南工学院ACM集训第二次周测题解
MCDF实验1
Extrapolated scatter data
GCC compilation process and dynamic link library and static link library
外插散点数据
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
Vulnhub's presidential
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
2022 东北四省赛 VP记录/补题