Because I needed a transparent material which will take
BOTH texture color AND diffuse color into consideration.
*update*: You'll also need to add a few lines to EMaterialTypes.h and COpenGLDriver.cpp
Code: Select all
// >> added by arch.jslin 2010.06.27
//! Transparency is determined by modulating both the texture and the primary color.
class COpenGLMaterialRenderer_TRANSPARENT_MODULATE : public COpenGLMaterialRenderer
{
public:
COpenGLMaterialRenderer_TRANSPARENT_MODULATE(video::COpenGLDriver* d)
: COpenGLMaterialRenderer(d) {}
virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
bool resetAllRenderstates, IMaterialRendererServices* services)
{
Driver->disableTextures(1);
Driver->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates
|| material.MaterialTypeParam != lastMaterial.MaterialTypeParam )
{
#ifdef GL_ARB_texture_env_combine
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);
#else
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_PRIMARY_COLOR_EXT);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_EXT, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_EXT, GL_PRIMARY_COLOR_EXT);
#endif //GL_ARB_texture_env_combine
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, material.MaterialTypeParam);
}
}
virtual void OnUnsetMaterial() {
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
#ifdef GL_ARB_texture_env_combine
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE );
#else
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_MODULATE );
#endif
glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);
}
//! Returns if the material is transparent.
virtual bool isTransparent() const {
return true;
}
};
// << end of addition
texture, and somehow you want to blend that object with
a specific color, say originally you have a gray monster,
and now you want it to be a red monster, you can simply
set node->getMaterial(0).DiffuseColor = SColor(255,255,0,0);
and boom! you have a red monster.
Lets think of another example. Now the player character
killed the red monster, and you want that monster to "fade
out", just set up a loop which decreases the alpha value
of that DiffuseColor from 255 to 0 frame by frame, and the
task is taken care of.
If sometimes you need to setup a loading splash screen which
will fade in and out before and after the scene transition,
this material will also help.
In Irrlicht 1.7 a ColorMaterial property is added to
SMaterial struct, it's defaulted to ECM_DIFFUSE, which
will use vertex color as if it is diffuse color instead of
using SMaterial.DiffuseColor to modulate the final color,
if you happens to need to adjust those colors vertex by
vertex or face by face, this is a cool feature. But if you
are like me, just need the SMaterial.DiffuseColor to get
everything done, set SMaterial.ColorMaterial to ECM_NONE.
I actually am not a OpenGL / D3D programmer, so I just
simply hacked this material to suit my need, and I don't
know how to code that in D3D. If anyone happens to be able
to help, I'd be very very appreciated.



