Skip to content
This repository was archived by the owner on Jul 14, 2021. It is now read-only.

add depth buffering to 3d contours #188

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions assets/shader/volume.frag
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ uniform int algorithm;
uniform float isovalue;
uniform float isorange;

uniform mat4 model, projectionview;

const float max_distance = 1.3;

const int num_samples = 200;
Expand Down Expand Up @@ -206,11 +208,14 @@ vec4 contours(vec3 front, vec3 dir)
vec3 Lo = vec3(0.0);
int i = 0;
vec3 camdir = normalize(-dir);
float depth = 100000.0;
for (i; i < num_samples; ++i) {
float intensity = texture(volumedata, pos).x;
vec4 density = color_lookup(intensity, color_map, color_norm, color);
float opacity = density.a;
if(opacity > 0.0){
vec4 frag_coord = projectionview * model * vec4(pos, 1);
depth = min(depth, frag_coord.z / frag_coord.w);
vec3 N = gennormal(pos, step_size);
vec3 L = normalize(o_light_dir - pos);
vec3 opaque = blinnphong(N, camdir, L, density.rgb);
Expand All @@ -221,6 +226,7 @@ vec4 contours(vec3 front, vec3 dir)
}
pos += dir;
}
gl_FragDepth = depth == 100000.0 ? gl_FragDepth : 0.5 * depth + 0.5;
return vec4(Lo, 1-T);
}

Expand All @@ -231,9 +237,12 @@ vec4 isosurface(vec3 front, vec3 dir)
int i = 0;
vec4 diffuse_color = color_lookup(isovalue, color_map, color_norm, color);
vec3 camdir = normalize(-dir);
float depth = 100000.0;
for (i; i < num_samples; ++i){
float density = texture(volumedata, pos).x;
if(abs(density - isovalue) < isorange){
vec4 frag_coord = projectionview * model * vec4(pos, 1);
depth = min(depth, frag_coord.z / frag_coord.w);
vec3 N = gennormal(pos, step_size);
vec3 L = normalize(o_light_dir - pos);
// back & frontface...
Expand All @@ -244,6 +253,7 @@ vec4 isosurface(vec3 front, vec3 dir)
}
pos += dir;
}
gl_FragDepth = depth == 100000.0 ? gl_FragDepth : 0.5 * depth + 0.5;
return c;
}

Expand Down