2017-04-20 2 views
0

Ich versuche, meine GLSL Shader zu bekommen, gerichtetes Licht für uns zu berechnen, aber ich laufe in das Problem, dass die Richtung auf der viewMatrix zu werden abhängig scheint, während ich es in Worldspace festlegen möchte. Meine ursprüngliche Idee war, nur mit dem viewMatrix Worldspace Vector multipy (Directional Licht in Worldspace ist abhängig von viewMatrix

Vector4f dir = new Vector4f(dirLight.direction, 1); 
dir.mul(window.getCamera().viewMatrix); 

) in dem Code, bevor die Richtung einheitliche Einstellung, aber es scheint, dass das Licht immer noch auf meinem viewMatrix abhing ändert, so dass ich offensichtlich etwas nicht in Ordnung . der entsprechende Code meines Shader:

//vertex shader 

layout (location =0) in vec3 position; 
layout (location =1) in vec2 texCoord; 
layout (location =2) in vec3 vertexNormal; 
layout (location=3) in vec4 jointWeights; 
layout (location=4) in ivec4 jointIndices; 

out vec3 gmvVertexNormal; 
out vec3 gmvVertexPos; 

uniform mat4 projectionMatrix; 
uniform mat4 modelViewMatrix; 


struct Material 
{ 
    vec3 color; 
    int hasTexture; 
    float reflectance; 
}; 

uniform Material material; 



void main() 
{ 
    vec4 mvPos = modelViewMatrix * vec4(position, 1.0); 
    gl_Position = vec4(position,1.0); 
    gmvVertexNormal = normalize(modelViewMatrix * vec4(vertexNormal, 0.0)).xyz; 
    gmvVertexPos = position; 
} 

//geometry shader 
layout (triangles) in; 
layout (triangle_strip, max_vertices = 3) out; 

uniform mat4 projectionMatrix; 
uniform mat4 modelViewMatrix; 

out vec3 mvVertexNormal; 
out vec3 mvVertexPos; 

in vec3 gmvVertexNormal[3]; 
in vec3 gmvVertexPos[3]; 

vec3 calculateTriangleNormal(){ 
    vec3 tangent = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz; 
    vec3 bitangent = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz; 
    vec3 normal = cross(tangent, bitangent);  
    return normalize(normal); 
} 

void main() 
{ 
    vec4 mvPos = modelViewMatrix * vec4(gmvVertexPos[0], 1.0); 
    gl_Position = projectionMatrix * mvPos; 
    mvVertexNormal=calculateTriangleNormal(); 
    mvVertexPos=mvPos.xyz; 
    EmitVertex(); 
    mvPos = modelViewMatrix * vec4(gmvVertexPos[1], 1.0); 
    gl_Position = projectionMatrix * mvPos; 
    mvVertexNormal=calculateTriangleNormal(); 
    mvVertexPos=mvPos.xyz; 
    EmitVertex(); 
    mvPos = modelViewMatrix * vec4(gmvVertexPos[2], 1.0); 
    gl_Position = projectionMatrix * mvPos; 
    mvVertexNormal=calculateTriangleNormal(); 
    mvVertexPos=mvPos.xyz; 
    EmitVertex(); 
    EndPrimitive(); 
} 

//fragment shader 

in vec3 mvVertexNormal; 
in vec3 mvVertexPos; 

struct DirectionalLight { 
    vec3 color; 
    vec3 direction; 
    float intensity; 
}; 

const int MAX_DIRECTIONALLIGHT = 10; 
uniform int USED_DIRECTIONALLIGHTS; 
uniform DirectionalLight directionalLight[MAX_DIRECTIONALLIGHT]; 

vec4 calcDirectionalLight(DirectionalLight light, vec3 position, vec3 normal) 
{ 
    return calcLightColor(light.color, light.intensity, position, normalize(light.direction), normal); 
} 

vec4 calcLightColor(vec3 light_color, float light_intensity, vec3 position, vec3 to_light_dir, vec3 normal) 
{ 
    vec4 diffuseColor = vec4(0, 0, 0, 0); 
    vec4 specColor = vec4(0, 0, 0, 0); 

    // Diffuse Light 
    float diffuseFactor = max(dot(normal, to_light_dir), 0.0); 
    diffuseColor = vec4(light_color, 1.0) * light_intensity * diffuseFactor; 

    // Specular Light 
    vec3 camera_direction = normalize(- position); 
    vec3 from_light_dir = -to_light_dir; 
    vec3 reflected_light = normalize(reflect(from_light_dir , normal)); 
    float specularFactor = max(dot(camera_direction, reflected_light), 0.0); 
    specularFactor = pow(specularFactor, specularPower); 
    specColor = light_intensity * specularFactor * material.reflectance * vec4(light_color, 1.0); 

    return (diffuseColor + specColor); 
} 

void main() 
{ 


    vec4 totalLight = vec4(0); 
    //directional Light 
    for (int i=0; i<USED_DIRECTIONALLIGHTS; i++) { 
     totalLight += calcDirectionalLight(directionalLight[i], mvVertexPos, mvVertexNormal); 
    } 
    //... 

    fragColor = vec4(ambientLight, 1.0) + totalLight; 
} 

ich irgendwie bin neu in Shader so ich weiß nicht mehr, was ich zu tun. Um den Effekt zu bestimmen, den ich bekomme: das gerichtete Licht, das nur aus einer Richtung kommen sollte (in worldSpace), kommt aus verschiedenen Richtungen basierend auf der viewMatrix

Antwort

0

Ich fühle mich jetzt dumm. Ich fand die Antwort nur nach der Einlieferung.

Die Geometrie-Shader übergibt die vertexNormal direkt anstatt es mit dem modelViewMatrix mutiplying.

So ist die Antwort so:

mvVertexNormal=normalize(modelViewMatrix * vec4(calculateTriangleNormal(), 0.0)).xyz; 

statt dessen:

mvVertexNormal=calculateTriangleNormal();