gllinevector_vert.glsl 3.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * OpenGL vertex shader used for rendering GLVector instances in
 * line mode.
 *
 * Author: Paul McCarthy <pauldmccarthy@gmail.com>
 */
#version 120

/*
 * Vector image containing XYZ vector data.
 */
uniform sampler3D vectorTexture;

/*
15
 * Transformations between voxel and
16
17
18
19
20
 * display coordinate systems.
 */
uniform mat4 displayToVoxMat;
uniform mat4 voxToDisplayMat;

Paul McCarthy's avatar
Paul McCarthy committed
21
22
23
24
25
26
27
28
29
/*
 * Matrices which transform from vector
 * texture coordinates to colour/clip/
 * modulate image texture coordinates.
 */
uniform mat4 colourCoordXform;
uniform mat4 clipCoordXform;
uniform mat4 modCoordXform;

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * Transformation matrix which transforms the
 * vector texture data to its original data range.
 */
uniform mat4 voxValXform;

/*
 * Shape of the image texture.
 */
uniform vec3 imageShape;

/*
 * Dimensions of one voxel in the image texture.
 */
uniform vec3 imageDims;

/*
47
 * If true, the vectors are
48
49
50
51
52
53
54
55
56
57
58
 * inverted about the x axis.
 */
uniform bool xFlip;

/*
 * Line vectors are interpreted as directed - each
 * line begins in the centre of its voxel, and extends
 * outwards.
 */
uniform bool directed;

59
60
61
62
/*
 * Scale vector lengths by this amount.
 */
uniform float lengthScale;
63

64
65
66
/*
 * The current vertex on the current line.
 */
67
attribute vec3 voxel;
68
69
70
71
72
73
74
75
76
77

/*
 * Vertex index - the built-in gl_VertexID
 * variable is not available in GLSL 120
 */
attribute float vertexID;


varying vec3 fragVoxCoord;
varying vec3 fragTexCoord;
Paul McCarthy's avatar
Paul McCarthy committed
78
79
80
varying vec3 fragVecTexCoord;
varying vec3 fragClipTexCoord;
varying vec3 fragModTexCoord;
81
82
83
84
varying vec4 fragColourFactor;

void main(void) {

85
86
87
  vec3 texCoord;
  vec3 vector;
  vec3 voxCoord;
88
89
90
91
92
93
94
95

  /*
   * Normalise the voxel coordinates to [0.0, 1.0],
   * so they can be used for texture lookup. Add
   * 0.5 to the voxel coordinates first, to re-centre
   * voxel coordinates from  from [i - 0.5, i + 0.5]
   * to [i, i + 1].
   */
96
  voxCoord = voxel;
97
98
99
100
101
102
103
104
105
106
107
108
  texCoord = (voxCoord + 0.5) / imageShape;

  /*
   * Retrieve the vector values for this voxel
   */
  vector = texture3D(vectorTexture, texCoord).xyz;

  /*
   * Transform the vector values  from their
   * texture range of [0,1] to the original
   * data range
   */
109
110
  vector *= voxValXform[0].x;
  vector += voxValXform[3].x;
111
112

  /* Invert about the x axis if necessary */
113
  if (xFlip) {
114
    vector.x = -vector.x;
115
  }
116

117
118
119
  /*
   * Kill the vector if its length is 0.
   * We have to be tolerant of errors,
120
   * because of the transformation to/
121
   * from the texture data range. This
122
123
   * may end up being too tolerant.
   */
124
  if (length(vector) < 0.0001) {
125
126
127
128
    fragColourFactor = vec4(0, 0, 0, 0);
    return;
  }

129
130
  vector *= lengthScale;

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  /*
   * Vertices are coming in as line pairs - flip
   * every second vertex about the origin
   */
  if (mod(vertexID, 2) == 1) {
    if (directed) vector = vec3(0, 0, 0);
    else          vector = -vector;
  }

  /*
   * Output the final vertex position - offset
   * the voxel coordinates by the vector values,
   * and transform back to display coordinates
   */
  gl_Position = gl_ModelViewProjectionMatrix *
                voxToDisplayMat              *
147
                vec4(voxCoord + vector, 1);
148
149

  fragVoxCoord     = voxCoord;
Paul McCarthy's avatar
Paul McCarthy committed
150
151
152
153
  fragVecTexCoord  = texCoord;
  fragTexCoord     = (colourCoordXform * vec4(texCoord, 1)).xyz;
  fragClipTexCoord = (clipCoordXform   * vec4(texCoord, 1)).xyz;
  fragModTexCoord  = (modCoordXform    * vec4(texCoord, 1)).xyz;
154
155
  fragColourFactor = vec4(1, 1, 1, 1);
}