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 "MessageHandler.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/CinemaManager.h"
24 : #include "graphics/GameView.h"
25 : #include "graphics/Terrain.h"
26 : #include "lib/debug.h"
27 : #include "maths/MathUtil.h"
28 : #include "maths/Matrix3D.h"
29 : #include "maths/Quaternion.h"
30 : #include "maths/Vector3D.h"
31 : #include "ps/Game.h"
32 : #include "ps/World.h"
33 : #include "renderer/Renderer.h"
34 : #include "tools/atlas/GameInterface/GameLoop.h"
35 : #include "tools/atlas/GameInterface/Messages.h"
36 : #include "tools/atlas/GameInterface/Shareable.h"
37 : #include "tools/atlas/GameInterface/SharedTypes.h"
38 : #include "tools/atlas/GameInterface/View.h"
39 :
40 : namespace
41 : {
42 : bool g_BirdsEyeView{false};
43 : } // namespace
44 :
45 : namespace AtlasMessage {
46 :
47 0 : MESSAGEHANDLER(CameraReset)
48 : {
49 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
50 0 : return;
51 :
52 0 : CVector3D focus = g_Game->GetView()->GetCamera().GetFocus();
53 :
54 0 : CVector3D target;
55 0 : if (!g_Game->GetWorld()->GetTerrain().IsOnMap(focus.X, focus.Z))
56 : {
57 0 : target = CVector3D(
58 0 : g_Game->GetWorld()->GetTerrain().GetMaxX()/2.f,
59 : focus.Y,
60 0 : g_Game->GetWorld()->GetTerrain().GetMaxZ()/2.f);
61 : }
62 : else
63 : {
64 0 : target = focus;
65 : }
66 :
67 0 : g_Game->GetView()->ResetCameraTarget(target);
68 :
69 0 : g_BirdsEyeView = false;
70 : }
71 :
72 0 : MESSAGEHANDLER(ScrollConstant)
73 : {
74 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
75 0 : return;
76 :
77 0 : if (msg->dir < 0 || msg->dir > 5)
78 : {
79 0 : debug_warn(L"ScrollConstant: invalid direction");
80 : }
81 : else
82 : {
83 0 : g_AtlasGameLoop->input.scrollSpeed[msg->dir] = msg->speed;
84 : }
85 : }
86 :
87 : // TODO: change all these g_Game->...GetCamera() bits to use the current AtlasView's
88 : // camera instead.
89 :
90 0 : MESSAGEHANDLER(Scroll)
91 : {
92 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying()) // TODO: do this better (probably a separate AtlasView class for cinematics)
93 0 : return;
94 :
95 0 : static CVector3D targetPos;
96 0 : static float targetDistance = 0.f;
97 :
98 0 : CCamera camera{g_Game->GetView()->GetCamera()};
99 :
100 0 : static CVector3D lastCameraPos{camera.m_Orientation.GetTranslation()};
101 :
102 : // Ensure roughly correct motion when dragging is combined with other
103 : // movements.
104 0 : if (lastCameraPos != camera.m_Orientation.GetTranslation())
105 0 : targetPos += camera.m_Orientation.GetTranslation() - lastCameraPos;
106 :
107 : // General operation:
108 : //
109 : // When selecting a target point to drag, remember targetPos (a world-space
110 : // point on the terrain, underneath the mouse) and targetDistance (from the
111 : // camera to the target point).
112 : //
113 : // When dragging to a different position, the target point should remain
114 : // under the moved mouse; so calculate the ray through the camera and mouse,
115 : // multiply by targetDistance and add to targetPos, resulting in the required
116 : // camera position.
117 :
118 0 : if (msg->type == eScrollType::FROM)
119 : {
120 0 : targetPos = msg->pos->GetWorldSpace();
121 0 : targetDistance = (targetPos - camera.m_Orientation.GetTranslation()).Length();
122 : }
123 0 : else if (msg->type == eScrollType::TO)
124 : {
125 0 : float x, y;
126 0 : msg->pos->GetScreenSpace(x, y);
127 0 : auto [origin, dir] = g_Game->GetView()->GetCamera().BuildCameraRay((int)x, (int)y);
128 0 : dir *= targetDistance;
129 0 : camera.m_Orientation.Translate(targetPos - dir - origin);
130 0 : camera.UpdateFrustum();
131 0 : g_Game->GetView()->SetCamera(camera);
132 : }
133 : else
134 : {
135 0 : debug_warn(L"Scroll: Invalid type");
136 : }
137 0 : lastCameraPos = camera.m_Orientation.GetTranslation();
138 0 : }
139 :
140 0 : MESSAGEHANDLER(SmoothZoom)
141 : {
142 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
143 0 : return;
144 :
145 0 : g_AtlasGameLoop->input.zoomDelta += msg->amount;
146 : }
147 :
148 0 : MESSAGEHANDLER(RotateAround)
149 : {
150 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying() || g_BirdsEyeView)
151 0 : return;
152 :
153 0 : static CVector3D focusPos;
154 0 : static float lastX = 0.f, lastY = 0.f;
155 :
156 0 : CCamera camera{g_Game->GetView()->GetCamera()};
157 :
158 0 : if (msg->type == eRotateAroundType::FROM)
159 : {
160 0 : msg->pos->GetScreenSpace(lastX, lastY); // get mouse position
161 0 : focusPos = msg->pos->GetWorldSpace(); // get point on terrain under mouse
162 : }
163 0 : else if (msg->type == eRotateAroundType::TO)
164 : {
165 0 : float x, y;
166 0 : msg->pos->GetScreenSpace(x, y); // get mouse position
167 :
168 : // Rotate around X and Y axes by amounts depending on the mouse delta
169 0 : float rotX = 6.f * (y-lastY) / g_Renderer.GetHeight();
170 0 : float rotY = 6.f * (x-lastX) / g_Renderer.GetWidth();
171 :
172 0 : CQuaternion q0, q1;
173 0 : q0.FromAxisAngle(camera.m_Orientation.GetLeft(), -rotX);
174 0 : q1.FromAxisAngle(CVector3D(0.f, 1.f, 0.f), rotY);
175 0 : CQuaternion q = q0*q1;
176 :
177 0 : CVector3D origin = camera.m_Orientation.GetTranslation();
178 0 : CVector3D offset = q.Rotate(origin - focusPos);
179 :
180 0 : q *= camera.m_Orientation.GetRotation();
181 0 : q.Normalize(); // to avoid things blowing up when turning upside-down, for some reason I don't understand
182 0 : q.ToMatrix(camera.m_Orientation);
183 :
184 : // Make sure up is still pointing up, regardless of any rounding errors.
185 : // (Maybe this distorts the camera in other ways, but at least the errors
186 : // are far less noticeable to me.)
187 0 : camera.m_Orientation._21 = 0.f; // (_21 = Y component returned by GetLeft())
188 :
189 0 : camera.m_Orientation.Translate(focusPos + offset);
190 0 : camera.UpdateFrustum();
191 0 : g_Game->GetView()->SetCamera(camera);
192 :
193 0 : lastX = x;
194 0 : lastY = y;
195 : }
196 : else
197 : {
198 0 : debug_warn(L"RotateAround: Invalid type");
199 : }
200 0 : }
201 :
202 0 : MESSAGEHANDLER(RotateY)
203 : {
204 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
205 0 : return;
206 :
207 0 : g_AtlasGameLoop->input.rotateDelta = msg->angle;
208 : }
209 :
210 0 : MESSAGEHANDLER(LookAt)
211 : {
212 : // TODO: different camera depending on msg->view
213 0 : CCamera camera{AtlasView::GetView_Actor()->GetCamera()};
214 :
215 0 : CVector3D tgt = msg->target->GetWorldSpace();
216 0 : CVector3D eye = msg->pos->GetWorldSpace();
217 0 : tgt.Y = -tgt.Y; // ??? why is this needed?
218 0 : eye.Y = -eye.Y; // ???
219 :
220 : // Based on http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html
221 0 : CVector3D f = tgt - eye;
222 0 : f.Normalize();
223 0 : CVector3D s = f.Cross(CVector3D(0, 1, 0));
224 0 : CVector3D u = s.Cross(f);
225 0 : s.Normalize(); // (not in that man page, but necessary for correctness, and done by Mesa)
226 0 : u.Normalize();
227 0 : CMatrix3D M (
228 0 : s[0], s[1], s[2], 0,
229 0 : u[0], u[1], u[2], 0,
230 0 : -f[0], -f[1], -f[2], 0,
231 : 0, 0, 0, 1
232 0 : );
233 :
234 0 : camera.m_Orientation = M.GetTranspose();
235 0 : camera.m_Orientation.Translate(-eye);
236 :
237 0 : camera.UpdateFrustum();
238 :
239 0 : AtlasView::GetView_Actor()->SetCamera(camera);
240 0 : }
241 :
242 0 : QUERYHANDLER(GetView)
243 : {
244 0 : if (!g_Game)
245 0 : return;
246 :
247 0 : CVector3D focus = g_Game->GetView()->GetCamera().GetFocus();
248 0 : sCameraInfo info;
249 :
250 0 : info.pX = focus.X;
251 0 : info.pY = focus.Y;
252 0 : info.pZ = focus.Z;
253 :
254 0 : CQuaternion quatRot = g_Game->GetView()->GetCamera().GetOrientation().GetRotation();
255 0 : quatRot.Normalize();
256 0 : CVector3D rotation = quatRot.ToEulerAngles();
257 :
258 0 : info.rX = RADTODEG(rotation.X);
259 0 : info.rY = RADTODEG(rotation.Y);
260 0 : info.rZ = RADTODEG(rotation.Z);
261 :
262 0 : msg->info = info;
263 : }
264 :
265 0 : MESSAGEHANDLER(SetView)
266 : {
267 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
268 0 : return;
269 :
270 0 : CGameView* view = g_Game->GetView();
271 0 : view->ResetCameraTarget(view->GetCamera().GetFocus());
272 :
273 0 : sCameraInfo cam = msg->info;
274 :
275 0 : view->ResetCameraTarget(CVector3D(cam.pX, cam.pY, cam.pZ));
276 :
277 : // TODO: Rotation
278 : }
279 :
280 0 : MESSAGEHANDLER(SetBirdsEyeView)
281 : {
282 0 : if (g_BirdsEyeView == msg->enabled)
283 0 : return;
284 :
285 0 : if (!g_Game || g_Game->GetView()->GetCinema()->IsPlaying())
286 0 : return;
287 :
288 0 : static float declination{0.f};
289 :
290 0 : CCamera camera{AtlasView::GetView_Game()->GetCamera()};
291 0 : CMatrix3D& orientation{camera.GetOrientation()};
292 0 : const CVector3D focus{camera.GetFocus()};
293 :
294 0 : if (!g_BirdsEyeView)
295 : {
296 0 : CVector3D in = orientation.GetIn();
297 0 : declination = atan2(sqrt(in.X*in.X + in.Z*in.Z), in.Y) - std::numbers::pi_v<float> / 2.f;
298 : }
299 :
300 0 : CQuaternion q;
301 : // If really 90° then the camera movement bugs out as it has no clear
302 : // forward anymore, so stick with 89°.
303 0 : q.FromAxisAngle(orientation.GetLeft(), g_BirdsEyeView ?
304 0 : DEGTORAD(89.f) - declination : DEGTORAD(-89.f) + declination);
305 :
306 0 : CVector3D origin = orientation.GetTranslation();
307 0 : CVector3D offset = q.Rotate(origin - focus);
308 :
309 0 : q *= orientation.GetRotation();
310 0 : q.Normalize();
311 0 : q.ToMatrix(orientation);
312 :
313 0 : orientation.Translate(focus + offset);
314 :
315 0 : camera.UpdateFrustum();
316 0 : g_Game->GetView()->SetCamera(camera);
317 :
318 0 : g_BirdsEyeView = msg->enabled;
319 0 : }
320 :
321 : }
|