法线贴图,镜面贴图和环境贴图

时间:2012-11-24 04:18:18

标签: opengl glsl

我一直在使用http://www.opengl-tutorial.org/,我的下面的着色器来自他的教程,主要是我更改了变量名称,我删除了标记v coord inversion,因为我使用的是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并翻转纹理。

我猜我在着色器中遗漏了一些东西。

0 个答案:

没有答案