summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/engine/shaders/texture3dslice.frag
diff options
context:
space:
mode:
authorMiikka Heikkinen <miikka.heikkinen@digia.com>2014-09-09 11:30:15 +0300
committerMiikka Heikkinen <miikka.heikkinen@digia.com>2014-09-10 12:30:54 +0300
commitddb9be979d93b7e17f1067dc6056de54d9828b29 (patch)
treedf612c56e4a804481297ae55b29f0ee379c5f8ab /src/datavisualization/engine/shaders/texture3dslice.frag
parentbb30ea555c71604de9a2bc5096fa35c9532b26bd (diff)
Limit volume to axis ranges
The volume object that would go partially outside axis ranges is scale and repositioned so that it only renders the portion that is inside the axis ranges. Change-Id: I792494e437998ba6276f58fab645767276c1476d Reviewed-by: Mika Salmela <mika.salmela@digia.com>
Diffstat (limited to 'src/datavisualization/engine/shaders/texture3dslice.frag')
-rw-r--r--src/datavisualization/engine/shaders/texture3dslice.frag97
1 files changed, 55 insertions, 42 deletions
diff --git a/src/datavisualization/engine/shaders/texture3dslice.frag b/src/datavisualization/engine/shaders/texture3dslice.frag
index 00584744..c555af98 100644
--- a/src/datavisualization/engine/shaders/texture3dslice.frag
+++ b/src/datavisualization/engine/shaders/texture3dslice.frag
@@ -1,14 +1,16 @@
#version 120
varying highp vec3 pos;
+varying highp vec3 rayDir;
uniform highp sampler3D textureSampler;
-uniform highp vec3 cameraPositionRelativeToModel;
uniform highp vec3 volumeSliceIndices;
uniform highp vec4 colorIndex[256];
uniform highp int color8Bit;
uniform highp float alphaMultiplier;
uniform highp int preserveOpacity;
+uniform highp vec3 minBounds;
+uniform highp vec3 maxBounds;
const highp vec3 xPlaneNormal = vec3(1.0, 0, 0);
const highp vec3 yPlaneNormal = vec3(0, 1.0, 0);
@@ -16,21 +18,17 @@ const highp vec3 zPlaneNormal = vec3(0, 0, 1.0);
void main() {
// Find out where ray intersects the slice planes
- highp vec3 rayDir = -(cameraPositionRelativeToModel - pos);
- rayDir = normalize(rayDir);
+ vec3 normRayDir = normalize(rayDir);
highp vec3 rayStart = pos;
- // Flip Y and Z so QImage bits work directly for texture and first image is in the front
- rayStart.yz = -rayStart.yz;
- rayDir.yz = -rayDir.yz;
highp float minT = 2.0f;
- if (rayDir.x != 0.0 && rayDir.y != 0.0 && rayDir.z != 0.0) {
+ if (normRayDir.x != 0.0 && normRayDir.y != 0.0 && normRayDir.z != 0.0) {
highp vec3 boxBounds = vec3(1.0, 1.0, 1.0);
- highp vec3 invRayDir = 1.0 / rayDir;
- if (rayDir.x < 0)
+ highp vec3 invRayDir = 1.0 / normRayDir;
+ if (normRayDir.x < 0)
boxBounds.x = -1.0;
- if (rayDir.y < 0)
+ if (normRayDir.y < 0)
boxBounds.y = -1.0;
- if (rayDir.z < 0)
+ if (normRayDir.z < 0)
boxBounds.z = -1.0;
highp vec3 t = (boxBounds - rayStart) * invRayDir;
minT = min(t.x, min(t.y, t.z));
@@ -43,12 +41,12 @@ void main() {
highp float secondD = firstD;
highp float thirdD = firstD;
if (volumeSliceIndices.x >= -1.0) {
- highp float dx = dot(xPoint - rayStart, xPlaneNormal) / dot(rayDir, xPlaneNormal);
+ highp float dx = dot(xPoint - rayStart, xPlaneNormal) / dot(normRayDir, xPlaneNormal);
if (dx >= 0.0 && dx <= minT)
firstD = min(dx, firstD);
}
if (volumeSliceIndices.y >= -1.0) {
- highp float dy = dot(yPoint - rayStart, yPlaneNormal) / dot(rayDir, yPlaneNormal);
+ highp float dy = dot(yPoint - rayStart, yPlaneNormal) / dot(normRayDir, yPlaneNormal);
if (dy >= 0.0 && dy <= minT) {
if (dy < firstD) {
secondD = firstD;
@@ -59,7 +57,7 @@ void main() {
}
}
if (volumeSliceIndices.z >= -1.0) {
- highp float dz = dot(zPoint - rayStart, zPlaneNormal) / dot(rayDir, zPlaneNormal);
+ highp float dz = dot(zPoint - rayStart, zPlaneNormal) / dot(normRayDir, zPlaneNormal);
if (dz >= 0.0) {
if (dz < firstD && dz <= minT) {
thirdD = secondD;
@@ -83,43 +81,35 @@ void main() {
// Convert intersection to texture coords
if (firstD <= minT) {
- highp vec3 firstTex = rayStart + rayDir * firstD;
- firstTex = 0.5 * (firstTex + 1.0);
- curColor = texture3D(textureSampler, firstTex);
- if (color8Bit != 0)
- curColor = colorIndex[int(curColor.r * 255.0)];
-
- if (curColor.a > 0.0) {
- curAlpha = curColor.a;
- if (curColor.a == 1.0 && preserveOpacity != 0)
- curAlpha = 1.0;
- else
- curAlpha = clamp(curColor.a * alphaMultiplier, 0.0, 1.0);
- destColor.rgb = curColor.rgb * curAlpha;
- totalAlpha = curAlpha;
- }
- if (secondD <= minT && totalAlpha < 1.0) {
- highp vec3 secondTex = rayStart + rayDir * secondD;
- secondTex = 0.5 * (secondTex + 1.0);
- curColor = texture3D(textureSampler, secondTex);
+ highp vec3 texelVec = rayStart + normRayDir * firstD;
+ if (clamp(texelVec.x, minBounds.x, maxBounds.x) == texelVec.x
+ && clamp(texelVec.y, maxBounds.y, minBounds.y) == texelVec.y
+ && clamp(texelVec.z, maxBounds.z, minBounds.z) == texelVec.z) {
+ texelVec = 0.5 * (texelVec + 1.0);
+ curColor = texture3D(textureSampler, texelVec);
if (color8Bit != 0)
curColor = colorIndex[int(curColor.r * 255.0)];
+
if (curColor.a > 0.0) {
+ curAlpha = curColor.a;
if (curColor.a == 1.0 && preserveOpacity != 0)
curAlpha = 1.0;
else
curAlpha = clamp(curColor.a * alphaMultiplier, 0.0, 1.0);
- curRgb = curColor.rgb * curAlpha * (1.0 - totalAlpha);
- destColor.rgb += curRgb;
- totalAlpha += curAlpha;
+ destColor.rgb = curColor.rgb * curAlpha;
+ totalAlpha = curAlpha;
}
- if (thirdD <= minT && totalAlpha < 1.0) {
- highp vec3 thirdTex = rayStart + rayDir * thirdD;
- thirdTex = 0.5 * (thirdTex + 1.0);
- curColor = texture3D(textureSampler, thirdTex);
+ }
+ if (secondD <= minT && totalAlpha < 1.0) {
+ texelVec = rayStart + normRayDir * secondD;
+ if (clamp(texelVec.x, minBounds.x, maxBounds.x) == texelVec.x
+ && clamp(texelVec.y, maxBounds.y, minBounds.y) == texelVec.y
+ && clamp(texelVec.z, maxBounds.z, minBounds.z) == texelVec.z) {
+ texelVec = 0.5 * (texelVec + 1.0);
+ curColor = texture3D(textureSampler, texelVec);
+ if (color8Bit != 0)
+ curColor = colorIndex[int(curColor.r * 255.0)];
if (curColor.a > 0.0) {
- if (color8Bit != 0)
- curColor = colorIndex[int(curColor.r * 255.0)];
if (curColor.a == 1.0 && preserveOpacity != 0)
curAlpha = 1.0;
else
@@ -129,9 +119,32 @@ void main() {
totalAlpha += curAlpha;
}
}
+ if (thirdD <= minT && totalAlpha < 1.0) {
+ texelVec = rayStart + normRayDir * thirdD;
+ if (clamp(texelVec.x, minBounds.x, maxBounds.x) == texelVec.x
+ && clamp(texelVec.y, maxBounds.y, minBounds.y) == texelVec.y
+ && clamp(texelVec.z, maxBounds.z, minBounds.z) == texelVec.z) {
+ texelVec = 0.5 * (texelVec + 1.0);
+ curColor = texture3D(textureSampler, texelVec);
+ if (curColor.a > 0.0) {
+ if (color8Bit != 0)
+ curColor = colorIndex[int(curColor.r * 255.0)];
+ if (curColor.a == 1.0 && preserveOpacity != 0)
+ curAlpha = 1.0;
+ else
+ curAlpha = clamp(curColor.a * alphaMultiplier, 0.0, 1.0);
+ curRgb = curColor.rgb * curAlpha * (1.0 - totalAlpha);
+ destColor.rgb += curRgb;
+ totalAlpha += curAlpha;
+ }
+ }
+ }
}
}
+ if (totalAlpha == 0.0)
+ discard;
+
// Brighten up the final color if there is some transparency left
if (totalAlpha > 0.0 && totalAlpha < 1.0)
destColor *= 1.0 / totalAlpha;