1
0
forked from 0ad/0ad

Indentation corrections and other code style fixes in preparation for the cinematic camera patch.

Refs #3301

This was SVN commit r17578.
This commit is contained in:
Yves 2015-12-31 13:40:56 +00:00
parent 460f17e6f7
commit a144ec7e08
5 changed files with 216 additions and 213 deletions

View File

@ -38,7 +38,7 @@ void CCinemaManager::AddPath(CCinemaPath path, const CStrW& name)
m_Paths[name] = path;
}
void CCinemaManager::QueuePath(const CStrW& name, bool queue )
void CCinemaManager::QueuePath(const CStrW& name, bool queue)
{
if (!m_PathQueue.empty() && queue == false)
{
@ -58,7 +58,7 @@ void CCinemaManager::OverridePath(const CStrW& name)
m_PathQueue.push_back( m_Paths[name] );
}
void CCinemaManager::SetAllPaths( const std::map<CStrW, CCinemaPath>& paths)
void CCinemaManager::SetAllPaths(const std::map<CStrW, CCinemaPath>& paths)
{
CStrW name;
m_Paths = paths;
@ -96,12 +96,12 @@ void CCinemaManager::MoveToPointAt(float time)
StopPlaying();
m_CurrentPath->second.m_TimeElapsed = time;
if ( !m_CurrentPath->second.Validate() )
if (!m_CurrentPath->second.Validate())
return;
m_CurrentPath->second.MoveToPointAt(m_CurrentPath->second.m_TimeElapsed /
m_CurrentPath->second.GetDuration(), m_CurrentPath->second.GetNodeFraction(),
m_CurrentPath->second.m_PreviousRotation );
m_CurrentPath->second.m_PreviousRotation);
}
bool CCinemaManager::Update(const float deltaRealTime)

View File

@ -868,7 +868,7 @@ void CXMLReader::ReadCinema(XMBElement parent)
{
int elementName = element.GetNodeName();
if ( elementName == el_path )
if (elementName == el_path)
{
XMBAttributeList attrs = element.GetAttributes();
CStrW name(attrs.GetNamedItem(at_name).FromUTF8());
@ -883,7 +883,7 @@ void CXMLReader::ReadCinema(XMBElement parent)
attrs = pathChild.GetAttributes();
//Load distortion attributes
if ( elementName == el_distortion )
if (elementName == el_distortion)
{
pathData.m_Mode = attrs.GetNamedItem(at_mode).ToInt();
pathData.m_Style = attrs.GetNamedItem(at_style).ToInt();
@ -892,7 +892,7 @@ void CXMLReader::ReadCinema(XMBElement parent)
}
//Load node data used for spline
else if ( elementName == el_node )
else if (elementName == el_node)
{
SplineData data;
XERO_ITER_EL(pathChild, nodeChild)
@ -901,21 +901,21 @@ void CXMLReader::ReadCinema(XMBElement parent)
attrs = nodeChild.GetAttributes();
//Fix?: assumes that time is last element
if ( elementName == el_position )
if (elementName == el_position)
{
data.Position.X = attrs.GetNamedItem(at_x).ToFloat();
data.Position.Y = attrs.GetNamedItem(at_y).ToFloat();
data.Position.Z = attrs.GetNamedItem(at_z).ToFloat();
continue;
}
else if ( elementName == el_rotation )
else if (elementName == el_rotation)
{
data.Rotation.X = attrs.GetNamedItem(at_x).ToFloat();
data.Rotation.Y = attrs.GetNamedItem(at_y).ToFloat();
data.Rotation.Z = attrs.GetNamedItem(at_z).ToFloat();
continue;
}
else if ( elementName == el_time )
else if (elementName == el_time)
data.Distance = nodeChild.GetText().ToFloat();
else
debug_warn(L"Invalid cinematic element for node child");
@ -932,14 +932,14 @@ void CXMLReader::ReadCinema(XMBElement parent)
//Construct cinema path with data gathered
CCinemaPath temp(pathData, backwardSpline);
const std::vector<SplineData>& nodes = temp.GetAllNodes();
if ( nodes.empty() )
if (nodes.empty())
{
debug_warn(L"Failure loading cinematics");
return;
}
for ( std::vector<SplineData>::const_reverse_iterator it = nodes.rbegin();
it != nodes.rend(); ++it )
for (std::vector<SplineData>::const_reverse_iterator it = nodes.rbegin();
it != nodes.rend(); ++it)
{
spline.AddNode(it->Position, it->Rotation, it->Distance);
}

View File

@ -70,7 +70,7 @@ private:
// UnpackTerrain: unpack the terrain from the input stream
int UnpackTerrain();
//UnpackCinema: unpack the cinematic tracks from the input stream
// UnpackCinema: unpack the cinematic tracks from the input stream
int UnpackCinema();
// UnpackMap: unpack the given data from the raw data stream into local variables

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -20,121 +20,122 @@
#include "Matrix3D.h"
//Note: column major order! Each set of 4 constitutes a column.
CMatrix3D HermiteSpline( 2.f, -3.f, 0.f, 1.f,
-2.f, 3.f, 0.f, 0.f,
1.f, -2.f, 1.f, 0.f,
1.f, -1.f, 0.f, 0.f ); // Matrix H in article
CMatrix3D HermiteSpline(2.f, -3.f, 0.f, 1.f,
-2.f, 3.f, 0.f, 0.f,
1.f, -2.f, 1.f, 0.f,
1.f, -1.f, 0.f, 0.f); // Matrix H in article
// cubic curve defined by 2 positions and 2 velocities
CVector3D GetPositionOnCubic(const CVector3D &startPos, const CVector3D &startVel, const CVector3D &endPos, const CVector3D &endVel, float time)
CVector3D GetPositionOnCubic(const CVector3D& startPos, const CVector3D& startVel, const CVector3D& endPos, const CVector3D& endVel, float time)
{
CMatrix3D m( startPos.X, endPos.X, startVel.X, endVel.X,
startPos.Y, endPos.Y, startVel.Y, endVel.Y,
startPos.Z, endPos.Z, startVel.Z, endVel.Z,
0.0f, 0.0f, 0.0f, 1.0f );
m = m * HermiteSpline; // multiply by the mixer
CMatrix3D m(startPos.X, endPos.X, startVel.X, endVel.X,
startPos.Y, endPos.Y, startVel.Y, endVel.Y,
startPos.Z, endPos.Z, startVel.Z, endVel.Z,
0.0f, 0.0f, 0.0f, 1.0f);
m = m * HermiteSpline; // multiply by the mixer
CVector3D TimeVector(time*time*time, time*time, time);
CVector3D Result;
m.Transform(TimeVector, Result);
return Result;
CVector3D TimeVector(time*time*time, time*time, time);
CVector3D Result;
m.Transform(TimeVector, Result);
return Result;
}
/*********************************** R N S **************************************************/
// adds node and updates segment length
void RNSpline::AddNode(const CVector3D &pos)
void RNSpline::AddNode(const CVector3D& pos)
{
if ( NodeCount >= MAX_SPLINE_NODES )
return;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = (Node[NodeCount-1].Position - pos).Length();
MaxDistance += Node[NodeCount-1].Distance;
}
SplineData temp;
temp.Position = pos;
Node.push_back(temp);
NodeCount++;
if (NodeCount >= MAX_SPLINE_NODES)
return;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = (Node[NodeCount-1].Position - pos).Length();
MaxDistance += Node[NodeCount-1].Distance;
}
SplineData temp;
temp.Position = pos;
Node.push_back(temp);
++NodeCount;
}
// called after all nodes added. This function calculates the node velocities
void RNSpline::BuildSpline()
{
if ( NodeCount == 2 )
{
Node[0].Velocity = GetStartVelocity(0);
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
return;
}
else if ( NodeCount < 2 )
return;
for (int i = 1; i<NodeCount-1; i++)
{
CVector3D Next = Node[i+1].Position - Node[i].Position;
CVector3D Previous = Node[i-1].Position - Node[i].Position;
Next.Normalize();
Previous.Normalize();
// split the angle (figure 4)
Node[i].Velocity = Next - Previous;
Node[i].Velocity.Normalize();
}
// calculate start and end velocities
Node[0].Velocity = GetStartVelocity(0);
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
if (NodeCount == 2)
{
Node[0].Velocity = GetStartVelocity(0);
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
return;
}
else if (NodeCount < 2)
return;
for (int i = 1; i < NodeCount-1; ++i)
{
CVector3D Next = Node[i+1].Position - Node[i].Position;
CVector3D Previous = Node[i-1].Position - Node[i].Position;
Next.Normalize();
Previous.Normalize();
// split the angle (figure 4)
Node[i].Velocity = Next - Previous;
Node[i].Velocity.Normalize();
}
// calculate start and end velocities
Node[0].Velocity = GetStartVelocity(0);
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
}
// spline access function. time is 0 -> 1
CVector3D RNSpline::GetPosition(float time) const
{
if ( NodeCount < 2 )
return CVector3D(0.0f, 0.0f, 0.0f);
if ( time > 1.0f )
if (NodeCount < 2)
return CVector3D(0.0f, 0.0f, 0.0f);
if (time > 1.0f)
time = 1.0f;
float Distance = time * MaxDistance;
float CurrentDistance = 0.f;
int i = 0;
//Find which node we're on
while (CurrentDistance + Node[i].Distance < Distance
&& i < NodeCount - 2)
{
CurrentDistance += Node[i].Distance;
i++;
}
ENSURE( i < NodeCount - 1 );
float t = Distance - CurrentDistance;
t /= Node[i].Distance; // scale t in range 0 - 1
CVector3D startVel = Node[i].Velocity * Node[i].Distance;
CVector3D endVel = Node[i+1].Velocity * Node[i].Distance;
return GetPositionOnCubic(Node[i].Position, startVel,
Node[i+1].Position, endVel, t);
float Distance = time * MaxDistance;
float CurrentDistance = 0.f;
int i = 0;
//Find which node we're on
while (CurrentDistance + Node[i].Distance < Distance && i < NodeCount - 2)
{
CurrentDistance += Node[i].Distance;
++i;
}
ENSURE(i < NodeCount - 1);
float t = Distance - CurrentDistance;
t /= Node[i].Distance; // scale t in range 0 - 1
CVector3D startVel = Node[i].Velocity * Node[i].Distance;
CVector3D endVel = Node[i+1].Velocity * Node[i].Distance;
return GetPositionOnCubic(Node[i].Position, startVel, Node[i+1].Position, endVel, t);
}
// internal. Based on Equation 14
CVector3D RNSpline::GetStartVelocity(int index)
{
if ( index >= NodeCount - 1 || index < 0)
return CVector3D(0.0f, 0.0f, 0.0f);
CVector3D temp = (Node[index+1].Position - Node[index].Position) * 3.0f * ( 1.0f / Node[index].Distance) ;
return (temp - Node[index+1].Velocity)*0.5f;
if (index >= NodeCount - 1 || index < 0)
return CVector3D(0.0f, 0.0f, 0.0f);
CVector3D temp = (Node[index+1].Position - Node[index].Position) * 3.0f * (1.0f / Node[index].Distance);
return (temp - Node[index+1].Velocity)*0.5f;
}
// internal. Based on Equation 15
CVector3D RNSpline::GetEndVelocity(int index)
{
if ( index >= NodeCount || index < 1)
return CVector3D(0.0f, 0.0f, 0.0f);
CVector3D temp = (Node[index].Position - Node[index-1].Position) * 3.0f * (1.0f / Node[index-1].Distance);
return (temp - Node[index-1].Velocity) * 0.5f;
if (index >= NodeCount || index < 1)
return CVector3D(0.0f, 0.0f, 0.0f);
CVector3D temp = (Node[index].Position - Node[index-1].Position) * 3.0f * (1.0f / Node[index-1].Distance);
return (temp - Node[index-1].Velocity) * 0.5f;
}
/*********************************** S N S **************************************************/
@ -142,110 +143,111 @@ CVector3D RNSpline::GetEndVelocity(int index)
// smoothing filter.
void SNSpline::Smooth()
{
if ( NodeCount < 3 )
return;
CVector3D newVel;
CVector3D oldVel = GetStartVelocity(0);
for (int i = 1; i<NodeCount-1; i++)
{
// Equation 12
newVel = GetEndVelocity(i) * Node[i].Distance +
GetStartVelocity(i) * Node[i-1].Distance;
newVel = newVel * ( 1 / (Node[i-1].Distance + Node[i].Distance) );
Node[i-1].Velocity = oldVel;
oldVel = newVel;
}
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
Node[NodeCount-2].Velocity = oldVel;
if (NodeCount < 3)
return;
CVector3D newVel;
CVector3D oldVel = GetStartVelocity(0);
for (int i = 1; i < NodeCount-1; ++i)
{
// Equation 12
newVel = GetEndVelocity(i) * Node[i].Distance + GetStartVelocity(i) * Node[i-1].Distance;
newVel = newVel * ( 1 / (Node[i-1].Distance + Node[i].Distance) );
Node[i-1].Velocity = oldVel;
oldVel = newVel;
}
Node[NodeCount-1].Velocity = GetEndVelocity(NodeCount-1);
Node[NodeCount-2].Velocity = oldVel;
}
/*********************************** T N S **************************************************/
// as with RNSpline but use timePeriod in place of actual node spacing
// ie time period is time from last node to this node
void TNSpline::AddNode(const CVector3D &pos, const CVector3D& rotation, float timePeriod)
void TNSpline::AddNode(const CVector3D& pos, const CVector3D& rotation, float timePeriod)
{
if ( NodeCount >= MAX_SPLINE_NODES )
return;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = timePeriod;
MaxDistance += Node[NodeCount-1].Distance;
}
if (NodeCount >= MAX_SPLINE_NODES)
return;
SplineData temp;
temp.Position = pos;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = timePeriod;
MaxDistance += Node[NodeCount-1].Distance;
}
//make sure we don't end up using undefined numbers...
temp.Distance = 0.0f;
temp.Velocity = CVector3D( 0.0f, 0.0f, 0.0f );
temp.Rotation = rotation;
Node.push_back(temp);
NodeCount++;
SplineData temp;
temp.Position = pos;
//make sure we don't end up using undefined numbers...
temp.Distance = 0.0f;
temp.Velocity = CVector3D(0.0f, 0.0f, 0.0f);
temp.Rotation = rotation;
Node.push_back(temp);
++NodeCount;
}
//Inserts node before position
void TNSpline::InsertNode(const int index, const CVector3D &pos, const CVector3D& rotation, float timePeriod)
void TNSpline::InsertNode(const int index, const CVector3D& pos, const CVector3D& rotation, float timePeriod)
{
if ( NodeCount >= MAX_SPLINE_NODES || index < NodeCount - 1 )
return;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = timePeriod;
Node[NodeCount-1].Rotation = rotation;
MaxDistance += Node[NodeCount-1].Distance;
}
SplineData temp;
temp.Position = pos;
Node.insert(Node.begin() + index, temp);
NodeCount++;
if (NodeCount >= MAX_SPLINE_NODES || index < NodeCount - 1)
return;
if (NodeCount == 0)
MaxDistance = 0.f;
else
{
Node[NodeCount-1].Distance = timePeriod;
Node[NodeCount-1].Rotation = rotation;
MaxDistance += Node[NodeCount-1].Distance;
}
SplineData temp;
temp.Position = pos;
Node.insert(Node.begin() + index, temp);
++NodeCount;
}
//Removes node at index
void TNSpline::RemoveNode(const int index)
{
if (NodeCount == 0 || index > NodeCount - 1 )
{
return;
}
else
{
MaxDistance -= Node[index].Distance;
MaxDistance -= Node[index-1].Distance;
Node[index-1].Distance = 0.0f;
Node.erase( Node.begin() + index, Node.begin() + index + 1 );
}
NodeCount--;
}
void TNSpline::UpdateNodeTime(const int index, float time)
{
if (NodeCount == 0 || index > NodeCount - 1 )
{
return;
}
Node[index].Distance = time;
}
void TNSpline::UpdateNodePos(const int index, const CVector3D &pos)
{
if (NodeCount == 0 || index > NodeCount - 1 )
{
return;
}
Node[index].Position = pos;
}
void TNSpline::Constrain()
{
if ( NodeCount < 3 )
return;
for (int i = 1; i<NodeCount-1; i++)
{
// Equation 13
float r0 = (Node[i].Position-Node[i-1].Position).Length() / Node[i-1].Distance;
float r1 = (Node[i+1].Position - Node[i].Position).Length() / Node[i].Distance;
Node[i].Velocity *= 4.0f*r0*r1/((r0+r1)*(r0+r1));
}
if (NodeCount == 0 || index > NodeCount - 1)
return;
MaxDistance -= Node[index].Distance;
MaxDistance -= Node[index-1].Distance;
Node[index-1].Distance = 0.0f;
Node.erase(Node.begin() + index, Node.begin() + index + 1);
--NodeCount;
}
void TNSpline::UpdateNodeTime(const int index, float time)
{
if (NodeCount == 0 || index > NodeCount - 1)
return;
Node[index].Distance = time;
}
void TNSpline::UpdateNodePos(const int index, const CVector3D& pos)
{
if (NodeCount == 0 || index > NodeCount - 1)
return;
Node[index].Position = pos;
}
void TNSpline::Constrain()
{
if (NodeCount < 3)
return;
for (int i = 1; i < NodeCount-1; ++i)
{
// Equation 13
float r0 = (Node[i].Position-Node[i-1].Position).Length() / Node[i-1].Distance;
float r1 = (Node[i+1].Position - Node[i].Position).Length() / Node[i].Distance;
Node[i].Velocity *= 4.0f*r0*r1/((r0+r1)*(r0+r1));
}
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -28,40 +28,41 @@
struct SplineData
{
CVector3D Position;
CVector3D Velocity;
CVector3D Position;
CVector3D Velocity;
CVector3D Rotation;
float Distance/*, DistanceOffset*/; //DistanceOffset is to keep track of how far into the spline this node is
float Distance/*, DistanceOffset*/; //DistanceOffset is to keep track of how far into the spline this node is
};
class RNSpline
{
public:
RNSpline() { NodeCount = 0; }
virtual ~RNSpline() {}
void AddNode(const CVector3D &pos);
void BuildSpline();
CVector3D GetPosition(float time) const;
CVector3D GetRotation(float time) const;
const std::vector<SplineData>& GetAllNodes() const { return Node; }
float MaxDistance;
int NodeCount;
void AddNode(const CVector3D& pos);
void BuildSpline();
CVector3D GetPosition(float time) const;
CVector3D GetRotation(float time) const;
const std::vector<SplineData>& GetAllNodes() const { return Node; }
float MaxDistance;
int NodeCount;
protected:
std::vector<SplineData> Node;
CVector3D GetStartVelocity(int index);
CVector3D GetEndVelocity(int index);
std::vector<SplineData> Node;
CVector3D GetStartVelocity(int index);
CVector3D GetEndVelocity(int index);
};
class SNSpline : public RNSpline
{
public:
virtual ~SNSpline() {}
void BuildSpline(){ RNSpline::BuildSpline(); Smooth(); Smooth(); Smooth(); }
void Smooth();
void BuildSpline(){ RNSpline::BuildSpline(); Smooth(); Smooth(); Smooth(); }
void Smooth();
};
class TNSpline : public SNSpline
@ -69,16 +70,16 @@ class TNSpline : public SNSpline
public:
virtual ~TNSpline() {}
void AddNode(const CVector3D& pos, const CVector3D& rotation, float timePeriod);
void PushNode() { Node.push_back( SplineData() ); }
void InsertNode(const int index, const CVector3D &pos, const CVector3D& rotation, float timePeriod);
void RemoveNode(const int index);
void UpdateNodeTime(const int index, float time);
void UpdateNodePos(const int index, const CVector3D &pos);
void BuildSpline(){ RNSpline::BuildSpline(); Smooth(); Smooth(); Smooth(); }
void Smooth(){ for( int x=0; x<3; x++ ) { SNSpline::Smooth(); Constrain(); } }
void Constrain();
void AddNode(const CVector3D& pos, const CVector3D& rotation, float timePeriod);
void PushNode() { Node.push_back( SplineData() ); }
void InsertNode(const int index, const CVector3D& pos, const CVector3D& rotation, float timePeriod);
void RemoveNode(const int index);
void UpdateNodeTime(const int index, float time);
void UpdateNodePos(const int index, const CVector3D& pos);
void BuildSpline(){ RNSpline::BuildSpline(); Smooth(); Smooth(); Smooth(); }
void Smooth(){ for (int x = 0; x < 3; ++x) { SNSpline::Smooth(); Constrain(); } }
void Constrain();
};
#endif // INCLUDED_NUSPLINE