gpt4 book ai didi

c++ - 法线贴图和光照出错,无法正确显示

转载 作者:太空狗 更新时间:2023-10-29 20:04:40 25 4
gpt4 key购买 nike

我正在研究法线贴图的实现,通过 ASSIMP 库计算切线 vector 。

法线贴图似乎非常适合模型矩阵接近单位矩阵的对象。只要我开始平移和缩放,我的照明似乎就关闭了。正如您在图片中看到的那样,法线贴图在容器立方体上完美运行,但照明在大地板上失败(镜面光的方向应该朝向玩家,而不是朝向容器)。

Lighting not working on Normal mapping

我觉得它与光的位置(当前从 x = -10 到 x = 10 随着时间的推移)有某种关系,只要我开始改变模型矩阵就没有正确地包含在计算中(通过翻译/缩放)。我发布了所有相关代码,希望你们能以某种方式看到我遗漏的东西,因为我已经盯着我的代码好几天了。

顶点着色器

#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 tangent;
layout(location = 3) in vec3 color;
layout(location = 4) in vec2 texCoord;

// fragment pass through
out vec3 Position;
out vec3 Normal;
out vec3 Tangent;
out vec3 Color;
out vec2 TexCoord;

out vec3 TangentSurface2Light;
out vec3 TangentSurface2View;

uniform vec3 lightPos;

// vertex transformation
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
mat3 normalMatrix = transpose(mat3(inverse(view * model)));
Position = vec3((view * model) * vec4(position, 1.0));
Normal = normalMatrix * normal;
Tangent = tangent;
Color = color;
TexCoord = texCoord;

gl_Position = projection * view * model * vec4(position, 1.0);

vec3 light = vec3(view * vec4(lightPos, 1.0));
vec3 n = normalize(normalMatrix * normal);
vec3 t = normalize(normalMatrix * tangent);
vec3 b = cross(n, t);
mat3 mat = mat3(t.x, b.x ,n.x, t.y, b.y ,n.y, t.z, b.z ,n.z);
vec3 vector = normalize(light - Position);
TangentSurface2Light = mat * vector;
vector = normalize(-Position);
TangentSurface2View = mat * vector;
}

片段着色器

#version 330

in vec3 Position;
in vec3 Normal;
in vec3 Tangent;
in vec3 Color;
in vec2 TexCoord;

in vec3 TangentSurface2Light;
in vec3 TangentSurface2View;

out vec4 outColor;

uniform vec3 lightPos;
uniform mat4 view;
uniform sampler2D texture0;
uniform sampler2D texture_normal; // normal

uniform float repeatFactor = 1;

void main()
{
vec4 texColor = texture(texture0, TexCoord * repeatFactor);
vec3 light = vec3(view * vec4(lightPos, 1.0));
float dist = length(light - Position);
float att = 1.0 / (1.0 + 0.01 * dist + 0.001 * dist * dist);
// Ambient
vec4 ambient = vec4(0.2);
// Diffuse
vec3 surface2light = normalize(TangentSurface2Light);
vec3 norm = normalize(texture(texture_normal, TexCoord * repeatFactor).xyz * 2.0 - 1.0);
float contribution = max(dot(norm, surface2light), 0.0);
vec4 diffuse = contribution * vec4(0.8);
// Specular
vec3 surf2view = normalize(TangentSurface2View);
vec3 reflection = reflect(-surface2light, norm); // reflection vector
float specContribution = pow(max(dot(surf2view, reflection), 0.0), 32);
vec4 specular = vec4(0.6) * specContribution;

outColor = (ambient + (diffuse * att)+ (specular * pow(att, 3))) * texColor;
}

OpenGL 绘图代码

void Render()
{
...

glm::mat4 view, projection; // Model will be done via MatrixStack
view = glm::lookAt(position, position + direction, up); // cam pos, look at (eye pos), up vec
projection = glm::perspective(45.0f, (float)width/(float)height, 0.1f, 1000.0f);
glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(basicShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

// Lighting
lightPos.x = 0.0 + sin(time / 125) * 10;

glUniform3f(glGetUniformLocation(basicShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);

// Objects (use bump mapping on this cube)
bumpShader.Use();
glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(bumpShader.shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(bumpShader.shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
MatrixStack::LoadIdentity();
MatrixStack::Scale(2);
MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container"));
glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "img"), 0);
glActiveTexture(GL_TEXTURE1); // Normal map
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("container_normal"));
glUniform1i(glGetUniformLocation(bumpShader.shaderProgram, "normalMap"), 1);

glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 1);
cubeNormal.Draw();

MatrixStack::LoadIdentity();
MatrixStack::Translate(glm::vec3(0.0f, -22.0f, 0.0f));
MatrixStack::Scale(glm::vec3(200.0f, 20.0f, 200.0f));
MatrixStack::ToShader(glGetUniformLocation(bumpShader.shaderProgram, "model"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor"));
glActiveTexture(GL_TEXTURE1); // Normal map
glBindTexture(GL_TEXTURE_2D, resources.GetTexture("floor_normal"));
glUniform1f(glGetUniformLocation(bumpShader.shaderProgram, "repeatFactor"), 100);
cubeNormal.Draw();

MatrixStack::LoadIdentity();
glActiveTexture(GL_TEXTURE0);

...
}

编辑我现在使用启用了“aiProcess_CalcTangentSpace”标志的 ASSIMP 库加载我的对象,并相应地更改我的着色器以适应新的变化。因为 ASSIMP 现在自动计算正确的切线 vector ,我应该有有效的切线 vector ,我的问题应该得到解决(正如 Nicol Bolas 所指出的),但我仍然有同样的问题,镜面照明表现得很奇怪,漫射照明没有真正出现.我想还有其他东西不能正常工作。我没有将您的答案标记为正确答案 Nicol Bolas(目前)并相应地更新了我的代码,因为我仍然缺少一些东西。

这可能与翻译有关。一旦我向模型矩阵添加平移(y 方向上的 -22.0f),它就会对奇怪的光照使用react。只要地板(实际上是一个立方体)没有平移,照明看起来就很好。

最佳答案

calculating the tangent vectors in the vertex shader

那么这就是你的问题。这对于任意表面是不可能的。

切线和副切线不是相互垂直的任意 vector 。它们是指向纹理坐标方向的模型空间方向 vector 。切线指向 S 纹理坐标的方向,双切线指向 T 纹理坐标的方向(如果您愿意,也可以是纹理坐标的 U 和 V)。

这有效地计算了纹理相对于表面上每个顶点的方向。您需要这种方向,因为当您想要理解切线空间 vector 时,纹理映射到表面的方式很重要。

请记住:切线空间是垂直于表面的空间。但是您需要知道该表面是如何映射到对象的,以便知道“向上”在哪里,例如。取一个正方形表面。您可以映射纹理,使正方形的 +Y 部分沿纹理​​的 +T 方向定向。或者它可以沿着正方形的 +X。您甚至可以对其进行映射,使纹理变形或以任意角度旋转。

切线和副切线 vector 旨在校正此映射。它们指向模型空间中的 S 和 T 方向。因此,结合法线,它们形成一个变换矩阵,从切线空间变换到 3 个 vector 所在的任何空间(您通常将 NBT 变换到相机空间或在使用它们之前用于照明的任何空间)。

不能仅通过采用法线并将其与某个任意 vector 相交来计算它们。这会产生垂直法线,但不是正确的法线。

为了正确计算切线/副切线,您需要访问多个顶点。您需要能够看到纹理坐标如何在网格表面上发生变化,这就是您计算相对于网格的 S 和 T 方向的方式。

顶点着色器不能访问多个顶点。几何着色器也不能(通常)访问足够的顶点来执行此操作。在 CPU 上离线计算切线/副切线。

关于c++ - 法线贴图和光照出错,无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18120651/

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