c++ material Topic is solved

Problems building or running the engine, queries about how to use features etc.
slapin
Bronze Sponsor
Bronze Sponsor
Posts: 246
Joined: Fri May 23, 2025 5:04 pm
x 16

c++ material

Post by slapin »

hi, all!

what would be c++ equivalent for

Code: Select all

        rtshader_system
        {
                lighting_stage per_pixel
        }

?

The straightforward version

Code: Select all

					Ogre::RTShader::RenderState *renderState =
						Ogre::RTShader::ShaderGenerator::
							getSingletonPtr()
								->createOrRetrieveRenderState(
									Ogre::MSN_SHADERGEN)
								.first;
					Ogre::RTShader::SubRenderState *perPixelLightModel =
						Ogre::RTShader::ShaderGenerator::getSingletonPtr()
							->createSubRenderState(
								Ogre::RTShader::
									SRS_PER_PIXEL_LIGHTING);
					renderState->addTemplateSubRenderState(
						perPixelLightModel);

does not work for some reason...

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

Re: c++ material

Post by paroj »

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 246
Joined: Fri May 23, 2025 5:04 pm
x 16

Re: c++ material

Post by slapin »

Well, tried to implement in my code and it did not work :(

Code: Select all

			if (!mat) {
					mat = Ogre::MaterialManager::getSingleton()
						      .create("Water/Above",
							      "Water");
					Ogre::Technique *tech =
						mat->getTechnique(0);
					Ogre::Pass *pass = tech->getPass(0);
					pass->setLightingEnabled(true);
					pass->setAmbient(
						Ogre::ColourValue(1, 1, 1, 1));
					pass->setDiffuse(Ogre::ColourValue(
						0.0f, 0.2f, 0.5f, 1.0f));
					pass->setCullingMode(Ogre::CULL_NONE);
					pass->setManualCullingMode(
						Ogre::MANUAL_CULL_NONE);
					pass->setVertexProgram(
						"Water/water_vp");
					pass->setFragmentProgram(
						"Water/water_fp");
					Ogre::TextureUnitState *texture_unit =
						pass->createTextureUnitState();
					texture_unit->setTextureName(
						"ReflectionRefractionTexture");
					Ogre::Sampler::UVWAddressingMode uvw;
					uvw.u = Ogre::TextureUnitState::TAM_MIRROR;
					uvw.v = Ogre::TextureUnitState::TAM_MIRROR;
					uvw.w = Ogre::TextureUnitState::TAM_MIRROR;
					texture_unit->setTextureAddressingMode(
						uvw);
					texture_unit->setTextureFiltering(
						Ogre::FT_MIN, Ogre::FO_LINEAR);
					texture_unit->setTextureFiltering(
						Ogre::FT_MAG, Ogre::FO_LINEAR);
					texture_unit->setTextureFiltering(
						Ogre::FT_MIP, Ogre::FO_LINEAR);
#if 1 // here everything breaks
					bool success =
						Ogre::RTShader::ShaderGenerator::getSingletonPtr()
							->createShaderBasedTechnique(
								*mat,
								Ogre::MSN_DEFAULT,
								Ogre::MSN_SHADERGEN);
					OgreAssert(
						success,
						"createShaderBasedTechnique"); // this assert rises :(
					Ogre::RTShader::RenderState *renderState =
						Ogre::RTShader::ShaderGenerator::
							getSingletonPtr()
								->getRenderState(
									Ogre::MSN_SHADERGEN,
									*mat,
									0);

				Ogre::RTShader::SubRenderState *perPixelLightModel =
					Ogre::RTShader::ShaderGenerator::getSingletonPtr()
						->createSubRenderState(
							Ogre::RTShader::
								SRS_PER_PIXEL_LIGHTING);
				renderState->addTemplateSubRenderState(
					perPixelLightModel);
#endif
				}
paroj
OGRE Team Member
OGRE Team Member
Posts: 2233
Joined: Sun Mar 30, 2014 2:51 pm
x 1214

Re: c++ material

Post by paroj »

thats to be expected, the RTSS skips passes that already have shaders.

btw: per pixel lighting is default since ogre 13

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 246
Joined: Fri May 23, 2025 5:04 pm
x 16

Re: c++ material

Post by slapin »

But how to write that snippet correctly?

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 246
Joined: Fri May 23, 2025 5:04 pm
x 16

Re: c++ material

Post by slapin »

@paroj sorry for bother but I need help with C++ material.

Code: Select all

				Ogre::MaterialPtr mat =
					Ogre::MaterialManager::getSingleton()
						.getByName("Water/Above",
							   "Water");
				if (!mat) {
					mat = Ogre::MaterialManager::getSingleton()
						      .create("Water/Above",
							      "Water");
					Ogre::Technique *tech =
						mat->getTechnique(0);
					Ogre::Pass *pass = tech->getPass(0);
					pass->setLightingEnabled(true);
					pass->setAmbient(
						Ogre::ColourValue(1, 1, 1, 1));
					pass->setDiffuse(Ogre::ColourValue(
						0.0f, 0.2f, 0.5f, 1.0f));
					pass->setCullingMode(Ogre::CULL_NONE);
					pass->setManualCullingMode(
						Ogre::MANUAL_CULL_NONE);
					pass->setVertexProgram(
						"Water/water_vp");
					pass->setFragmentProgram(
						"Water/water_fp");
					/* this one works well*/
					Ogre::TextureUnitState *texture_unit =
						pass->createTextureUnitState();
					texture_unit->setTextureName(
						"ReflectionRefractionTexture");
					Ogre::Sampler::UVWAddressingMode uvw;
					uvw.u = Ogre::TextureUnitState::TAM_MIRROR;
					uvw.v = Ogre::TextureUnitState::TAM_MIRROR;
					uvw.w = Ogre::TextureUnitState::TAM_MIRROR;
					texture_unit->setTextureAddressingMode(
						uvw);
					texture_unit->setTextureFiltering(
						Ogre::FT_MIN, Ogre::FO_LINEAR);
					texture_unit->setTextureFiltering(
						Ogre::FT_MAG, Ogre::FO_LINEAR);
					texture_unit->setTextureFiltering(
						Ogre::FT_MIP, Ogre::FO_LINEAR);
					/* this one doesn't work */
					Ogre::TextureUnitState *texture_unit2 = 
						pass->createTextureUnitState();
					texture_unit2->setTextureName(
						"waves2.png");
					texture_unit2->setTextureAddressingMode(
						uvw);
					texture_unit2->setTextureFiltering(
						Ogre::FT_MIN, Ogre::FO_LINEAR);
					texture_unit2->setTextureFiltering(
						Ogre::FT_MAG, Ogre::FO_LINEAR);
					texture_unit2->setTextureFiltering(
						Ogre::FT_MIP, Ogre::FO_NONE);
				}

The problem is that texture_unit2 behaves exactly like texture_unit sampling the same image in fragment shader as if it is duplicate,
no shader errors are produced. However waves2.png loads successfully. "ReflectionRefractionTexture" is RTT created beforehand.

Fragment shader code:

Code: Select all

OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>
SAMPLER2D(reflectMap, 0);
SAMPLER2D(noiseMap, 1);
MAIN_PARAMETERS
IN(vec4 clipSpace, TEXCOORD0)
IN(vec2 textureCoords, TEXCOORD1)
MAIN_DECLARATION
{
	vec2 ndc = clipSpace.xy / clipSpace.w / 2.0 + vec2(0.5, 0.5);
	vec2 reflectionUV = vec2(ndc.x, 1.0 - ndc.y) * 0.5;
	vec2 refractionUV = vec2(ndc.x, 1.0 - ndc.y) * 0.5 + vec2(0.5, 0.0);
	vec4 distortion_color = texture2D(noiseMap, textureCoords);
	vec4 reflectionColour = texture2D(reflectMap, reflectionUV);
	vec4 refractionColour = texture2D(reflectMap, refractionUV);
	gl_FragColor = distortion_color;
	// gl_FragColor = mix(reflectionColour, refractionColour, 0.5);
}

}
It looks like I miss something basic. Any ideas?

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

Re: c++ material

Post by paroj »

slapin
Bronze Sponsor
Bronze Sponsor
Posts: 246
Joined: Fri May 23, 2025 5:04 pm
x 16

Re: c++ material

Post by slapin »

Thanks a lot! Totally forgot I need to add

Code: Select all

		param_named reflectMap int 0
		param_named noiseMap int 1

which gone missing when I started from scratch.