私はhttp://www.opengl-tutorial.org/を使用しており、以下のシェーダーは彼のチュートリアルから派生したものです。主に、変数名を変更し、dds イメージを使用しているため、タグ v 座標反転を削除しました。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 に変換してテクスチャを反転することでした。
シェーダーに何かが欠けていると思います。