Here's code for a single pass lighting system

Anyone more then me interested in this?



I am using this code in the grass/tree system. It renders all grass geometry in only one pass. The shaders does lots of non light-related stuff as well (like moving verts around etc.) so I kind of need it.



It works just like regular single-pass but ambient lights does not pollute the list, and there’s a variable so you don’t have to iterate over all four lights inside the shaders (when there are less then four non-ambient lights in the list it’s filled up with empty lights normally to reach 4).



Btw. if you don’t know what this is, or what’s going on, then you probably don’t need this stuff so its no loss.



A minimal matdef:





MaterialDef My MaterialDef {



MaterialParameters {

Int NumLights

}



Technique {



LightMode SinglePass



VertexShader GLSL100: Blablabla.vert

FragmentShader GLSL100: Blablabla.frag



Defines {

NUM_LIGHTS : NumLights

}

}



}





Shader stuff:









uniform vec4 g_LightPosition[4];

uniform vec4 g_LightColor[4];

uniform vec4 g_LightDirection[4];



void main() {



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



// light stuff



}





}







The material class just has to override updateLightListUniforms.



The material class:



http://pastebin.com/7MgBZQde



Its still a work in progress but it looks pretty good so far. Gonna keep an updated version in the forester lib anyways and notify when its changed.

Why not

[java]

uniform vec4 g_LightPosition[NUM_LIGHTS];

uniform vec4 g_LightColor[NUM_LIGHTS];

uniform vec4 g_LightDirection[NUM_LIGHTS];

[/java]

?

It doesn’t work otherwise. Have to set a maximum value because of how the uniforms are handled within the engine. This pass is not really supported any more so it must be “tricked” into working.



Its still a work in progress tho, may find a better way later.

If you find a way to set boolean[] (bool[]) and Texture2D[] (sampler2D[]) uniforms, please let me know. Setting Matrix4fArray seems to be supported, but it freezes my rendering. A solution might be something like this.

[java]

#if NUM_PROJECTORS > 0

uniform vec3 m_ProjectorLocation0;

uniform mat4 m_ProjectorViewProjectionMatrix0;

uniform sampler2D m_ProjectiveMap0;

#endif



#if NUM_PROJECTORS > 1

uniform vec3 m_ProjectorLocation1;

uniform mat4 m_ProjectorViewProjectionMatrix1;

uniform sampler2D m_ProjectiveMap1;

#endif



//…



// and processing the same

[/java]

On older hardware, loops are unrolled anyway afaik.

I know texture arrays are supported. I think there is an example of it in the jme tests even. Never used it myself tho.



I know there is are matrix arrays but I never used it. Maybe make a thread about it if there are problems.



EDIT: tho you have to be careful when using defines for array lengths. That’s why i use size 4 on the light lists in my example. The NUM_LIGHTS only limits the number of iterations, it does not change the size of the arrays themselves. It is a weakness atm, but i am sure it will become better later.