1 | /* Copyright (C) 2021 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 <sstream>
|
---|
21 | #include <string>
|
---|
22 |
|
---|
23 | #include "graphics/CinemaManager.h"
|
---|
24 |
|
---|
25 | #include "graphics/Camera.h"
|
---|
26 | #include "graphics/GameView.h"
|
---|
27 | #include "lib/ogl.h"
|
---|
28 | #include "maths/MathUtil.h"
|
---|
29 | #include "maths/Quaternion.h"
|
---|
30 | #include "maths/Vector3D.h"
|
---|
31 | #include "maths/Vector4D.h"
|
---|
32 | #include "ps/CLogger.h"
|
---|
33 | #include "ps/ConfigDB.h"
|
---|
34 | #include "ps/CStr.h"
|
---|
35 | #include "ps/Game.h"
|
---|
36 | #include "ps/GameSetup/Config.h"
|
---|
37 | #include "ps/Hotkey.h"
|
---|
38 | #include "ps/World.h"
|
---|
39 | #include "simulation2/components/ICmpCinemaManager.h"
|
---|
40 | #include "simulation2/components/ICmpOverlayRenderer.h"
|
---|
41 | #include "simulation2/components/ICmpRangeManager.h"
|
---|
42 | #include "simulation2/components/ICmpSelectable.h"
|
---|
43 | #include "simulation2/components/ICmpTerritoryManager.h"
|
---|
44 | #include "simulation2/MessageTypes.h"
|
---|
45 | #include "simulation2/system/ComponentManager.h"
|
---|
46 | #include "simulation2/Simulation2.h"
|
---|
47 | #include "renderer/Renderer.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | CCinemaManager::CCinemaManager()
|
---|
51 | : m_DrawPaths(false)
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 | void CCinemaManager::Update(const float deltaRealTime) const
|
---|
56 | {
|
---|
57 | CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
|
---|
58 | if (!cmpCinemaManager)
|
---|
59 | return;
|
---|
60 |
|
---|
61 | if (IsPlaying())
|
---|
62 | cmpCinemaManager->PlayQueue(deltaRealTime, g_Game->GetView()->GetCamera());
|
---|
63 | }
|
---|
64 |
|
---|
65 | void CCinemaManager::Render() const
|
---|
66 | {
|
---|
67 | if (!IsEnabled() && m_DrawPaths)
|
---|
68 | DrawPaths();
|
---|
69 | }
|
---|
70 |
|
---|
71 | void CCinemaManager::DrawPaths() const
|
---|
72 | {
|
---|
73 | CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
|
---|
74 | if (!cmpCinemaManager)
|
---|
75 | return;
|
---|
76 |
|
---|
77 | for (const std::pair<const CStrW, CCinemaPath>& p : cmpCinemaManager->GetPaths())
|
---|
78 | {
|
---|
79 | DrawSpline(p.second, CColor(0.2f, 0.2f, 1.f, 0.9f), 128, true);
|
---|
80 | DrawNodes(p.second, CColor(0.1f, 1.f, 0.f, 1.f));
|
---|
81 |
|
---|
82 | if (p.second.GetTargetSpline().GetAllNodes().empty())
|
---|
83 | continue;
|
---|
84 |
|
---|
85 | DrawSpline(p.second.GetTargetSpline(), CColor(1.f, 0.3f, 0.4f, 0.9f), 128, true);
|
---|
86 | DrawNodes(p.second.GetTargetSpline(), CColor(1.f, 0.1f, 0.f, 1.f));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void CCinemaManager::DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness, bool lines) const
|
---|
91 | {
|
---|
92 | if (spline.GetAllNodes().size() < 2)
|
---|
93 | return;
|
---|
94 | if (spline.GetAllNodes().size() == 2 && lines)
|
---|
95 | smoothness = 2;
|
---|
96 |
|
---|
97 | float start = spline.MaxDistance.ToFloat() / smoothness;
|
---|
98 | float time = 0;
|
---|
99 |
|
---|
100 | #if CONFIG2_GLES
|
---|
101 | #warning TODO : implement CCinemaPath on GLES
|
---|
102 | #else
|
---|
103 |
|
---|
104 | glDisable(GL_DEPTH_TEST);
|
---|
105 | glEnable(GL_BLEND);
|
---|
106 | glColor4f(splineColor.r, splineColor.g, splineColor.b, splineColor.a);
|
---|
107 | if (lines)
|
---|
108 | {
|
---|
109 | glLineWidth(1.8f);
|
---|
110 | glEnable(GL_LINE_SMOOTH);
|
---|
111 | glBegin(GL_LINE_STRIP);
|
---|
112 | for (int i = 0; i <= smoothness; ++i)
|
---|
113 | {
|
---|
114 | time = start * i / spline.MaxDistance.ToFloat();
|
---|
115 | CVector3D tmp = spline.GetPosition(time);
|
---|
116 | glVertex3f(tmp.X, tmp.Y, tmp.Z);
|
---|
117 | }
|
---|
118 | glEnd();
|
---|
119 |
|
---|
120 | // Height indicator
|
---|
121 | if (g_Game && g_Game->GetWorld() && g_Game->GetWorld()->GetTerrain())
|
---|
122 | {
|
---|
123 | glLineWidth(1.1f);
|
---|
124 | glBegin(GL_LINES);
|
---|
125 | for (int i = 0; i <= smoothness; ++i)
|
---|
126 | {
|
---|
127 | time = start * i / spline.MaxDistance.ToFloat();
|
---|
128 | CVector3D tmp = spline.GetPosition(time);
|
---|
129 | float groundY = g_Game->GetWorld()->GetTerrain()->GetExactGroundLevel(tmp.X, tmp.Z);
|
---|
130 | glVertex3f(tmp.X, tmp.Y, tmp.Z);
|
---|
131 | glVertex3f(tmp.X, groundY, tmp.Z);
|
---|
132 | }
|
---|
133 | glEnd();
|
---|
134 | }
|
---|
135 |
|
---|
136 | glDisable(GL_LINE_SMOOTH);
|
---|
137 | glLineWidth(1.0f);
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | smoothness /= 2;
|
---|
142 | start = spline.MaxDistance.ToFloat() / smoothness;
|
---|
143 | glEnable(GL_POINT_SMOOTH);
|
---|
144 | glPointSize(3.0f);
|
---|
145 | glBegin(GL_POINTS);
|
---|
146 | for (int i = 0; i <= smoothness; ++i)
|
---|
147 | {
|
---|
148 | time = start * i / spline.MaxDistance.ToFloat();
|
---|
149 | CVector3D tmp = spline.GetPosition(time);
|
---|
150 | glVertex3f(tmp.X, tmp.Y, tmp.Z);
|
---|
151 | }
|
---|
152 | glEnd();
|
---|
153 | glPointSize(1.0f);
|
---|
154 | glDisable(GL_POINT_SMOOTH);
|
---|
155 | }
|
---|
156 | glDisable(GL_BLEND);
|
---|
157 | glEnable(GL_DEPTH_TEST);
|
---|
158 |
|
---|
159 | #endif
|
---|
160 | }
|
---|
161 |
|
---|
162 | void CCinemaManager::DrawNodes(const RNSpline& spline, const CColor& nodeColor) const
|
---|
163 | {
|
---|
164 | #if CONFIG2_GLES
|
---|
165 | #warning TODO : implement CCinemaPath on GLES
|
---|
166 | #else
|
---|
167 |
|
---|
168 | glDisable(GL_DEPTH_TEST);
|
---|
169 | glEnable(GL_POINT_SMOOTH);
|
---|
170 | glPointSize(7.0f);
|
---|
171 | glColor4f(nodeColor.r, nodeColor.g, nodeColor.b, nodeColor.a);
|
---|
172 | glBegin(GL_POINTS);
|
---|
173 |
|
---|
174 | for (const SplineData& node : spline.GetAllNodes())
|
---|
175 | glVertex3f(node.Position.X.ToFloat(), node.Position.Y.ToFloat(), node.Position.Z.ToFloat());
|
---|
176 |
|
---|
177 | glEnd();
|
---|
178 | glPointSize(1.0f);
|
---|
179 | glDisable(GL_POINT_SMOOTH);
|
---|
180 | glEnable(GL_DEPTH_TEST);
|
---|
181 | #endif
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool CCinemaManager::IsEnabled() const
|
---|
185 | {
|
---|
186 | CmpPtr<ICmpCinemaManager> cmpCinemaManager(g_Game->GetSimulation2()->GetSimContext().GetSystemEntity());
|
---|
187 | return cmpCinemaManager && cmpCinemaManager->IsEnabled();
|
---|
188 | }
|
---|
189 |
|
---|
190 | bool CCinemaManager::IsPlaying() const
|
---|
191 | {
|
---|
192 | return IsEnabled() && g_Game && !g_Game->m_Paused;
|
---|
193 | }
|
---|
194 |
|
---|
195 | bool CCinemaManager::GetPathsDrawing() const
|
---|
196 | {
|
---|
197 | return m_DrawPaths;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void CCinemaManager::SetPathsDrawing(const bool drawPath)
|
---|
201 | {
|
---|
202 | m_DrawPaths = drawPath;
|
---|
203 | }
|
---|