Cg lighting rotating sphere

Started by
2 comments, last by genesys 17 years, 5 months ago
Looking for someone to spark an idea for me, currently im developing a solar system simulation. Im using the cg fragment shader in opengl and producing sunlight on my planets, i dont want opengl generic lighting on for certain reasons. Now this may sound stupid, i know why this is happening, As the planet rotates the calculations for light is still being done on the pre-rotated positioning. So it appears the light is rotating around the planet at the same speed the planet is rotating Whats the best method to apply the rotation to the vertices, then do the pass on the fragment shader? Im taking a break so this is what i was about to implement, if there is a better way let me know. store the rotation in a m[16] matrix and pass that into the vertex shader and have it do the required rotations to the textured sphere. Later down the pipeline the fragment shader should do the lighting calculations correctly and i 'should' see a rotating texture sphere with sunlight hitting in the correct direction. Basically the light shouldnt look like its rotating around the texture but stationary. Thoughts, New to shaders:)
Advertisement
PS Is doing the translation and rotation cheaper with the vertex shader? Is there something else i need to watch out for? Or will the above mentioned method work?


Thanks
Pick a coordinate space in which to do all your lighting calculations, let's say we'll use Object Space.

So, you have your light position (the sun, whatever) in world space, and you want to get it into object space so that all your lighting calcs work out properly. What you've got to do is take the world-space light position, and then transform it by the inverse modelview matrix for each object, taking it from world space and putting it into object space. Pass this value into your vertex shader as the light position. From there, everything works out!
I don't know CG but i'm using GLSL so i hope it's similar...

multiply your normal vectors with the modelTransformMatrix!

This topic is closed to new replies.

Advertisement