Skip to content

Commit afb1a0a

Browse files
committed
Added time_value and sample templates and fieldOfview/nearFar support to CameraSampler.
1 parent beb377f commit afb1a0a

File tree

4 files changed

+97
-114
lines changed

4 files changed

+97
-114
lines changed

include/vsg/animation/CameraSampler.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,59 @@ namespace vsg
2828
std::string name;
2929

3030
/// position key frames
31-
std::vector<VectorKey> origins;
31+
std::vector<time_dvec3> origins;
3232

3333
/// position key frames
34-
std::vector<VectorKey> positions;
34+
std::vector<time_dvec3> positions;
3535

3636
/// rotation key frames
37-
std::vector<QuatKey> rotations;
37+
std::vector<time_dquat> rotations;
3838

3939
/// field of view key frames
40-
std::vector<VectorKey> projections;
40+
std::vector<time_double> fieldOfViews;
41+
42+
/// near/far key frames
43+
std::vector<time_dvec2> nearFars;
4144

4245
void clear()
4346
{
4447
origins.clear();
4548
positions.clear();
4649
rotations.clear();
47-
projections.clear();
50+
fieldOfViews.clear();
51+
nearFars.clear();
52+
}
53+
54+
void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation, double fov, const dvec2& nearFar)
55+
{
56+
origins.push_back(VectorKey{time, origin});
57+
positions.push_back(VectorKey{time, position});
58+
rotations.push_back(QuatKey{time, rotation});
59+
fieldOfViews.push_back(time_double{time, fov});
60+
nearFars.push_back(time_dvec2{time, nearFar});
4861
}
4962

50-
void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation, const dvec3& projection)
63+
void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation, double fov)
5164
{
5265
origins.push_back(VectorKey{time, origin});
5366
positions.push_back(VectorKey{time, position});
5467
rotations.push_back(QuatKey{time, rotation});
55-
projections.push_back(VectorKey{time, projection});
68+
fieldOfViews.push_back(time_double{time, fov});
69+
}
70+
71+
void add(double time, const dvec3& position, const dquat& rotation, double fov, const dvec2& nearFar)
72+
{
73+
positions.push_back(VectorKey{time, position});
74+
rotations.push_back(QuatKey{time, rotation});
75+
fieldOfViews.push_back(time_double{time, fov});
76+
nearFars.push_back(time_dvec2{time, nearFar});
5677
}
5778

58-
void add(double time, const dvec3& position, const dquat& rotation, const dvec3& projection)
79+
void add(double time, const dvec3& position, const dquat& rotation, double fov)
5980
{
6081
positions.push_back(VectorKey{time, position});
6182
rotations.push_back(QuatKey{time, rotation});
62-
projections.push_back(VectorKey{time, projection});
83+
fieldOfViews.push_back(time_double{time, fov});
6384
}
6485

6586
void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation)
@@ -94,7 +115,8 @@ namespace vsg
94115
dvec3 origin;
95116
dvec3 position;
96117
dquat rotation;
97-
dvec3 projection;
118+
double fieldOfView;
119+
dvec2 nearFar;
98120

99121
void update(double time) override;
100122
double maxTime() const override;

include/vsg/animation/time_value.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,49 @@ namespace vsg
3535
using time_dvec4 = time_value<dvec4>;
3636
using time_dquat = time_value<dquat>;
3737

38+
template<typename T, typename V>
39+
bool sample(double time, const T& values, V& value)
40+
{
41+
if (values.size() == 0) return false;
42+
43+
if (values.size() == 1)
44+
{
45+
value = values.front().value;
46+
return true;
47+
}
48+
49+
auto pos_itr = values.begin();
50+
if (time <= pos_itr->time)
51+
{
52+
value = pos_itr->value;
53+
return true;
54+
}
55+
else
56+
{
57+
using value_type = typename T::value_type;
58+
pos_itr = std::lower_bound(values.begin(), values.end(), time, [](const value_type& elem, double t) -> bool { return elem.time < t; });
59+
60+
if (pos_itr == values.begin())
61+
{
62+
value = values.front().value;
63+
return true;
64+
}
65+
66+
if (pos_itr == values.end())
67+
{
68+
value = values.back().value;
69+
return true;
70+
}
71+
72+
auto before_pos_itr = pos_itr - 1;
73+
double delta_time = (pos_itr->time - before_pos_itr->time);
74+
double r = delta_time != 0.0 ? (time - before_pos_itr->time) / delta_time : 0.5;
75+
76+
value = mix(before_pos_itr->value, pos_itr->value, r);
77+
78+
return true;
79+
}
80+
}
81+
82+
3883
} // namespace vsg

src/vsg/animation/CameraSampler.cpp

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ void CameraKeyframes::read(Input& input)
5757
}
5858

5959
// read scale key frames
60-
uint32_t num_projections = input.readValue<uint32_t>("projections");
61-
projections.resize(num_projections);
62-
for (auto& scale : projections)
60+
uint32_t num_fieldOfViews = input.readValue<uint32_t>("fieldOfViews");
61+
fieldOfViews.resize(num_fieldOfViews);
62+
for (auto& scale : fieldOfViews)
6363
{
6464
input.matchPropertyName("projection");
6565
input.read(1, &scale.time);
@@ -94,8 +94,8 @@ void CameraKeyframes::write(Output& output) const
9494
}
9595

9696
// write scale key frames
97-
output.writeValue<uint32_t>("projections", projections.size());
98-
for (const auto& scale : projections)
97+
output.writeValue<uint32_t>("fieldOfViews", fieldOfViews.size());
98+
for (const auto& scale : fieldOfViews)
9999
{
100100
output.writePropertyName("projection");
101101
output.write(1, &scale.time);
@@ -104,50 +104,6 @@ void CameraKeyframes::write(Output& output) const
104104
}
105105
}
106106

107-
template<typename T, typename V>
108-
bool sample(double time, const T& values, V& value)
109-
{
110-
if (values.size() == 0) return false;
111-
112-
if (values.size() == 1)
113-
{
114-
value = values.front().value;
115-
return true;
116-
}
117-
118-
auto pos_itr = values.begin();
119-
if (time <= pos_itr->time)
120-
{
121-
value = pos_itr->value;
122-
return true;
123-
}
124-
else
125-
{
126-
using value_type = typename T::value_type;
127-
pos_itr = std::lower_bound(values.begin(), values.end(), time, [](const value_type& elem, double t) -> bool { return elem.time < t; });
128-
129-
if (pos_itr == values.begin())
130-
{
131-
value = values.front().value;
132-
return true;
133-
}
134-
135-
if (pos_itr == values.end())
136-
{
137-
value = values.back().value;
138-
return true;
139-
}
140-
141-
auto before_pos_itr = pos_itr - 1;
142-
double delta_time = (pos_itr->time - before_pos_itr->time);
143-
double r = delta_time != 0.0 ? (time - before_pos_itr->time) / delta_time : 0.5;
144-
145-
value = mix(before_pos_itr->value, pos_itr->value, r);
146-
147-
return true;
148-
}
149-
}
150-
151107
////////////////////////////////////////////////////////////////////////////////////////////////////
152108
//
153109
// CameraSampler
@@ -156,7 +112,8 @@ CameraSampler::CameraSampler() :
156112
origin(0.0, 0.0, 0.0),
157113
position(0.0, 0.0, 0.0),
158114
rotation(),
159-
projection(60.0, 0.1, 1000.0)
115+
fieldOfView(60.0),
116+
nearFar(1.0, 1e10)
160117
{
161118
}
162119

@@ -166,7 +123,8 @@ CameraSampler::CameraSampler(const CameraSampler& rhs, const CopyOp& copyop) :
166123
object(copyop(rhs.object)),
167124
position(rhs.position),
168125
rotation(rhs.rotation),
169-
projection(rhs.projection)
126+
fieldOfView(rhs.fieldOfView),
127+
nearFar(rhs.nearFar)
170128
{
171129
}
172130

@@ -187,7 +145,8 @@ void CameraSampler::update(double time)
187145
sample(time, keyframes->origins, origin);
188146
sample(time, keyframes->positions, position);
189147
sample(time, keyframes->rotations, rotation);
190-
sample(time, keyframes->projections, projection);
148+
sample(time, keyframes->fieldOfViews, fieldOfView);
149+
sample(time, keyframes->nearFars, nearFar);
191150
}
192151

193152
if (object) object->accept(*this);
@@ -201,7 +160,8 @@ double CameraSampler::maxTime() const
201160
if (!keyframes->origins.empty()) maxTime = std::max(maxTime, keyframes->origins.back().time);
202161
if (!keyframes->positions.empty()) maxTime = std::max(maxTime, keyframes->positions.back().time);
203162
if (!keyframes->rotations.empty()) maxTime = std::max(maxTime, keyframes->rotations.back().time);
204-
if (!keyframes->projections.empty()) maxTime = std::max(maxTime, keyframes->projections.back().time);
163+
if (!keyframes->fieldOfViews.empty()) maxTime = std::max(maxTime, keyframes->fieldOfViews.back().time);
164+
if (!keyframes->nearFars.empty()) maxTime = std::max(maxTime, keyframes->nearFars.back().time);
205165
}
206166

207167
return maxTime;
@@ -221,7 +181,6 @@ void CameraSampler::apply(LookAt& lookAt)
221181
{
222182
if (keyframes)
223183
{
224-
vsg::info("CameraSampler::apply(LookAt&)");
225184
if (!keyframes->origins.empty()) lookAt.origin = origin;
226185
if (!keyframes->positions.empty() || !keyframes->rotations.empty())
227186
{
@@ -234,7 +193,6 @@ void CameraSampler::apply(LookDirection& lookDirection)
234193
{
235194
if (keyframes)
236195
{
237-
vsg::info("CameraSampler::apply(LookDirection&)");
238196
if (!keyframes->origins.empty()) lookDirection.origin = origin;
239197
if (!keyframes->positions.empty()) lookDirection.position = position;
240198
if (!keyframes->rotations.empty()) lookDirection.rotation = rotation;
@@ -243,12 +201,14 @@ void CameraSampler::apply(LookDirection& lookDirection)
243201

244202
void CameraSampler::apply(Perspective& perspective)
245203
{
246-
if (keyframes && !keyframes->projections.empty())
204+
if (keyframes && !keyframes->fieldOfViews.empty())
205+
{
206+
perspective.fieldOfViewY = fieldOfView;
207+
}
208+
if (keyframes && !keyframes->nearFars.empty())
247209
{
248-
vsg::info("CameraSampler::apply(Perspective&)");
249-
perspective.fieldOfViewY = projection.x;
250-
perspective.nearDistance = projection.y;
251-
perspective.farDistance = projection.z;
210+
perspective.nearDistance = nearFar[0];
211+
perspective.farDistance = nearFar[1];
252212
}
253213
}
254214

src/vsg/animation/TransformSampler.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -104,50 +104,6 @@ void TransformKeyframes::write(Output& output) const
104104
}
105105
}
106106

107-
template<typename T, typename V>
108-
bool sample(double time, const T& values, V& value)
109-
{
110-
if (values.size() == 0) return false;
111-
112-
if (values.size() == 1)
113-
{
114-
value = values.front().value;
115-
return true;
116-
}
117-
118-
auto pos_itr = values.begin();
119-
if (time <= pos_itr->time)
120-
{
121-
value = pos_itr->value;
122-
return true;
123-
}
124-
else
125-
{
126-
using value_type = typename T::value_type;
127-
pos_itr = std::lower_bound(values.begin(), values.end(), time, [](const value_type& elem, double t) -> bool { return elem.time < t; });
128-
129-
if (pos_itr == values.begin())
130-
{
131-
value = values.front().value;
132-
return true;
133-
}
134-
135-
if (pos_itr == values.end())
136-
{
137-
value = values.back().value;
138-
return true;
139-
}
140-
141-
auto before_pos_itr = pos_itr - 1;
142-
double delta_time = (pos_itr->time - before_pos_itr->time);
143-
double r = delta_time != 0.0 ? (time - before_pos_itr->time) / delta_time : 0.5;
144-
145-
value = mix(before_pos_itr->value, pos_itr->value, r);
146-
147-
return true;
148-
}
149-
}
150-
151107
////////////////////////////////////////////////////////////////////////////////////////////////////
152108
//
153109
// TransformSampler

0 commit comments

Comments
 (0)