我一直在使用http://www.opengl-tutorial.org/并且我下面的着色器来自他的教程,主要是我更改了变量名称并删除了标签 v 坐标反转,因为我使用的是 dds 图像。我确实也用环境纹理中的值替换了 vec3。
顶点着色器
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 modelVertex;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 modelNormal;
layout(location = 3) in vec3 modelVertexTangent;
layout(location = 4) in vec3 modelVertexBitangent;
// Output data will be interpolated for each fragment.
out vec2 texcoord;
out vec3 worldPosition;
out vec3 cameraEyeDirection;
out vec3 cameraLightDirection;
out vec3 lightDirectionTangent;
out vec3 eyeDirectionTangent;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
uniform mat4 V;
uniform mat4 M;
uniform mat3 MVR;
uniform vec3 worldLightPosition;
void main()
{
// Output position of the vertex, in clip space : MVP * position
gl_Position = MVP * vec4(modelVertex,1);
// Position of the vertex, in worldspace : M * position
worldPosition = (M * vec4(modelVertex,1)).xyz;
// Vector that goes from the vertex to the camera, in camera space.
// In camera space, the camera is at the origin (0,0,0).
vec3 cameraVertexPosition = (V * M * vec4(modelVertex,1)).xyz;
cameraEyeDirection = vec3(0,0,0) - cameraVertexPosition;
// Vector that goes from the vertex to the light, in camera space. M is ommited because it's identity.
vec3 cameraLightPosition = ( V * vec4(worldLightPosition,1)).xyz;
cameraLightDirection = worldLightPosition + cameraEyeDirection;
// UV of the vertex. No special space for this one.
texcoord = uv;
// model to camera = ModelView
vec3 cameraVertexTangent = MVR * normalize(modelVertexTangent);
vec3 cameraVertexBitangent = MVR * normalize(modelVertexBitangent);
vec3 cameraVertexNormal = MVR * normalize(modelNormal);
// You can use dot products instead of building this matrix and transposing it. See References for details.
mat3 tbn = transpose(mat3(cameraVertexTangent,cameraVertexBitangent,cameraVertexNormal));
lightDirectionTangent = tbn * cameraLightDirection;
eyeDirectionTangent = tbn * cameraEyeDirection;
}
片段着色器
#version 330 core
// Interpolated values from the vertex shaders
in vec2 texcoord;
in vec3 worldPosition;
in vec3 cameraEyeDirection;
in vec3 cameraLightDirection;
in vec3 lightDirectionTangent;
in vec3 eyeDirectionTangent;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D diffuseTextureSampler;
uniform sampler2D normalTextureSampler;
uniform sampler2D specularTextureSampler;
uniform sampler2D ambientTextureSampler;
uniform mat4 V;
uniform mat4 M;
uniform mat3 MVR;
uniform vec3 worldLightPosition;
uniform float LightPower;
uniform vec3 LightColor;
void main()
{
// Light emission properties
// You probably want to put them as uniforms
// Material properties
vec3 MaterialDiffuseColor = texture2D(diffuseTextureSampler, texcoord).rgb;
vec3 MaterialAmbientColor = texture2D(ambientTextureSampler, texcoord).rgb * MaterialDiffuseColor;
vec3 MaterialSpecularColor = texture2D(specularTextureSampler, texcoord).rgb * 0.3;
// Local normal, in tangent space.
vec3 tangentTextureNormal = normalize(texture2D(normalTextureSampler, texcoord).rgb*2.0 - 1.0);
// Distance to the light
float distanceBetween = length(worldLightPosition - worldPosition);
// Normal of the computed fragment, in camera space
vec3 n = tangentTextureNormal;
// Direction of the light (from the fragment to the light)
vec3 l = normalize(lightDirectionTangent);
// Cosine of the angle between the normal and the light direction,
// clamped above 0
// - light is at the vertical of the triangle -> 1
// - light is perpendicular to the triangle -> 0
// - light is behind the triangle -> 0
float cosTheta = clamp(dot(n,l),0,1);
// Eye vector (towards the camera)
vec3 E = normalize(eyeDirectionTangent);
// Direction in which the triangle reflects the light
vec3 R = reflect(-l,n);
// Cosine of the angle between the Eye vector and the Reflect vector,
// clamped to 0
// - Looking into the reflection -> 1
// - Looking elsewhere -> < 1
float cosAlpha = clamp(dot(E,R),0,1);
color = MaterialAmbientColor + // Ambient : simulates indirect lighting
MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distanceBetween*distanceBetween) + // Diffuse : "color" of the object
MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha,5) / (distanceBetween*distanceBetween); //Specular
}
所以我已经测试过,这不是加载着色器或生成我的模型所具有的值的问题,我正在关注这些部分的“T”教程。
该模型看起来带有漫反射纹理,并且在整个模型中更暗,而不是像预期的那样每个顶点,我根本看不到法线或镜面贴图。我添加了一个灯光位置作为着色器参数,我通过在 Maya 中加载我的模型并为我的灯光选择一个点来选择坐标。
所有纹理都是从 maya/mudbox 生成的库存纹理,我唯一做的就是转换为 dds DXT3 并翻转纹理。
我猜我的着色器中缺少一些东西。