From f6fd3df6f578bb746fec2ea51a123e5a4a843a47 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Mon, 7 Dec 2015 16:57:26 +0000
Subject: [PATCH] Negative colour map implemented in GL14.

---
 fsl/fsleyes/gl/gl14/glvolume_frag.prog | 38 ++++++++++++++++++++++++--
 fsl/fsleyes/gl/gl14/glvolume_funcs.py  |  3 +-
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/fsl/fsleyes/gl/gl14/glvolume_frag.prog b/fsl/fsleyes/gl/gl14/glvolume_frag.prog
index 387efb55e..3c43b5688 100644
--- a/fsl/fsleyes/gl/gl14/glvolume_frag.prog
+++ b/fsl/fsleyes/gl/gl14/glvolume_frag.prog
@@ -59,6 +59,8 @@ TEMP  voxClipHi;
 TEMP  voxValue;
 TEMP  posColour;
 TEMP  negColour;
+TEMP  useNegCmap;
+TEMP  negVoxValue;
 
 PARAM imageShape = program.local[4];
 PARAM clipping   = program.local[5];
@@ -80,9 +82,36 @@ MOV voxCoord, fragment.texcoord[1];
 
 # look up image voxel value
 # from 3D image texture
-TEX voxValue, fragment.texcoord[0], texture[0], 3D;
-
+TEX voxValue.x, fragment.texcoord[0], texture[0], 3D;
 
+# Figure out which negative colour map
+# should be used for this fragment.
+# We use the negative colour map ...
+# 
+# if the voxel value is less than
+# texZero (the display range centre),
+SLT useNegCmap.x, voxValue.x, negCmap.y;
+
+# and the negative colour map is active.
+# The useNegCmap vector will be negative
+# if both of these conditions are true,
+# positive otherwise.
+MUL useNegCmap.x, useNegCmap.x, negCmap.x;
+SUB useNegCmap.x, useNegCmap.x, 0.5;
+MUL useNegCmap.x, useNegCmap.x, -1;
+
+# If using the negative colour map,
+# we need to flip the voxel value about
+# the display range centre.
+
+# Calculate the inverted voxel value
+ADD negVoxValue.x, negCmap.y,     negCmap.y;
+SUB negVoxValue.x, negVoxValue.x, voxValue.x;
+
+# If we're using the negative colour
+# map, replace the original voxel
+# value with the inverted one.
+CMP voxValue.x, useNegCmap.x, negVoxValue.x, voxValue.x;
 
 # Test the low clipping range
 SUB voxClipLo, voxValue.x, clipping.x;
@@ -117,6 +146,9 @@ MAD voxValue, voxValue, voxValXform[0].x, voxValXform[3].x;
 TEX posColour, voxValue.x, texture[1], 1D;
 TEX negColour, voxValue.x, texture[2], 1D;
 
-MOV result.color, posColour;
+# useNegCmap is negative if the
+# negative colour map should be
+# used, positive otherwise.
+CMP result.color, useNegCmap.x, negColour, posColour;
 
 END
diff --git a/fsl/fsleyes/gl/gl14/glvolume_funcs.py b/fsl/fsleyes/gl/gl14/glvolume_funcs.py
index d3462b447..9fa8aa1b5 100644
--- a/fsl/fsleyes/gl/gl14/glvolume_funcs.py
+++ b/fsl/fsleyes/gl/gl14/glvolume_funcs.py
@@ -93,11 +93,10 @@ def updateShaderState(self):
 
     # And the clipping range, normalised
     # to the image texture value range
-    xform = self.imageTexture.invVoxValXform
-    
     invClip    = 1 if opts.invertClipping     else -1
     useNegCmap = 1 if opts.enableNegativeCmap else  0
     
+    xform   = self.imageTexture.invVoxValXform
     clipLo  = opts.clippingRange[0] * xform[0, 0] + xform[3, 0]
     clipHi  = opts.clippingRange[1] * xform[0, 0] + xform[3, 0]
     texZero = 0.0                   * xform[0, 0] + xform[3, 0]
-- 
GitLab