着色器是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 = 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 = randColor;
}
运行结果:
以上! aaa
|