Skip to content

Commit c59cdaa

Browse files
committed
- split & pretty viewer and model renderer
1 parent 423d444 commit c59cdaa

9 files changed

Lines changed: 877 additions & 786 deletions

File tree

DriverLevelTool/viewer/camera.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

DriverLevelTool/viewer/camera.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CAMERA_H
2+
#define CAMERA_H
3+
4+
#include "math/Vector.h"
5+
6+
class Volume;
7+
8+
extern Vector3D g_cameraVelocity;
9+
extern Vector3D g_cameraPosition;
10+
extern Vector3D g_cameraAngles;
11+
12+
extern Vector3D g_cameraMoveDir;
13+
14+
void UpdateCameraMovement(float deltaTime, float speedModifier);
15+
16+
void SetupCameraViewAndMatrices(const Vector3D& cameraPosition, const Vector3D& cameraAngles, Volume& outFrustum);
17+
18+
#endif // CAMERA_H
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "debug_overlay.h"
2+
#include "driver_routines/regions_d2.h"
3+
#include "math/isin.h"
4+
#include "math/Vector.h"
5+
6+
extern CBaseLevelMap* g_levMap;
7+
8+
VECTOR_NOPAD g_debugCellPos;
9+
10+
void DebugDrawSdNode(sdNode* node)
11+
{
12+
Vector3D cpos(g_debugCellPos.vx - 512, g_debugCellPos.vy, g_debugCellPos.vz - 512);
13+
cpos /= ONE_F;
14+
15+
Vector3D dir(icos(node->angle), 0.0f, isin(node->angle));
16+
dir /= ONE_F;
17+
18+
Vector3D tangent = cross(dir, vec3_up);
19+
20+
ColorRGBA color(0, 1, 0, 1);
21+
DebugOverlay_Line(cpos - dir + tangent * float(node->dist / ONE_F), cpos + dir + tangent * float(node->dist / ONE_F), color);
22+
}
23+
24+
// recursively walks heightmap nodes
25+
short* DebugDriver2SdCell_r(sdNode* node, XZPAIR* pos)
26+
{
27+
if (node->node)
28+
{
29+
DebugDrawSdNode(node);
30+
31+
DebugDriver2SdCell_r(node + 1, pos);
32+
DebugDriver2SdCell_r(node + node->offset, pos);
33+
}
34+
35+
return SdGetBSP(node, pos);
36+
}
37+
38+
//-------------------------------------------------------------
39+
// Displays heightmap cell BSP
40+
//-------------------------------------------------------------
41+
void DebugDrawDriver2HeightmapCell(const VECTOR_NOPAD& cellPos, const ColorRGBA& color)
42+
{
43+
// cell bounds
44+
int cellMinX = (((cellPos.vx - 512) >> 10) << 10) + 512;
45+
int cellMinZ = (((cellPos.vz - 512) >> 10) << 10) + 512;
46+
47+
Vector3D cMin, cMax;
48+
cMin.y = cMax.y = cellPos.vy / ONE_F;
49+
50+
cMin.x = cellMinX / ONE_F;
51+
cMin.z = cellMinZ / ONE_F;
52+
53+
cMax.x = (cellMinX + 1024) / ONE_F;
54+
cMax.z = (cellMinZ + 1024) / ONE_F;
55+
56+
XZPAIR cell;
57+
g_levMap->WorldPositionToCellXZ(cell, cellPos);
58+
CDriver2LevelRegion* region = (CDriver2LevelRegion*)g_levMap->GetRegion(cell);
59+
60+
VECTOR_NOPAD cpos = cellPos;
61+
cpos.vx -= 512;
62+
cpos.vz -= 512;
63+
64+
DebugOverlay_Line(Vector3D(cMax.x, cMin.y, cMin.z),
65+
Vector3D(cMax.x, cMin.y, cMax.z), color);
66+
67+
DebugOverlay_Line(Vector3D(cMin.x, cMin.y, cMax.z),
68+
Vector3D(cMin.x, cMin.y, cMin.z), color);
69+
70+
DebugOverlay_Line(Vector3D(cMin.x, cMin.y, cMin.z),
71+
Vector3D(cMax.x, cMin.y, cMin.z), color);
72+
73+
DebugOverlay_Line(Vector3D(cMin.x, cMin.y, cMax.z),
74+
Vector3D(cMax.x, cMin.y, cMax.z), color);
75+
76+
g_debugCellPos.vx = cellMinX + 512;
77+
g_debugCellPos.vy = cellPos.vy;
78+
g_debugCellPos.vz = cellMinZ + 512;
79+
80+
int level = 0;
81+
region->SdGetCell(cpos, level, DebugDriver2SdCell_r);
82+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef RENDERHEIGHTMAP_H
2+
#define RENDERHEIGHTMAP_H
3+
4+
#include "math/Vector.h"
5+
struct VECTOR_NOPAD;
6+
7+
void DebugDrawDriver2HeightmapCell(const VECTOR_NOPAD& cellPos, const ColorRGBA& color);
8+
9+
#endif // RENDERHEIGHTMAP_H

0 commit comments

Comments
 (0)