0

I am creating a shader that would deal with up to, say, 10 point lights for a scene ; but I have trouble with the "up to". Here is what I want to do :

vec3 temp = CalcPointLight(pointLights[1], norm, FragPos, viewDir, fragDiffuse, fragSpecular);
temp *= (1.0 - ShadowCalculationPointLight(FragPos, pointLights[1].position, pointLights[1].shadowCubeMap));
temp *= pointLights[1].use;
result += temp

So I have an array of structures pointLights and I'll just do the same thing for all 10 of them, and in order to avoid branching by adding ifstatements to check if the attribute useis equal to 1 I multiply the result of the light / shadow calculation by the pointLights[1].value.

But for some reasons it seems that if the different attributes of pointLights[1]are not initialized, then the resultvariable (which is later sent as the fragment color) is not drawn anymore, even when the temp vector is supposed to be multiplied by 0.

To me there are two solutions to that problem : either I have to generate / write a shader for each number of point lights I have to deal with (so if I want to use up to 10 lights I'll have to write 11 different shaders) ; or I always initialize all of my 10 lights with values that will make my functions always return (0,0,0). For the latter solution I am not sure about how to initialize a samplerCube such that it will contain exclusively 0s... Or is there another solution ?

EDIT : I am trying to implement the second solution but as I mentioned earlier but again, how should I initialize the samplerCube ? Should I have 6 textures of 1x1px loaded as a cubemap in the GPU memory and when I want to initialize the uniforms in my shaders, then send that "empty" cubemap sent, or is there a more fancy way of doing so (having a sampler giving only (0,0,0,1) ) ?

Jambon
  • 115
  • 10
  • If `pointLights[]` is a uniform, what prevents you from initializating it on CPU side with 0 or 1 right before passing it to the GPU? – Ripi2 Oct 21 '19 at 18:58
  • Uniform are by default initialized by 0. – Rabbid76 Oct 21 '19 at 19:00
  • "*in order to avoid branching*" Why are you trying to avoid a static branch? Do you think it will [make your code faster](https://stackoverflow.com/q/37827216/734069)? – Nicol Bolas Oct 21 '19 at 20:41
  • Oh thank you Nicol Bolas, I heard about that branching problem and never tried to dig further into it, did not know about all the details your answer provides. – Jambon Oct 21 '19 at 20:47

0 Answers0