Shaders and Material serialization

Problems building or running the engine, queries about how to use features etc.
Post Reply
WhitAngl
Gnoblar
Posts: 21
Joined: Thu Nov 30, 2006 12:06 pm
Location: Cannes
Contact:

Shaders and Material serialization

Post by WhitAngl »

Hi,

I've seen several topics on the forum which talk about that... but which didn't solve the problem. It seems that the shader's parameters are not serialized in the .material file generated.
A topic talks about a patch which have been created which solves this problem, but the only file this topic gives is a zip file (patch.zip) containing a .inc and a .txt file with no code in it.
I've downloaded the very last version of Ogre SDK (12/11/06), rebuilt my programs and still get the same problem....

It would be very nice if you could help me please !
Here is my code :

Code: Select all

MaterialPtr mat = MaterialManager::getSingleton().create(matName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mat->load();

Pass* pass0 = mat->getTechnique(0)->getPass(0);
pass0->setAlphaRejectSettings(CMPF_GREATER_EQUAL, 127);
pass0->setFragmentProgram("Ogre/BasicVertexPrograms/AmbientOneTexture2"); // a small modification of the original ambient shader to handle transparent textures
Ogre::GpuProgramParametersSharedPtr fpConsts = pass0->getFragmentProgramParameters(); 
fpConsts->setNamedAutoConstant("sceneAmbient", Ogre::GpuProgramParameters::ACT_AMBIENT_LIGHT_COLOUR); 
pass0->setFragmentProgramParameters(fpConsts);
TextureUnitState *tex = pass0->createTextureUnitState(name,0);
tex->setColourOperation(LBO_REPLACE);
pass0->_load();

// [...] same kind of code with other passes
Pass* pass1 = mat->getTechnique(0)->createPass();
// [........]


mat->setCullingMode(CULL_CLOCKWISE);
mat->compile();

submesh->setMaterialName( matName  );

// then saving of the material
MaterialSerializer mats;
mats.queueForExport(mat);
// then queues other materials
mats.exportQueued("test.material", true);
which exports a material with the field :

Code: Select all

fragment_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture2
{

}
which stays blank.

A topic talks about fpConsts->addConstantDefinition(...) but I didn't understand how to use it...


Lots of thanks !

Nicolas
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Yes, you need to use addConstantDefinition to create a persistent record of this parameter. You can see an example of its use in processManualProgramParam in OgreMaterialSerializer.cpp.
WhitAngl
Gnoblar
Posts: 21
Joined: Thu Nov 30, 2006 12:06 pm
Location: Cannes
Contact:

Post by WhitAngl »

Thanks, but that's what I had seen, but I still don't understand how to use it properly... :s
User avatar
Praetor
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3335
Joined: Tue Jun 21, 2005 8:26 pm
Location: Rochester, New York, US
x 3
Contact:

Post by Praetor »

It is a little complicated.

Setting constants sets them for runtime use. The serializer cannot read this data. The constant definitions are what create "persistent" records of the constants. So, to make your program's serializable every time you set a constant for your Gpu program you should also call addConstantDefinition.

Constant definitions need two functions (depending on the type). addConstantDefinition is the first. After calling this function, if the constant is an auto-constant you call setConstantDefinitionAutoState.

How about an example.

Code: Select all

setConstant(0, 0.0);

addConstantDefinition("", 0, 1, ET_REAL);
The only thing I can't remember is the significance of the constant definition name (the first parameter). Obviously the constant I set is not a named constant. Just remember to pair up constant definitions with each constant you set.
WhitAngl
Gnoblar
Posts: 21
Joined: Thu Nov 30, 2006 12:06 pm
Location: Cannes
Contact:

Post by WhitAngl »

Thanks for your answer, but I still don't understand :s What code should I use to get the equivalent formulation of the parameter line in the .material file :

param_named_auto sceneAmbient ambient_light_colour

the ambient_light_colour is a predefined constant, so I can't use setConstant(), and sceneAmbient is the name of the parameter which is used by the vertex program... (maybe it's the first parameter of the addConstantDefinition() ?..)

Thanks ! :)
User avatar
Praetor
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3335
Joined: Tue Jun 21, 2005 8:26 pm
Location: Rochester, New York, US
x 3
Contact:

Post by Praetor »

Do you not know how to setup auto constants in a gpu program's parameters? I'll assume you are asking about that. Here's something off the top of my head.

Code: Select all

setNamedAutoConstant("sceneAmbient", 
    GpuProgramParameters::ACT_AMBIENT_LIGHT_COLOR);
That sets the ambient color to be program. Now, if you want to make this constant serializable later, that involved quite a bit more code, because we are playing with basically a system designed for reading scripts in runtime. It is still quite manageable though.
WhitAngl
Gnoblar
Posts: 21
Joined: Thu Nov 30, 2006 12:06 pm
Location: Cannes
Contact:

Post by WhitAngl »

So, that's what I use in my code to render my materials (see the code in my original question...)... and if I want to serialize them ?

Thanks ! :)
Post Reply