Skip to content

Commit cc4d603

Browse files
authored
make RenderImage participate in depth peeling, fix transparency bugs (#359)
1 parent b1f5ae8 commit cc4d603

12 files changed

+78
-92
lines changed

include/polyscope/render_image_quantity_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class RenderImageQuantityBase : public FloatingQuantity, public FullscreenArtist
8080
void prepareGeometryBuffers();
8181
void addOptionsPopupEntries();
8282
void preparePick();
83+
void setRenderImageUniforms(render::ShaderProgram& program, bool withTonemap = false);
8384
};
8485

8586

src/color_image_quantity.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void ColorImageQuantity::showFullscreen() {
8787
BlendMode::AlphaOver); // WARNING: I never really thought through this, may cause problems
8888

8989
// Set uniforms
90-
fullscreenProgram->setUniform("u_transparency", getTransparency());
90+
fullscreenProgram->setUniform("u_textureTransparency", getTransparency());
9191
render::engine->setTonemapUniforms(*fullscreenProgram);
9292

9393
fullscreenProgram->draw();
@@ -128,7 +128,7 @@ void ColorImageQuantity::showInBillboard(glm::vec3 center, glm::vec3 upVec, glm:
128128

129129
// set uniforms
130130
parent.setStructureUniforms(*billboardProgram);
131-
billboardProgram->setUniform("u_transparency", getTransparency());
131+
billboardProgram->setUniform("u_textureTransparency", getTransparency());
132132
billboardProgram->setUniform("u_billboardCenter", center);
133133
billboardProgram->setUniform("u_billboardUp", upVec);
134134
billboardProgram->setUniform("u_billboardRight", rightVec);

src/color_render_image_quantity.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ void ColorRenderImageQuantity::drawDelayed() {
2727

2828
if (!program) prepare();
2929

30-
// set uniforms
31-
glm::mat4 P = view::getCameraPerspectiveMatrix();
32-
glm::mat4 Pinv = glm::inverse(P);
33-
34-
program->setUniform("u_projMatrix", glm::value_ptr(P));
35-
program->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
36-
program->setUniform("u_viewport", render::engine->getCurrentViewport());
37-
program->setUniform("u_transparency", transparency.get());
38-
render::engine->setMaterialUniforms(*program, material.get());
30+
setRenderImageUniforms(*program);
3931

4032
// draw
4133
program->draw();
@@ -69,13 +61,13 @@ void ColorRenderImageQuantity::prepare() {
6961
// clang-format off
7062
program = render::engine->requestShader("TEXTURE_DRAW_RENDERIMAGE_PLAIN",
7163
render::engine->addMaterialRules(material.get(),
72-
{
64+
parent.addStructureRules({
7365
getImageOriginRule(imageOrigin),
7466
hasNormals ? "SHADE_NORMAL_FROM_TEXTURE" : "SHADE_NORMAL_FROM_VIEWPOS_VAR",
7567
"TEXTURE_SHADE_COLOR"
76-
}
68+
})
7769
),
78-
render::ShaderReplacementDefaults::Process);
70+
render::ShaderReplacementDefaults::SceneObjectNoSlice);
7971
// clang-format on
8072

8173
program->setAttribute("a_position", render::engine->screenTrianglesCoords());

src/depth_render_image_quantity.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ void DepthRenderImageQuantity::drawDelayed() {
2323
2424
if (!program) prepare();
2525
26-
// set uniforms
27-
glm::mat4 P = view::getCameraPerspectiveMatrix();
28-
glm::mat4 Pinv = glm::inverse(P);
29-
30-
program->setUniform("u_projMatrix", glm::value_ptr(P));
31-
program->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
32-
program->setUniform("u_viewport", render::engine->getCurrentViewport());
26+
setRenderImageUniforms(*program);
3327
program->setUniform("u_baseColor", color.get());
34-
program->setUniform("u_transparency", transparency.get());
3528
render::engine->setMaterialUniforms(*program, material.get());
3629
3730
// draw
@@ -73,13 +66,13 @@ void DepthRenderImageQuantity::prepare() {
7366
// clang-format off
7467
program = render::engine->requestShader("TEXTURE_DRAW_RENDERIMAGE_PLAIN",
7568
render::engine->addMaterialRules(material.get(),
76-
{
69+
parent.addStructureRules({
7770
getImageOriginRule(imageOrigin),
7871
hasNormals ? "SHADE_NORMAL_FROM_TEXTURE" : "SHADE_NORMAL_FROM_VIEWPOS_VAR",
7972
"SHADE_BASECOLOR",
80-
}
73+
})
8174
),
82-
render::ShaderReplacementDefaults::Process);
75+
render::ShaderReplacementDefaults::SceneObjectNoSlice);
8376
// clang-format on
8477
8578
program->setAttribute("a_position", render::engine->screenTrianglesCoords());

src/polyscope.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,11 @@ void renderScene() {
606606
if (!isRedraw) {
607607
// Only on first pass (kinda weird, but works out, and doesn't really matter)
608608
renderSlicePlanes();
609-
render::engine->applyTransparencySettings();
610-
drawStructuresDelayed();
611609
}
612610

611+
render::engine->applyTransparencySettings();
612+
drawStructuresDelayed();
613+
613614
// Composite the result of this pass in to the result buffer
614615
render::engine->sceneBufferFinal->bind();
615616
render::engine->setDepthMode(DepthMode::Disable);

src/raw_color_alpha_render_image_quantity.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@ void RawColorAlphaRenderImageQuantity::drawDelayed() {
2828

2929
if (!program) prepare();
3030

31-
// set uniforms
32-
glm::mat4 P = view::getCameraPerspectiveMatrix();
33-
glm::mat4 Pinv = glm::inverse(P);
34-
35-
program->setUniform("u_projMatrix", glm::value_ptr(P));
36-
program->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
37-
program->setUniform("u_viewport", render::engine->getCurrentViewport());
38-
program->setUniform("u_transparency", transparency.get());
39-
render::engine->setTonemapUniforms(*program);
40-
31+
setRenderImageUniforms(*program, true);
4132

4233
// draw
4334
program->draw();
@@ -72,10 +63,11 @@ void RawColorAlphaRenderImageQuantity::prepare() {
7263
// applied after compositing.
7364

7465
// Create the sourceProgram
75-
program = render::engine->requestShader("TEXTURE_DRAW_RAW_RENDERIMAGE_PLAIN",
76-
{getImageOriginRule(imageOrigin), "TEXTURE_SHADE_COLORALPHA",
77-
"INVERSE_TONEMAP", getIsPremultiplied() ? "" : "TEXTURE_PREMULTIPLY_OUT"},
78-
render::ShaderReplacementDefaults::Process);
66+
program = render::engine->requestShader(
67+
"TEXTURE_DRAW_RAW_RENDERIMAGE_PLAIN",
68+
parent.addStructureRules({getImageOriginRule(imageOrigin), "TEXTURE_SHADE_COLORALPHA", "INVERSE_TONEMAP",
69+
getIsPremultiplied() ? "" : "TEXTURE_PREMULTIPLY_OUT"}),
70+
render::ShaderReplacementDefaults::SceneObjectNoSlice);
7971

8072
program->setAttribute("a_position", render::engine->screenTrianglesCoords());
8173
program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get());

src/raw_color_render_image_quantity.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ void RawColorRenderImageQuantity::drawDelayed() {
2727

2828
if (!program) prepare();
2929

30-
// set uniforms
31-
glm::mat4 P = view::getCameraPerspectiveMatrix();
32-
glm::mat4 Pinv = glm::inverse(P);
33-
34-
program->setUniform("u_projMatrix", glm::value_ptr(P));
35-
program->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
36-
program->setUniform("u_viewport", render::engine->getCurrentViewport());
37-
program->setUniform("u_transparency", transparency.get());
38-
render::engine->setTonemapUniforms(*program);
30+
setRenderImageUniforms(*program, true);
3931

4032
// draw
4133
program->draw();
@@ -66,10 +58,11 @@ void RawColorRenderImageQuantity::refresh() {
6658
void RawColorRenderImageQuantity::prepare() {
6759

6860
// Create the sourceProgram
69-
program = render::engine->requestShader(
70-
"TEXTURE_DRAW_RAW_RENDERIMAGE_PLAIN",
71-
{getImageOriginRule(imageOrigin), "TEXTURE_SHADE_COLOR", "INVERSE_TONEMAP", "PREMULTIPLY_LIT_COLOR"},
72-
render::ShaderReplacementDefaults::Process);
61+
program =
62+
render::engine->requestShader("TEXTURE_DRAW_RAW_RENDERIMAGE_PLAIN",
63+
parent.addStructureRules({getImageOriginRule(imageOrigin), "TEXTURE_SHADE_COLOR",
64+
"INVERSE_TONEMAP", "PREMULTIPLY_LIT_COLOR"}),
65+
render::ShaderReplacementDefaults::SceneObjectNoSlice);
7366

7467
program->setAttribute("a_position", render::engine->screenTrianglesCoords());
7568
program->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get());

src/render/opengl/shaders/texture_draw_shaders.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const ShaderStageSpecification PLAIN_RENDERIMAGE_TEXTURE_DRAW_FRAG_SHADER = {
133133
{"u_projMatrix", RenderDataType::Matrix44Float},
134134
{"u_invProjMatrix", RenderDataType::Matrix44Float},
135135
{"u_viewport", RenderDataType::Vector4Float},
136-
{"u_transparency", RenderDataType::Float},
136+
{"u_textureTransparency", RenderDataType::Float},
137137
},
138138

139139
// attributes
@@ -151,7 +151,7 @@ R"(
151151
uniform mat4 u_projMatrix;
152152
uniform mat4 u_invProjMatrix;
153153
uniform vec4 u_viewport;
154-
uniform float u_transparency;
154+
uniform float u_textureTransparency;
155155
156156
in vec2 tCoord;
157157
uniform sampler2D t_depth;
@@ -167,6 +167,9 @@ R"(
167167
168168
// Fetch values from texture
169169
float depth = texture(t_depth, tCoord).r;
170+
171+
${ GLOBAL_FRAGMENT_FILTER_PREP }$
172+
${ GLOBAL_FRAGMENT_FILTER }$
170173
171174
if(depth > LARGE_FLOAT()) {
172175
discard;
@@ -192,7 +195,7 @@ R"(
192195
${ GENERATE_LIT_COLOR }$
193196
194197
// Set alpha
195-
float alphaOut = u_transparency;
198+
float alphaOut = u_textureTransparency;
196199
${ GENERATE_ALPHA }$
197200
198201
${ PERTURB_LIT_COLOR }$
@@ -215,7 +218,7 @@ const ShaderStageSpecification PLAIN_RAW_RENDERIMAGE_TEXTURE_DRAW_FRAG_SHADER =
215218
{"u_projMatrix", RenderDataType::Matrix44Float},
216219
{"u_invProjMatrix", RenderDataType::Matrix44Float},
217220
{"u_viewport", RenderDataType::Vector4Float},
218-
{"u_transparency", RenderDataType::Float},
221+
{"u_textureTransparency", RenderDataType::Float},
219222
},
220223

221224
// attributes
@@ -233,7 +236,7 @@ R"(
233236
uniform mat4 u_projMatrix;
234237
uniform mat4 u_invProjMatrix;
235238
uniform vec4 u_viewport;
236-
uniform float u_transparency;
239+
uniform float u_textureTransparency;
237240
238241
in vec2 tCoord;
239242
uniform sampler2D t_depth;
@@ -275,7 +278,7 @@ R"(
275278
${ GENERATE_LIT_COLOR }$
276279
277280
// Set alpha
278-
float alphaOut = u_transparency;
281+
float alphaOut = u_textureTransparency;
279282
${ GENERATE_ALPHA }$
280283
281284
${ PERTURB_LIT_COLOR }$
@@ -608,14 +611,14 @@ const ShaderReplacementRule TEXTURE_SET_TRANSPARENCY(
608611
/* rule name */ "TEXTURE_SET_TRANSPARENCY",
609612
{ /* replacement sources */
610613
{"FRAG_DECLARATIONS", R"(
611-
uniform float u_transparency;
614+
uniform float u_textureTransparency;
612615
)" },
613616
{"TEXTURE_OUT_ADJUST", R"(
614-
textureOut = vec4(textureOut.rgb, textureOut.a * u_transparency);
617+
textureOut = vec4(textureOut.rgb, textureOut.a * u_textureTransparency);
615618
)"}
616619
},
617620
/* uniforms */ {
618-
{"u_transparency", RenderDataType::Float},
621+
{"u_textureTransparency", RenderDataType::Float},
619622
},
620623
/* attributes */ {},
621624
/* textures */ {}
@@ -625,14 +628,14 @@ const ShaderReplacementRule TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED(
625628
/* rule name */ "TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED",
626629
{ /* replacement sources */
627630
{"FRAG_DECLARATIONS", R"(
628-
uniform float u_transparency;
631+
uniform float u_textureTransparency;
629632
)" },
630633
{"TEXTURE_OUT_ADJUST", R"(
631-
textureOut *= u_transparency;
634+
textureOut *= u_textureTransparency;
632635
)"}
633636
},
634637
/* uniforms */ {
635-
{"u_transparency", RenderDataType::Float},
638+
{"u_textureTransparency", RenderDataType::Float},
636639
},
637640
/* attributes */ {},
638641
/* textures */ {}

src/render_image_quantity_base.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ void RenderImageQuantityBase::updateBaseBuffers(const std::vector<float>& newDep
6464
requestRedraw();
6565
}
6666

67+
void RenderImageQuantityBase::setRenderImageUniforms(render::ShaderProgram& program, bool withTonemap) {
68+
parent.setStructureUniforms(program);
69+
70+
glm::mat4 P = view::getCameraPerspectiveMatrix();
71+
glm::mat4 Pinv = glm::inverse(P);
72+
73+
program.setUniform("u_projMatrix", glm::value_ptr(P));
74+
program.setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
75+
program.setUniform("u_viewport", render::engine->getCurrentViewport());
76+
program.setUniform("u_textureTransparency", transparency.get());
77+
if (program.hasUniform("u_transparency")) {
78+
program.setUniform("u_transparency", 1.0f);
79+
}
80+
81+
if (withTonemap) {
82+
render::engine->setTonemapUniforms(program);
83+
}
84+
}
6785

6886
void RenderImageQuantityBase::drawPickDelayed() {
6987
if (!isEnabled()) return;
@@ -77,19 +95,13 @@ void RenderImageQuantityBase::drawPickDelayed() {
7795
pickProgram->setUniform("u_projMatrix", glm::value_ptr(P));
7896
pickProgram->setUniform("u_invProjMatrix", glm::value_ptr(Pinv));
7997
pickProgram->setUniform("u_viewport", render::engine->getCurrentViewport());
80-
pickProgram->setUniform("u_transparency", 1.0);
98+
pickProgram->setUniform("u_textureTransparency", 1.0);
8199
pickProgram->setUniform("u_color", pickColor);
82100

83101
// draw
84102
pickProgram->draw();
85103
}
86104

87-
void RenderImageQuantityBase::refresh() {
88-
pickProgram = nullptr;
89-
Quantity::refresh();
90-
}
91-
92-
93105
void RenderImageQuantityBase::preparePick() {
94106

95107
// Request pick indices
@@ -100,10 +112,10 @@ void RenderImageQuantityBase::preparePick() {
100112
// Create the sourceProgram
101113
// clang-format off
102114
pickProgram = render::engine->requestShader("TEXTURE_DRAW_RENDERIMAGE_PLAIN",
103-
{
115+
parent.addStructureRules({
104116
getImageOriginRule(imageOrigin),
105117
"SHADECOLOR_FROM_UNIFORM",
106-
},
118+
}),
107119
render::ShaderReplacementDefaults::Pick
108120
);
109121
// clang-format on
@@ -112,6 +124,11 @@ void RenderImageQuantityBase::preparePick() {
112124
pickProgram->setTextureFromBuffer("t_depth", depths.getRenderTextureBuffer().get());
113125
}
114126

127+
void RenderImageQuantityBase::refresh() {
128+
pickProgram = nullptr;
129+
Quantity::refresh();
130+
}
131+
115132
void RenderImageQuantityBase::disableFullscreenDrawing() {
116133
if (isEnabled()) {
117134
setEnabled(false);

src/scalar_image_quantity.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void ScalarImageQuantity::showFullscreen() {
7777

7878
// Set uniforms
7979
this->setScalarUniforms(*fullscreenProgram);
80-
fullscreenProgram->setUniform("u_transparency", getTransparency());
80+
fullscreenProgram->setUniform("u_textureTransparency", getTransparency());
8181

8282
fullscreenProgram->draw();
8383

@@ -90,7 +90,7 @@ void ScalarImageQuantity::renderIntermediate() {
9090

9191
// Set uniforms
9292
this->setScalarUniforms(*fullscreenProgram);
93-
fullscreenProgram->setUniform("u_transparency", getTransparency());
93+
fullscreenProgram->setUniform("u_textureTransparency", getTransparency());
9494

9595
// render to the intermediate texture
9696
render::engine->pushBindFramebufferForRendering(*framebufferIntermediate);
@@ -133,7 +133,7 @@ void ScalarImageQuantity::showInBillboard(glm::vec3 center, glm::vec3 upVec, glm
133133

134134
// set uniforms
135135
parent.setStructureUniforms(*billboardProgram);
136-
billboardProgram->setUniform("u_transparency", getTransparency());
136+
billboardProgram->setUniform("u_textureTransparency", getTransparency());
137137
billboardProgram->setUniform("u_billboardCenter", center);
138138
billboardProgram->setUniform("u_billboardUp", upVec);
139139
billboardProgram->setUniform("u_billboardRight", rightVec);

0 commit comments

Comments
 (0)