Fixed light order.

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1889
Joined: Sun Mar 08, 2009 5:25 am
x 104

Fixed light order.

Post by mkultra333 »

I am lighting a scene with 4 lights. My understanding is that once the lights go out to the shaders they might be in any order, light 0 in my program might not be light 0 in the shader, they get sorted by distance and stuff.

I don't want this. I want to know that light 0 to light 3 as I declare them in my program will also be light 0 to light 3 in my shader. I need this because my shader is going to do some different things in one pass depending on which light it is.

Doing searching, I see some stuff about lightlists and renderable listeners or something, I didnt understand it and it seemed like a lot of work to achieve something very simple.

How do I turn off all this unwanted scrambling of the light orders?
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.

User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: Princeton, New Jersey, USA
x 13
Contact:

Re: Fixed light order.

Post by nikki »

What if you have 12 lights and the limit is 4, and the first light is is furthest away from the object, so that the first light in your shader cannot be the first light that you created?

Maybe you could store some 'light index' in the alpha of the light colour and then check with that?

User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1889
Joined: Sun Mar 08, 2009 5:25 am
x 104

Re: Fixed light order.

Post by mkultra333 »

nikki wrote:What if you have 12 lights and the limit is 4, and the first light is is furthest away from the object, so that the first light in your shader cannot be the first light that you created?

Maybe you could store some 'light index' in the alpha of the light colour and then check with that?
There will never be 12 lights, only 4.

That alpha idea might work, but the problem then is that my shader has to waste time, at the fragment level, working out which light is which before proceeding. If instead it just knew that light 0 was really light 0, that would be much better.

Edit: Do lights have an alpha colour? The Api says you set the diffuse by

Code: Select all

void Ogre::Light::setDiffuseColour ( Real red, Real green, Real blue) 	
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.

User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1889
Joined: Sun Mar 08, 2009 5:25 am
x 104

Re: Fixed light order.

Post by mkultra333 »

After poking around in the Ogre source and the API, I found Ogre::SceneManager::Listener::sortLightsAffectingFrustum(LightList & lightList ) [virtual]

This sounds like it might be able to do what I want, allowing me to control the light order manually. But I don't really understand how I'm supposed to use it. Any tips? (Assume I have a low understanding of listeners and lightlists :) )
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.

User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 9

Re: Fixed light order.

Post by nullsquared »

Create a class that inherits from SceneManager::Listener, override that function, inside clear the LightList and push_back() your lights in the correct order, and then do sceneMgr->setListener(&anInstanceOfYourListener);

User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1889
Joined: Sun Mar 08, 2009 5:25 am
x 104

Re: Fixed light order.

Post by mkultra333 »

Thanks nullsquared.

I set it up as follows, copied more or less from the shadow listener already in the code (unused):

Code: Select all

	struct LightOrderListener: public Ogre::SceneManager::Listener
	{


		bool sortLightsAffectingFrustum(LightList & lightList )
		{
			return true ;			
		}


		// these are pure virtual but we don't need them...  so just make them empty
    // otherwise we get "cannot declare of type Mgr due to missing abstract
    // functions" and so on
    void shadowTexturesUpdated(size_t) {}
    void shadowTextureReceiverPreViewProj(Ogre::Light*, Ogre::Frustum*) {}
    void preFindVisibleObjects(Ogre::SceneManager*, Ogre::SceneManager::IlluminationRenderStage, Ogre::Viewport*) {}
    void postFindVisibleObjects(Ogre::SceneManager*, Ogre::SceneManager::IlluminationRenderStage, Ogre::Viewport*) {}
		void shadowTextureCasterPreViewProj(Ogre::Light *light, Ogre::Camera *cam, size_t) {}

	} LightOrderUpdater;

I initially had a version that re-added the lights, but the performance hit was terrible, dropping one scene from 24fps down to 15fps. However I noticed something interesting. Just by having sortLightsAffectingFrustum do nothing except return true, it seems light sorting is skipped and my lights stay in the original order they were when I set them up. I still lose a couple of fps (why not sorting should be slower than sorting, I don't know) but it seems acceptable.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.

Post Reply