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: isosurface_spheremap_sm3.fp
00016
00017 #! VOLUME = 0
00018 #! BACKGROUND = 1
00019 #! SPHEREMAP = 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 lightVec;
00039 TEMP texCoord;
00040 TEMP prevPos;
00041 TEMP texblen;
00042 TEMP diffVec;
00043 TEMP halfway;
00044 TEMP eyeVec;
00045 TEMP intens;
00046 TEMP camera;
00047 TEMP normal;
00048 TEMP weight;
00049 TEMP temp1;
00050 TEMP temp2;
00051 TEMP temp;
00052 TEMP grad;
00053 TEMP sign;
00054 TEMP pos;
00055 TEMP src;
00056 TEMP dst;
00057 TEMP dir;
00058 TEMP tex;
00059 TEMP scalAct;
00060 TEMP scalPre;
00061
00062 # Compute the ray's starting point
00063 MOV geomPos, fragment.texcoord[0];
00064 MUL pos, geomPos, scaleFactors;
00065 MOV pos.a, 0.0;
00066
00067 # Compute the camera position by translating the origin to the center of the
00068 # volume
00069 MOV camera, state.matrix.modelview.invtrans.row[3];
00070
00071 # Compute the ray direction
00072 SUB geomDir, geomPos, camera;
00073
00074 # Normalize the direction
00075 DP3 geomDir.w, geomDir, geomDir;
00076 RSQ geomDir.w, geomDir.w;
00077 MUL geomDir, geomDir, geomDir.w;
00078 MOV geomDir.w, 0.0;
00079
00080 MUL dir, geomDir, scaleFactors;
00081
00082 # Initialize the 'previous' intensity
00083 TXL intens, pos, texture[0], 3D;
00084 MOV intens.g, intens.a;
00085
00086 # Initialize texture corrdinates
00087 # texcood.a inherits level of detail
00088 MOV texCoord, 0.0;
00089
00090 # Initialize destination color
00091 MOV dst, 0.0;
00092
00093 REP params2.g;
00094
00095 REP params2.g;
00096 # Lookup scalar and gradient
00097 MOV texCoord.rgb, pos;
00098 TXL tex, texCoord, texture[0], 3D;
00099 MOV intens.r, tex.a;
00100
00101 # Move one step forward
00102 MAD pos, dir, params.r, pos;
00103
00104 # Isosurface if differences with past and present
00105 # scalar values and isovalue have different signs
00106 SUB temp.rg, intens, params2.b;
00107 MULC temp, temp.r, temp.g;
00108 IF LE.x;
00109 # Isosurface found
00110
00111 # Get scalar value from actual position
00112 MAD pos, -dir, params.r, pos;
00113 TXL scalAct, pos, texture[0], 3D;
00114
00115 # Get scalar value from previous position
00116 MAD prevPos, -dir, params.r, pos;
00117 TXL scalPre, prevPos, texture[0], 3D;
00118
00119 SUB temp.r, params2.b, scalPre.a;
00120 SUB temp.g, scalAct.a, scalPre.a;
00121
00122 # Perform linear interpolation to increase accuracy
00123 DIV_SAT temp.b, temp.r, temp.g;
00124 MAD prevPos, -dir, params.r, pos;
00125 LRP pos, temp.b, pos, prevPos;
00126
00127 # Determine gradient at corrected position (same as above)
00128 MOV texCoord.rgb, pos;
00129 TXL tex, pos, texture[0], 3D;
00130 MOV grad.rgb, tex;
00131
00132 #
00133 DIV pos.x, pos, scaleFactors.x;
00134 DIV pos.y, pos, scaleFactors.y;
00135 DIV pos.z, pos, scaleFactors.z;
00136 DIV dir.x, dir, scaleFactors.x;
00137 DIV dir.y, dir, scaleFactors.y;
00138 DIV dir.z, dir, scaleFactors.z;
00139
00140 # Determine scale factor for gradient
00141 SNE temp.rgb, grad, 0.0;
00142 DP3_SAT temp.r, temp, 1.0;
00143
00144 # Reconstruct the gradient
00145 MAD_SSAT grad.rgb, tex, 2.0, -1.0;
00146 NRM_SSAT grad.rgb, grad;
00147
00148 # Set gradient to zero if it was zero
00149 MUL grad.rgb, grad, temp.r;
00150
00151 # Re-orient the gradient to get a
00152 # correct surface normal
00153 MOV temp, 0.0;
00154 DP3 temp.r, -dir, grad;
00155 NRM temp, temp;
00156 MUL grad.rgb, grad, temp.r;
00157
00158 # Determine the reflection vector
00159 DP3 temp.r, grad, dir;
00160 MUL temp.r, temp.r, 2.0;
00161 MUL temp.rgb, grad, temp.r;
00162 SUB temp.rgb, temp, dir;
00163 MOV dir, temp;
00164
00165 # Compute the texture coordinates
00166 ADD temp.z, temp.z, 1.0;
00167 DP3 temp.a, temp, temp;
00168 MUL temp.a, temp.a, 4.0;
00169 RSQ temp.a, temp.a;
00170 MAD temp.rg, temp, temp.a, .5;
00171
00172 # Look up the sphere map
00173 MOV texCoord.rgb, temp;
00174 TXL dst, texCoord, texture[2], 2D;
00175
00176 # Everything is done, jump out of loop
00177 MOVC temp.x, 0.0;
00178 BRK (EQ.x);
00179 ENDIF;
00180
00181 MOV intens.g, intens.r;
00182
00183 # Set values to zero if outside volume
00184 SGE temp1, pos, 0.0;
00185 SLE temp2, pos, texMax;
00186 DP3 weight.r, temp1, temp2;
00187 SEQC weight.r, weight.r, 3.0;
00188
00189 # Already outside volume, skip the rest
00190 BRK (EQ.x);
00191
00192 ENDREP;
00193
00194 BRK (EQ.x);
00195
00196 ENDREP;
00197
00198 # Compute the normal of the background plane (this is just the negative view
00199 # direction which initially is (0, 0, -1))
00200 MOV normal, state.matrix.modelview.row[2];
00201
00202 # Compute the plane constant (we want the plane to be located in the volume
00203 # center)
00204 DP3 temp1.r, normal, center;
00205
00206 # Move the plane behind the volume: d' = <n,(x - l n)> = <n, x> - l <n, n>;
00207 # l = 0.71 is chosen since it is greater than half the cube diagonal
00208 DP3 temp1.g, normal, normal;
00209 MAD temp1.r, temp1.g, -.71, temp1.r;
00210
00211 # Compute ray parameter
00212 DP3 temp1.g, normal, geomPos;
00213 SUB temp1.g, temp1.r, temp1.g;
00214 DP3 temp1.b, normal, geomDir;
00215 DIV temp.r, temp1.g, temp1.b;
00216
00217 # Compute ray/plane intersection
00218 MAD temp.rgb, temp.r, geomDir, geomPos;
00219
00220 # Compute the difference vector
00221 SUB diffVec, temp, center;
00222
00223 # Compute the texture coordinates
00224 DP3 temp.r, diffVec, state.matrix.modelview.row[0];
00225 DP3 temp.g, diffVec, state.matrix.modelview.row[1];
00226 MUL temp.rg, temp, params2.r;
00227
00228 # Center background image
00229 ADD temp.rg, temp, .5;
00230
00231 # Look up the texel value
00232 TEX temp.rgb, temp, texture[1], 2D;
00233 MOV temp.a, 1.0;
00234
00235 # Blend the background pixel
00236 SUB texblen, 1.0, dst.a;
00237 MAD dst, temp, texblen.x, dst;
00238
00239 # Write the output color
00240 MOV result.color, dst;
00241
00242 END