gpt4 book ai didi

opengl - 切线空间法线贴图-着色器完整性检查

转载 作者:行者123 更新时间:2023-12-04 04:31:41 27 4
gpt4 key购买 nike

我从切线空间法线贴图着色器中得到了一些非常奇特的结果:)。在这里显示的场景中,茶壶和方格墙正在使用我的普通Phong-Blinn着色器进行着色(显然,茶壶背面剔除器使它具有短暂的外观:-)。我尝试将法线贴图添加到球体中,并产生迷幻效果:

光线从右侧发出(几乎可见为黑色 Blob )。我在球体上使用的法线贴图如下所示:

我正在使用AssImp处理输入模型,因此它会自动为我计算每个顶点的切线和双法线。

像素和顶点着色器在下面。我不太确定出了什么问题,但是如果正切基矩阵某种程度上出了错,也不会令我感到惊讶。我假设我必须将事物计算到眼睛空间中,然后将眼睛和光向量转换为切线空间,这是解决该问题的正确方法。请注意,光源位置已进入 View 空间中的着色器。

// Vertex Shader
#version 420

// Uniform Buffer Structures

// Camera.
layout (std140) uniform Camera
{
mat4 Camera_Projection;
mat4 Camera_View;
};

// Matrices per model.
layout (std140) uniform Model
{
mat4 Model_ViewModelSpace;
mat4 Model_ViewModelSpaceInverseTranspose;
};

// Spotlight.
layout (std140) uniform OmniLight
{
float Light_Intensity;

vec3 Light_Position; // Already in view space.
vec4 Light_Ambient_Colour;
vec4 Light_Diffuse_Colour;
vec4 Light_Specular_Colour;
};

// Streams (per vertex)
layout(location = 0) in vec3 attrib_Position;
layout(location = 1) in vec3 attrib_Normal;
layout(location = 2) in vec3 attrib_Tangent;
layout(location = 3) in vec3 attrib_BiNormal;
layout(location = 4) in vec2 attrib_Texture;

// Output streams (per vertex)
out vec3 attrib_Fragment_Normal;
out vec4 attrib_Fragment_Position;
out vec3 attrib_Fragment_Light;
out vec3 attrib_Fragment_Eye;

// Shared.
out vec2 varying_TextureCoord;

// Main
void main()
{
// Compute normal.
attrib_Fragment_Normal = (Model_ViewModelSpaceInverseTranspose * vec4(attrib_Normal, 0.0)).xyz;

// Compute position.
vec4 position = Model_ViewModelSpace * vec4(attrib_Position, 1.0);

// Generate matrix for tangent basis.
mat3 tangentBasis = mat3( attrib_Tangent,
attrib_BiNormal,
attrib_Normal);

// Light vector.
attrib_Fragment_Light = tangentBasis * normalize(Light_Position - position.xyz);

// Eye vector.
attrib_Fragment_Eye = tangentBasis * normalize(-position.xyz);

// Return position.
gl_Position = Camera_Projection * position;
}

...,像素着色器如下所示:
// Pixel Shader
#version 420

// Samplers
uniform sampler2D Map_Normal;

// Global Uniforms

// Material.
layout (std140) uniform Material
{
vec4 Material_Ambient_Colour;
vec4 Material_Diffuse_Colour;
vec4 Material_Specular_Colour;
vec4 Material_Emissive_Colour;

float Material_Shininess;
float Material_Strength;
};

// Spotlight.
layout (std140) uniform OmniLight
{
float Light_Intensity;

vec3 Light_Position;
vec4 Light_Ambient_Colour;
vec4 Light_Diffuse_Colour;
vec4 Light_Specular_Colour;
};

// Input streams (per vertex)
in vec3 attrib_Fragment_Normal;
in vec3 attrib_Fragment_Position;
in vec3 attrib_Fragment_Light;
in vec3 attrib_Fragment_Eye;

// Shared.
in vec2 varying_TextureCoord;

// Result
out vec4 Out_Colour;

// Main
void main(void)
{
// Compute normals.
vec3 N = normalize(texture(Map_Normal, varying_TextureCoord).xyz * 2.0 - 1.0);
vec3 L = normalize(attrib_Fragment_Light);
vec3 V = normalize(attrib_Fragment_Eye);
vec3 R = normalize(-reflect(L, N));

// Compute products.
float NdotL = max(0.0, dot(N, L));
float RdotV = max(0.0, dot(R, V));

// Compute final colours.
vec4 ambient = Light_Ambient_Colour * Material_Ambient_Colour;
vec4 diffuse = Light_Diffuse_Colour * Material_Diffuse_Colour * NdotL;
vec4 specular = Light_Specular_Colour * Material_Specular_Colour * (pow(RdotV, Material_Shininess) * Material_Strength);

// Final colour.
Out_Colour = ambient + diffuse + specular;
}

编辑:3D Studio渲染场景(以显示球面上的UV正常):

最佳答案

我认为您的着色器还可以,但是您在球体上的纹理坐标完全不可用。好像它们在经度上向两极扭曲。

关于opengl - 切线空间法线贴图-着色器完整性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com