Author Topic: Light position transformation  (Read 5233 times)

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Light position transformation
« on: February 08, 2015, 09:19:16 am »
Hi egon,

How do we apply light position final transformation for shader? I tried use Camera.transform the the light position and the final result is incorrect. I need it for uploading custom light position to shader. Thanks n look forward to ur reply.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #1 on: February 08, 2015, 12:58:55 pm »
The final light positions in world space will be injected into the shader if that's what you mean. If it's then...what do you mean exactly?

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #2 on: February 08, 2015, 02:33:12 pm »
I thought final light position for shader is in camera space. How do we get it in world space?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #3 on: February 08, 2015, 04:38:07 pm »
My bad. You are right, they are in camera space. If you need them in world space, you have to add your own uniform and inject the position that you can obtain from each light into it but with y and z negated. But what do you need them for anyway?

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #4 on: February 08, 2015, 04:54:17 pm »
Quote
they are in camera space. If you need them in world space, you have to add your own uniform and inject the position that you can obtain from each light into it but with y and z negated
Im using the same light calculation from JPCT default shader. So should the final light position in shader be in camera space or world space? If camera space, we just use Camera.tranform(), else negate y and z for world space?

Quote
But what do you need them for anyway?
I want certain objects to receive light from different location, while not affecting the rest.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #5 on: February 09, 2015, 08:46:58 am »
Camera space, if you want to apply the same calculations on it. You can obtain such a position by simply using Camera.transform() and then negate y and z, just as you've mentioned.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #6 on: February 09, 2015, 03:32:08 pm »
Strangely, it works correctly when I negate first, and then camera.tranform() it. I tested again using modelViewMatrix * originalLightPosition and it gives the same result. But it's different result when I camera.tranform() first, then negate it. What's going on there exactly? Wonder if I missed anything. 

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #7 on: February 09, 2015, 05:15:26 pm »
I've double checked what i'm doing with all the normal lights and it's exactly that: transform into camera space, negate y and z, inject it into the shader. Where do your initiali light positions come from? Maybe they are already negated somehow!?

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #8 on: February 10, 2015, 03:40:02 pm »
My bad.. The initial light position is negated at the beginning. Im using Blender to convert the light and other 3D object position into OBJ model and grab the position from there, so I dun have to rotate the mesh again. And the mapping is -Y for Y up and -Z for Z forward in Blender. But is it the correct way if we negate first and camera.tranform in this case? I tried just tranform w/o negating it, but the result seems incorrect.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #9 on: February 10, 2015, 06:31:37 pm »
Not sure what you mean. Convert the positions so that their locations in jPCT's world space are fine and then do the transform/negate steps mentioned above.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #10 on: February 12, 2015, 03:23:39 pm »
I mean the initial light position is already negated. I think it might mess up the whole mapping coordination. I'll try to adjust the mapping and use the method you mentioned earlier to transform. Thanks alot ; )

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #11 on: February 14, 2015, 06:02:26 am »
Hi egon,

I saw the Camera.transform() is creating new SimpleVector object for every use. Can we have another method to pass our own object for the result?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #12 on: February 15, 2015, 09:03:56 pm »
Yes, i've noticed that myself. I'll add such a method.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Light position transformation
« Reply #13 on: February 15, 2015, 09:12:31 pm »

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Light position transformation
« Reply #14 on: February 16, 2015, 04:14:29 pm »
Thanks egon ; )