File: CinemaPath.cpp

package info (click to toggle)
0ad 0.0.21-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 54,068 kB
  • sloc: cpp: 230,527; ansic: 23,115; python: 13,559; perl: 2,499; sh: 948; xml: 776; makefile: 696; java: 533; ruby: 229; erlang: 53; sql: 21
file content (372 lines) | stat: -rw-r--r-- 9,121 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Copyright (C) 2016 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "CinemaPath.h"

#include <sstream>
#include <string>

#include "Camera.h"
#include "CinemaManager.h"
#include "GameView.h"
#include "gui/CGUI.h"
#include "gui/GUIManager.h"
#include "gui/IGUIObject.h"
#include "lib/ogl.h"
#include "maths/MathUtil.h"
#include "maths/Quaternion.h"
#include "maths/Vector3D.h"
#include "maths/Vector4D.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/Game.h"
#include "renderer/Renderer.h"


CCinemaPath::CCinemaPath(const CCinemaData& data, const TNSpline& spline, const TNSpline& targetSpline)
	: CCinemaData(data), TNSpline(spline), m_TargetSpline(targetSpline), m_TimeElapsed(0.f)
{
	m_TimeElapsed = 0;

	// Calculate curves by nodes
	BuildSpline();
	m_TargetSpline.BuildSpline();

	if (m_Orientation == L"target")
	{
		m_LookAtTarget = true;
		ENSURE(!m_TargetSpline.GetAllNodes().empty());
	}

	// Set distortion mode and style
	if (data.m_Mode == L"ease_in")
		DistModePtr = &CCinemaPath::EaseIn;
	else if (data.m_Mode == L"ease_out")
		DistModePtr = &CCinemaPath::EaseOut;
	else if (data.m_Mode == L"ease_inout")
		DistModePtr = &CCinemaPath::EaseInOut;
	else if (data.m_Mode == L"ease_outin")
		DistModePtr = &CCinemaPath::EaseOutIn;
	else
	{
		LOGWARNING("Cinematic mode not found for '%s'", data.m_Mode.ToUTF8().c_str());
		DistModePtr = &CCinemaPath::EaseInOut;
	}

	if (data.m_Style == L"default")
		DistStylePtr = &CCinemaPath::EaseDefault;
	else if (data.m_Style == L"growth")
		DistStylePtr = &CCinemaPath::EaseGrowth;
	else if (data.m_Style == L"expo")
		DistStylePtr = &CCinemaPath::EaseExpo;
	else if (data.m_Style == L"circle")
		DistStylePtr = &CCinemaPath::EaseCircle;
	else if (data.m_Style == L"sine")
		DistStylePtr = &CCinemaPath::EaseSine;
	else
	{
		LOGWARNING("Cinematic style not found for '%s'", data.m_Style.ToUTF8().c_str());
		DistStylePtr = &CCinemaPath::EaseDefault;
	}
}

void CCinemaPath::Draw() const
{
	DrawSpline(*this, CVector4D(0.2f, 0.2f, 1.f, 0.5f), 100, true);
	DrawNodes(*this, CVector4D(0.5f, 1.0f, 0.f, 0.5f));

	if (!m_LookAtTarget)
		return;

	DrawSpline(m_TargetSpline, CVector4D(1.0f, 0.2f, 0.2f, 0.5f), 100, true);
	DrawNodes(m_TargetSpline, CVector4D(1.0f, 0.5f, 0.f, 0.5f));
}

void CCinemaPath::DrawSpline(const RNSpline& spline, const CVector4D& RGBA, int smoothness, bool lines) const
{
	if (spline.NodeCount < 2 || DistModePtr == NULL)
		return;
	if (spline.NodeCount == 2 && lines)
		smoothness = 2;

	float start = spline.MaxDistance.ToFloat() / smoothness;
	float time = 0;

#if CONFIG2_GLES
	#warning TODO: do something about CCinemaPath on GLES
#else

	glColor4f(RGBA.X, RGBA.Y, RGBA.Z, RGBA.W);
	if (lines)
	{
		glLineWidth(1.8f);
		glEnable(GL_LINE_SMOOTH);
		glBegin(GL_LINE_STRIP);

		for (int i = 0; i <= smoothness; ++i)
		{
			// Find distorted time
			time = start*i / spline.MaxDistance.ToFloat();
			CVector3D tmp = spline.GetPosition(time);
			glVertex3f(tmp.X, tmp.Y, tmp.Z);
		}
		glEnd();
		glDisable(GL_LINE_SMOOTH);
		glLineWidth(1.0f);
	}
	else
	{
		smoothness /= 2;
		start = spline.MaxDistance.ToFloat() / smoothness;
		glEnable(GL_POINT_SMOOTH);
		glPointSize(3.0f);
		glBegin(GL_POINTS);

		for (int i = 0; i <= smoothness; ++i)
		{
			// Find distorted time
			time = (this->*DistModePtr)(start*i / spline.MaxDistance.ToFloat());
			CVector3D tmp = spline.GetPosition(time);
			glVertex3f(tmp.X, tmp.Y, tmp.Z);
		}
		glColor3f(1.0f, 1.0f, 0.0f); // yellow

		for (size_t i = 0; i < spline.GetAllNodes().size(); ++i)
			glVertex3f(
				spline.GetAllNodes()[i].Position.X.ToFloat(),
				spline.GetAllNodes()[i].Position.Y.ToFloat(),
				spline.GetAllNodes()[i].Position.Z.ToFloat()
			);

		glEnd();
		glPointSize(1.0f);
		glDisable(GL_POINT_SMOOTH);
	}

#endif
}

void CCinemaPath::DrawNodes(const RNSpline& spline, const CVector4D& RGBA) const
{
#if CONFIG2_GLES
	#warning TODO : do something about CCinemaPath on GLES
#else
	glEnable(GL_POINT_SMOOTH);
	glPointSize(5.0f);

	glColor4f(RGBA.X, RGBA.Y, RGBA.Z, RGBA.W);
	glBegin(GL_POINTS);
	const std::vector<SplineData>& nodes = spline.GetAllNodes();
	for (size_t i = 0; i < nodes.size(); ++i)
		glVertex3f(nodes[i].Position.X.ToFloat(), nodes[i].Position.Y.ToFloat(), nodes[i].Position.Z.ToFloat());
	glEnd();

	glPointSize(1.0f);
	glDisable(GL_POINT_SMOOTH);
#endif
}

CVector3D CCinemaPath::GetNodePosition(const int index) const
{
	return Node[index].Position;
}

fixed CCinemaPath::GetNodeDuration(const int index) const
{
	return Node[index].Distance;
}

fixed CCinemaPath::GetDuration() const
{
	return MaxDistance;
}

float CCinemaPath::GetNodeFraction() const
{
	return (m_TimeElapsed - m_PreviousNodeTime) / Node[m_CurrentNode].Distance.ToFloat();
}

float CCinemaPath::GetElapsedTime() const
{
	return m_TimeElapsed;
}

CStrW CCinemaPath::GetName() const
{
	return m_Name;
}

void CCinemaPath::SetTimescale(fixed scale)
{
	m_Timescale = scale;
}

void CCinemaPath::MoveToPointAt(float t, float nodet, const CVector3D& startRotation)
{
	CCamera *camera = g_Game->GetView()->GetCamera();
	t = (this->*DistModePtr)(t);

	CVector3D pos = GetPosition(t);

	if (m_LookAtTarget)
	{
		if (m_TimeElapsed <= m_TargetSpline.MaxDistance.ToFloat())
			camera->LookAt(pos, m_TargetSpline.GetPosition(m_TimeElapsed / m_TargetSpline.MaxDistance.ToFloat()), CVector3D(0, 1, 0));
		else
			camera->LookAt(pos, m_TargetSpline.GetAllNodes().back().Position, CVector3D(0, 1, 0));
	}
	else
	{
		CVector3D nodeRotation = Node[m_CurrentNode + 1].Rotation;
		CQuaternion start, end;
		start.FromEulerAngles(DEGTORAD(startRotation.X), DEGTORAD(startRotation.Y), DEGTORAD(startRotation.Z));
		end.FromEulerAngles(DEGTORAD(nodeRotation.X), DEGTORAD(nodeRotation.Y), DEGTORAD(nodeRotation.Z));
		start.Slerp(start, end, nodet);

		camera->m_Orientation.SetIdentity();
		camera->m_Orientation.Rotate(start);
		camera->m_Orientation.Translate(pos);
	}
	camera->UpdateFrustum();
}

// Distortion mode functions
float CCinemaPath::EaseIn(float t) const
{
	return (this->*DistStylePtr)(t);
}

float CCinemaPath::EaseOut(float t) const
{
	return 1.0f - EaseIn(1.0f-t);
}

float CCinemaPath::EaseInOut(float t) const
{
	if (t < m_Switch)
		return EaseIn(1.0f/m_Switch * t) * m_Switch;
	return EaseOut(1.0f/m_Switch * (t-m_Switch)) * m_Switch + m_Switch;
}

float CCinemaPath::EaseOutIn(float t) const
{
	if (t < m_Switch)
		return EaseOut(1.0f/m_Switch * t) * m_Switch;
	return EaseIn(1.0f/m_Switch * (t-m_Switch)) * m_Switch + m_Switch;
}

// Distortion style functions
float CCinemaPath::EaseDefault(float t) const
{
	return t;
}

float CCinemaPath::EaseGrowth(float t) const
{
	return pow(t, m_Growth);
}

float CCinemaPath::EaseExpo(float t) const
{
	if (t == 0)
		return t;
	return powf(m_Growth, 10*(t-1.0f));
}

float CCinemaPath::EaseCircle(float t) const
{
	t = -(sqrt(1.0f - t*t) - 1.0f);
	if (m_GrowthCount > 1.0f)
	{
		--m_GrowthCount;
		return (this->*DistStylePtr)(t);
	}
	return t;
}

float CCinemaPath::EaseSine(float t) const
{
	t = 1.0f - cos(t * (float)M_PI/2);
	if (m_GrowthCount > 1.0f)
	{
		--m_GrowthCount;
		return (this->*DistStylePtr)(t);
	}
	return t;
}

const CCinemaData* CCinemaPath::GetData() const
{
	return CCinemaData::GetData();
}

bool CCinemaPath::Validate()
{
	if (m_TimeElapsed > GetDuration().ToFloat() || m_TimeElapsed < 0.0f)
		return false;

	// Find current node and past "node time"
	float previousTime = 0.0f, cumulation = 0.0f;

	// Ignore the last node, since it is a blank (node time values are shifted down one from interface)
	for (size_t i = 0; i < Node.size() - 1; ++i)
	{
		cumulation += Node[i].Distance.ToFloat();
		if (m_TimeElapsed <= cumulation)
		{
			m_PreviousNodeTime = previousTime;
			m_PreviousRotation = Node[i].Rotation;
			m_CurrentNode = i; // We're moving toward this next node, so use its rotation
			return true;
		}
		previousTime += Node[i].Distance.ToFloat();
	}
	debug_warn("validation of cinema path is wrong\n");
	return false;
}

bool CCinemaPath::Play(const float deltaRealTime)
{
	m_TimeElapsed += m_Timescale.ToFloat() * deltaRealTime;
	if (!Validate())
		return false;

	MoveToPointAt(m_TimeElapsed / GetDuration().ToFloat(), GetNodeFraction(), m_PreviousRotation);
	return true;
}

bool CCinemaPath::Empty() const
{
	return Node.empty();
}

void CCinemaPath::Reset()
{
	m_TimeElapsed = 0.0f;
}

fixed CCinemaPath::GetTimescale() const
{
	return m_Timescale;
}

const TNSpline& CCinemaPath::GetTargetSpline() const
{
	return m_TargetSpline;
}