I made, some months ago, a "very ugly way" to pass extra information to shaders. A bunch of error was appointed. Now, I made another way to do it, more clean, based on what you friends said to me.
I want that you, if possible, evaluate it for me if I'm making something wrong again. Is working perfectly, but I don't have experience to say that is good or bad or I should improve something:
Code: Select all
//INITMAT.CPP
s32 EMT_REFMAPSHADER;
class refMap : public video::IShaderConstantSetCallBack
{
public:
vector2df extents;
void setUserData(void * extraUserData)
{
IMeshSceneNode * mesh = (IMeshSceneNode *)extraUserData;
vector3df extBox = mesh->getMesh()->getBoundingBox().getExtent();
extents.X = extBox.X;
extents.Y = extBox.Z;
};
private:
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
int texture = 0;
services->setPixelShaderConstant("difTex",(float*)(&texture),1);
int reflect = 1;
services->setPixelShaderConstant("refTex",(float*)(&reflect),1);
int reflectMap = 2;
services->setPixelShaderConstant("refMap",(float*)(&reflectMap),1);
services->setVertexShaderConstant("size", reinterpret_cast<f32*>(&extents), 2);
}
};
void extraMaterials::initMaterial(void * userData, u16 materialType)
{
switch(materialType)
{
case REFMAPSHADER:
//MATERIAL SÓLIDO COM REFLEXÃO E MAPA DE REFLEXÃO
path psRefMap = "../shaders_code/refMap.frag";
path vsRefMap = "../shaders_code/refMap.vert";
refMap * rfmcb = new refMap();
if(userData)
rfmcb->setUserData(userData);
EMT_REFMAPSHADER = gpu->addHighLevelShaderMaterialFromFiles
(
vsRefMap, "vertexMain", video::EVST_VS_1_1,
psRefMap, "pixelMain", video::EPST_PS_1_1,
rfmcb, video::EMT_SOLID
);
rfmcb->drop();
break;
};
};
//MAIN.CPP
int main()
{
extern s32 EMT_REFMAPSHADER;
.
.
.
extraMaterials extraMat (device);
IMeshSceneNode* esferaShader = smgr->addMeshSceneNode(mesh);
extraMat.initMaterial(esferaShader, REFMAPSHADER);
esferaShader->setMaterialType((E_MATERIAL_TYPE)(EMT_REFMAPSHADER));
.
.
.
};
This method receives an int (ENUMERATOR) and a pointer to some extra data (in this example, the object). This pointer is passed to the callback by the method setUserData and treated there and finally passed to the shader.
I guess it is safe to use and fast.