当前位置:网站首页>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
边栏推荐
- mysql使用update联表更新的方法
- PHP Basics
- Xml的(DTD,xml解析,xml建模)
- After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
- 机器学习 3.2 决策树模型 学习笔记(待补)
- 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
- Redis things
- (数据库提权——Redis)Redis未授权访问漏洞总结
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
猜你喜欢

2022 东北四省赛 VP记录/补题

Groovy test class and JUnit test

uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。

Vulnhub narak

2022 northeast four provinces match VP record / supplementary questions

Raven2 of vulnhub

How should intermediate software designers prepare for the soft test

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?

Unity3D学习笔记5——创建子Mesh

ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
随机推荐
vulnhub之momentum
Phpcms prompt message page Jump to showmessage
Deploying WordPress instance tutorial under coreos
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
POI excel cell wrap
Notes on 32-96 questions of sword finger offer
vulnhub之GeminiInc
牛牛的组队竞赛
Cacti监控Redis实现过程
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
(数据库提权——Redis)Redis未授权访问漏洞总结
Multi dimensional monitoring: the data base of intelligent monitoring
聊聊Flink框架中的状态管理机制
简单工厂和工厂方法模式
vulnhub之raven2
PHP server interacts with redis with a large number of close_ Wait analysis
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
vulnhub之Ripper
【学习笔记】dp 状态与转移
Kibana - installation and configuration of kibana