From 2b991114d874b38df44c4b2c90dce50674904cc3 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Thu, 17 Dec 2015 16:27:35 +0000
Subject: [PATCH] Interrupted vector opts cmap to add the ability to VolumeOpts
 to clip by another image - this will be needed by VectorOpts cmap, if I want
 to re-use the GLVolume fragment shader.

---
 fsl/fsleyes/gl/gl21/glvolume_frag.glsl | 32 ++++++++++++++++++++++----
 fsl/fsleyes/gl/gl21/glvolume_funcs.py  | 23 ++++++++++--------
 2 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/fsl/fsleyes/gl/gl21/glvolume_frag.glsl b/fsl/fsleyes/gl/gl21/glvolume_frag.glsl
index 12e0684fe..8895507c8 100644
--- a/fsl/fsleyes/gl/gl21/glvolume_frag.glsl
+++ b/fsl/fsleyes/gl/gl21/glvolume_frag.glsl
@@ -9,10 +9,15 @@
 #pragma include test_in_bounds.glsl
 
 /*
- * image data texture.
+ * image data texture, used for colouring.
  */
 uniform sampler3D imageTexture;
 
+/*
+ * image data texture, used for clipping.
+ */
+uniform sampler3D clipTexture;
+
 /*
  * Texture containing the colour map.
  */
@@ -23,6 +28,14 @@ uniform sampler1D colourTexture;
  */
 uniform sampler1D negColourTexture;
 
+/*
+ * Flag which tells the shader whether 
+ * the image and clip textures are actually
+ * the same - if they are, set this to true
+ * to avoid an extra texture lookup.
+ */
+uniform bool imageIsClip;
+
 /*
  * Flag which determines whether to 
  * use the negative colour map.
@@ -30,7 +43,7 @@ uniform sampler1D negColourTexture;
 uniform bool useNegCmap;
 
 /*
- * Shape of the imageTexture.
+ * Shape of the imageTexture/clipTexture.
  */
 uniform vec3 imageShape;
 
@@ -86,6 +99,7 @@ varying vec3 fragTexCoord;
 void main(void) {
 
     float voxValue;
+    float clipValue;
     vec4  normVoxValue;
     bool  negCmap  = false;
     vec3  voxCoord = fragVoxCoord;
@@ -109,6 +123,15 @@ void main(void) {
     else           voxValue = texture3D(    imageTexture,
                                             fragTexCoord).r;
 
+    
+    if      (imageIsClip) clipValue = voxValue;
+    else if (useSpline)   clipValue = spline_interp(clipTexture,
+                                                    fragTexCoord,
+                                                    imageShape,
+                                                    0);
+    else                  clipValue = texture3D(    clipTexture,
+                                                    fragTexCoord).r; 
+
     /*
      * If we are using a negative colour map, 
      * and the voxel value is below the negative 
@@ -122,11 +145,12 @@ void main(void) {
         voxValue = texZero + (texZero - voxValue);
     }
 
+
     /*
      * Clip out of range voxel values
      */
-    if ((!invertClip && (voxValue <  clipLow || voxValue >  clipHigh)) ||
-        ( invertClip && (voxValue >= clipLow && voxValue <= clipHigh))) {
+    if ((!invertClip && (clipValue <  clipLow || clipValue >  clipHigh)) ||
+        ( invertClip && (clipValue >= clipLow && clipValue <= clipHigh))) {
       
         gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
         return;
diff --git a/fsl/fsleyes/gl/gl21/glvolume_funcs.py b/fsl/fsleyes/gl/gl21/glvolume_funcs.py
index 95b2c39a4..551707abe 100644
--- a/fsl/fsleyes/gl/gl21/glvolume_funcs.py
+++ b/fsl/fsleyes/gl/gl21/glvolume_funcs.py
@@ -59,11 +59,12 @@ def compileShaders(self):
 
     shaderVars = {}
 
-    vertAtts     = ['vertex',       'voxCoord',      'texCoord']
-    fragUniforms = ['imageTexture', 'colourTexture', 'negColourTexture',
-                    'useNegCmap',   'imageShape',    'useSpline',
-                    'voxValXform',  'clipLow',       'clipHigh',
-                    'texZero',      'invertClip']
+    vertAtts     = ['vertex',           'voxCoord',    'texCoord']
+    fragUniforms = ['imageTexture',     'clipTexture', 'colourTexture',
+                    'negColourTexture', 'imageIsClip', 'useNegCmap',
+                    'imageShape',       'useSpline',   'voxValXform',
+                    'clipLow',          'clipHigh',    'texZero',
+                    'invertClip']
 
     for va in vertAtts:
         shaderVars[va] = gl.glGetAttribLocation(self.shaders, va)
@@ -111,16 +112,18 @@ def updateShaderState(self):
     gl.glUniform3fv(svars['imageShape'], 1, np.array(self.image.shape,
                                                      dtype=np.float32))
 
-    gl.glUniform1f(svars['clipLow'],    clipLow)
-    gl.glUniform1f(svars['clipHigh'],   clipHigh)
-    gl.glUniform1f(svars['texZero'],    texZero)
-    gl.glUniform1f(svars['invertClip'], opts.invertClipping)
-    gl.glUniform1f(svars['useNegCmap'], opts.useNegativeCmap)
+    gl.glUniform1f(svars['clipLow'],     clipLow)
+    gl.glUniform1f(svars['clipHigh'],    clipHigh)
+    gl.glUniform1f(svars['texZero'],     texZero)
+    gl.glUniform1f(svars['invertClip'],  opts.invertClipping)
+    gl.glUniform1f(svars['useNegCmap'],  opts.useNegativeCmap)
+    gl.glUniform1f(svars['imageIsClip'], 1)
  
     gl.glUniformMatrix4fv(svars['voxValXform'], 1, False, vvx)
 
     # Set up the colour and image textures
     gl.glUniform1i(svars['imageTexture'],     0)
+    gl.glUniform1i(svars['clipTexture'],      0)
     gl.glUniform1i(svars['colourTexture'],    1)
     gl.glUniform1i(svars['negColourTexture'], 2)
 
-- 
GitLab