Author Topic: Light type attribute  (Read 1321 times)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Light type attribute
« on: June 15, 2012, 04:35:07 pm »
Please, could you add attribute for type of light, that will be sent to shader? Something like this...

Code: [Select]
uniform lowp int lightTypes[8];
For me is impossible to solve this problem in another way...

I wrote per-pixel spotlight, but I can't use point light and spotlight together...



edit: oh, and of course it's needs next attribute like light direction, spot cut off and spot exp... so maybe add some user variable?
« Last Edit: June 15, 2012, 04:42:47 pm by Thomas. »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Light type attribute
« Reply #1 on: June 15, 2012, 05:23:44 pm »
You can already add as many user variables as you like. Just use one of the setUniform(...)-methods in GLSLShader. If you want to do it on a per object base, just extend GLSLShader and set the uniforms in setCurrentObject3D() of your implementations.

As for detecting which light is of which type...can't you simply define the first light as spot light and all others as point lights? That's what i did in my RPG in the dungeons. The first light is the light that influences the parallax mapping, all others use simple gouraud shading. The remaining problem is to ensure that the first light is always the same light, but you can do this with http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Light.html#setDistanceOverride(float) set to 0.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Light type attribute
« Reply #2 on: June 15, 2012, 08:47:56 pm »
But, what if I want 2 lights as spotlight? there isn't any other solution for this?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Light type attribute
« Reply #3 on: June 15, 2012, 09:23:56 pm »
Set both to 0...i know that it's a little kludgy, but you don't want to use a shader that bases this decision on a loop and some if(type==xxx)s in the fragment shader anyway, because it will be too slow on current devices (and will most likely render crap on Adreno...). The best solution is to use a specific shader for a specific situation. And if you already know the number of spot lights beforehand, you can easily make sure that they come first this way and greatly simplify your shader code.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Light type attribute
« Reply #4 on: June 15, 2012, 09:35:22 pm »
But I can't use different parameters for individual lights...? Any idea why is compare integer number so slow? Ok, I try do it this way... excellent, so now I have 16 vertex, 16 fragment shaders for lights, 16 and 16 for particles and other billboard planes ;D ...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Light type attribute
« Reply #5 on: June 15, 2012, 09:55:55 pm »
Every kind of dynamic branching stalls the pipeline if no branch prediction is being used. It's not the integer compare that is slow, it's the jump that might or might not happen. Some gpus even execute the code inside the if-block in all cases to avoid the pipeline stall and simplify the chip itself and only trash the result of the operation afterwards. ifs in the vertex shader are fine but they should be avoided in the fragment shader if possible. It not always is, of course.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Light type attribute
« Reply #6 on: June 15, 2012, 10:02:16 pm »
Thank you for the explanation :)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Light type attribute
« Reply #7 on: June 21, 2012, 08:15:19 pm »
After several hours of work everything running. I dynamically generate shaders depending on what is needed. For now working point and spot lights (but I need array of SimpleVector) in any number of. Direction light will be also added  :)