📄 mgcbillboardnode.cpp
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement. The various license agreements may be found at
// the Magic Software web site. This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf
#include "MgcBillboardNode.h"
#include "MgcRenderer.h"
MgcImplementRTTI(MgcBillboardNode,MgcNode);
MgcImplementStream(MgcBillboardNode);
//----------------------------------------------------------------------------
MgcBillboardNode::MgcBillboardNode (unsigned int uiQuantity,
unsigned int uiGrowBy)
:
MgcNode(uiQuantity,uiGrowBy)
{
m_fLastUpdateTime = -MgcMath::INFINITY;
}
//----------------------------------------------------------------------------
void MgcBillboardNode::RotateBillboard (const MgcCamera* pkCamera)
{
// This routine is a deferred call of UpdateGS. The billboard orientation
// is computed just before drawing, so it is only at this point that the
// local transforms are computed and that the world transforms can be
// computed from them. The first part of this code is similar to
// MgcSpatial::UpdateWorldData in that the transforms are computed. The
// twist is that after the world transforms are known, the billboard must
// be rotated in world space to face the camera. The last part of the
// code is the recursive update on the children.
// Compute billboard's world transforms based on its parent's world
// transform and its local transforms.
MgcNode* pkParent = GetParent();
if ( pkParent )
{
m_kWorldRotate = pkParent->WorldRotate()*m_kRotate;
m_kWorldTranslate = pkParent->WorldTranslate() +
pkParent->WorldScale()*(pkParent->WorldRotate()*m_kTranslate);
m_fWorldScale = pkParent->WorldScale()*m_fScale;
}
else
{
m_kWorldRotate = m_kRotate;
m_kWorldTranslate = m_kTranslate;
m_fWorldScale = m_fScale;
}
// Compute the additional rotation required for the billboard to face
// the camera. To do this, the camera must be inverse-transformed into
// the model space of the billboard.
MgcVector3 kDiff = pkCamera->GetLocation() - m_kWorldTranslate;
MgcReal fInvWorldScale = 1.0/m_fWorldScale;
MgcVector3 kCLoc = fInvWorldScale*(kDiff*m_kWorldRotate);
// squared length of the camera projection in the xz-plane
const MgcReal fEpsilon = 1e-06;
MgcReal fSqrLength = kCLoc.x*kCLoc.x + kCLoc.z*kCLoc.z;
if ( fSqrLength < fEpsilon )
{
// camera on the billboard axis, rotation not defined
return;
}
// unitize the projection
MgcReal fInvLength = 1.0/MgcMath::Sqrt(fSqrLength);
kCLoc.x *= fInvLength;
kCLoc.y = 0.0;
kCLoc.z *= fInvLength;
// compute the local orientation matrix for the billboard
MgcMatrix3 kOrient(kCLoc.z,0.0,kCLoc.x,0.0,1.0,0.0,-kCLoc.x,0.0,kCLoc.z);
// The billboard must be oriented to face the camera before it is
// transformed into the world.
m_kWorldRotate = m_kWorldRotate*kOrient;
// compute the update now that the billboard orientation is known
for (unsigned int uiI = 0; uiI < m_aspkChild.GetQuantity(); uiI++)
{
MgcSpatial* pkChild = m_aspkChild[uiI];
if ( pkChild )
pkChild->UpdateGS(m_fLastUpdateTime,false);
}
}
//----------------------------------------------------------------------------
void MgcBillboardNode::UpdateWorldData (MgcReal fAppTime)
{
// Save the update time. The update is deferred until Draw so that the
// billboard is rotated only when it is visible. At this time the
// update time is needed to pass to the UpdateGS call.
m_fLastUpdateTime = fAppTime;
// The deferred update means that none of the children world data is
// computed by a recursive traversal. Just compute the world bound.
UpdateWorldBound();
}
//----------------------------------------------------------------------------
void MgcBillboardNode::Draw (MgcRenderer& rkRenderer)
{
RotateBillboard(rkRenderer.GetCamera());
MgcNode::Draw(rkRenderer);
}
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcBillboardNode::Factory (MgcStream& rkStream)
{
MgcBillboardNode* pkObject = new MgcBillboardNode;
MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
pkObject->Load(rkStream,pkLink);
return pkObject;
}
//---------------------------------------------------------------------------
void MgcBillboardNode::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
MgcNode::Load(rkStream,pkLink);
}
//---------------------------------------------------------------------------
void MgcBillboardNode::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
MgcNode::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcBillboardNode::Register (MgcStream& rkStream)
{
return MgcNode::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcBillboardNode::Save (MgcStream& rkStream)
{
MgcNode::Save(rkStream);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -