2017-11-20 12 views
0

Also ich befolge ThinMatrixs Tutorial über mehrere Lichter und Punktlichter. Ich glaube, ich habe alles richtig verfolgt.LWJGL2 Mehrere Lichter funktionieren nicht? Alle Entitäten sind schwarz

enter image description here

Ich folgte zunächst die mehrere Lichter Tutorial und keine der Einheiten und Gelände beeinflusst zu werden wurden. Ich dachte, das nächste Tutorial, das mit Dämpfung zu tun hat, würde dieses Problem lösen. Jetzt sind alle meine Objekte schwarz schattiert.

Ich bin mir nicht sicher, was könnte falsch sein. Jede Hilfe wäre willkommen.

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); 

} 

Antwort

1

In Ihrem StaticShader Klasse:

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 + "}"); 
} 

Sie verwenden}/{anstelle von]/[, deshalb kann opengl die Uniform nicht finden und die Helligkeitsberechnung funktioniert nicht.

Wenn Sie möchten, zu überprüfen, ob eine einheitliche chage gefunden wird nur diesen Code in Ihre ShaderProgram Klasse:

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; 
} 

Von der opengl Dokumentation:

glGetUniformLocation eine ganze Zahl zurückgibt, die die Position darstellt einer bestimmten Uniform-Variable innerhalb eines Programmobjekts. Der Name muss null terminierte Zeichenfolge sein, die kein Leerzeichen enthält. Name muss ein aktiver Uniform-Variablenname im Programm sein, der keine Struktur, kein Array von Strukturen oder eine Unterkomponente eines Vektors oder einer Matrix ist. Diese Funktion gibt -1 zurück, wenn der Name einer aktiven Uniform Variable im Programm nicht entspricht, wenn Name mit dem reservierten Präfix "gl_" beginnt, oder , wenn der Name einem Atomzähler oder einem benannten Einheitsblock zugeordnet ist.

Wenn es immer noch nicht funktioniert, überprüfen Sie die einzelnen Farbwerte. Überprüfen Sie zuerst die Textur:

out_Color = textureColour; 

Zweite das diffuse Licht überprüfen:

out_Color = vec4(totalDiffuse,1.0); 

Dritte die spiegelnde Licht überprüfen:

out_Color = vec4(totalSpecular,1.0); 

Ich hoffe, das hilft.

+0

Ich wiederholte das Tutorial und aus irgendeinem Grund funktionierte es zu dieser Zeit. Aber Ihre Kommentare werden sehr geschätzt. – winnieTheWind

Verwandte Themen