1
0
mirror of https://github.com/LearnOpenGL-CN/LearnOpenGL-CN.git synced 2025-08-23 04:35:28 +08:00

Update 几何着色器 Geometry Shader.md

This commit is contained in:
liaoyijian
2022-06-14 19:03:00 +08:00
committed by Gary Wang
parent 9356a9a337
commit 8b2a9ef60c

View File

@@ -420,7 +420,7 @@ normalDisplayShader.use();
DrawScene(); DrawScene();
``` ```
这次在几何着色器中,我们会使用模型提供的顶点法线,而不是自己生成,为了适配(观察和模型矩阵的)缩放和旋转,我们在将法线变换到裁剪空间坐标之前,先使用法线矩阵变换一次(几何着色器接受的位置向量是剪裁空间坐标,所以我们应该将法向量变换到相同的空间中)。这可以在顶点着色器中完成: 这次在几何着色器中,我们会使用模型提供的顶点法线,而不是自己生成,为了适配(观察和模型矩阵的)缩放和旋转,我们在将法线变换到观察空间坐标之前,先使用法线矩阵变换一次(几何着色器接受的位置向量是观察空间坐标,所以我们应该将法向量变换到相同的空间中)。这可以在顶点着色器中完成:
```c++ ```c++
#version 330 core #version 330 core
@@ -431,19 +431,18 @@ out VS_OUT {
vec3 normal; vec3 normal;
} vs_out; } vs_out;
uniform mat4 projection;
uniform mat4 view; uniform mat4 view;
uniform mat4 model; uniform mat4 model;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(aPos, 1.0); gl_Position = view * model * vec4(aPos, 1.0);
mat3 normalMatrix = mat3(transpose(inverse(view * model))); mat3 normalMatrix = mat3(transpose(inverse(view * model)));
vs_out.normal = normalize(vec3(projection * vec4(normalMatrix * aNormal, 0.0))); vs_out.normal = normalize(vec3(vec4(normalMatrix * aNormal, 0.0)));
} }
``` ```
变换后的裁剪空间法向量会以接口块的形式传递到下个着色器阶段。接下来,几何着色器会接收每一个顶点(包括一个位置向量和一个法向量),并在每个位置向量处绘制一个法线向量: 变换后的观察空间法向量会以接口块的形式传递到下个着色器阶段。接下来,几何着色器会接收每一个顶点(包括一个位置向量和一个法向量),并在每个位置向量处绘制一个法线向量:
```c++ ```c++
#version 330 core #version 330 core
@@ -456,11 +455,14 @@ in VS_OUT {
const float MAGNITUDE = 0.4; const float MAGNITUDE = 0.4;
uniform mat4 projection;
void GenerateLine(int index) void GenerateLine(int index)
{ {
gl_Position = gl_in[index].gl_Position; gl_Position = projection * gl_in[index].gl_Position;
EmitVertex(); EmitVertex();
gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0) * MAGNITUDE; gl_Position = projection * (gl_in[index].gl_Position +
vec4(gs_in[index].normal, 0.0) * MAGNITUDE);
EmitVertex(); EmitVertex();
EndPrimitive(); EndPrimitive();
} }