GLSL 4.0网格旋转是否正常?请帮忙

时间:2013-07-06 02:04:51

标签: rotation glsl

有人可以帮助我使用我的OpenGL GLSL 4.0着色器。我遇到的问题是当加载和渲染3d(0bj文件)时,除了网格文件的法线之外,所有工作(光照良好,网格顶点显示都很好)。具体来说,当obj文件在其局部/模型空间中旋转时,法线看起来不会根据光线位置及其当前方向照亮网格(我希望这有点意义)。

我认为问题出在我的普通矩阵上。

问题:当我的3D网格旋转时,光照网格化(不反映光线位置)。

非常感谢任何帮助。提前谢谢

VertexShader

 #version 400
        //Handle translation, projection, etc
        struct Matrix {
            mat4 mvp;
            mat4 mv;
            mat4 view;
            mat4 projection;
        };  
        struct Light {
            vec3 position;
            vec3 color;
            vec3 direction;
            float intensity;
            vec3 ambient;
        };
        //---------------------------------------------------
        //INPUT
        //---------------------------------------------------
        //Per-Vertex Data
        //---------------------------------------------------
        layout (location = 0) in vec3 inputPosition;
        layout (location = 1) in vec3 inputNormal;
        layout (location = 2) in vec3 inputTexture;
        //--------------------------------------------
        // UNIFORM:INPUT Supplied Data from C++ application
        //--------------------------------------------
        uniform Matrix matrix;
        uniform Light light;
        uniform vec3 cameraPosition;

        out vec3 fragmentNormal;
        out vec3 cameraVector;
        out vec3 lightVector;
        out vec2 texCoord;
        void main() {   
            // output the transformed vertex
            gl_Position = matrix.mvp * vec4(inputPosition,1.0);


            //When using, (vec3,0.0)
            mat3 Normal_Matrix = mat3(    transpose(inverse(matrix.mv))  );


            // set the normal for the fragment shader and
            // the vector from the vertex to the camera
            vec3 vertex     = (matrix.mv * vec4(inputPosition,1.0)).xyz;

            //----------------------------------------------------------
            //The problem (i think) is here
            //----------------------------------------------------------
            fragmentNormal  = normalize(Normal_Matrix * inputNormal);
            cameraVector    = (matrix.mv *vec4(cameraPosition,1.0)).xyz - vertex ;


            lightVector = vertex - (matrix.mv * vec4(light.position,1.0)).xyz;  


            //store the texture data
            texCoord = inputTexture.xy;




        }

片段着色器

#version 400


const int   NUM_LIGHTS       = 3;
const float     MAX_DIST         = 15.0;
const float     MAX_DIST_SQUARED = MAX_DIST * MAX_DIST;

const vec3 AMBIENT = vec3(0.152, 0.152, 0.152); //0.2 for all component is a good dark value



struct Light {
    vec3 position;
    vec3 color;
    vec3 direction;
    float intensity;
    vec3 ambient;
};

//the image
uniform sampler2D textureSampler;

uniform Light light;

//in: used interpolation, must define both in vertex&fragment shader; 


out vec4 finalOutput;


in vec2 texCoord;   //Texture Coordinate
//in: used interpolation, must define both in vertex&fragment shader; 
in vec3 fragmentNormal;
in vec3 cameraVector;
in vec3 lightVector;

void main() {
    vec4 texColor = texture2D(textureSampler, texCoord);
    // initialize diffuse/specular lighting
    vec3 diffuse =  vec3(0.005f, 0.005f, 0.005f);
    vec3 specular = vec3(0.00f, 0.00f, 0.00f);

    // normalize the fragment normal and camera direction
    vec3 normal     = normalize(fragmentNormal);
    vec3 cameraDir  = normalize(cameraVector);

    // loop through each light

        // calculate distance between 0.0 and 1.0
        float dist = min(dot(lightVector, lightVector), MAX_DIST_SQUARED) / MAX_DIST_SQUARED;
        float distFactor = 1.0 - dist;

        // diffuse
        vec3 lightDir = normalize(lightVector);
        float diffuseDot = dot(normal, lightDir);

        diffuse += light.color * clamp(diffuseDot, 0.0, 1.0) * distFactor;

        // specular
        vec3 halfAngle = normalize(cameraDir + lightDir);
        vec3 specularColor = min(light.color + 0.8, 1.0);
        float specularDot = dot(normal, halfAngle);
        specular += specularColor * pow(clamp(specularDot, 0.0, 1.0), 16.0) * distFactor;


    vec4 sample0 = vec4(1.0, 1.0, 1.0, 1.0);

    vec3 ambDifCombo = (diffuse + AMBIENT);
    //calculate the final color
    vec3 color = clamp(sample0.rgb * ambDifCombo + specular, 0.0, 1.0);

    finalOutput = vec4(color * vec3(texColor), sample0.a);
}

1 个答案:

答案 0 :(得分:1)

你不应该改变你的光线位置。在网格旋转时,您的灯应保持静止。而不是:

lightVector = vertex - (matrix.mv * vec4(light.position,1.0)).xyz; 

这样做:

lightVector = vertex - light.position; 

我也会尝试不改变你的相机位置。