Okay, I did some testing again and I finally found the source of the problem. It turns out it has to do with the fragment shader. And a really stupid thing too. Here is my shadercode:
#version 120
struct Light
{
vec3 position;
vec4 color;
float range;
int directional;
int enabled;
};
uniform mat4 MVP;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 NormalMatrix;
//Material
uniform vec4 MaterialDiffuse = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 MaterialAmbient = vec4(0, 0, 0, 0);
uniform vec4 MaterialSpecular = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 MaterialEmission = vec4(0.0, 0.0, 0.0, 1.0);
uniform float MaterialShininess = 0;
uniform sampler2D MaterialTexture0;
uniform sampler2D MaterialTexture1;
uniform sampler2D MaterialTexture2;
uniform sampler2D MaterialTexture3;
uniform sampler2D MaterialTexture4;
uniform sampler2D MaterialTexture5;
uniform sampler2D MaterialTexture6;
uniform sampler2D MaterialTexture7;
uniform int UseTexture0;
uniform int UseTexture1;
uniform int UseTexture2;
uniform int UseTexture3;
uniform int UseTexture4;
uniform int UseTexture5;
uniform int UseTexture6;
uniform int UseTexture7;
//Light
uniform Light lights[100];
uniform int LightCount;
varying vec3 VertexPosition;
varying vec3 VertexNormal;
varying vec3 EyeDirection;
void main()
{
//The final fragment color
vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);
//Apply the material based on all the lights in the scene
vec4 finalTexture = vec4(1.0, 1.0, 1.0, 1.0);
if (UseTexture0 == 1) finalTexture = texture2D(MaterialTexture0, gl_TexCoord[0].st);
if (UseTexture1 == 1) finalTexture += texture2D(MaterialTexture1, gl_TexCoord[0].st);
if (UseTexture2 == 1) finalTexture += texture2D(MaterialTexture2, gl_TexCoord[0].st);
if (UseTexture3 == 1) finalTexture += texture2D(MaterialTexture3, gl_TexCoord[0].st);
if (UseTexture4 == 1) finalTexture += texture2D(MaterialTexture4, gl_TexCoord[0].st);
if (UseTexture5 == 1) finalTexture += texture2D(MaterialTexture5, gl_TexCoord[0].st);
if (UseTexture6 == 1) finalTexture += texture2D(MaterialTexture6, gl_TexCoord[0].st);
if (UseTexture7 == 1) finalTexture += texture2D(MaterialTexture7, gl_TexCoord[0].st);
for (int i=0; i<LightCount; i++) {
if (lights[i].enabled == 1) {
vec4 finalMaterial = vec4(0.0, 0.0, 0.0, 0.0);
vec3 lightDirection = (NormalMatrix * vec4(lights[i].position, 1)).xyz + EyeDirection;
if (lights[i].directional == 0) {
float lightDistance = length(lights[i].position - VertexPosition);
float lightAttenuation = 1 / (1.0 + (0 * lights[i].range) + ((1 / (lights[i].range * lights[i].range)) * (lightDistance * lightDistance)));
vec3 n = normalize(VertexNormal);
vec3 l = normalize(lightDirection);
float theta = clamp(dot(l, n), 0, 1);
finalMaterial += MaterialAmbient * finalTexture * lights[i].color;
finalMaterial += MaterialDiffuse * finalTexture * lights[i].color * theta;
finalMaterial += MaterialSpecular * finalTexture * pow(theta, 5);
finalMaterial *= lightAttenuation;
finalColor += finalMaterial / (lightDistance / lights[i].range);
} else {
vec3 n = normalize(VertexNormal);
vec3 l = normalize(lights[i].position);
float theta = clamp(dot(n, -l), 0, 1);
finalMaterial += MaterialAmbient * MaterialDiffuse * finalTexture;
finalMaterial += MaterialDiffuse * finalTexture * lights[i].color * theta;
finalMaterial += MaterialSpecular * finalTexture * pow(theta, 5);
finalColor += finalMaterial;
}
}
}
finalColor.w = MaterialDiffuse.w * finalTexture.w;
finalColor += vec4(MaterialEmission.xyz, 0.0);
//Set the result
gl_FragColor = finalColor;
}
The problem is rather odd and I have not been able to figure out how to fix it but at least I do know the source of the problem. It has to do with this line:
if (lights[i].enabled == 1) {
For some reason the condition is true, even when I set the uniform in my Java code to 0. So why is the condition still true? The reason I see that black quad is because none of the other lighting uniform variables have values, so it only would make sense strange results occur. Because if the condition is true, it would check if it is directional or not, since directional is 0, it assumes it is a point light and from that point on shit happens. When I put the entire code inside that if condition as a multiline comment that black quad won't render and things are the way I expected.
I'll search and try on, but also here any help would be appreciated! Kai, thanks for your help so far! Your advice helped me figuring this one out.
EDIT:
Is it just me or is the codeblock not appearing correct? I tried to edit and adjust it, but for some odd reason no scrollbars appear when they do in the previewmode when editing this message.