Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "CinemaPath.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "lib/debug.h"
24 : #include "maths/MathUtil.h"
25 : #include "maths/Matrix3D.h"
26 : #include "maths/Quaternion.h"
27 : #include "maths/Vector3D.h"
28 : #include "ps/CLogger.h"
29 : #include "ps/CStr.h"
30 :
31 : #include <cmath>
32 : #include <numbers>
33 : #include <string>
34 : #include <vector>
35 :
36 7 : CCinemaPath::CCinemaPath(const CCinemaData& data, const TNSpline& spline, const TNSpline& targetSpline)
37 7 : : CCinemaData(data), TNSpline(spline), m_TargetSpline(targetSpline), m_TimeElapsed(0.f)
38 : {
39 : // Calculate curves by nodes
40 7 : BuildSpline();
41 7 : m_TargetSpline.BuildSpline();
42 :
43 7 : if (m_Orientation == L"target")
44 : {
45 7 : m_LookAtTarget = true;
46 7 : ENSURE(!m_TargetSpline.GetAllNodes().empty());
47 : }
48 :
49 : // Set distortion mode and style
50 7 : if (data.m_Mode == L"ease_in")
51 0 : DistModePtr = &CCinemaPath::EaseIn;
52 7 : else if (data.m_Mode == L"ease_out")
53 0 : DistModePtr = &CCinemaPath::EaseOut;
54 7 : else if (data.m_Mode == L"ease_inout")
55 7 : DistModePtr = &CCinemaPath::EaseInOut;
56 0 : else if (data.m_Mode == L"ease_outin")
57 0 : DistModePtr = &CCinemaPath::EaseOutIn;
58 : else
59 : {
60 0 : LOGWARNING("Cinematic mode not found for '{}'", data.m_Mode.ToUTF8());
61 0 : DistModePtr = &CCinemaPath::EaseInOut;
62 : }
63 :
64 7 : if (data.m_Style == L"default")
65 7 : DistStylePtr = &CCinemaPath::EaseDefault;
66 0 : else if (data.m_Style == L"growth")
67 0 : DistStylePtr = &CCinemaPath::EaseGrowth;
68 0 : else if (data.m_Style == L"expo")
69 0 : DistStylePtr = &CCinemaPath::EaseExpo;
70 0 : else if (data.m_Style == L"circle")
71 0 : DistStylePtr = &CCinemaPath::EaseCircle;
72 0 : else if (data.m_Style == L"sine")
73 0 : DistStylePtr = &CCinemaPath::EaseSine;
74 : else
75 : {
76 0 : LOGWARNING("Cinematic style not found for '{}'", data.m_Style.ToUTF8());
77 0 : DistStylePtr = &CCinemaPath::EaseDefault;
78 : }
79 :
80 7 : if (m_Timescale <= fixed::FromInt(0))
81 : {
82 0 : LOGWARNING("Invalid cinema path timescale specified: {:f}. It must be greater than 0.",
83 : m_Timescale.ToFloat());
84 0 : m_Timescale = fixed::FromInt(1);
85 : }
86 7 : }
87 :
88 0 : CVector3D CCinemaPath::GetNodePosition(const int index) const
89 : {
90 0 : return m_Nodes[index].Position;
91 : }
92 :
93 0 : fixed CCinemaPath::GetNodeDuration(const int index) const
94 : {
95 0 : return m_Nodes[index].Distance.Multiply(m_Timescale);
96 : }
97 :
98 233 : fixed CCinemaPath::GetDuration() const
99 : {
100 0 : return GetMaxDistance().Multiply(m_Timescale);
101 : }
102 :
103 0 : float CCinemaPath::GetNodeFraction() const
104 : {
105 0 : return (m_TimeElapsed - m_PreviousNodeTime) / GetNodeDuration(m_CurrentNode).ToFloat();
106 : }
107 :
108 0 : float CCinemaPath::GetElapsedTime() const
109 : {
110 0 : return m_TimeElapsed;
111 : }
112 :
113 33 : const CStrW& CCinemaPath::GetName() const
114 : {
115 33 : return m_Name;
116 : }
117 :
118 0 : void CCinemaPath::SetTimescale(fixed scale)
119 : {
120 0 : if (scale <= fixed::FromInt(0))
121 : {
122 0 : LOGWARNING("Invalid cinema path timescale specified: {:f}. It must be greater than 0.",
123 : scale.ToFloat());
124 : }
125 : else
126 0 : m_Timescale = scale;
127 0 : }
128 :
129 0 : void CCinemaPath::MoveToPointAt(float t, float nodet, const CVector3D& startRotation, CCamera& camera) const
130 : {
131 0 : t = (this->*DistModePtr)(t);
132 :
133 0 : CVector3D pos = GetPosition(t);
134 :
135 0 : if (m_LookAtTarget)
136 : {
137 0 : float targetDuration = m_TargetSpline.GetMaxDistance().Multiply(m_Timescale).ToFloat();
138 0 : if (m_TimeElapsed <= targetDuration)
139 0 : camera.LookAt(pos, m_TargetSpline.GetPosition(m_TimeElapsed / targetDuration), CVector3D(0, 1, 0));
140 : else
141 0 : camera.LookAt(pos, m_TargetSpline.GetAllNodes().back().Position, CVector3D(0, 1, 0));
142 : }
143 : else
144 : {
145 0 : CVector3D nodeRotation = m_Nodes[m_CurrentNode + 1].Rotation;
146 0 : CQuaternion start, end;
147 0 : start.FromEulerAngles(DEGTORAD(startRotation.X), DEGTORAD(startRotation.Y), DEGTORAD(startRotation.Z));
148 0 : end.FromEulerAngles(DEGTORAD(nodeRotation.X), DEGTORAD(nodeRotation.Y), DEGTORAD(nodeRotation.Z));
149 0 : start.Slerp(start, end, nodet);
150 :
151 0 : camera.m_Orientation.SetIdentity();
152 0 : camera.m_Orientation.Rotate(start);
153 0 : camera.m_Orientation.Translate(pos);
154 : }
155 0 : camera.UpdateFrustum();
156 0 : }
157 :
158 : // Distortion mode functions
159 0 : float CCinemaPath::EaseIn(float t) const
160 : {
161 0 : return (this->*DistStylePtr)(t);
162 : }
163 :
164 0 : float CCinemaPath::EaseOut(float t) const
165 : {
166 0 : return 1.0f - EaseIn(1.0f-t);
167 : }
168 :
169 0 : float CCinemaPath::EaseInOut(float t) const
170 : {
171 0 : if (t < m_Switch)
172 0 : return EaseIn(1.0f/m_Switch * t) * m_Switch;
173 0 : return EaseOut(1.0f/m_Switch * (t-m_Switch)) * m_Switch + m_Switch;
174 : }
175 :
176 0 : float CCinemaPath::EaseOutIn(float t) const
177 : {
178 0 : if (t < m_Switch)
179 0 : return EaseOut(1.0f/m_Switch * t) * m_Switch;
180 0 : return EaseIn(1.0f/m_Switch * (t-m_Switch)) * m_Switch + m_Switch;
181 : }
182 :
183 : // Distortion style functions
184 0 : float CCinemaPath::EaseDefault(float t) const
185 : {
186 0 : return t;
187 : }
188 :
189 0 : float CCinemaPath::EaseGrowth(float t) const
190 : {
191 0 : return pow(t, m_Growth);
192 : }
193 :
194 0 : float CCinemaPath::EaseExpo(float t) const
195 : {
196 0 : if (t == 0)
197 : return t;
198 0 : return powf(m_Growth, 10*(t-1.0f));
199 : }
200 :
201 0 : float CCinemaPath::EaseCircle(float t) const
202 : {
203 0 : t = -(sqrt(1.0f - t*t) - 1.0f);
204 0 : if (m_GrowthCount > 1.0f)
205 : {
206 0 : --m_GrowthCount;
207 0 : return (this->*DistStylePtr)(t);
208 : }
209 : return t;
210 : }
211 :
212 0 : float CCinemaPath::EaseSine(float t) const
213 : {
214 0 : t = 1.0f - cos(t * std::numbers::pi_v<float> / 2.f);
215 0 : if (m_GrowthCount > 1.0f)
216 : {
217 0 : --m_GrowthCount;
218 0 : return (this->*DistStylePtr)(t);
219 : }
220 : return t;
221 : }
222 :
223 18 : const CCinemaData* CCinemaPath::GetData() const
224 : {
225 18 : return CCinemaData::GetData();
226 : }
227 :
228 0 : bool CCinemaPath::Validate()
229 : {
230 0 : if (m_TimeElapsed > GetDuration().ToFloat() || m_TimeElapsed < 0.0f)
231 : return false;
232 :
233 : // Find current node and past "node time"
234 : float previousTime = 0.0f, cumulation = 0.0f;
235 :
236 : // Ignore the last node, since it is a blank (node time values are shifted down one from interface)
237 0 : for (size_t i = 0; i < m_Nodes.size() - 1; ++i)
238 : {
239 0 : cumulation += GetNodeDuration(i).ToFloat();
240 0 : if (m_TimeElapsed <= cumulation)
241 : {
242 0 : m_PreviousNodeTime = previousTime;
243 0 : m_PreviousRotation = m_Nodes[i].Rotation;
244 0 : m_CurrentNode = i; // We're moving toward this next node, so use its rotation
245 0 : return true;
246 : }
247 0 : previousTime += GetNodeDuration(i).ToFloat();
248 : }
249 0 : debug_warn("validation of cinema path is wrong\n");
250 : return false;
251 : }
252 :
253 0 : bool CCinemaPath::Play(const float deltaRealTime, CCamera& camera)
254 : {
255 0 : m_TimeElapsed += deltaRealTime;
256 0 : if (!Validate())
257 : return false;
258 :
259 0 : MoveToPointAt(m_TimeElapsed / GetDuration().ToFloat(), GetNodeFraction(), m_PreviousRotation, camera);
260 : return true;
261 : }
262 :
263 0 : bool CCinemaPath::Empty() const
264 : {
265 0 : return m_Nodes.empty();
266 : }
267 :
268 5 : void CCinemaPath::Reset()
269 : {
270 5 : m_TimeElapsed = 0.0f;
271 5 : }
272 :
273 0 : fixed CCinemaPath::GetTimescale() const
274 : {
275 0 : return m_Timescale;
276 : }
277 :
278 18 : const TNSpline& CCinemaPath::GetTargetSpline() const
279 : {
280 18 : return m_TargetSpline;
281 : }
|