Skip to content

Commit

Permalink
Add shader internal tf and data scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturk committed Dec 10, 2024
1 parent 5b3c3d4 commit 139d0af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
32 changes: 30 additions & 2 deletions yt_idv/scene_components/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ def render_gui(self, imgui, renderer, scene):
scene.components.append(GridOutlines(data=gp))
if self.render_method == "transfer_function":
# Now for the transfer function stuff
if self.tf_log:
values = np.log10([self.tf_min, self.tf_max])
else:
values = self.tf_min, self.tf_max
_, values = imgui.input_float2(
"Transfer Function Range",
values[0],
values[1],
format="%0.2f",
flags=imgui.INPUT_TEXT_ENTER_RETURNS_TRUE,
)
if _:
if self.tf_log:
values = 10 ** values[0], 10 ** values[1]
self.tf_min, self.tf_max = values
changed = True
imgui.image_button(
self.transfer_function.texture_name, 256, 32, frame_padding=0
)
Expand Down Expand Up @@ -104,13 +120,15 @@ def render_gui(self, imgui, renderer, scene):
elif renderer.io.key_ctrl:
yv1 = yv2 = 0.0
data[xb1:xb2, 0, i] = np.mgrid[yv1 : yv2 : (xb2 - xb1) * 1j]

if imgui.button("Gaussians"):
data = np.zeros((256, 1, 4), dtype="f4")
v = np.arange(256)
for center in [0.25, 0.5, 0.75]:
data[:, 0, :] += np.exp(-(((v - center * 256) / 20.0) ** 2))[
for i, center in enumerate([0.25, 0.5, 0.75]):
data[:, :, i] += np.exp(-(((v - center * 256) / 10.0) ** 2))[
:, None
]
data[:, :, 3] = data.sum(axis=(1, 2))[:, None]
update = True
if update:
self.transfer_function.data = (data * 255).astype("u1")
Expand All @@ -136,6 +154,14 @@ def _default_transfer_function(self):
tf = TransferFunctionTexture(data=np.ones((256, 1, 4), dtype="u1") * 255)
return tf

@traitlets.default("tf_min")
def _default_tf_min(self):
return self.data.min_val

@traitlets.default("tf_max")
def _default_tf_max(self):
return self.data.max_val

def draw(self, scene, program):
each = self.data.vertex_array.each
GL.glEnable(GL.GL_CULL_FACE)
Expand All @@ -152,6 +178,8 @@ def _set_uniforms(self, scene, shader_program):
shader_program._set_uniform("ds_tex", np.array([0, 0, 0, 0, 0, 0]))
shader_program._set_uniform("bitmap_tex", 1)
shader_program._set_uniform("tf_tex", 2)
shader_program._set_uniform("data_min_val", self.data.min_val)
shader_program._set_uniform("data_max_val", self.data.max_val)
shader_program._set_uniform("tf_min", self.tf_min)
shader_program._set_uniform("tf_max", self.tf_max)
shader_program._set_uniform("tf_log", float(self.tf_log))
Expand Down
2 changes: 2 additions & 0 deletions yt_idv/shaders/known_uniforms.inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ uniform float y_origin;
uniform float tf_log;
uniform float tf_max;
uniform float tf_min;
uniform float data_min_val;
uniform float data_max_val;

// Control of RGB channel information
uniform int channel;
Expand Down
2 changes: 1 addition & 1 deletion yt_idv/shaders/transfer_function.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bool sample_texture(vec3 tex_curr_pos, inout vec4 curr_color, float tdelta,
if (!(map_sample > 0.0)) return false;

vec3 offset_pos = get_offset_texture_position(ds_tex[0], tex_curr_pos);
float tex_sample = texture(ds_tex[0], offset_pos).r;
float tex_sample = data_min_val + (data_max_val-data_min_val) * texture(ds_tex[0], offset_pos).r;

if (tf_log > 0.5) {
if(tex_sample <= 0.0) return false;
Expand Down
6 changes: 6 additions & 0 deletions yt_idv/simple_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def render(self, scene):
self.snapshot_format.format(count=self.snapshot_count),
)
self.snapshot_count += 1
if imgui.button("Log Image Stats"):
scene.render()
print(f"Min: {scene.image.min(axis=(0, 1))}")
print(f"Max: {scene.image.max(axis=(0, 1))}")
print(f"Mean: {scene.image.mean(axis=(0, 1))}")
print(f"Std: {scene.image.std(axis=(0, 1))}")
imgui.tree_pop()
_ = self.render_camera(scene)
changed = changed or _
Expand Down

0 comments on commit 139d0af

Please sign in to comment.