平均值
/*
Volume fragment shader
*/
#version 330 core
uniform sampler3D volume;
in vec4 origin;
in vec3 dir;
uniform float ww;
uniform float wl;
out vec3 color;
void main()
{
/*
Perform ray/volume intersection test
*/
//bounding box
vec3 bmin = vec3(0,0,0);
vec3 bmax = vec3(1,1,1);
vec3 invDir = 1.0f / normalize(dir);
//intersection vectors with planes
vec3 tmin = (bmin - origin.xyz) * invDir;
vec3 tmax = (bmax - origin.xyz) * invDir;
vec3 tnear = min(tmin, tmax);
vec3 tfar = max(tmin, tmax);
//entry point
float t0 = max(max(tnear.x, 0.0f), max(tnear.y, tnear.z));
//exit point
float t1 = min(tfar.x, min(tfar.y, tfar.z));
bool intersects = (t1 > 0.0f) && (t0 < t1);
/*
Trace between intersection points
*/
vec3 start = origin.xyz + (normalize(dir) * t0);
vec3 end = origin.xyz + (normalize(dir) * t1);
const float stepSize = (1.0f / 300);
vec3 step = normalize(dir) * stepSize;
vec3 val = vec3(0,0,0);
float result_gray=0.0f;
float ratio = 1000.0f;
float ratio_r = 1.0f / 1000.0f;
float sum = 0.0f;
int count=0;
if (intersects)
{
vec3 cur = start;
while(distance(cur, end) > stepSize)
{
//val = max(val, texture(volume, cur).rrr);
val = texture(volume, cur).rrr;
cur += step;
sum += val.r*1024;
count++;
}
//result_gray=val.r*1024.0f;
result_gray = sum/count; //* (1.0f / count) * ratio;
float wl_min_gray = wl - 0.5 * ww;
result_gray= (result_gray - wl_min_gray) / ww;
result_gray = clamp(result_gray, 0.0, 1.0);
if(result_gray < 0.000001)
{
discard;
}
/*if(result_gray>0&&result_gray<0.3)color =vec3(1.0,0.0,0.0);
else if(result_gray>0.3&&result_gray<0.6)color =vec3(0.0,0.0,1.0);
else if(result_gray>0.6)color =vec3(1.0,1.0,1.0);*/
color =vec3(result_gray,result_gray,result_gray);
}else
color =vec3(0.0,0.0,0.0);
}
最大密度
/*
Volume fragment shader
*/
#version 330 core
uniform sampler3D volume;
in vec4 origin;
in vec3 dir;
uniform float ww;
uniform float wl;
out vec3 color;
void main()
{
/*
Perform ray/volume intersection test
*/
//bounding box
vec3 bmin = vec3(0,0,0);
vec3 bmax = vec3(1,1,1);
vec3 invDir = 1.0f / normalize(dir);
//intersection vectors with planes
vec3 tmin = (bmin - origin.xyz) * invDir;
vec3 tmax = (bmax - origin.xyz) * invDir;
vec3 tnear = min(tmin, tmax);
vec3 tfar = max(tmin, tmax);
//entry point
float t0 = max(max(tnear.x, 0.0f), max(tnear.y, tnear.z));
//exit point
float t1 = min(tfar.x, min(tfar.y, tfar.z));
bool intersects = (t1 > 0.0f) && (t0 < t1);
/*
Trace between intersection points
*/
vec3 start = origin.xyz + (normalize(dir) * t0);
vec3 end = origin.xyz + (normalize(dir) * t1);
const float stepSize = (1.0f / 300);
vec3 step = normalize(dir) * stepSize;
vec3 val = vec3(0,0,0);
float result_gray=0.0f;
float ratio = 1000.0f;
float ratio_r = 1.0f / 1000.0f;
float sum = 0.0f;
int count=0;
if (intersects)
{
vec3 cur = start;
while(distance(cur, end) > stepSize)
{
val = max(val, texture(volume, cur).rrr);
//val = texture(volume, cur).rrr;
cur += step;
sum += val.r*1024;
count++;
}
result_gray=val.r*1024.0f;
//result_gray = sum/count; //* (1.0f / count) * ratio;
float wl_min_gray = wl - 0.5 * ww;
result_gray= (result_gray - wl_min_gray) / ww;
result_gray = clamp(result_gray, 0.0, 1.0);
if(result_gray < 0.000001)
{
discard;
}
/*if(result_gray>0&&result_gray<0.3)color =vec3(1.0,0.0,0.0);
else if(result_gray>0.3&&result_gray<0.6)color =vec3(0.0,0.0,1.0);
else if(result_gray>0.6)color =vec3(1.0,1.0,1.0);*/
color =vec3(result_gray,result_gray,result_gray);
}else
color =vec3(0.0,0.0,0.0);
}