Evaluate this for me, please :D

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Evaluate this for me, please :D

Post by Auradrummer »

Hello guys,

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));
    .
    .
    .
};
 
Only to give a direction, this shader projects a planar texture over an object. To do this, I have to pass the objects dimensions to the shader (don't know an way to do this only with OpenGL). So, I made a class "extraMaterials" with a constructor (not here, only initializes some vars...) with a method initMaterial.

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.
Professional Software Developer and Amateur Game Designer ;-)
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Re: Evaluate this for me, please :D

Post by devsh »

USE


services->setVertexShaderConstant("size", reinterpret_cast<f32*>(&extents.X), 2);
Post Reply