LWJGL2多灯不工作?所有实体都是黑色

时间:2017-11-20 11:34:33

标签: java glsl lwjgl light

所以,我正在关注多个灯光和点光源的ThinMatrixs教程。我相信我已经正确地遵循了一切。

enter image description here

我首先遵循了多灯教程,并没有任何实体和地形受到影响。我认为做下一个与衰减有关的教程可以解决这个问题。现在我的所有物体都是黑色阴影。

我不确定会出现什么问题。任何帮助将不胜感激。

Code here

Thinmatrix tutorial 25 multiple lights

// FRAGMENT SHADER

#version 400 core

in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector[4];
in vec3 toCameraVector;
in float visibility;

out vec4 out_Color;

uniform sampler2D modelTexture;
uniform vec3 lightColour[4];
uniform vec3 attenuation[4];
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;

void main(void){


    vec3 unitNormal = normalize(surfaceNormal);
    vec3 unitVectorToCamera = normalize(toCameraVector);

    vec3 totalDiffuse = vec3(0.0);
    vec3 totalSpecular = vec3(0.0);

    for(int i = 0; i < 4; i++) {

        float distance = length(toLightVector[i]);
        float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
        vec3 unitLightVector = normalize(toLightVector[i]);
        float nDot1 = dot(unitNormal, unitLightVector);
        float brightness = max(nDot1, 0.0);
        vec3 lightDirection = -unitLightVector;
        vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
        float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
        specularFactor = max(specularFactor, 0.0);
        float dampedFactor = pow(specularFactor, shineDamper);

        totalDiffuse = totalDiffuse + (brightness * lightColour[i])/attFactor;
        totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColour[i])/attFactor;
}

  totalDiffuse = max(totalDiffuse, 0.2);


    vec4 textureColour = texture(modelTexture,pass_textureCoordinates);
    if(textureColour.a<0.5) {
        discard;
    }

    out_Color =  vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular,1.0);
    out_Color = mix(vec4(skyColour,1.0),out_Color, visibility);

}

VERTEX SHADER:

#version 400 core

in vec3 position;
in vec2 textureCoordinates;
in vec3 normal;

out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector[4];
out vec3 toCameraVector;
out float visibility;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition[4];

uniform float useFakeLighting;

uniform float numberOfRows;
uniform vec2 offset;

const float density = 0.0035;
const float gradient = 5.0;

void main(void){



    vec4 worldPosition = transformationMatrix * vec4(position,1.0);
    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelativeToCam;
    pass_textureCoordinates = (textureCoordinates/numberOfRows) + offset;

    vec3 actualNormal = normal;
    if(useFakeLighting > 0.5) {
    actualNormal = vec3(0.0,1.0,0.0);
    }

    surfaceNormal = (transformationMatrix * vec4(actualNormal,0.0)).xyz;
    for(int i =0; i< 4;i++) {
        toLightVector[i] = lightPosition[i] - worldPosition.xyz;
    }

    toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;

    float distance = length(positionRelativeToCam.xyz);
    visibility = exp(-pow((distance*density),gradient));
    visibility = clamp(visibility,0.0,0.9);

}

1 个答案:

答案 0 :(得分:1)

在您的StaticShader类中:

for(int i = 0; i < MAX_LIGHTS; i++) {
    location_lightPosition[i] = super.getUniformLocation("lightPosition{" + i + "}");
    location_lightColour[i] = super.getUniformLocation("lightColour{" + i + "}");
    location_attenuation[i] = super.getUniformLocation("attenuation[" + i + "}");
}

你正在使用} / {而不是] / [,因为opengl无法找到制服并且亮度计算不起作用。

如果你想检查是否找到了制服,只需在你的ShaderProgram类中使用这段代码:

protected int getUniformLocation(String uniformName){
    int loc = GL20.glGetUniformLocation(programID,uniformName);
    if(loc==-1) System.err.println("Uniform with name \""+uniformName+"\" not found!");
    return loc;
}

来自opengl文档:

  

glGetUniformLocation返回表示位置的整数   程序对象中的特定统一变量。名字必须是   null终止的字符串,不包含空格。名字必须是   程序中非活动的统一变量名,不是结构,   结构数组,或向量或矩阵的子组件。 此   如果name与活动制服不对应,则函数返回-1   程序中的变量,如果名称以保留的前缀&#34; gl _&#34;,或开头   如果name与原子计数器或命名的统一块相关联。

如果仍然无法检查各个颜色值。 首先检查纹理:

out_Color = textureColour;

再次检查漫反射光:

out_Color = vec4(totalDiffuse,1.0);

第三次检查高光:

out_Color = vec4(totalSpecular,1.0);

我希望这会有所帮助。