【发布时间】:2025-03-28 06:45:01
【问题描述】:
我尝试在我的 opengl 应用程序中实现法线映射,但无法正常工作。
This 是漫反射贴图(我添加了棕色),this 是法线贴图。
为了获得切线和双切线(在其他地方称为副法线?)向量,我为网格中的每个三角形运行此函数:
void getTangent(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2,
const glm::vec2 &uv0, const glm::vec2 &uv1, const glm::vec2 &uv2,
std::vector<glm::vec3> &vTangents, std::vector<glm::vec3> &vBiangents)
{
// Edges of the triangle : postion delta
glm::vec3 deltaPos1 = v1-v0;
glm::vec3 deltaPos2 = v2-v0;
// UV delta
glm::vec2 deltaUV1 = uv1-uv0;
glm::vec2 deltaUV2 = uv2-uv0;
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y)*r;
glm::vec3 bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x)*r;
for(int i = 0; i < 3; i++) {
vTangents.push_back(tangent);
vBiangents.push_back(bitangent);
}
}
之后,我调用 glBufferData 将顶点、法线、uvs、切线和双切线上传到 GPU。 顶点着色器:
#version 430
uniform mat4 ProjectionMatrix;
uniform mat4 CameraMatrix;
uniform mat4 ModelMatrix;
in vec3 vertex;
in vec3 normal;
in vec2 uv;
in vec3 tangent;
in vec3 bitangent;
out vec2 fsCoords;
out vec3 fsVertex;
out mat3 TBNMatrix;
void main()
{
gl_Position = ProjectionMatrix * CameraMatrix * ModelMatrix * vec4(vertex, 1.0);
fsCoords = uv;
fsVertex = vertex;
TBNMatrix = mat3(tangent, bitangent, normal);
}
片段着色器:
#version 430
uniform sampler2D diffuseMap;
uniform sampler2D normalMap;
uniform mat4 ModelMatrix;
uniform vec3 CameraPosition;
uniform struct Light {
float ambient;
vec3 position;
} light;
uniform float shininess;
in vec2 fsCoords;
in vec3 fsVertex;
in mat3 TBNMatrix;
out vec4 color;
void main()
{
//base color
const vec3 brownColor = vec3(153.0 / 255.0, 102.0 / 255.0, 51.0 / 255.0);
color = vec4(brownColor * (texture(diffuseMap, fsCoords).rgb + 0.25), 1.0);//add a fixed base color (0.25), because its dark as hell
//general vars
vec3 normal = texture(normalMap, fsCoords).rgb * 2.0 - 1.0;
vec3 surfacePos = vec3(ModelMatrix * vec4(fsVertex, 1.0));
vec3 surfaceToLight = normalize(TBNMatrix * (light.position - surfacePos)); //unit vector
vec3 eyePos = TBNMatrix * CameraPosition;
//diffuse
float diffuse = max(0.0, dot(normal, surfaceToLight));
//specular
float specular;
vec3 incidentVector = -surfaceToLight; //unit
vec3 reflectionVector = reflect(incidentVector, normal); //unit vector
vec3 surfaceToCamera = normalize(eyePos - surfacePos); //unit vector
float cosAngle = max(0.0, dot(surfaceToCamera, reflectionVector));
if(diffuse > 0.0)
specular = pow(cosAngle, shininess);
//add lighting to the fragment color (no attenuation for now)
color.rgb *= light.ambient;
color.rgb += diffuse + specular;
}
我得到的图像完全不正确。 (光定位在相机上)
我在这里做错了什么?
【问题讨论】:
-
我的赌注是片段着色器中的颜色设置/混合... 1. 你设置的输出颜色不止一次(如果我没记错的话,是在一些有大问题的 gfx 驱动程序上) 2.您正在添加
color和intensities而不是color*intensity但我可能会忽略一些事情。 3. 首先尝试普通/凹凸着色(忽略环境、反射、镜面反射...),然后如果可行,则一个接一个地添加其余部分...始终检查着色器编译日志 -
关于3,你的意思是我应该只使用漫反射照明吗?另外如何检查着色器编译日志?我的编译器不熟悉 glsl,但只熟悉 c++。
-
是的,您应该只设置一次颜色变量,并且对于具有以下内容的初学者:
color.rgb=browncolor.rgb*fabs(dot(surface_normal,light_direction));还添加了与我的着色器类似的答案,这些着色器的功能类似于您想要实现的目标。如果你不检查日志,那么你很容易错过一些东西,比如优化的 i/o 变量、语法错误等......
标签: c++ opengl glsl glm-math lighting