NL3D::CMeshVPPerPixelLight Class Reference

#include <meshvp_per_pixel_light.h>

Inheritance diagram for NL3D::CMeshVPPerPixelLight:

NL3D::IMeshVertexProgram NLMISC::IStreamable NLMISC::CRefCount NLMISC::IClassable

Detailed Description

This vertex program is used to perform perpixel lighting with meshs. Its ouputs are :

Coord Tex 0 : duplicate the tex Coord 0 set from the v.b. Coord Tex 1 : The light vector in tangent space. Coord Tex 2 : The half-angle vector in tangent space.

This is designed to work with material that got a 'PerPixelLighting' shader. Note that no bump is used with this shader.

It requires that the input mesh has its tangent space first column vector encoded in the 2nd texture coordinate. Other stages are not supported for now.

Note that this can be mixed with gouraud rendered light (the gouraud diffuse and specular component are still computed)

Author:
Nicolas Vizerie

Nevrax France

Date:
2002

Definition at line 58 of file meshvp_per_pixel_light.h.

Public Member Functions

 CMeshVPPerPixelLight ()
virtual std::string getClassName ()=0
virtual float getMaxVertexMove ()
const sintgetRefCount () const
IMeshVertexProgram implementation
virtual bool begin (IDriver *drv, CScene *scene, CMeshBaseInstance *mbi, const NLMISC::CMatrix &invertedModelMat, const NLMISC::CVector &viewerPos)
virtual void end (IDriver *drv)
virtual void initInstance (CMeshBaseInstance *mbi)
virtual bool needTangentSpace () const
 NLMISC_DECLARE_CLASS (CMeshVPPerPixelLight)
virtual void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
virtual void setupForMaterial (const CMaterial &mat, IDriver *drv, CScene *scene, IVertexBufferHard *vb)
virtual void setupForMaterial (const CMaterial &mat, IDriver *drv, CScene *scene, CVertexBuffer *vb)
MBR support.
Actually, it's targeted to work optimally with WindTree. Requirement are:
  • No special Validity test per instance (hence isMBRVpOk() takes only the driver).
  • begin(), end(), setupForMaterial() is replaced with the following interface.
  • there is no setupMBRMaterial(), because the caller has just to call per material (and must do it): renderTrav->changeVPLightSetupMaterial(mat, false);
  • beginMBRMesh MUST activate the VertexProgram, because called before VB activation
  • beginMBRInstance still can change of VertexProgram, but cannot disable VP!!


virtual void beginMBRInstance (IDriver *drv, CScene *scene, CMeshBaseInstance *mbi, const NLMISC::CMatrix &invertedModelMat)
virtual void beginMBRMesh (IDriver *drv, CScene *scene)
virtual void endMBRMesh (IDriver *drv)
virtual bool isMBRVpOk (IDriver *drv) const
virtual bool supportMeshBlockRendering () const
MBR support.
Actually, it's targeted to work optimally with WindTree. Requirement are:
  • No special Validity test per instance (hence isMBRVpOk() takes only the driver).
  • begin(), end(), setupForMaterial() is replaced with the following interface.
  • there is no setupMBRMaterial(), because the caller has just to call per material (and must do it): renderTrav->changeVPLightSetupMaterial(mat, false);
  • beginMBRMesh MUST activate the VertexProgram, because called before VB activation
  • beginMBRInstance still can change of VertexProgram, but cannot disable VP!!


virtual void beginMBRInstance (IDriver *drv, CScene *scene, CMeshBaseInstance *mbi, const NLMISC::CMatrix &invertedModelMat)
virtual void beginMBRMesh (IDriver *drv, CScene *scene)
virtual void endMBRMesh (IDriver *drv)
virtual bool isMBRVpOk (IDriver *drv) const
virtual bool supportMeshBlockRendering () const

Data Fields

sint crefs
CPtrInfo * pinfo
bool SpecularLighting
 true if want Specular Lighting.


Static Public Attributes

CPtrInfo NullPtrInfo

Private Types

enum  { NumVp = 8 }

Private Member Functions

void enable (bool enabled, IDriver *drv)
virtual bool setupForMaterial (const CMaterial &mat, IDriver *drv, CScene *scene)

Private Attributes

bool _Enabled
bool _IsPointLight

Static Private Attributes

std::auto_ptr< CVertexProgram_VertexProgram [NumVp]

Friends

struct CPtrInfo


Member Enumeration Documentation

anonymous enum [private]
 

Enumeration values:
NumVp 

Definition at line 99 of file meshvp_per_pixel_light.h.

00099 { NumVp = 8};   


Constructor & Destructor Documentation

NL3D::CMeshVPPerPixelLight::CMeshVPPerPixelLight  )  [inline]
 

Definition at line 64 of file meshvp_per_pixel_light.h.

References _Enabled, and SpecularLighting.

00064 : SpecularLighting(true), _Enabled(false) {}    


Member Function Documentation

bool NL3D::CMeshVPPerPixelLight::begin IDriver drv,
CScene scene,
CMeshBaseInstance mbi,
const NLMISC::CMatrix invertedModelMat,
const NLMISC::CVector viewerPos
[virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NL3D::IMeshVertexProgram.

Definition at line 433 of file meshvp_per_pixel_light.cpp.

References _IsPointLight, NL3D::CRenderTrav::beginVPLightSetup(), enable(), NL3D::CLight::getDirection(), NL3D::CRenderTrav::getDriverLight(), NL3D::CLight::getMode(), NL3D::CLight::getPosition(), NL3D::CScene::getRenderTrav(), NL3D::CRenderTrav::getStrongestLightIndex(), NL3D::IDriver::isVertexProgramEmulated(), NL3D::IDriver::isVertexProgramSupported(), NLMISC::CMatrix::mulVector(), NL3D::IDriver::setConstant(), NL3D::IDriver::setConstantMatrix(), sint, SpecularLighting, and NL3D::IDriver::supportPerPixelLighting().

00437 {
00438         // test if supported by driver
00439         if (!
00440                  (drv->isVertexProgramSupported() 
00441                   && !drv->isVertexProgramEmulated()
00442                   &&  drv->supportPerPixelLighting(SpecularLighting)
00443                  )
00444            )
00445         {
00446                 return false;
00447         }
00448         //                      
00449         CRenderTrav             *renderTrav= &scene->getRenderTrav();                   
00451         renderTrav->beginVPLightSetup(VPLightConstantStart,
00452                                                                   SpecularLighting,
00453                                                                   invertedModelMat);
00454         //
00455         sint strongestLightIndex = renderTrav->getStrongestLightIndex();
00456         if (strongestLightIndex == -1) return false; // if no strongest light, disable this vertex program              
00457         // setup the strongest light
00459         const CLight &strongestLight  = renderTrav->getDriverLight(strongestLightIndex);
00460 
00461         switch (strongestLight.getMode())
00462         {
00463                 case CLight::DirectionalLight:
00464                 {
00465                         // put light direction in object space
00466                         NLMISC::CVector lPos = invertedModelMat.mulVector(strongestLight.getDirection());
00467                         drv->setConstant(4, lPos);
00468                         _IsPointLight = false;
00469                 }
00470                 break;
00471                 case CLight::PointLight:
00472                 {
00473                         // put light in object space
00474                         NLMISC::CVector lPos = invertedModelMat * strongestLight.getPosition();
00475                         drv->setConstant(4, lPos);
00476                         _IsPointLight = true;
00477                 }
00478                 break;
00479                 default:
00480                         return false;
00481                 break;
00482         }
00483                                 
00484         
00485         if (SpecularLighting)
00486         {
00487                 // viewer pos in object space
00488                 NLMISC::CVector vPos = invertedModelMat * viewerPos;
00489                 drv->setConstant(5, vPos);
00490         }
00491                 
00492         // c[0..3] take the ModelViewProjection Matrix. After setupModelMatrix();
00493         drv->setConstantMatrix(0, IDriver::ModelViewProjection, IDriver::Identity);                     
00494         //      
00495         enable(true, drv); // must enable the vertex program before the vb is activated
00496         //
00497         return true;
00498 }

virtual void NL3D::IMeshVertexProgram::beginMBRInstance IDriver drv,
CScene scene,
CMeshBaseInstance mbi,
const NLMISC::CMatrix invertedModelMat
[inline, virtual, inherited]
 

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 123 of file mesh_vertex_program.h.

00123 {};

virtual void NL3D::IMeshVertexProgram::beginMBRMesh IDriver drv,
CScene scene
[inline, virtual, inherited]
 

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 122 of file mesh_vertex_program.h.

00122 {};

void NL3D::CMeshVPPerPixelLight::enable bool  enabled,
IDriver drv
[private]
 

Definition at line 517 of file meshvp_per_pixel_light.cpp.

References _Enabled, _IsPointLight, NL3D::IDriver::activeVertexProgram(), NL3D::IDriver::isForceNormalize(), nlassert, SpecularLighting, and uint.

Referenced by begin(), end(), and setupForMaterial().

00518 {
00519         if (enabled != _Enabled)
00520         {
00521                 nlassert(drv);
00522                 if (enabled)
00523                 {
00524                         /* for (uint k = 0; k < NumVp; ++k)
00525                         {
00526                                 nlinfo("test vp %d", k);
00527                                 drv->activeVertexProgram(_VertexProgram[k].get());
00528                         } */
00529                         uint    idVP =   (drv->isForceNormalize() ? 4 : 0) 
00530                                                    | (SpecularLighting        ? 2 : 0)
00531                                                    | (_IsPointLight                   ? 1 : 0);
00532                         //
00533                         drv->activeVertexProgram(_VertexProgram[idVP].get());
00534                 }
00535                 else
00536                 {
00537                         drv->activeVertexProgram(NULL);
00538                 }
00539                 _Enabled = enabled;
00540         }
00541 }

void NL3D::CMeshVPPerPixelLight::end IDriver drv  )  [virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NL3D::IMeshVertexProgram.

Definition at line 502 of file meshvp_per_pixel_light.cpp.

References enable().

00503 {
00504         enable(false, drv);
00505 }

virtual void NL3D::IMeshVertexProgram::endMBRMesh IDriver drv  )  [inline, virtual, inherited]
 

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 124 of file mesh_vertex_program.h.

00124 {}

virtual std::string NLMISC::IClassable::getClassName  )  [pure virtual, inherited]
 

Implemented in NLAIAGENT::CNumericIndex, NLAIC::IPointerGestion, NLAIC::CIdentType, and CAutomataDesc.

Referenced by NLMISC::CClassRegistry::checkObject(), and NL3D::GetTextureSize().

virtual float NL3D::IMeshVertexProgram::getMaxVertexMove  )  [inline, virtual, inherited]
 

This return the (over-evaluated) max delta the VertexProgram apply to vertex (in world basis) NB: This method call is relevant only after begin() or beginMBRInstance()

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 131 of file mesh_vertex_program.h.

00131 {return 0;}

const sint& NLMISC::CRefCount::getRefCount  )  const [inline, inherited]
 

Definition at line 70 of file smart_ptr.h.

References NLMISC::CRefCount::crefs, and sint.

00071         {
00072                 return  crefs;
00073         }

void NL3D::CMeshVPPerPixelLight::initInstance CMeshBaseInstance mbi  )  [virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NL3D::IMeshVertexProgram.

Definition at line 371 of file meshvp_per_pixel_light.cpp.

References nlassert, nlwarning, NumVp, CVPParser::parse(), NL3D::PPLightingDirectionnalNoSpecVPCodeBegin, NL3D::PPLightingDirectionnalNoSpecVPNormalizeCodeBegin, NL3D::PPLightingDirectionnalVPCodeBegin, NL3D::PPLightingDirectionnalVPNormalizeCodeBegin, NL3D::PPLightingNoSpecVPCodeBegin, NL3D::PPLightingVPCodeBegin, NL3D::PPLightingVPCodeEnd, NL3D::PPLightingVPNormalizeCodeBegin, NL3D::PPLightingVPNormalizeNoSpecCodeBegin, CVPParser::TProgram, and uint.

00372 {
00373         // init the vertexProgram code.
00374         static  bool    vpCreated= false;
00375         if(!vpCreated)
00376         {
00377                 vpCreated= true;                
00378 
00379                 // Gives each vp name
00380                 // Bit 0 : 1 when it is a directionnal light
00381                 // Bit 1 : 1 when specular is needed
00382                 // Bit 2 : 1 when normalization of the tangent space is needed
00383                 static const char *vpName[] =
00384                 {
00385                         //  no spec
00386                         PPLightingDirectionnalNoSpecVPCodeBegin, 
00387                         PPLightingNoSpecVPCodeBegin,
00388                         // specular
00389                         PPLightingDirectionnalVPCodeBegin,
00390                         PPLightingVPCodeBegin,                                                                  
00392                         // no spec
00393                         PPLightingDirectionnalNoSpecVPNormalizeCodeBegin,
00394                         PPLightingVPNormalizeNoSpecCodeBegin,
00395                         // spec
00396                         PPLightingDirectionnalVPNormalizeCodeBegin,
00397                         PPLightingVPNormalizeCodeBegin,                                         
00398                 };
00399 
00400                 uint numvp  = sizeof(vpName) / sizeof(const char *);
00401                 nlassert(NumVp == numvp); // make sure that it is in sync with header..todo : compile time assert :)
00402                 for (uint vp = 0; vp < NumVp; ++vp)
00403                 {
00404                         // \todo yoyo TODO_OPTIM Manage different number of pointLights
00405                         // NB: never call getLightVPFragment() with normalize, because already done by PerPixel fragment before.
00406                         std::string vpCode      = std::string(vpName[vp])
00407                                                                   + std::string("# ***************") // temp for debug
00408                                                                   + CRenderTrav::getLightVPFragment(CRenderTrav::MaxVPLight-1, VPLightConstantStart, (vp & 2) != 0, false)
00409                                                                   + std::string("# ***************") // temp for debug
00410                                                                   + std::string(PPLightingVPCodeEnd);
00411                         #ifdef NL_DEBUG
00412 
00417                                 CVPParser                       vpParser;
00418                                 CVPParser::TProgram result;
00419                                 std::string          parseOutput;
00420                                 if (!vpParser.parse(vpCode.c_str(), result, parseOutput))
00421                                 {                                       
00422                                         nlwarning(parseOutput.c_str());
00423                                         nlassert(0);
00424                                 }
00425                         #endif
00426                         _VertexProgram[vp]= std::auto_ptr<CVertexProgram>(new CVertexProgram(vpCode.c_str()));
00427                 }
00428                                 
00429         }
00430 }

virtual bool NL3D::IMeshVertexProgram::isMBRVpOk IDriver drv  )  const [inline, virtual, inherited]
 

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 121 of file mesh_vertex_program.h.

00121 {return false;}

virtual bool NL3D::CMeshVPPerPixelLight::needTangentSpace  )  const [inline, virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Reimplemented from NL3D::IMeshVertexProgram.

Definition at line 84 of file meshvp_per_pixel_light.h.

00084 { return true; }

NL3D::CMeshVPPerPixelLight::NLMISC_DECLARE_CLASS CMeshVPPerPixelLight   ) 
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

void NL3D::CMeshVPPerPixelLight::serial NLMISC::IStream f  )  throw (NLMISC::EStream) [virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NLMISC::IStreamable.

Definition at line 509 of file meshvp_per_pixel_light.cpp.

00510 {
00511         (void)f.serialVersion(0);
00512         f.serial(SpecularLighting);     
00513 }       

bool NL3D::CMeshVPPerPixelLight::setupForMaterial const CMaterial mat,
IDriver drv,
CScene scene
[private, virtual]
 

Definition at line 544 of file meshvp_per_pixel_light.cpp.

References _Enabled, NL3D::CRenderTrav::changeVPLightSetupMaterial(), enable(), NL3D::CScene::getRenderTrav(), NL3D::CMaterial::getShader(), NL3D::CMaterial::getShininess(), NL3D::CRenderTrav::getStrongestLightColors(), and NL3D::IDriver::setPerPixelLightingLight().

00548 {
00549         bool enabled = (mat.getShader() == CMaterial::PerPixelLighting || mat.getShader() == CMaterial::PerPixelLightingNoSpec);
00550         if (enabled)
00551         {
00552                 CRenderTrav             *renderTrav= &scene->getRenderTrav();
00553                 renderTrav->changeVPLightSetupMaterial(mat, true /* exclude strongest*/);               
00554 
00555                 NLMISC::CRGBA pplDiffuse, pplSpecular;          
00556                 renderTrav->getStrongestLightColors(pplDiffuse, pplSpecular);
00557                 drv->setPerPixelLightingLight(pplDiffuse, pplSpecular, mat.getShininess());
00558         }
00559         bool change = (enabled != _Enabled);
00560         enable(enabled, drv); // enable disable the vertex program (for material that don't have the right shader)
00561         return change;
00562 }

void NL3D::CMeshVPPerPixelLight::setupForMaterial const CMaterial mat,
IDriver drv,
CScene scene,
IVertexBufferHard vb
[virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NL3D::IMeshVertexProgram.

Definition at line 577 of file meshvp_per_pixel_light.cpp.

References NL3D::IDriver::activeVertexBufferHard(), and setupForMaterial().

00581 {
00582         
00583         if (setupForMaterial(mat, drv, scene)) // a switch from v.p enabled / disabled force to reactivate the vertex buffer.
00584         {
00585                 drv->activeVertexBufferHard(vb);                
00586         }
00587 }

void NL3D::CMeshVPPerPixelLight::setupForMaterial const CMaterial mat,
IDriver drv,
CScene scene,
CVertexBuffer vb
[virtual]
 

Setup for gouraud lighting

Todo:
disabling of specular lighting with this shader

Implements NL3D::IMeshVertexProgram.

Definition at line 564 of file meshvp_per_pixel_light.cpp.

References NL3D::IDriver::activeVertexBuffer().

Referenced by setupForMaterial().

00568 {
00569         
00570         if (setupForMaterial(mat, drv, scene)) // a switch from v.p enabled / disabled force to reactivate the vertex buffer.
00571         {
00572                 drv->activeVertexBuffer(*vb);           
00573         }
00574 }

virtual bool NL3D::IMeshVertexProgram::supportMeshBlockRendering  )  const [inline, virtual, inherited]
 

Reimplemented in NL3D::CMeshVPWindTree.

Definition at line 120 of file mesh_vertex_program.h.

00120 { return false; }


Friends And Related Function Documentation

friend struct CPtrInfo [friend, inherited]
 

Definition at line 67 of file smart_ptr.h.


Field Documentation

bool NL3D::CMeshVPPerPixelLight::_Enabled [private]
 

Definition at line 96 of file meshvp_per_pixel_light.h.

Referenced by CMeshVPPerPixelLight(), enable(), and setupForMaterial().

bool NL3D::CMeshVPPerPixelLight::_IsPointLight [private]
 

Definition at line 97 of file meshvp_per_pixel_light.h.

Referenced by begin(), and enable().

std::auto_ptr< CVertexProgram > NL3D::CMeshVPPerPixelLight::_VertexProgram [static, private]
 

Definition at line 44 of file meshvp_per_pixel_light.cpp.

sint NLMISC::CRefCount::crefs [mutable, inherited]
 

Definition at line 79 of file smart_ptr.h.

Referenced by NLMISC::CRefCount::CRefCount(), NLMISC::CRefCount::getRefCount(), and NLMISC::CRefCount::~CRefCount().

CRefCount::CPtrInfo NLMISC::CRefCount::NullPtrInfo [static, inherited]
 

Referenced by NLMISC::CRefCount::CRefCount().

CPtrInfo* NLMISC::CRefCount::pinfo [mutable, inherited]
 

Definition at line 80 of file smart_ptr.h.

Referenced by NLMISC::CRefCount::CRefCount(), and NLMISC::CRefCount::~CRefCount().

bool NL3D::CMeshVPPerPixelLight::SpecularLighting
 

true if want Specular Lighting.

Definition at line 62 of file meshvp_per_pixel_light.h.

Referenced by begin(), CMeshVPPerPixelLight(), and enable().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 06:56:12 2004 for NeL by doxygen 1.3.6