VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
VolumeShader.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9 
10 namespace visLU.Effects
11 {
16  class VolumeShader: Effect
17  {
18 
19  #region variables
20 
21  GraphicsDevice device;
22  Effect effect;
23 
24  private EffectParameter worldViewProjection;
25  public EffectParameter world;
26  public EffectParameter cameraPosition;
27  private EffectParameter frontFaces;
28  private EffectParameter backFaces;
29  private EffectParameter transferValues;
30  private EffectParameter transferValuesAlpha;
31  private EffectParameter CVDtransferValues;
32  private EffectParameter volumeTexture;
33  private EffectParameter gradientTexture;
34  private EffectParameter directLightPosition;
35  private EffectParameter stepSize;
36  private EffectParameter baseSampleDist;
37  private EffectParameter sampleDist;
38  private EffectParameter iterations;
39  private EffectParameter side;
40  private EffectParameter scaleFactor;
41  private EffectParameter texSize; //correct the texture coordinates
42  private EffectParameter slice;
43  // boolean parameters
44  private EffectParameter frontToBack;
45  private EffectParameter shading;
46  private EffectParameter useAlpha;
47  private EffectParameter useTransferColors;
48  private EffectParameter useCVDTransferColors;
49 
50  // RGB to L*a*b*
51  private EffectParameter ep_RGBtoXYZmatrix;
52  private EffectParameter ep_rotation;
53 
54  // L*a*b* to RGB
55  private EffectParameter ep_rotationBack;
56  private EffectParameter ep_XYZtoRGBmatrix;
57 
58  private Texture3D volume;
59  private Texture3D gradients;
60  private Texture2D front;
61  private Texture2D back;
62  private Texture2D transfer;
63  private Texture2D cvdTransfer;
64  private Texture2D alpha;
65 
66  private Matrix rgbToXYZ;
67  private Matrix xyzToRGB;
68 
69  #endregion
70 
71  #region properties
72 
76  public Texture3D VolumeTexture
77  {
78  get { return volume; }
79  set { volume = value; }
80  }
81 
85  public Texture3D GradientTexture
86  {
87  get { return gradients; }
88  set { gradients = value; }
89  }
90 
94  public Texture2D FrontFacesTexture
95  {
96  get { return front; }
97  set { front = value; Console.WriteLine("frontfacetexture set"); }
98  }
99 
103  public Texture2D BackFacesTexture
104  {
105  get { return back; }
106  set { back = value; }
107  }
108 
112  public Texture2D TransferTexture
113  {
114  get { return transfer; }
115  set { transfer = value; }
116  }
117 
121  public Texture2D TransferTextureAlpha
122  {
123  get { return alpha; }
124  set { alpha = value; }
125  }
126 
130  public Texture2D CVDTransferTexture
131  {
132  get { return cvdTransfer; }
133  set { cvdTransfer = value; }
134  }
135 
136  #endregion
137 
138 
145  public VolumeShader(GraphicsDevice _device, Effect _effect, bool CVDon)
146  : base(_effect)
147  {
148  if (!CVDon)
149  {
150  #region create normal volume shader
151 
152  Console.WriteLine("creating shader");
153  device = _device;
154  effect = _effect;
155 
156  // WorldViewProjection
157  worldViewProjection = this.Parameters["myWorldViewProjectionMatrix"];
158 
159  // WorldProjection
160  world = this.Parameters["myWorldMatrix"];
161 
162  // Position of Camera
163  cameraPosition = this.Parameters["CameraPosition"];
164 
165  // Texture for FrontFaces
166  frontFaces = this.Parameters["Front"];
167 
168  // Texture for Backfaces
169  backFaces = this.Parameters["Back"];
170 
171  // Texture for Transferfunction
172  transferValues = this.Parameters["Transfer"];
173 
174  // Texture for Transferfunction
175  transferValuesAlpha = this.Parameters["TransferAlpha"];
176 
177  // Texture for the CVD Transferfunction
178  CVDtransferValues = this.Parameters["CVDTransfer"];
179 
180  // 3D Texture Volume
181  volumeTexture = this.Parameters["Volume"];
182 
183  // 3D Texture Volume
184  gradientTexture = this.Parameters["Gradients"];
185 
186  //direct light position
187  directLightPosition = this.Parameters["DirectLightVector"];
188 
189  // stepSize
190  stepSize = this.Parameters["StepSize"];
191 
192  //base sample distance for the dataset
193  baseSampleDist = this.Parameters["baseSampleDist"];
194 
195  //current sample distance tha we use
196  sampleDist = this.Parameters["sampleDist"];
197 
198  // number of iterations to be done
199  iterations = this.Parameters["Iterations"];
200 
201  // side TODO: find better name
202  side = this.Parameters["Side"];
203 
204  // scaleFactor
205  scaleFactor = this.Parameters["ScaleFactor"];
206 
207  //texture size
208  texSize = this.Parameters["textureSize"];
209 
210  // slice
211  slice = this.Parameters["Slice"];
212 
213  frontToBack = this.Parameters["frontToBack"];
214 
215  shading = this.Parameters["shading"];
216 
217  useAlpha = this.Parameters["useAlpha"];
218 
219  useTransferColors = this.Parameters["useTransferColors"];
220 
221  useCVDTransferColors = this.Parameters["useCVDTransferColors"];
222 
223  Console.WriteLine("SHADER CREATED");
224  #endregion
225  }
226  else
227  {
228  #region create CVD volume shader
229 
230  Console.WriteLine("creating shader");
231  device = _device;
232  effect = _effect;
233 
234  // WorldViewProjection
235  worldViewProjection = this.Parameters["myWorldViewProjectionMatrix"];
236 
237  // WorldProjection
238  world = this.Parameters["myWorldMatrix"];
239 
240  // Position of Camera
241  cameraPosition = this.Parameters["CameraPosition"];
242 
243  // Texture for FrontFaces
244  frontFaces = this.Parameters["Front"];
245 
246  // Texture for Backfaces
247  backFaces = this.Parameters["Back"];
248 
249  // Texture for Transferfunction
250  transferValuesAlpha = this.Parameters["TransferAlpha"];
251 
252  // Texture for the CVD Transferfunction
253  CVDtransferValues = this.Parameters["CVDTransfer"];
254 
255  // 3D Texture Volume
256  volumeTexture = this.Parameters["Volume"];
257 
258  // 3D Texture Volume
259  gradientTexture = this.Parameters["Gradients"];
260 
261  //direct light position
262  directLightPosition = this.Parameters["DirectLightVector"];
263 
264  // stepSize
265  stepSize = this.Parameters["StepSize"];
266 
267  //base sample distance for the dataset
268  baseSampleDist = this.Parameters["baseSampleDist"];
269 
270  //current sample distance tha we use
271  sampleDist = this.Parameters["sampleDist"];
272 
273  // number of iterations to be done
274  iterations = this.Parameters["Iterations"];
275 
276  // scaleFactor
277  scaleFactor = this.Parameters["ScaleFactor"];
278 
279  //texture size
280  texSize = this.Parameters["textureSize"];
281 
282  frontToBack = this.Parameters["frontToBack"];
283 
284  shading = this.Parameters["shading"];
285 
286  useAlpha = this.Parameters["useAlpha"];
287 
288  useCVDTransferColors = this.Parameters["useCVDTransferColors"];
289 
290  // RGB to L*a*b*
291 
292  //Adobe RGB (1998) D50:
293  rgbToXYZ = new Matrix(0.6097559f, 0.2052401f, 0.1492240f, 0.0f,
294  0.3111242f, 0.6256560f, 0.0632197f, 0.0f,
295  0.0194811f, 0.0608902f, 0.7448387f, 0.0f,
296  0.0f, 0.0f, 0.0f, 1.0f);
297 
298  ep_RGBtoXYZmatrix = this.Parameters["mRGBtoXYZ"];
299  ep_rotation = this.Parameters["rotmat"];
300 
301  // L*a*b* to RGB
302 
303  //Adobe RGB (1998) D50:
304  xyzToRGB = new Matrix(1.9624274f, -0.6105343f, -0.3413404f, 0.0f,
305  -0.9787684f, 1.9161415f, 0.0334540f, 0.0f,
306  0.0286869f, -0.1406752f, 1.3487655f, 0.0f,
307  0.0f, 0.0f, 0.0f, 1.0f);
308 
309  ep_rotationBack = this.Parameters["rotmatBack"];
310  ep_XYZtoRGBmatrix = this.Parameters["mXYZtoRGB"];
311 
312  Console.WriteLine("CVD SHADER CREATED");
313 
314  #endregion
315  }
316  }
317 
318 
319  #region SetEffectParameter
320 
321 
322  //set by the engine:
328  public void SetEffectParameter(Matrix _worldViewProjection, Matrix _world)
329  {
330  this.worldViewProjection.SetValue(_worldViewProjection);
331  this.world.SetValue(_world);
332  }
333 
338  public void SetEffectParameter(Vector3 _cameraPosition)
339  {
340  this.cameraPosition.SetValue(_cameraPosition);
341  }
342 
343  public void SetEffectParameter_CVD(Texture3D _volumeTexture,
344  Texture3D _gradientTexture,
345  Vector3 _stepSize,
346  int _iterations,
347  Vector4 _scaleFactor,
348  Vector2 _texSize,
349  Matrix _rotateToLMS,
350  Matrix _rotateFromLMS)
351  {
352  this.volumeTexture.SetValue(_volumeTexture);
353  this.gradientTexture.SetValue(_gradientTexture);
354  this.stepSize.SetValue(_stepSize);
355  this.iterations.SetValue(_iterations);
356  this.scaleFactor.SetValue(_scaleFactor);
357  this.texSize.SetValue(_texSize);
358 
359  this.frontToBack.SetValue(GameProperties.Instance.enableFrontToBackBlend);
360  this.shading.SetValue(GameProperties.Instance.enableShading);
361  this.useAlpha.SetValue(GameProperties.Instance.enableTransferAlphaValues);
362  this.useCVDTransferColors.SetValue(GameProperties.Instance.enableCVDView && GameProperties.Instance.cvdView);
363  this.directLightPosition.SetValue(GameProperties.Instance.directLightPos);
364 
365  // RGB to L*a*b*
366  this.ep_RGBtoXYZmatrix.SetValue(rgbToXYZ);
367  this.ep_rotation.SetValue(_rotateToLMS);
368 
369  // L*a*b* to RGB
370  this.ep_rotationBack.SetValue(_rotateFromLMS);
371  this.ep_XYZtoRGBmatrix.SetValue(xyzToRGB);
372 
373  // TODO: set these to shader:
374  if (front != null)
375  {
376  this.frontFaces.SetValue(front);
377  }
378  if (back != null)
379  {
380  this.backFaces.SetValue(back);
381  }
382  if (alpha != null)
383  {
384  this.transferValuesAlpha.SetValue(alpha);
385  }
386  if (cvdTransfer != null)
387  {
388  this.CVDtransferValues.SetValue(cvdTransfer);
389  }
390 
391  }
392 
393  //set by the data itself
403  public void SetEffectParameter(Texture3D _volumeTexture,
404  Texture3D _gradientTexture,
405  Vector3 _stepSize,
406  int _iterations,
407  int _side,
408  Vector4 _scaleFactor,
409  float _slice)
410  {
411  this.volumeTexture.SetValue(_volumeTexture);
412  this.gradientTexture.SetValue(_gradientTexture);
413  this.stepSize.SetValue(_stepSize);
414  this.iterations.SetValue(_iterations);
415  this.side.SetValue(_side);
416  this.scaleFactor.SetValue(_scaleFactor);
417  this.frontToBack.SetValue(GameProperties.Instance.enableFrontToBackBlend);
418  this.shading.SetValue(GameProperties.Instance.enableShading);
419  this.useAlpha.SetValue(GameProperties.Instance.enableTransferAlphaValues);
420  this.useTransferColors.SetValue(GameProperties.Instance.enableTransferFunction);
421  this.useCVDTransferColors.SetValue(GameProperties.Instance.enableCVDView && GameProperties.Instance.cvdView);
422 
423  //device.SetRenderTarget(null);
424  if (front != null)
425  {
426  this.frontFaces.SetValue(front);
427  }
428  if (back != null)
429  {
430  this.backFaces.SetValue(back);
431  }
432  if (transfer != null)
433  {
434  this.transferValues.SetValue(transfer);
435  }
436  if (alpha != null)
437  {
438  this.transferValuesAlpha.SetValue(alpha);
439  }
440  if (cvdTransfer != null)
441  {
442  this.CVDtransferValues.SetValue(cvdTransfer);
443  }
444  this.slice.SetValue(_slice);
445  }
446 
461  public void SetEffectParameter(Texture3D _volumeTexture,
462  Texture3D _gradientTexture,
463  Vector3 _directLightPosition,
464  Vector3 _stepSize,
465  float _baseSampleDist,
466  float _sampleDist,
467  int _iterations,
468  int _side,
469  Vector4 _scaleFactor,
470  Vector2 _texSize,
471  float _slice)
472  {
473  this.volumeTexture.SetValue(_volumeTexture);
474  this.gradientTexture.SetValue(_gradientTexture);
475  this.stepSize.SetValue(_stepSize);
476  this.iterations.SetValue(_iterations);
477  this.side.SetValue(_side);
478  this.scaleFactor.SetValue(_scaleFactor);
479  this.frontToBack.SetValue(GameProperties.Instance.enableFrontToBackBlend);
480  this.shading.SetValue(GameProperties.Instance.enableShading);
481  this.useAlpha.SetValue(GameProperties.Instance.enableTransferAlphaValues);
482  this.useTransferColors.SetValue(GameProperties.Instance.enableTransferFunction);
483  this.useCVDTransferColors.SetValue(GameProperties.Instance.enableCVDView && GameProperties.Instance.cvdView);
484 
485  //device.SetRenderTarget(null);
486  if (front != null)
487  {
488  this.frontFaces.SetValue(front);
489  }
490  if (back != null)
491  {
492  this.backFaces.SetValue(back);
493  }
494  if (transfer != null)
495  {
496  this.transferValues.SetValue(transfer);
497  }
498  if (alpha != null)
499  {
500  this.transferValuesAlpha.SetValue(alpha);
501  }
502  if (cvdTransfer != null)
503  {
504  this.CVDtransferValues.SetValue(cvdTransfer);
505  }
506  this.slice.SetValue(_slice);
507 
508  //extended
509  this.directLightPosition.SetValue(_directLightPosition);
510  this.baseSampleDist.SetValue(_baseSampleDist);
511  this.sampleDist.SetValue(_sampleDist);
512  this.texSize.SetValue(_texSize);
513  }
514  #endregion
515  }
516 }