Figuring out SubRenderState/SubRenderStateFactory and material file

Problems building or running the engine, queries about how to use features etc.
las3rlars
Bronze Sponsor
Bronze Sponsor
Posts: 5
Joined: Tue Nov 11, 2025 10:07 pm
x 1

Figuring out SubRenderState/SubRenderStateFactory and material file

Post by las3rlars »

Ogre Version: :14.3.4:
Operating System: :Linux:
Render System: :GL3Plus:

Hi, I have been trying to understand how I would go about to add my own "extension" to the rtshader_system.

I have a .material file like this:

Code: Select all

material island5_water.glbdummyMat0
{
	technique
	{
		pass 
		{
			rtshader_system
			{
				DepthFoam
			}
		}
	}
}

And then I have

Code: Select all

class DepthFoam : public Ogre::RTShader::SubRenderState
{
public:
    static constexpr Ogre::String Type = "DepthFoam";
    DepthFoam() = default;
    ~DepthFoam() = default;

float foamWidth = 1.0f;
float foamSharpness = 1.5f;
Ogre::ColourValue foamColor {1.0f, 1.0f, 1.0f, 1.0f};

const Ogre::String& getType() const override { return Type; }
int getExecutionOrder() const override { return Ogre::RTShader::FFP_PS_COLOUR_BEGIN + 10; }

void copyFrom(const Ogre::RTShader::SubRenderState& rhs) override {
    const auto& o = static_cast<const DepthFoam&>(rhs);        
}

bool createCpuSubPrograms(Ogre::RTShader::ProgramSet* programSet) override;
};

class DepthFoamShaderFactory: public Ogre::RTShader::SubRenderStateFactory
{
public:

const Ogre::String& getType() const override { return DepthFoam::Type; }

Ogre::RTShader::SubRenderState* createInstance() override {
    return OGRE_NEW DepthFoam();
}

Ogre::RTShader::SubRenderState* createInstance(Ogre::ScriptCompiler* compiler, Ogre::PropertyAbstractNode* prop, Ogre::Pass* pass, Ogre::RTShader::SGScriptTranslator* translator) override
{
    return OGRE_NEW DepthFoam();
}

Ogre::RTShader::SubRenderState* createInstanceImpl() override {
    return OGRE_NEW DepthFoam();
}
};

And during initialization i do the following:

Code: Select all

	OgreBites::ApplicationContext::setup();
	addInputListener(this);

auto* root = getRoot();
auto* scnMgr = root->createSceneManager();

auto* shaderGen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
shaderGen->addSceneManager(scnMgr);
shaderGen->addSubRenderStateFactory(OGRE_NEW DepthFoamShaderFactory);
    ...........

But I never see that it calls createCpuSubPrograms in the DepthFoam subrenderstate.

I don't really understand if it's related to some SCHEMA thing? I don't want the shader to be applied to everything. Only the mesh that uses island5_water.glbdummyMat0

Any points are much appreciated!

Best regards

paroj
OGRE Team Member
OGRE Team Member
Posts: 2274
Joined: Sun Mar 30, 2014 2:51 pm
x 1239

Re: Figuring out SubRenderState/SubRenderStateFactory and material file

Post by paroj »

scripts are parsed during ApplicationContext::setup(), but your Factory is not registered yet.

Override this function to register it in time:
https://github.com/OGRECave/ogre/blob/8 ... e.cpp#L570

Also, I suggest you to follow this template - currently your factory will add DepthFoam to all passes:
https://github.com/OGRECave/ogre/blob/8 ... r.cpp#L227

las3rlars
Bronze Sponsor
Bronze Sponsor
Posts: 5
Joined: Tue Nov 11, 2025 10:07 pm
x 1

Re: Figuring out SubRenderState/SubRenderStateFactory and material file

Post by las3rlars »

Thank you so much! Much appreciated. Signed up on the Patreon! :)

I noticed that there was some changes in the SubRenderStateFactory interface and I realized that I wasn't running the latest 14.4.1. It seems the download link on the download page is still pointing to the older 14.3.4 release.

Best regards