Just a note:
If you render all lights in one loop (forward lighting) you can simply have an array with let’s say size 40 but only loop through the amount of lights you actually use.
Not sure if I’m way too late here, but yeah.
#define MAXLIGHT 20
float3 PointLightPosition[MAXLIGHT];
float4 PointLightColor[MAXLIGHT];
float PointLightIntensity[MAXLIGHT];
float3 PointLightDirection[MAXLIGHT];
float PointLightRadius[MAXLIGHT];
int MaxLightsRendered = 0;
then in the pixel or vertex shader
…
[loop]
for (int i = 0; i < MaxLightsRendered; i++)
{
float3 DirectionToLight = PointLightPosition[i] - worldPos;
float DistanceSq = lengthSquared(DirectionToLight);
float radius = PointLightRadius[i];
[branch]
if (DistanceSq < abs(radius*radius))
{
//calculate lighting
etc...
in the c# code to pass arrays I use…
private static readonly Vector3[] PointLightPosition = new Vector3[MaxLightsGpu];
private static readonly Vector4[] PointLightColor = new Vector4[MaxLightsGpu];
private static readonly float[] PointLightIntensity = new float[MaxLightsGpu];
private static readonly float[] PointLightRadius = new float[MaxLightsGpu];
private static readonly Vector3[] PointLightDirection = new Vector3[MaxLightsGpu];
…
public static void Initialize(Effect lightingEffect)
{
//POINTLIGHTS
_lightingEffectPointLightPosition = lightingEffect.Parameters["PointLightPosition"];
_lightingEffectPointLightColor = lightingEffect.Parameters["PointLightColor"];
_lightingEffectPointLightIntensity = lightingEffect.Parameters["PointLightIntensity"];
_lightingEffectPointLightDirection = lightingEffect.Parameters["PointLightDirection"];
_lightingEffectPointLightRadius = lightingEffect.Parameters["PointLightRadius"];
_lightingEffectMaxLightsRendered = lightingEffect.Parameters["MaxLightsRendered"];
//LightTiles = new int[LightTilesHeight*LightTilesWidth];
}
…
blabla calculations - is our light actually in the scene etc.
if(currentIndex>0)
{
_lightingEffectPointLightPosition.SetValue(PointLightPosition);
_lightingEffectPointLightColor.SetValue(PointLightColor);
_lightingEffectPointLightIntensity.SetValue(PointLightIntensity);
_lightingEffectPointLightRadius.SetValue(PointLightRadius);
_lightingEffectPointLightDirection.SetValue(PointLightDirection);
}
_lightingEffectMaxLightsRendered.SetValue(currentIndex);
I post this just as a short comprehensive post of how to pass arrays, not saying this lighting set up is ideal.