Skip to content

Commit 37575b2

Browse files
authored
Merge pull request #1327 from asarium/fix/1326
Apply clipping planes to passthrough shader
2 parents 353c281 + 6ddca15 commit 37575b2

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ root = true
22

33
[*.{h,cpp}]
44
indent_style = tab
5+
6+
[*.sdr]
7+
indent_style = tab

code/def_files/passthrough-v.sdr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ out vec4 fragColor;
66
uniform mat4 modelViewMatrix;
77
uniform mat4 projMatrix;
88
uniform vec4 color;
9+
10+
uniform mat4 modelMatrix;
11+
uniform bool clipEnabled;
12+
uniform vec4 clipEquation;
13+
914
void main()
1015
{
1116
fragTexCoord = vertTexCoord;
1217
fragColor = vertColor * color;
1318
gl_Position = projMatrix * modelViewMatrix * vertPosition;
14-
}
19+
20+
if (clipEnabled) {
21+
gl_ClipDistance[0] = dot(clipEquation, modelMatrix * vertPosition);
22+
}
23+
}

code/graphics/opengl/gropenglshader.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static opengl_shader_type_t GL_shader_types[] = {
106106
{ opengl_vert_attrib::POSITION, opengl_vert_attrib::TEXCOORD }, "Video Playback" },
107107

108108
{ SDR_TYPE_PASSTHROUGH_RENDER, "passthrough-v.sdr", "passthrough-f.sdr", 0,
109-
{ "modelViewMatrix", "projMatrix", "baseMap", "noTexturing", "alphaTexture", "srgb", "intensity", "color", "alphaThreshold" },
109+
{ "modelViewMatrix", "projMatrix", "baseMap", "noTexturing", "alphaTexture", "srgb", "intensity", "color", "alphaThreshold", "clipEnabled", "clipEquation", "modelMatrix" },
110110
{ opengl_vert_attrib::POSITION, opengl_vert_attrib::TEXCOORD, opengl_vert_attrib::COLOR }, "Passthrough" },
111111

112112
{ SDR_TYPE_SHIELD_DECAL, "shield-impact-v.sdr", "shield-impact-f.sdr", 0,
@@ -788,7 +788,7 @@ void opengl_shader_compile_passthrough_shader()
788788
opengl_shader_set_current();
789789
}
790790

791-
void opengl_shader_set_passthrough(bool textured, bool alpha, vec4 *clr, float color_scale)
791+
void opengl_shader_set_passthrough(bool textured, bool alpha, vec4 *clr, float color_scale, const material::clip_plane& clip_plane)
792792
{
793793
opengl_shader_set_current(gr_opengl_maybe_create_shader(SDR_TYPE_PASSTHROUGH_RENDER, 0));
794794

@@ -820,6 +820,21 @@ void opengl_shader_set_passthrough(bool textured, bool alpha, vec4 *clr, float c
820820
Current_shader->program->Uniforms.setUniform4f("color", 1.0f, 1.0f, 1.0f, 1.0f);
821821
}
822822

823+
if (clip_plane.enabled) {
824+
Current_shader->program->Uniforms.setUniformi("clipEnabled", 1);
825+
826+
vec4 clip_equation;
827+
clip_equation.xyzw.x = clip_plane.normal.xyz.x;
828+
clip_equation.xyzw.y = clip_plane.normal.xyz.y;
829+
clip_equation.xyzw.z = clip_plane.normal.xyz.z;
830+
clip_equation.xyzw.w = -vm_vec_dot(&clip_plane.normal, &clip_plane.position);
831+
832+
Current_shader->program->Uniforms.setUniform4f("clipEquation", clip_equation);
833+
Current_shader->program->Uniforms.setUniformMatrix4f("modelMatrix", GL_model_matrix_stack.get_transform());
834+
} else {
835+
Current_shader->program->Uniforms.setUniformi("clipEnabled", 0);
836+
}
837+
823838
Current_shader->program->Uniforms.setUniformMatrix4f("modelViewMatrix", GL_model_view_matrix);
824839
Current_shader->program->Uniforms.setUniformMatrix4f("projMatrix", GL_projection_matrix);
825840
}

code/graphics/opengl/gropenglshader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "globalincs/pstypes.h"
1515
#include "graphics/2d.h"
1616
#include "graphics/opengl/gropengl.h"
17+
#include "graphics/material.h"
1718
#include "ShaderProgram.h"
1819

1920
#include <string>
@@ -166,7 +167,7 @@ void opengl_shader_compile_deferred_light_shader();
166167
void opengl_shader_compile_deferred_light_clear_shader();
167168

168169
void opengl_shader_compile_passthrough_shader();
169-
void opengl_shader_set_passthrough(bool textured = true, bool alpha = false, vec4* clr = NULL, float color_scale = 1.0f);
170+
void opengl_shader_set_passthrough(bool textured = true, bool alpha = false, vec4* clr = NULL, float color_scale = 1.0f, const material::clip_plane& clip_plane = material::clip_plane());
170171
void opengl_shader_set_passthrough(bool textured, bool alpha, color *clr);
171172

172173
#define ANIMATED_SHADER_LOADOUTSELECT_FS1 0

code/graphics/opengl/gropengltnl.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,9 @@ void gr_opengl_set_clip_plane(vec3d *clip_normal, vec3d *clip_point)
796796
if ( clip_normal == NULL || clip_point == NULL ) {
797797
GL_state.ClipDistance(0, false);
798798
} else {
799-
Assertion(Current_shader != NULL && Current_shader->shader == SDR_TYPE_MODEL, "Clip planes are only supported by the model shader!");
799+
Assertion(Current_shader != NULL &&
800+
(Current_shader->shader == SDR_TYPE_MODEL || Current_shader->shader == SDR_TYPE_PASSTHROUGH_RENDER),
801+
"Clip planes are not supported by this shader!");
800802

801803
GL_state.ClipDistance(0, true);
802804
}
@@ -863,10 +865,6 @@ void opengl_tnl_set_material(material* material_info, bool set_base_map)
863865

864866
opengl_shader_set_current(shader_handle);
865867

866-
if ( Current_shader->shader == SDR_TYPE_PASSTHROUGH_RENDER ) {
867-
opengl_shader_set_passthrough(base_map >= 0, material_info->get_texture_type() == TCACHE_TYPE_AABITMAP, &clr, material_info->get_color_scale());
868-
}
869-
870868
GL_state.SetAlphaBlendMode(material_info->get_blend_mode());
871869
GL_state.SetZbufferType(material_info->get_depth_mode());
872870

@@ -894,6 +892,14 @@ void opengl_tnl_set_material(material* material_info, bool set_base_map)
894892
gr_opengl_set_clip_plane(NULL, NULL);
895893
}
896894

895+
if ( Current_shader->shader == SDR_TYPE_PASSTHROUGH_RENDER ) {
896+
opengl_shader_set_passthrough(base_map >= 0,
897+
material_info->get_texture_type() == TCACHE_TYPE_AABITMAP,
898+
&clr,
899+
material_info->get_color_scale(),
900+
material_info->get_clip_plane());
901+
}
902+
897903
if ( set_base_map && base_map >= 0 ) {
898904
float u_scale, v_scale;
899905

code/graphics/opengl/gropengltnl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern float shadow_middist;
3333
extern float shadow_fardist;
3434
extern bool Rendering_to_shadow_map;
3535

36+
extern transform_stack GL_model_matrix_stack;
3637
extern matrix4 GL_view_matrix;
3738
extern matrix4 GL_model_view_matrix;
3839
extern matrix4 GL_projection_matrix;

0 commit comments

Comments
 (0)