第二十一课,几何着色器(使用篇-法向量可视化)

法向量可视化

  1. 绘制物体
  2. 绘制物体法向量

VS

#version 450 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;

out VS_OUT {
    vec3 normal;
} vs_out;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

void main(){
    gl_Position = projection * view * model * vec4(aPos, 1.0); 
    
    //法向量旋转矩阵,当物体有旋转缩放操作时,可正确改变其法向量
    mat3 normalMatrix = mat3(transpose(inverse(view * model)));
    
    vs_out.normal = normalize(vec3(projection * vec4(normalMatrix * aNormal, 0.0)));
}

GS

#version 330 core
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in VS_OUT {
    vec3 normal;
} gs_in[];

const float MAGNITUDE = 0.1;//调整法线可视长度

out vec3 color;

void GenerateLine(int index)
{
    gl_Position = gl_in[index].gl_Position;
    color = vec3(0.0, 0.0, 0.0);
    EmitVertex();
    gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0) * MAGNITUDE;
    color = vec3(1.0, 1.0, 1.0);
    EmitVertex();
    EndPrimitive();
}

void main()
{
    GenerateLine(0); // 第一个顶点法线
    GenerateLine(1); // 第二个顶点法线
    GenerateLine(2); // 第三个顶点法线
}

FS

#version 450 core
out vec4 FragColor;

in vec3 color;

void main()
{
FragColor = vec4(color , 1.0);
}

绘制

normalDisplayShader.use();
normalDisplayShader.setMat4("projection", projection);
normalDisplayShader.setMat4("view", view);
normalDisplayShader.setMat4("model", model);
ourModel.Draw(normalDisplayShader);

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elsa的迷弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值