88
99static bool initialized = false ;
1010
11- SplineNode::SplineNode (std::string name) : Node(typeid (SplineNode)), m_name(name), m_animate(false ), m_speed(1 ), m_time(0 ), m_spline(nullptr ), m_quat_spline(nullptr )
11+ SplineNode::SplineNode (std::string name, bool looping ) : Node(typeid (SplineNode)), m_name(name), m_animate(false ), m_speed(1 ), m_time(0 ), m_spline(nullptr ), m_quat_spline(nullptr ), m_looping(looping )
1212{
1313 if (initialized) return ;
1414 initialized = true ;
@@ -110,6 +110,11 @@ SplineNode::SplineNode(std::string name) : Node(typeid(SplineNode)), m_name(name
110110 scene_graph->GetActiveCamera ()->SetRotation (cp.m_rotation );
111111 }
112112
113+ if (ImGui::Button (" Remove" ))
114+ {
115+ spline_node->m_control_points .erase (spline_node->m_control_points .begin () + i);
116+ }
117+
113118 ImGui::TreePop ();
114119 }
115120 }
@@ -130,36 +135,43 @@ void SplineNode::UpdateSplineNode(float delta, std::shared_ptr<wr::Node> node)
130135
131136 auto num_points = m_control_points.size ();
132137
133- m_time += delta * m_speed;
134- m_time = std::fmod (m_time, m_spline->getMaxT ());
135-
136- auto lerp_delta = m_time / m_spline->getMaxT ();
137- #ifdef LOOPING
138- auto segment_i = lerp_delta * ((float )num_points);
139- auto alpha = (lerp_delta * ((float )num_points)) - floor (segment_i);
140- #else
141- auto segment_i = lerp_delta * ((float )num_points - 1 .f );
142- auto alpha = (lerp_delta * ((float )num_points - 1 .f )) - floor (segment_i);
143- #endif
144-
145- float backback = std::fmod ((std::fmod (floor (segment_i), num_points) + num_points), num_points);
146- auto back_point = m_control_points[backback];
147- auto prev_point = m_control_points[floor (segment_i)];
148- auto next_point = m_control_points[fmod (ceil (segment_i), num_points)];
149- auto end_point = m_control_points[fmod (ceil (segment_i+1 ), num_points)];
150-
151- auto rot_a = DirectX::XMQuaternionRotationRollPitchYawFromVector (prev_point.m_rotation );
152- auto rot_b = DirectX::XMQuaternionRotationRollPitchYawFromVector (next_point.m_rotation );
153- auto interp = DirectX::XMQuaternionSlerp (rot_a, rot_b, alpha);
154-
155- auto new_pos = m_spline->getPosition (m_time);
156- auto new_rot = m_quat_spline->getPosition (m_time);
157-
158- LOG (" {} -- {}" , floor (segment_i), fmod (ceil (segment_i), num_points));
138+ auto impl = [&](auto spline, auto quat_spline)
139+ {
140+ m_time += delta * m_speed;
141+ m_time = std::fmod (m_time, spline->getMaxT ());
142+
143+ auto lerp_delta = m_time / spline->getMaxT ();
144+
145+ auto mod = m_looping ? 0 .f : 1 .f ;
146+ auto segment_i = lerp_delta * ((float )num_points - mod);
147+ auto alpha = (lerp_delta * ((float )num_points - mod)) - floor (segment_i);
148+
149+ float backback = std::fmod ((std::fmod (floor (segment_i), num_points) + num_points), num_points);
150+ auto back_point = m_control_points[backback];
151+ auto prev_point = m_control_points[floor (segment_i)];
152+ auto next_point = m_control_points[fmod (ceil (segment_i), num_points)];
153+ auto end_point = m_control_points[fmod (ceil (segment_i + 1 ), num_points)];
154+
155+ auto rot_a = DirectX::XMQuaternionRotationRollPitchYawFromVector (prev_point.m_rotation );
156+ auto rot_b = DirectX::XMQuaternionRotationRollPitchYawFromVector (next_point.m_rotation );
157+ auto interp = DirectX::XMQuaternionSlerp (rot_a, rot_b, alpha);
158+
159+ auto new_pos = spline->getPosition (m_time);
160+ auto new_rot = quat_spline->getPosition (m_time);
161+
162+ node->SetPosition ({ new_pos[0 ], new_pos[1 ], new_pos[2 ] });
163+ node->SetRotationQuat (interp);
164+ node->SetRotationQuat ({ new_rot[0 ], new_rot[1 ], new_rot[2 ], new_rot[3 ] });
165+ };
159166
160- node->SetPosition ({ new_pos[0 ], new_pos[1 ], new_pos[2 ] });
161- node->SetRotationQuat (interp);
162- node->SetRotationQuat ({ new_rot[0 ], new_rot[1 ], new_rot[2 ], new_rot[3 ] });
167+ if (m_looping)
168+ {
169+ impl (reinterpret_cast <LoopingNaturalSpline<Vector<3 >>*>(m_spline), reinterpret_cast <LoopingNaturalSpline<Vector<4 >>*>(m_quat_spline));
170+ }
171+ else
172+ {
173+ impl (reinterpret_cast <NaturalSpline<Vector<3 >>*>(m_spline), reinterpret_cast <NaturalSpline<Vector<4 >>*>(m_quat_spline));
174+ }
163175}
164176
165177void SplineNode::UpdateNaturalSpline ()
@@ -176,7 +188,7 @@ void SplineNode::UpdateNaturalSpline()
176188 m_quat_spline = nullptr ;
177189 }
178190
179- if (m_control_points.size () < 3 )
191+ if (m_control_points.size () < (m_looping ? 2 : 3 ) )
180192 {
181193 return ;
182194 }
@@ -189,11 +201,14 @@ void SplineNode::UpdateNaturalSpline()
189201 m_spline_positions.push_back (Vector<3 >({ cp.m_position .m128_f32 [0 ], cp.m_position .m128_f32 [1 ], cp.m_position .m128_f32 [2 ] }));
190202 }
191203
192- #ifdef LOOPING
193- m_spline = new LoopingNaturalSpline<Vector<3 >>(m_spline_positions);
194- #else
195- m_spline = new NaturalSpline<Vector<3 >>(m_spline_positions);
196- #endif
204+ if (m_looping)
205+ {
206+ m_spline = new LoopingNaturalSpline<Vector<3 >>(m_spline_positions);
207+ }
208+ else
209+ {
210+ m_spline = new NaturalSpline<Vector<3 >>(m_spline_positions);
211+ }
197212 }
198213
199214 {
@@ -204,11 +219,15 @@ void SplineNode::UpdateNaturalSpline()
204219 auto quat = DirectX::XMQuaternionRotationRollPitchYawFromVector (cp.m_rotation );
205220 m_spline_rotations.push_back (Vector<4 >({ quat.m128_f32 [0 ], quat.m128_f32 [1 ], quat.m128_f32 [2 ], quat.m128_f32 [3 ] }));
206221 }
207- #ifdef LOOPING
208- m_quat_spline = new LoopingNaturalSpline<Vector<4 >>(m_spline_positions);
209- #else
210- m_quat_spline = new NaturalSpline<Vector<4 >>(m_spline_rotations);
211- #endif
222+
223+ if (m_looping)
224+ {
225+ m_quat_spline = new LoopingNaturalSpline<Vector<4 >>(m_spline_rotations);
226+ }
227+ else
228+ {
229+ m_quat_spline = new NaturalSpline<Vector<4 >>(m_spline_rotations);
230+ }
212231 }
213232}
214233
0 commit comments