Shader with diffuse lighting and vertex paint shadows.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Shader with diffuse lighting and vertex paint shadows.

Post by 3DModelerMan »

Code: Select all

//Shader
//{Vertex shader
const c8* shadShaderVert =
"float4x4 matWorldViewProj;                                                                                           \
 float4x4 matWorld;                                                                                                   \
 float3 vecLightDir;                                                                                                  \
                                                                                                                      \
struct VS_OUTPUT                                                                                                      \
{                                                                                                                     \
  float4 Pos: POSITION;                                                                                               \
  float2 Tex : TEXCOORD0;                                                                                             \
  float3 Light: TEXCOORD1;                                                                                            \
  float3 Norm: TEXCOORD2;                                                                                             \
  float4 Color: TEXCOORD3;                                                                                               \
};                                                                                                                    \
                                                                                                                      \
VS_OUTPUT vertMain( float4 Pos: POSITION, float3 Normal: NORMAL, float2 Tex: TEXCOORD0, float4 Color: COLOR0 )        \
{                                                                                                                     \
 VS_OUTPUT Out = (VS_OUTPUT) 0;                                                                                       \
 Out.Pos = mul(Pos, matWorldViewProj);                                                                                \
 Out.Light = vecLightDir;                                                                                             \
 Out.Tex = Tex.xy;                                                                                                    \
 Out.Norm = normalize(mul(Normal, matWorld));                                                                         \
 Out.Color = Color;                                                                                                   \
 return Out;                                                                                                          \
}                                                                                                                     \
";
That's the shader code. This is the callback.

Code: Select all

class shadShaderCallback : public video::IShaderConstantSetCallBack
{
public:
        
        const video::SMaterial *UsedMaterial;

        virtual void OnSetMaterial(const video::SMaterial& material)
        {
          UsedMaterial=&material;
        }

        virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
        {
                video::IVideoDriver* driver = services->getVideoDriver();

                //WorldViewProjection matrix.
                core::matrix4 worldViewProj;
                worldViewProj = driver->getTransform(video::ETS_PROJECTION);
                worldViewProj *= driver->getTransform(video::ETS_VIEW);
                worldViewProj *= driver->getTransform(video::ETS_WORLD);
                services->setVertexShaderConstant("matWorldViewProj", worldViewProj.pointer(), 16);

                //World matrix.
                core::matrix4 worldMatrix;
                worldMatrix = driver->getTransform(video::ETS_WORLD);
                services->setVertexShaderConstant("matWorld", worldMatrix.pointer(), 16);

                //Ambient color.
                irr::video::SColorf ambient = (irr::video::SColorf)UsedMaterial->AmbientColor;
                services->setPixelShaderConstant("ambientColor", reinterpret_cast<f32*>(&ambient), 4);
                //Diffuse color.
                irr::video::SColorf diffuse = (irr::video::SColorf)UsedMaterial->DiffuseColor;
                services->setPixelShaderConstant("diffuseColor", reinterpret_cast<f32*>(&diffuse), 4);

                //Light direction.
                services->setVertexShaderConstant("vecLightDir", reinterpret_cast<f32*>(&lightDir), 3);//lightDir should be replaced with your lights direction.

        }
};
You only need to paint shadows into the vertex colors of an object for this shader to take those values (I use vertex paint mode in blender).
Anyways, it was my first shader so it's kinda weird, but it looks cool so I thought I'd share it.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
Post Reply