📄 wmlbumpmap.cpp
字号:
-kBinormal.X(), -kBinormal.Y(), -kBinormal.Z(),
apkN[i]->X(), apkN[i]->Y(), apkN[i]->Z());
// Compute the tangent space light vector. Conceptually
// kTSLight = kRotTS*(kMLight-Vertex[i])/|kMLight-Vertex[i]|
Vector3f kTSLight = kMLight;
// Subtract off the vertex position if we have a positional light.
if ( m_spkLight->GetType() != Light::LT_DIRECTIONAL )
kTSLight -= *apkV[i];
kTSLight.Normalize();
kTSLight = kRotTS*kTSLight;
// Transform the light vector into [0,1]^3 to make it a valid
// ColorRGB object.
rkColor.r = 0.5f*(kTSLight.X() + 1.0f);
rkColor.g = 0.5f*(kTSLight.Y() + 1.0f);
rkColor.b = 0.5f*(kTSLight.Z() + 1.0f);
}
}
}
//----------------------------------------------------------------------------
void BumpMap::UpdateWorldBound()
{
m_kWorldBound = m_spkObjects->WorldBound();
}
//----------------------------------------------------------------------------
void BumpMap::Draw (Renderer& rkRenderer)
{
m_spkObjects->ForceCull() = m_bForceCullObjects;
rkRenderer.Draw(*this);
m_spkObjects->ForceCull() = true;
}
//----------------------------------------------------------------------------
void BumpMap::SetCurrentAmbientMaterial (const ColorRGB& rkColor)
{
// This is used to capture material changes for bump mapping. Materials
// are implemented by using additional texture units with a constant color
// as one of the inputs for a texture add.
m_spkTextureStateModulated->Get(2)->BlendColor() =
rkColor*m_spkLight->Ambient();
}
//----------------------------------------------------------------------------
void BumpMap::SetCurrentDiffuseMaterial (const ColorRGB& rkColor,
int iMaxTextureUnits)
{
// This is used to capture material changes for bump mapping. Materials
// are implemented by using additional texture units with a constant color
// as one of the inputs for a texture modulate. If there are enough
// texture units, then ambient is added. If not, then it gets modulated
// by L.N.
if ( iMaxTextureUnits > 2 )
{
m_spkTextureStateModulated->Get(1)->BlendColor() = rkColor *
m_spkLight->Diffuse();
}
else
{
// If we only have 2 texture units, then ambient gets modulated by L.N
// along with diffuse. Ambient must be set before diffuse for this to
// work transparently. This is done automatically inside
// WmlLightState.cpp.
m_spkTextureStateModulated->Get(1)->BlendColor() =
rkColor*m_spkLight->Diffuse() +
m_spkTextureStateModulated->Get(2)->BlendColor();
}
}
//----------------------------------------------------------------------------
Object* BumpMap::GetObjectByName (const char* acName)
{
Object* pkFound = Node::GetObjectByName(acName);
if ( pkFound )
return pkFound;
// The subtree m_spkObjects is not searched to avoid the possibility of
// infinite recursion.
if ( m_spkNormalMap )
{
pkFound = m_spkNormalMap->GetObjectByName(acName);
if ( pkFound )
return pkFound;
}
if ( m_spkTextureState )
{
pkFound = m_spkTextureState->GetObjectByName(acName);
if ( pkFound )
return pkFound;
}
if ( m_spkTextureStateModulated )
{
pkFound = m_spkTextureStateModulated->GetObjectByName(acName);
if ( pkFound )
return pkFound;
}
if ( m_spkLight )
{
pkFound = m_spkLight->GetObjectByName(acName);
if ( pkFound )
return pkFound;
}
if ( m_spkAlphaState )
{
pkFound = m_spkAlphaState->GetObjectByName(acName);
if ( pkFound )
return pkFound;
}
return NULL;
}
//----------------------------------------------------------------------------
void BumpMap::GetAllObjectsByName (const char* acName,
std::vector<Object*>& rkObjects)
{
Node::GetAllObjectsByName(acName,rkObjects);
// The subtree m_spkObjects is not searched to avoid the possibility of
// infinite recursion.
if ( m_spkNormalMap )
m_spkNormalMap->GetAllObjectsByName(acName,rkObjects);
if ( m_spkTextureState )
m_spkTextureState->GetAllObjectsByName(acName,rkObjects);
if ( m_spkTextureStateModulated )
m_spkTextureStateModulated->GetAllObjectsByName(acName,rkObjects);
if ( m_spkLight )
m_spkLight->GetAllObjectsByName(acName,rkObjects);
if ( m_spkAlphaState )
m_spkAlphaState->GetAllObjectsByName(acName,rkObjects);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// streaming
//----------------------------------------------------------------------------
Object* BumpMap::Factory (Stream& rkStream)
{
BumpMap* pkObject = new BumpMap;
Stream::Link* pkLink = new Stream::Link(pkObject);
pkObject->Load(rkStream,pkLink);
return pkObject;
}
//----------------------------------------------------------------------------
void BumpMap::Load (Stream& rkStream, Stream::Link* pkLink)
{
Node::Load(rkStream,pkLink);
// native data
StreamReadBool(rkStream,m_bModulate);
// link data (objects, normal map, texture state, modulated texture
// state, light state, alpha state, in that order)
Spatial* pkChild;
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
StreamRead(rkStream,pkChild);
pkLink->Add(pkChild);
}
//----------------------------------------------------------------------------
void BumpMap::Link (Stream& rkStream, Stream::Link* pkLink)
{
Node::Link(rkStream,pkLink);
Object* pkLinkID = pkLink->GetLinkID();
m_spkObjects = (Node*)rkStream.GetFromMap(pkLinkID);
pkLinkID = pkLink->GetLinkID();
m_spkNormalMap = (Texture*)rkStream.GetFromMap(pkLinkID);
pkLinkID = pkLink->GetLinkID();
m_spkTextureState = (TextureState*)rkStream.GetFromMap(pkLinkID);
pkLinkID = pkLink->GetLinkID();
m_spkTextureStateModulated = (TextureState*)rkStream.GetFromMap(pkLinkID);
pkLinkID = pkLink->GetLinkID();
m_spkLight = (Light*)rkStream.GetFromMap(pkLinkID);
pkLinkID = pkLink->GetLinkID();
m_spkAlphaState = (AlphaState*)rkStream.GetFromMap(pkLinkID);
// initialize the cull status for correct toggling during rendering
m_bForceCullObjects = m_spkObjects->ForceCull();
m_spkObjects->ForceCull() = true;
}
//----------------------------------------------------------------------------
bool BumpMap::Register (Stream& rkStream)
{
if ( !Node::Register(rkStream) )
return false;
if ( m_spkObjects )
m_spkObjects->Register(rkStream);
if ( m_spkNormalMap )
m_spkNormalMap->Register(rkStream);
if ( m_spkTextureState )
m_spkTextureState->Register(rkStream);
if ( m_spkTextureStateModulated )
m_spkTextureStateModulated->Register(rkStream);
if ( m_spkLight )
m_spkLight->Register(rkStream);
if ( m_spkAlphaState )
m_spkAlphaState->Register(rkStream);
return true;
}
//----------------------------------------------------------------------------
void BumpMap::Save (Stream& rkStream)
{
Node::Save(rkStream);
// native data
StreamWriteBool(rkStream,m_bModulate);
// link data
StreamWrite(rkStream,m_spkObjects);
StreamWrite(rkStream,m_spkNormalMap);
StreamWrite(rkStream,m_spkTextureState);
StreamWrite(rkStream,m_spkTextureStateModulated);
StreamWrite(rkStream,m_spkLight);
StreamWrite(rkStream,m_spkAlphaState);
}
//----------------------------------------------------------------------------
StringTree* BumpMap::SaveStrings ()
{
int iCQuantity = 1;
if ( m_spkObjects )
iCQuantity++;
if ( m_spkNormalMap )
iCQuantity++;
if ( m_spkTextureState )
iCQuantity++;
if ( m_spkTextureStateModulated )
iCQuantity++;
if ( m_spkLight )
iCQuantity++;
if ( m_spkAlphaState )
iCQuantity++;
StringTree* pkTree = new StringTree(2,0,iCQuantity,0);
// strings
pkTree->SetString(0,MakeString(&ms_kRTTI,GetName()));
pkTree->SetString(1,MakeString("modulated =",m_bModulate));
// children
pkTree->SetChild(0,Node::SaveStrings());
int iSlot = 1;
if ( m_spkObjects )
pkTree->SetChild(iSlot++,m_spkObjects->SaveStrings());
if ( m_spkNormalMap )
pkTree->SetChild(iSlot++,m_spkNormalMap->SaveStrings());
if ( m_spkTextureState )
pkTree->SetChild(iSlot++,m_spkTextureState->SaveStrings());
if ( m_spkTextureStateModulated )
pkTree->SetChild(iSlot++,m_spkTextureStateModulated->SaveStrings());
if ( m_spkLight )
pkTree->SetChild(iSlot++,m_spkLight->SaveStrings());
if ( m_spkAlphaState )
pkTree->SetChild(iSlot++,m_spkAlphaState->SaveStrings());
return pkTree;
}
//----------------------------------------------------------------------------
int BumpMap::GetMemoryUsed () const
{
int iBaseSize = sizeof(BumpMap) - sizeof(Node);
int iTotalSize = iBaseSize + Node::GetMemoryUsed();
return iTotalSize;
}
//----------------------------------------------------------------------------
int BumpMap::GetDiskUsed () const
{
return Node::GetDiskUsed() +
sizeof(m_bModulate) +
sizeof(m_spkObjects) +
sizeof(m_spkNormalMap) +
sizeof(m_spkTextureState) +
sizeof(m_spkTextureStateModulated) +
sizeof(m_spkLight) +
sizeof(m_spkAlphaState);
}
//----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -