There's something wrong in Material.java

There is an issue with ambient lights in single pass. Ambient lights are added to the g_lightColor[] uniform.



Below is the code for Material updateLightListUniforms. Ambient light colors are added to the ambientLightColor uniform, as they should. They are also added to the lightColor uniform but should be overwritten because if the light is ambient it breaks out of the loop without adding to the iterator. Somehow they remain tho.



I confirmed they are actually in the uniform list by doing this in the shader:





if(g_LightColor.w == 3.0){

diffuseLight += vec3(4.0,0.0,0.0);

}





The grass became red.







protected void updateLightListUniforms(Shader shader, Geometry g, int numLights) {

if (numLights == 0) { // this shader does not do lighting, ignore.

return;

}



LightList lightList = g.getWorldLightList();

Uniform lightColor = shader.getUniform(“g_LightColor”);

Uniform lightPos = shader.getUniform(“g_LightPosition”);

Uniform lightDir = shader.getUniform(“g_LightDirection”);

lightColor.setVector4Length(numLights);

lightPos.setVector4Length(numLights);

lightDir.setVector4Length(numLights);



Uniform ambientColor = shader.getUniform(“g_AmbientLightColor”);

ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList));



int lightIndex = 0;



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

if (lightList.size() <= i) {



lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);

lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);

} else {

Light l = lightList.get(i);

ColorRGBA color = l.getColor();

//Here’s where ambient light is added. Note its added at index “i”, not “lightIndex”.

lightColor.setVector4InArray(color.getRed(),

color.getGreen(),

color.getBlue(),

l.getType().getId(),

i);



switch (l.getType()) {

case Directional:

DirectionalLight dl = (DirectionalLight) l;

Vector3f dir = dl.getDirection();

lightPos.setVector4InArray(dir.getX(), dir.getY(), dir.getZ(), -1, lightIndex);

break;

case Point:

PointLight pl = (PointLight) l;

Vector3f pos = pl.getPosition();

float invRadius = pl.getInvRadius();

lightPos.setVector4InArray(pos.getX(), pos.getY(), pos.getZ(), invRadius, lightIndex);

break;

case Spot:

SpotLight sl = (SpotLight) l;

Vector3f pos2 = sl.getPosition();

Vector3f dir2 = sl.getDirection();

float invRange = sl.getInvSpotRange();

float spotAngleCos = sl.getPackedAngleCos();



lightPos.setVector4InArray(pos2.getX(), pos2.getY(), pos2.getZ(), invRange, lightIndex);

lightDir.setVector4InArray(dir2.getX(), dir2.getY(), dir2.getZ(), spotAngleCos, lightIndex);

break;

case Ambient:

// skip this light. Does not increase lightIndex

continue;

default:

throw new UnsupportedOperationException("Unknown type of light: " + l.getType());

}

}



lightIndex++;

}



while (lightIndex < numLights) {

lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);

lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex);



lightIndex++;

}

}

They just mess up the list. The light color values aren’t actually accessible in g_LightColor[]. Gonna look into this some more.



Could it have something to do with lightColor[] etc. being buffers? Is that maybe why overwriting them in the loop does not fully remove them?

If someone could move that “lightColor add” stuff to under the switch block so its never written to begin with, that would be great. At least to try.