Skip to content

Commit bca2721

Browse files
authored
lab orthographic projection (#7336)
1 parent 5acc748 commit bca2721

7 files changed

Lines changed: 50 additions & 1 deletion

File tree

code/graphics/matrix.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ static bool htl_2d_matrix_set = false;
2727

2828
static bool matrix_uniform_up_to_date = false;
2929

30+
static bool gr_ortho_override_active = false;
31+
static float gr_ortho_override_distance = 0.0f;
32+
3033
static matrix4 create_view_matrix(const vec3d* pos, const matrix* orient)
3134
{
3235
vec3d scaled_pos;
@@ -127,7 +130,18 @@ void gr_set_proj_matrix(fov_t fov, float aspect, float z_near, float z_far) {
127130

128131
gr_last_projection_matrix = gr_projection_matrix;
129132

130-
if (std::holds_alternative<float>(fov)) {
133+
if (gr_ortho_override_active && std::holds_alternative<float>(fov)) {
134+
float half_h = gr_ortho_override_distance * tanf(std::get<float>(fov) * 0.5f);
135+
float half_w = half_h * aspect;
136+
if (gr_screen.rendering_to_texture != -1) {
137+
create_orthographic_projection_matrix(&gr_projection_matrix, -half_w, half_w, half_h, -half_h, z_near, z_far);
138+
} else {
139+
create_orthographic_projection_matrix(&gr_projection_matrix, -half_w, half_w, -half_h, half_h, z_near, z_far);
140+
}
141+
// Clear after the first call so that shadow/deferred
142+
// restore calls later in the same frame are not affected
143+
gr_ortho_override_active = false;
144+
} else if (std::holds_alternative<float>(fov)) {
131145
float clip_width, clip_height;
132146
clip_height = tan(std::get<float>(fov) * 0.5f) * z_near;
133147
clip_width = clip_height * aspect;
@@ -393,3 +407,15 @@ void gr_matrix_set_uniforms()
393407

394408
matrix_uniform_up_to_date = true;
395409
}
410+
411+
void gr_activate_ortho_proj_override(float camera_distance)
412+
{
413+
gr_ortho_override_active = true;
414+
gr_ortho_override_distance = camera_distance;
415+
}
416+
417+
void gr_deactivate_ortho_proj_override()
418+
{
419+
gr_ortho_override_active = false;
420+
gr_ortho_override_distance = 0.0f;
421+
}

code/graphics/matrix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ void gr_end_instance_matrix();
2020
void gr_set_proj_matrix(fov_t fov, float aspect, float z_near, float z_far);
2121
void gr_end_proj_matrix();
2222

23+
// Orthographic projection override... when active gr_set_proj_matrix substitutes an
24+
// orthographic frustum sized to match the perspective view at the given camera distance.
25+
void gr_activate_ortho_proj_override(float camera_distance);
26+
void gr_deactivate_ortho_proj_override();
27+
2328
void gr_set_view_matrix(const vec3d* pos, const matrix* orient);
2429
void gr_end_view_matrix();
2530

code/lab/dialogs/lab_ui.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ void LabUi::show_render_options()
569569
Checkbox("Hide Post Processing", &hide_post_processing);
570570
Checkbox("Hide particles", &no_particles);
571571
Checkbox("Render as wireframe", &use_wireframe_rendering);
572+
Checkbox("Orthographic projection", &use_orthographic_projection);
572573
Checkbox("Render without light", &no_lighting);
573574
Checkbox("Render with emissive lighting", &show_emissive_lighting);
574575
SliderFloat("Light brightness", &light_factor, 0.0f, 10.0f);
@@ -688,6 +689,7 @@ void LabUi::show_render_options()
688689
getLabManager()->Renderer->setRenderFlag(LabRenderFlag::ShowEmissiveLighting, show_emissive_lighting);
689690
getLabManager()->Renderer->setRenderFlag(LabRenderFlag::MoveSubsystems, animate_subsystems);
690691
getLabManager()->Renderer->setRenderFlag(LabRenderFlag::NoParticles, no_particles);
692+
getLabManager()->Renderer->setRenderFlag(LabRenderFlag::UseOrthographicProjection, use_orthographic_projection);
691693
getLabManager()->Renderer->setEmissiveFactor(emissive_factor);
692694
getLabManager()->Renderer->setAmbientFactor(ambient_factor);
693695
getLabManager()->Renderer->setLightFactor(light_factor);

code/lab/dialogs/lab_ui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class LabUi {
121121
bool show_weapons = false;
122122
bool show_emissive_lighting = false;
123123
bool show_particles = true;
124+
bool use_orthographic_projection = false;
124125

125126
std::optional<vec3d> volumetrics_pos_backup = std::nullopt;
126127
};

code/lab/renderer/lab_cameras.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class LabCamera {
5454

5555
/// Resets the camera orientation, pan, and zoom to default values
5656
virtual void resetView() {}
57+
58+
/// Returns the distance from the camera to the point of interest, used to size the orthographic frustum.
59+
virtual float getCameraDistance() const { return 0.0f; }
5760
};
5861

5962
class OrbitCamera : public LabCamera {
@@ -84,6 +87,8 @@ class OrbitCamera : public LabCamera {
8487

8588
void updateCamera() override;
8689

90+
float getCameraDistance() const override { return distance; }
91+
8792
private:
8893
static constexpr float DEFAULT_DISTANCE = 100.0f;
8994
static constexpr float DEFAULT_PHI = 1.24f;

code/lab/renderer/lab_renderer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "globalincs/vmallocator.h"
44
#include "graphics/2d.h"
55
#include "graphics/light.h"
6+
#include "graphics/matrix.h"
67
#include "lab/labv2_internal.h"
78
#include "lab/renderer/lab_renderer.h"
89
#include "lighting/lighting_profiles.h"
@@ -237,10 +238,18 @@ void LabRenderer::renderModel(float frametime) {
237238
}
238239
shockwave_move_all(frametime);
239240

241+
if (renderFlags[LabRenderFlag::UseOrthographicProjection]) {
242+
float dist = labCamera->getCameraDistance();
243+
if (dist > 0.0f)
244+
gr_activate_ortho_proj_override(dist);
245+
}
246+
240247
Trail_render_override = true;
241248
game_render_frame(labCamera->FS_camera);
242249
Trail_render_override = false;
243250

251+
gr_deactivate_ortho_proj_override();
252+
244253
Motion_debris_enabled = lab_debris_override_save;
245254
Envmap_override = lab_envmap_override_save;
246255
Cmdline_emissive = lab_emissive_light_save;

code/lab/renderer/lab_renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ FLAG_LIST(LabRenderFlag) {
3737
ShowAfterburners,
3838
TimeStopped,
3939
NoParticles,
40+
UseOrthographicProjection,
4041

4142
NUM_VALUES
4243
};

0 commit comments

Comments
 (0)