当前位置:网站首页>OpenGL learning diary 2 - shaders
OpenGL learning diary 2 - shaders
2022-07-26 15:18:00 【herb.dr】
Learn video links
Catalog
One 、 Package code reading shader
1.3 Manipulate shaders with objects
2.1 One shader A typical structure of a program
One 、 Package code reading shader
1.1 Code using UTF8

1.2 Header files and objects

Comment out the relevant content

1.3 Manipulate shaders with objects

#include "openglwidget.h"
#include <QDebug>
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
// establish VBO and VAO object , And give ID
unsigned int VAO, VBO;
// unsigned int shaderProgram;
const char* vertexShaderScource =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\n\0";
const char* fragmentShaderScource =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
}
OpenGLWidget::~OpenGLWidget()
{
makeCurrent();
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &VAO);
//glDeleteProgram(shaderProgram);
doneCurrent();
}
void OpenGLWidget::drawShape(OpenGLWidget::Shape shape)
{
m_shape = shape;
update();
}
void OpenGLWidget::setWireframe(bool wireframe)
{
makeCurrent();
if(wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
update();
doneCurrent();
}
// Initialized slot function
// On the first call paintGL() or resizeGL() Before calling once , Then the widget is assigned a new QGLContext Time adjustment once
void OpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// binding VBO and VAO object
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Currently bound to target Create a new data store for the buffer object
// If data No NULL, Then initialize the data store with the data from this pointer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Tell the graphics card how to parse the attribute value in the buffer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
// Turn on VAO The first attribute value managed
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/*unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderScource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderScource, NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();*/
/*glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);*/
bool success;
shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderScource);
shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderScource);
success = shaderProgram.link();
if(success)
qDebug() << "ERR:" << shaderProgram.log();
/*glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);*/
}
void OpenGLWidget::resizeGL(int w, int h)
{
}
void OpenGLWidget::paintGL()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
switch (m_shape) {
case Triangle:
shaderProgram.bind();
//glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
break;
default:
break;
}
}
1.4 New shader



Shaders can be mixed

The running result is OK

Two 、 Shaders
2.1 One shader A typical structure of a program
For vertex shaders , The input variable is Vertex Attribute (vertex attribute)
#version version_number
in type in_variable_name ;
in type in_variable_name ;
out type out_variable name ;
uniform type uniform_name ;
void main ( )
{
// process input(s) and do some weird graphics stuff ...
// output processed stuff to output variable
out_variable_name = weird_stuff_we_processed;
}
2.2 Number of vertices
There is an upper limit to the number of vertex attributes we can declare , You can get... Through the following code :
nt nrAttributes;
glGetIntegerv(GL_MAX_VERTEX ATTRIBS, &nrAttributes) ;
OpenGL Make sure there is at least 16 Contains 4 The vertex attribute of the component is available , But some hardware may allow more vertex attributes
2.3 type
1、GLSL Contained in the C And most of the default basic data types in other languages :
int、float、double、uint and bool
2、GLSL There are also two types of containers :
(1) vector (Vector)
vecn: the default vector of n floats
bvecn: a vector of n booleans
ivecn: a vector of n integers
uvecn: a vector of n unsigned integers
dvecn: a vector of n double components
(2) matrix (Matrix)
3、 restructuring
Vectors allow some interesting and flexible component selection , It's called reorganization (Swizzling):
vec2 vect = vec2(0.5, 0.7);
vec4 result = vec4(vect, 0.0, 0.0);
vec4 otherResult = vec4(result.xyz, 1.0);
2.4 Input and output
Declare an output in the sender shader
Declare a similar input in the receiver shader
When the type and name are the same ,OpenGL Will link variables together ( When linking program objects )

2.5 Code
1、 Input 、 Output


2、 Other grammar

just so so .xxz And so on.
2.6 layout(location = ...)
Vertex shaders receive a special form of input , Otherwise it will be inefficient
Receive input directly from vertex data . To define how vertex data should be managed , We use location This metadata (mietadata) Specify input variables , So we can be in CPU Configure vertex attributes on . for example : layout (location = 0).layout The logo of this , Enables us to link it to vertex data .
You can ignore layout(location = 0) identifier , By means of OpenGL The code uses glGetAttribLocation Query attribute location value (Location), or glBindAttribLocation Attribute position value (Location), But it is recommended to set them in shaders , It will be easier to understand and save you ( and OpenGL) The amount of work .
In the following figure, from 0 Start saving data , from 2 Start reading data , The program will report an error


Now we get shader The data read inside is from 2 Start , So we store data from 2 Start , In this way, the program can run smoothly

边栏推荐
- How to write the format of a university thesis?
- 解决Typora图片显示不出来问题
- [basic] the difference between dynamic link library and static link library
- 如何进行学术文献翻译?
- Crystal special decoration submitted for registration: the first quarter revenue fell by 80%, and Chen Bo controlled 68.5% of the equity
- VS添加作者信息和时间信息的设置
- Prometheus adds email alarm and enterprise wechat robot alarm
- 带你熟悉云网络的“电话簿”:DNS
- The IPO of shengtaier technology was terminated: it was planned to raise 560million yuan, and Qiming and Jifeng capital were shareholders
- Vs add settings for author information and time information
猜你喜欢

领导抢功劳,我改个变量名让他下岗了

The most detailed patent application tutorial, teaching you how to apply for a patent

二叉树的创建以及遍历

FOC电机控制基础

The practice of software R & D should start from the design

【LeetCode每日一题】——268.丢失的数字

Character function and string function and memory function

Ner of NLP: Exploration and practice of product title attribute recognition

大学生如何申请实用新型专利?

Chuhuan technology is listed on Shenzhen Stock Exchange: Minsheng securities, with a market value of 2.7 billion, is a shareholder
随机推荐
How to search literature on nature?
广州地铁十三号线二期全线土建已完成53%,预计明年开通
The civil construction of the whole line of Guangzhou Metro Line 13 phase II has been completed by 53%, and it is expected to open next year
本科毕业论文外文文献翻译怎么找?
如何查询外文文献?
FOC电机控制基础
Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
Unity URP入门实战
如何进行学术文献翻译?
采购实用技巧,5个瓶颈物料的采购方法
Deep Packet Inspection Using Cuckoo Filter论文总结
Ner of NLP: Exploration and practice of product title attribute recognition
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
Sqldeveloper tools quick start
FOC motor control foundation
Vs add settings for author information and time information
9. Learn MySQL delete statement
Postman environment variable setting code storage
R语言使用lm函数构建带交互项的多元回归模型、使用step函数构建逐步回归模型筛选预测变量的最佳子集(step regression)
软测(七)性能测试(1)简要介绍
https://www.bilibili.com/video/BV1UL411W71w?p=12&vd_source=0471cde1c644648fafd07b54e303c905