OpengGL-GLSL detailed analysis (Code Interpretation shader bloggers article only)

table of Contents

 

The first triangle rectangle (two triangles)

Shader coloring and ordinary type (codes and methods of use)

Texture paste (including the use of all the code and stb_image.h)

Using the matrix transformation (scaling, rotation, displacement), the use of library glm

Coordinate system into the 3D world (depth testing)


This article is just to follow me with white OpenGL written, only the interpretation of the article bloggers shader code, not suitable for large readership before God and did not see the article.

In the article did not make interpretation of the shader part of the code, to add here.

The first triangle rectangle (two triangles)

main.cpp part of the code

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);

Vertex shader part of the code

//点着色器
const char *vertexShaderSource = "#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"
"}\0";

#version 330 core statement OpengGL version, essential

layout (location = 0) in vec3 aPos; main bound is 0, digging a three compartment, so the location is equal to 0, in APOS is variable, into a student

gl_Position is built output variables, plus the just obtained aPos extended gl_Position can be assigned to variables w.

Fragment shader portion of code

//片段着色器
const char *fragmentShaderSource = "#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";

FragColor output variables, to the subsequent use of the pipeline, the first three values ​​corresponding to three values ​​of RGB, after the default 1.0.0.0f Representative Representative 0,1.0f 255.

Shader coloring and ordinary type (codes and methods of use)

main.cpp code

    glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(6);
    glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
    glEnableVertexAttribArray(7);

 Vertex shader

#version 330 core
layout (location = 6) in vec3 aPos;
layout (location = 7) in vec3 aColor;
out vec4 vertexColor;
void main()
{
   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
   vertexColor = vec4(aColor.x, aColor.y, aColor.z, 1.0);
}

layout (location = 6) in vec3 aPos; main apex position 6 are used, every six to dig 3, since the vertex array further comprises a color.
layout (location = 7) in vec3 aColor; main vertex colors 7 are used, every six to dig 3.
out vec4 vertexColor; vertex color output vector, used directly in the fragment shader, the die does not have to write in the shader

#version 330 core
in vec4 vertexColor;
out vec4 FragColor;
void main()
{
   FragColor = vertexColor;
}		

 in vec4 vertexColor; vertex color input vectors are inputted from the vertex shader, directly to FragColor, this need not color-coded in the shader

Texture paste (including the use of all the code and stb_image.h)

main.cpp code

glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(6);
glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(7);
glVertexAttribPointer(8, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(8);



//使用着色器
myShader->use();
glUniform1i(glGetUniformLocation(myShader->ID, "ourTexture"), 0); // 手动设置
myShader->setInt("ourFace", 3); // 或者使用着色器类设置

Vertex shader part of the code

#version 330 core
layout (location = 6) in vec3 aPos;
layout (location = 7) in vec3 aColor;
layout (location = 8) in vec2 aTexCoord;
out vec4 vertexColor;
out vec2 TexCoord;
void main()
{
   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
   vertexColor = vec4(aColor.x, aColor.y, aColor.z, 1.0);
   TexCoord = aTexCoord;
}

layout (location = 8) in vec2 aTexCoord; texture coordinates main function is to bind 67 No. 8, No. 8 texture coordinates are stored, the compartment 8 is dug once.

out vec2 TexCoord; texture coordinates out type, output to the next step

Fragment shader portion of code

#version 330 core
out vec4 FragColor;
in vec4 vertexColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
uniform sampler2D ourFace;
void main()
{
   //FragColor = vertexColor;
   FragColor = mix(texture(ourTexture, TexCoord), texture(ourFace, TexCoord), 0.6)*vertexColor;
}								

 in vec2 TexCoord; read texture coordinates

uniform sampler2D ourTexture; dimensional texture sampler may also be used in the uniform variables main.cpp outside shader
uniform sampler2D ourFace; Similarly, the second two-dimensional texture corresponding to the texture sampler

GLSL built-in function in need mix two values as parameters, and they are linearly interpolated according to the third argument. If the third value 0.0, it returns the first input; if so 1.0, it will return to the second input value. 0.6Will return 40%to the first color input and 60%a second input color, i.e. returns mixed color of two textures.

Using the matrix transformation (scaling, rotation, displacement), the use of library glm

main.cpp part of the code

unsigned int transformLoc = glGetUniformLocation(myShader->ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));

Vertex shader part of the code

#version 330 core
layout (location = 6) in vec3 aPos;
layout (location = 7) in vec3 aColor;
layout (location = 8) in vec2 aTexCoord;
out vec4 vertexColor;
out vec2 TexCoord;

uniform mat4 transform;

void main()
{
   gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0);
   vertexColor = vec4(aColor.x, aColor.y, aColor.z, 1.0);
   TexCoord = aTexCoord;
}

gl_Position = transform * vec4 (aPos.x, aPos.y, aPos.z, 1.0); using a matrix multiplication, vertex position changes, which is why the front of the complement variable w.

Coordinate system into the 3D world (depth testing)

main.cpp part of the code

unsigned int modelMatLoc = glGetUniformLocation(myShader->ID, "modelMat");
unsigned int viewMatLoc = glGetUniformLocation(myShader->ID, "viewMat");
unsigned int projMatLoc = glGetUniformLocation(myShader->ID, "projMat");
glUniformMatrix4fv(modelMatLoc, 1, GL_FALSE, glm::value_ptr(modelMat));
glUniformMatrix4fv(viewMatLoc, 1, GL_FALSE, glm::value_ptr(viewMat));
glUniformMatrix4fv(projMatLoc, 1, GL_FALSE, glm::value_ptr(projMat));

 Vertex shader part of the code

#version 330 core
layout (location = 6) in vec3 aPos;
layout (location = 8) in vec2 aTexCoord;
out vec2 TexCoord;

uniform mat4 modelMat;
uniform mat4 viewMat;
uniform mat4 projMat;

void main()
{
   gl_Position = projMat *  viewMat  * modelMat * vec4(aPos.x, aPos.y, aPos.z, 1.0);
   TexCoord = aTexCoord;
}

gl_Position = projMat * viewMat * modelMat * vec4 (aPos.x, aPos.y, aPos.z, 1.0); by three matrix, better 3d shows

More OpenGL knowledge: Modern OpenGL Getting Started Tutorial

Have questions, please comment below, please indicate the source, along with the original link, thank you! If infringement, please contact.

 

 

Published 163 original articles · won praise 471 · views 260 000 +

Guess you like

Origin blog.csdn.net/lady_killer9/article/details/89520420