1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "Wm5GraphicsPCH.h"
#include "Wm5DlodNode.h"
#include "Wm5Camera.h"
using namespace Wm5;
WM5_IMPLEMENT_RTTI(Wm5, SwitchNode, DlodNode);
WM5_IMPLEMENT_STREAM(DlodNode);
WM5_IMPLEMENT_FACTORY(DlodNode);
WM5_IMPLEMENT_DEFAULT_NAMES(SwitchNode, DlodNode);
//----------------------------------------------------------------------------
DlodNode::DlodNode (int numLevelsOfDetail)
:
mNumLevelsOfDetail(numLevelsOfDetail)
{
mModelMinDist = new1<float>(mNumLevelsOfDetail);
mModelMaxDist = new1<float>(mNumLevelsOfDetail);
mWorldMinDist = new1<float>(mNumLevelsOfDetail);
mWorldMaxDist = new1<float>(mNumLevelsOfDetail);
}
//----------------------------------------------------------------------------
DlodNode::~DlodNode ()
{
delete1(mModelMinDist);
delete1(mModelMaxDist);
delete1(mWorldMinDist);
delete1(mWorldMaxDist);
}
//----------------------------------------------------------------------------
void DlodNode::SetModelDistance (int i, float minDist, float maxDist)
{
if (0 <= i && i < mNumLevelsOfDetail)
{
mModelMinDist[i] = minDist;
mModelMaxDist[i] = maxDist;
mWorldMinDist[i] = minDist;
mWorldMaxDist[i] = maxDist;
return;
}
assertion(false, "Invalid index in SetModelDistance.\n");
}
//----------------------------------------------------------------------------
void DlodNode::SelectLevelOfDetail (const Camera* camera)
{
// ASSERT: The child array of an DlodNode is compacted--there are no
// empty slots in the array and the number of children is mChild.size().
// Moreover, it is assumed that all model distance values were set for
// these children.
// Compute the world LOD center.
mWorldLodCenter = WorldTransform*mModelLodCenter;
// Compute the world squared-distance intervals.
int i;
for (i = 0; i < mNumLevelsOfDetail; ++i)
{
mWorldMinDist[i] = WorldTransform.GetUniformScale()*mModelMinDist[i];
mWorldMaxDist[i] = WorldTransform.GetUniformScale()*mModelMaxDist[i];
}
// Select the LOD child.
SetActiveChild(SN_INVALID_CHILD);
AVector diff = mWorldLodCenter - camera->GetPosition();
float dist = diff.Length();
for (i = 0; i < mNumLevelsOfDetail; ++i)
{
if (mWorldMinDist[i] <= dist && dist < mWorldMaxDist[i])
{
SetActiveChild(i);
break;
}
}
}
//----------------------------------------------------------------------------
void DlodNode::GetVisibleSet (Culler& culler, bool noCull)
{
SelectLevelOfDetail(culler.GetCamera());
SwitchNode::GetVisibleSet(culler, noCull);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Streaming support.
//----------------------------------------------------------------------------
DlodNode::DlodNode (LoadConstructor value)
:
SwitchNode(value),
mNumLevelsOfDetail(0),
mModelMinDist(0),
mModelMaxDist(0),
mWorldMinDist(0),
mWorldMaxDist(0)
{
}
//----------------------------------------------------------------------------
void DlodNode::Load (InStream& source)
{
WM5_BEGIN_DEBUG_STREAM_LOAD(source);
SwitchNode::Load(source);
source.ReadAggregate(mModelLodCenter);
source.ReadRR(mNumLevelsOfDetail, mModelMinDist);
source.ReadVR(mNumLevelsOfDetail, mModelMaxDist);
source.ReadVR(mNumLevelsOfDetail, mWorldMinDist);
source.ReadVR(mNumLevelsOfDetail, mWorldMaxDist);
WM5_END_DEBUG_STREAM_LOAD(DlodNode, source);
}
//----------------------------------------------------------------------------
void DlodNode::Link (InStream& source)
{
SwitchNode::Link(source);
}
//----------------------------------------------------------------------------
void DlodNode::PostLink ()
{
SwitchNode::PostLink();
}
//----------------------------------------------------------------------------
bool DlodNode::Register (OutStream& target) const
{
return SwitchNode::Register(target);
}
//----------------------------------------------------------------------------
void DlodNode::Save (OutStream& target) const
{
WM5_BEGIN_DEBUG_STREAM_SAVE(target);
SwitchNode::Save(target);
target.WriteAggregate(mModelLodCenter);
target.WriteW(mNumLevelsOfDetail, mModelMinDist);
target.WriteN(mNumLevelsOfDetail, mModelMaxDist);
target.WriteN(mNumLevelsOfDetail, mWorldMinDist);
target.WriteN(mNumLevelsOfDetail, mWorldMaxDist);
// The world LOD center is computed from the model LOD center in
// SelectLevelOfDetail. The world distance extremes are also computed
// from the model distance extremes in SelectLevelOfDetail. These
// world quantities do not need to be saved.
WM5_END_DEBUG_STREAM_SAVE(DlodNode, target);
}
//----------------------------------------------------------------------------
int DlodNode::GetStreamingSize () const
{
int size = SwitchNode::GetStreamingSize();
size += sizeof(mModelLodCenter);
size += sizeof(mNumLevelsOfDetail);
size += mNumLevelsOfDetail*sizeof(float);
size += mNumLevelsOfDetail*sizeof(float);
size += mNumLevelsOfDetail*sizeof(float);
size +=mNumLevelsOfDetail*sizeof(float);
return size;
}
//----------------------------------------------------------------------------
|