当前位置:网站首页>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

边栏推荐
- R语言使用lattice包中的histogram函数可视化直方图(histogram plot)、col参数自定义填充色、type参数自定义直方图显示模式(计数或者比例)
- 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
- cs224w(图机器学习)2021冬季课程学习笔记5
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the theme of ggpubr package_ The pubclean function sets the theme without axis lines in the visual image
- R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
- Deep Packet Inspection Using Quotient Filter论文总结
- Chapter 08_ Principles of index creation and design
- 【LeetCode】33、 搜索旋转排序数组
- 写综述,想用一个靠谱的整理文献的软件,有推荐的吗?
- 数据挖掘之数据预处理
猜你喜欢

Deep Packet Inspection Using Cuckoo Filter论文总结

广州地铁十三号线二期全线土建已完成53%,预计明年开通

Unity URP entry practice

How to query foreign literature?

二叉树的创建以及遍历
MySQL builds master-slave replication

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

Jintuo shares listed on the Shanghai Stock Exchange: the market value of 2.6 billion Zhang Dong family business has a strong color

DevSecOps,让速度和安全兼顾

Double the efficiency of dual screen collaboration lingyao x dual screen Pro leads the new trend of dual screen technology
随机推荐
sqlDeveloper工具快速入门
生泰尔科技IPO被终止:曾拟募资5.6亿 启明与济峰资本是股东
R语言ggplot2可视化:使用ggplot2可视化散点图、使用ggpubr包的theme_pubclean函数设置可视化图像不包含坐标轴线的主题(theme without axis lines)
How to write the format of a university thesis?
Data permissions should be designed like this, yyyds!
C # set different text watermarks for each page of word
Character function and string function and memory function
怎样在nature上查文献?
R语言使用lm函数构建带交互项的多元回归模型、使用step函数构建逐步回归模型筛选预测变量的最佳子集(step regression)
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
Qt开发高级进阶:如何在显示时适合视窗宽度和高度(fitWidth+fitHeight)
2. Add two numbers
Sqldeveloper tools quick start
Vs add settings for author information and time information
Selenium code storage
固态硬盘对游戏运行的帮助有多少
OSS deletes all files two days before the current time
[basic] the difference between dynamic link library and static link library
R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses fitted function to calculate y value (response value) vector
https://www.bilibili.com/video/BV1UL411W71w?p=12&vd_source=0471cde1c644648fafd07b54e303c905