1+
2+ #include " driver_level.h"
3+ #include " gl_renderer.h"
4+ #include " renderheightmap.h"
5+ #include " rendermodel.h"
6+ #include " math/psx_math_types.h"
7+ #include " math/Volume.h"
8+
9+ extern bool g_displayHeightMap;
10+
11+ const float Z_NEAR = 0 .01f ;
12+ const float Z_FAR = 100 .0f ;
13+ const float CAMERA_FOV = 75 .0f ;
14+
15+ const float CAMERA_MOVEMENT_SPEED_FACTOR = 140 * RENDER_SCALING;
16+ const float CAMERA_MOVEMENT_ACCELERATION = 15 * RENDER_SCALING;
17+ const float CAMERA_MOVEMENT_DECELERATION = 450 * RENDER_SCALING;
18+
19+ Vector3D g_cameraVelocity (0 );
20+ Vector3D g_cameraPosition (0 );
21+ Vector3D g_cameraAngles (25 .0f , 45 .0f , 0 );
22+
23+ Vector3D g_cameraMoveDir (0 );
24+
25+ // -------------------------------------------------------
26+ // Updates camera movement for level viewer
27+ // -------------------------------------------------------
28+ void UpdateCameraMovement (float deltaTime, float speedModifier)
29+ {
30+ Vector3D forward, right;
31+ AngleVectors (g_cameraAngles, &forward, &right);
32+
33+ const float maxSpeed = CAMERA_MOVEMENT_SPEED_FACTOR * speedModifier;
34+
35+ if (lengthSqr (g_cameraMoveDir) > 0 .1f &&
36+ length (g_cameraVelocity) < maxSpeed)
37+ {
38+ g_cameraVelocity += g_cameraMoveDir.x * right * deltaTime * CAMERA_MOVEMENT_ACCELERATION * speedModifier;
39+ g_cameraVelocity += g_cameraMoveDir.z * forward * deltaTime * CAMERA_MOVEMENT_ACCELERATION * speedModifier;
40+ }
41+ else
42+ {
43+ float speed = length (g_cameraVelocity);
44+ if (speed < 1 .0f )
45+ speed = 1 .0f ;
46+
47+ g_cameraVelocity -= (g_cameraVelocity / speed) * CAMERA_MOVEMENT_DECELERATION * deltaTime;
48+ }
49+
50+ g_cameraPosition += g_cameraVelocity * deltaTime;
51+
52+ VECTOR_NOPAD cameraPosition;
53+ cameraPosition.vx = g_cameraPosition.x * ONE_F;
54+ cameraPosition.vy = g_cameraPosition.y * ONE_F;
55+ cameraPosition.vz = g_cameraPosition.z * ONE_F;
56+
57+ int height = g_levMap->MapHeight (cameraPosition);
58+
59+ // debug display
60+ if (g_displayHeightMap)
61+ {
62+ // draw the cell
63+ VECTOR_NOPAD cameraCell = cameraPosition;
64+ cameraCell.vy = height;
65+ DebugDrawDriver2HeightmapCell (cameraCell, ColorRGBA (1 , 1 , 0.25 , 1 .0f ));
66+ }
67+
68+ if (cameraPosition.vy < height)
69+ {
70+ cameraPosition.vy = height;
71+ g_cameraPosition.y = float (height) / ONE_F;
72+ }
73+ }
74+
75+ extern int g_windowWidth;
76+ extern int g_windowHeight;
77+
78+ // -------------------------------------------------------
79+ // Sets up the camera matrices
80+ // -------------------------------------------------------
81+ void SetupCameraViewAndMatrices (const Vector3D& cameraPosition, const Vector3D& cameraAngles, Volume& outFrustum)
82+ {
83+ // calculate view matrices
84+ Matrix4x4 view, proj;
85+
86+ proj = perspectiveMatrixY (DEG2RAD (CAMERA_FOV), g_windowWidth, g_windowHeight, Z_NEAR, Z_FAR);
87+ view = rotateZXY4 (-DEG2RAD (cameraAngles.x ), -DEG2RAD (cameraAngles.y ), -DEG2RAD (cameraAngles.z ));
88+ view.translate (-cameraPosition);
89+
90+ // calculate frustum volume
91+ outFrustum.LoadAsFrustum (proj * view);
92+
93+ GR_SetMatrix (MATRIX_VIEW, view);
94+ GR_SetMatrix (MATRIX_PROJECTION, proj);
95+
96+ // setup default world position
97+ GR_SetMatrix (MATRIX_WORLD, identity4 ());
98+
99+ GR_UpdateMatrixUniforms ();
100+ }
0 commit comments