[Feature] New script events for material scripts loading

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Nodrev
Gremlin
Posts: 193
Joined: Fri Jan 25, 2008 6:55 pm
Location: Nantes / France
x 17

[Feature] New script events for material scripts loading

Post by Nodrev »

Hi,

I was seeking for a good way to encapsulate the Ogre::Material that are script loaded in my framework custom Material class, but without using a temporary group nor parsing all the materials stored in Ogre::MaterialManager (and filtering by this group), so I started to look at ScriptCompilerListener events.

That was pretty strait-forward to encapsulate the material instances, but I needed to encapsulate Technique and Pass instances too, so I added 2 new events to the script listener (and I don't know why they were not implemented when MaterialTranslator was? Any reason?):

Code: Select all

CreateTechniqueScriptCompilerEvent
and

Code: Select all

CreatePassScriptCompilerEvent
The patch can be found here:
https://sourceforge.net/tracker/?func=d ... tid=302997

If you don't see what I mean by "encapsulate", here a good sample of what I'm doing (my framework name is obviously Usul :), no "using namespace Ogre" in this sample, so everything that is not prefixed with "Ogre::" is related to my engine):

Code: Select all

// Generate Usul::GraphicMaterial/Usul::Technique/Usul::Pass for Ogre's script loaded materials.
bool OgreGraphicsManager::handleEvent(Ogre::ScriptCompiler* compiler, Ogre::ScriptCompilerEvent* evt, void* retval)
{
	// Get the type of the resource parsed by the script compiler
	bool processed = false;
	
	// Is it a material?
	if(evt->mType == Ogre::CreateMaterialScriptCompilerEvent::eventType)
	{
		// Convert the event.
		Ogre::CreateMaterialScriptCompilerEvent* convertedEvent = static_cast<Ogre::CreateMaterialScriptCompilerEvent*>(evt);
		
		// Create a new empty material using usul class
		GraphicMaterialDescriptor newGraphicMaterialDesc;
		newGraphicMaterialDesc.group = convertedEvent->mResourceGroup;
		GraphicMaterial* newGraphicMaterial = this->Create<GraphicMaterial>(newGraphicMaterialDesc, convertedEvent->mName);

		// Get the newly created Ogre::Material (in Usul::OgreGraphicMaterial constructor) and return it to the script compiler, it will populate it!
		Ogre::Material** retMaterial = (Ogre::Material**)retval;
		*retMaterial = static_cast<OgreGraphicMaterial*>(newGraphicMaterial)->GetOgreMaterialPtr().getPointer();
		
		// Tell Ogre that we have create the Ogre::Material, he do not need to do it.
		processed = true;
	}

	// Is it a technique?
	if(evt->mType == Ogre::CreateTechniqueScriptCompilerEvent::eventType)
	{
		// Convert the event.
		Ogre::CreateTechniqueScriptCompilerEvent* convertedEvent = static_cast<Ogre::CreateTechniqueScriptCompilerEvent*>(evt);

		// Get Usul parent material
		GraphicMaterial* parentGraphicMaterial = this->Get<GraphicMaterial>(convertedEvent->mParentMaterial->getName());

		// Create a new empty technique using usul class
		TechniqueDescriptor newTechniqueDesc;
		Technique* newTechnique = parentGraphicMaterial->Create<Technique>(newTechniqueDesc, convertedEvent->mName);

		// Get the newly created Ogre::Technique (in Usul::OgreTechnique constructor) and return it to the script compiler, it will populate it!
		Ogre::Technique** retTechnique = (Ogre::Technique**)retval;
		*retTechnique = static_cast<OgreTechnique*>(newTechnique)->GetOgreTechnique();

		// Tell Ogre that we have create the Ogre::Technique, he do not need to do it.
		processed = true;
	}

	// Is it a pass?
	if(evt->mType == Ogre::CreatePassScriptCompilerEvent::eventType)
	{
		// Convert the event.
		Ogre::CreatePassScriptCompilerEvent* convertedEvent = static_cast<Ogre::CreatePassScriptCompilerEvent*>(evt);

		// Get Usul parent technique
		Ogre::Any bindedTechnique = convertedEvent->mParentTechnique->getUserObjectBindings().getUserAny("Usul::OgreTechnique");
		if(!bindedTechnique.isEmpty())
		{
			OgreTechnique* parentTechnique = Ogre::any_cast<OgreTechnique*>(bindedTechnique);

			// Create a new empty technique using usul class
			PassDescriptor newPassDesc;
			Pass* newPass = parentTechnique->Create<Pass>(newPassDesc, convertedEvent->mName);

			// Get the newly created Ogre::Technique (in Usul::OgreTechnique constructor) and return it to the script compiler, it will populate it!
			Ogre::Pass** retPass = (Ogre::Pass**)retval;
			*retPass = static_cast<OgrePass*>(newPass)->GetOgrePass();

			// Tell Ogre that we have create the Ogre::Technique, he do not need to do it.
			processed = true;
		}
	}

	return processed;
}
Nodrev
Gremlin
Posts: 193
Joined: Fri Jan 25, 2008 6:55 pm
Location: Nantes / France
x 17

Re: [Feature] New script events for material scripts loading

Post by Nodrev »

Hi,

As I'm posting a little comment here to announce another patch on camera listener, i take the opportunity to ask if it could be possible to validate this patch please :).
The new script event patch: http://sourceforge.net/tracker/?func=de ... tid=302997
The camera listener patch: http://sourceforge.net/tracker/?func=de ... tid=302997

Thanks :) !