Archived

This topic is now archived and is closed to further replies.

McZ

Shader based rendering?

Recommended Posts

how does it work? I have created a test system that use shader based rendering.. but I don''t know how I define what shader is used for what type of material I have?! this is my material class
class cMaterial {

public:

 cColor4f Ambient;
 cColor4f Diffuse;
 cColor4f Specular;
 float Shininess;

 int nrOfTextures; // for multitexture

 cTexture *m_Texture[MAX_TEXTURES]; // array of pointers to textures


 eBlendFactor SrcBlendFactor;
 eBlendFactor DstBlendFactor;
};

// ******************

/* shader stuff */

// Shader Base class

class cShaderBase {

public:
 virtual bool IsAviable(void); // check for hardware support

 virtual void Enable(void); // done once for all 

 virtual void SetParams(...); // send the textures and so that will be used, and then set the states for this shader. (change texture if needed)

 virtual void Disable(void);

 virtual bool CanRender(DWORD EffectID);
};

// Render Loop

cRenderSystemGL::Render(void)
{
 cShaderBase *shader = NULL;
 for( [o=each object in list] )
 {
  if( shader != o->GetShader() )
  {
   if( shader != NULL ) // prevent to disable a null pointer.

    shader->Disable(); // disable states set by previous shader


   shader = o->GetShader(); // get new shader.

   shader->Enable(); // enable new stats for this shader.

  }

  shader->SetParams(...); // set texture for this data.

  
  // set pointers to vertices,normals,texcoords and so

  glDrawArray(...); // or something else.

 }
}
so far I understand how to do.. but I don''t know what defines a shader.. and how to attach a shader to an object depending on it''s material. (or attach it directly to the material) I though I set and EffectID to the cMaterial class that is fixed when the material-script is loaded depending on what type of data exist in the script..
enum { AMBIENT=1, DIFFUSE=2, SPECULAR=4, SHININESS=8, TEXTURE=16, MULTITEXTURE=32, TRANSPARENCY=64 };

// Material Load

cMaterial::Load(...)
{
 EffectID = 0;
 do{
   if( [script has AMBIENT] )
   {
    Ambient.r = [get the value]
    ...
    EffectID |= AMBIENT; // Now the effect id will be whatever it was and Ambient

   }

   if( [script has DIFFUSE] )
   {
    Diffuse.r = [get the value]
    ...
    EffectID |= DIFFUSE; // Now the effect id will be whatever it was and DIFFUSE

   }

   ....

 }while( [not eof] );
}

//***************

// Shader search

for(m=material in list)
{
 for(s=shader in list)
 {
  if( s->CanRender( m->EffectID ) )
  {
   m->Shader = s;
  }
 }
}
then how to do if I need MultiPass rendering... or maybe 2 shaders for one material? example: I want to render a mesh with diffuse texture and a bumpmap the engine has a shader that can render diffuse and another shader that can render bumpmap ( the shade that could render both didn''t pass the hardware test or something ) how to fix that render pass thing?

Share this post


Link to post
Share on other sites