00001 !!ARBfp1.0
00002 OPTION NV_fragment_program2;
00003
00004 # Copyright (c) 2005 Institute for Visualization and Interactive
00005 # Systems, University of Stuttgart, Germany
00006 #
00007 # This source code is distributed as part of the single-pass volume
00008 # rendering project. Details about this project can be found on the
00009 # project web page at http://www.vis.uni-stuttgart.de/eng/research/
00010 # fields/current/spvolren. This file may be distributed, modified,
00011 # and used free of charge as long as this copyright notice is
00012 # included in its original form. Commercial use is strictly
00013 # prohibited.
00014 #
00015 # Filename: volume_sm3.fp
00016
00017 #! VOLUME = 0
00018 #! TRANSFERFUNCTION = 1
00019 #! BACKGROUND = 2
00020
00021 # r = step
00022 # g = gradient scale
00023 # b = gradient offset
00024 # a = z-value of background plane
00025 PARAM params = program.local[0];
00026
00027 # r = texture coordinate scale
00028 # g = number of iterations
00029 # b = isovalue of isosurface
00030 PARAM params2 = program.local[1];
00031 PARAM center = program.local[2];
00032 PARAM texMax = program.local[3];
00033
00034 PARAM scaleFactors = program.local[5];
00035
00036 TEMP geomDir;
00037 TEMP geomPos;
00038 TEMP texblen;
00039 TEMP diffVec;
00040 TEMP scalar;
00041 TEMP camera;
00042 TEMP normal;
00043 TEMP temp1;
00044 TEMP temp2;
00045 TEMP temp;
00046 TEMP pos;
00047 TEMP src;
00048 TEMP dst;
00049 TEMP dir;
00050 TEMP tex;
00051
00052 # Compute the ray's starting point
00053 MOV geomPos, fragment.texcoord[0];
00054 MUL pos, geomPos, scaleFactors;
00055 MOV pos.a, 0.0;
00056
00057 # Compute the camera position by translating the origin to the center of the
00058 # volume
00059 MOV camera, state.matrix.modelview.invtrans.row[3];
00060
00061 # Compute the ray direction
00062 SUB geomDir, geomPos, camera;
00063
00064 # Normalize the direction (done manually instead of with NRM to improve
00065 # accuracy)
00066 DP3 geomDir.w, geomDir, geomDir;
00067 RSQ geomDir.w, geomDir.w;
00068 MUL geomDir, geomDir, geomDir.w;
00069 MOV geomDir.w, 0.0;
00070
00071 MUL dir, geomDir, scaleFactors;
00072
00073 # Initialize scalar value
00074 # RGB = gradient, alpha = scalar value
00075 TXL scalar, pos, texture[0], 3D;
00076 MOV scalar.g, scalar.a;
00077 MOV scalar.a, 0.0;
00078
00079 # Initialize destination color
00080 MOV dst, 0.0;
00081
00082 # Move one step forward
00083 MAD pos, dir, params.r, pos;
00084
00085 REP params2.g;
00086 REP params2.g;
00087
00088 # Lookup new scalar value
00089 TXL tex, pos, texture[0], 3D;
00090 MOV scalar.r, tex.a;
00091
00092 # Lookup color in pre-int texture
00093 TXL src, scalar, texture[1], 2D;
00094
00095 # TODO Early ray termination (no gain of speed so far)
00096 #SUBC texblen.r, 1.0, dst.a;
00097 #BRK (LE.x);
00098
00099 # Perform blending
00100 SUB texblen.r, 1.0, dst.a;
00101 MAD_SAT dst, src, texblen.r, dst;
00102
00103 # Move one step forward
00104 MAD pos, dir, params.r, pos;
00105
00106 # Terminate loop if outside volume
00107 SGE temp1, pos, 0.0;
00108 SLE temp2, pos, texMax;
00109 DP3 temp1.r, temp1, temp2;
00110 SEQC temp1.r, temp1.r, 3.0;
00111 BRK (EQ.x);
00112
00113 # Save current scalar value
00114 MOV scalar.g, scalar.r;
00115
00116 ENDREP;
00117
00118 BRK (LE.x);
00119
00120 ENDREP;
00121
00122 # Compute the normal of the background plane (this is just the negative view
00123 # direction which initially is (0, 0, -1))
00124 MOV normal, state.matrix.modelview.row[2];
00125
00126 # Compute the plane constant (we want the plane to be located in the volume
00127 # center)
00128 DP3 temp1.r, normal, center;
00129
00130 # Move the plane behind the volume: d' = <n,(x - l n)> = <n, x> - l <n, n>;
00131 # l = 0.71 is chosen since it is greater than half the cube diagonal
00132 DP3 temp1.g, normal, normal;
00133 MAD temp1.r, temp1.g, -.71, temp1.r;
00134
00135 # Compute ray parameter
00136 DP3 temp1.g, normal, geomPos;
00137 SUB temp1.g, temp1.r, temp1.g;
00138 DP3 temp1.b, normal, geomDir;
00139 DIV temp.r, temp1.g, temp1.b;
00140
00141 # Compute ray/plane intersection
00142 MAD temp.rgb, temp.r, geomDir, geomPos;
00143
00144 # Compute the difference vector
00145 SUB diffVec, temp, center;
00146
00147 # Compute the texture coordinates
00148 DP3 temp.r, diffVec, state.matrix.modelview.row[0];
00149 DP3 temp.g, diffVec, state.matrix.modelview.row[1];
00150 MUL temp.rg, temp, params2.r;
00151
00152 # Center background image
00153 ADD temp.rg, temp, .5;
00154
00155 # Look up the texel value
00156 TEX temp.rgb, temp, texture[2], 2D;
00157 MOV temp.a, 1.0;
00158
00159 # Blend the background pixel
00160 SUB texblen, 1.0, dst.a;
00161 MAD dst, temp, texblen.x, dst;
00162
00163 # Write the output color
00164 MOV result.color, dst;
00165
00166 END