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_transparent_sm3.fp
00016
00017 #! VOLUME = 0
00018 #! BACKGROUND = 1
00019
00020 # r = step
00021 # g = gradient scale
00022 # b = gradient offset
00023 # a = z-value of background plane
00024 PARAM params = program.local[0];
00025
00026 # r = texture coordinate scale
00027 # g = number of iterations
00028 # b = isovalue of isosurface
00029 PARAM params2 = program.local[1];
00030 PARAM center = program.local[2];
00031 PARAM texMax = program.local[3];
00032
00033 PARAM scaleFactors = program.local[5];
00034
00035 # Color(s) must be premultiplied by alpha
00036 PARAM isColor1 = {0.0, 0.15, 0.0, 0.15};
00037
00038 TEMP geomPos;
00039 TEMP geomDir;
00040 TEMP localDst;
00041 TEMP lightVec;
00042 TEMP origPos;
00043 TEMP prevPos;
00044 TEMP texblen;
00045 TEMP diffVec;
00046 TEMP halfway;
00047 TEMP eyeVec;
00048 TEMP intens;
00049 TEMP camera;
00050 TEMP normal;
00051 TEMP weight;
00052 TEMP temp1;
00053 TEMP temp2;
00054 TEMP temp;
00055 TEMP grad;
00056 TEMP pos;
00057 TEMP dst;
00058 TEMP dir;
00059 TEMP tex;
00060 TEMP scalAct;
00061 TEMP scalPre;
00062
00063 # Compute the ray's starting point
00064 MOV geomPos, fragment.texcoord[0];
00065 MUL pos, geomPos, scaleFactors;
00066 MOV pos.a, 0.0;
00067
00068 # Compute the camera position by translating the origin to the center of the
00069 # volume
00070 MOV camera, state.matrix.modelview.invtrans.row[3];
00071
00072 # Compute the ray direction
00073 SUB geomDir, geomPos, camera;
00074
00075 # Normalize the direction
00076 DP3 geomDir.w, geomDir, geomDir;
00077 RSQ geomDir.w, geomDir.w;
00078 MUL geomDir, geomDir, geomDir.w;
00079 MOV geomDir.w, 0.0;
00080
00081 MUL dir, geomDir, scaleFactors;
00082
00083 # Initialize the 'previous' intensity
00084 TXL intens, pos, texture[0], 3D;
00085 MOV intens.g, intens.a;
00086
00087 # Initialize destination color
00088 MOV dst, 0.0;
00089
00090 REP params2.g;
00091
00092 REP params2.g;
00093 # Lookup scalar and gradient
00094 TXL tex, pos, texture[0], 3D;
00095 MOV intens.r, tex.a;
00096
00097 # Move one step forward
00098 MAD pos, dir, params.r, pos;
00099
00100 # Isosurface if differences with past and present
00101 # scalar values and isovalue have different signs
00102 SUB temp.rg, intens, params2.b;
00103 MULC temp, temp.r, temp.g;
00104 IF LE.x;
00105 # Isosurface found
00106 MOV origPos, pos;
00107
00108 # Get scalar value from actual position
00109 MAD pos, -dir, params.r, pos;
00110 TXL scalAct, pos, texture[0], 3D;
00111
00112 # Get scalar value from previous position
00113 MAD prevPos, -dir, params.r, pos;
00114 TXL scalPre, prevPos, texture[0], 3D;
00115
00116 SUB temp.r, params2.b, scalPre.a;
00117 SUB temp.g, scalAct.a, scalPre.a;
00118
00119 # Perform linear interpolation to increase accuracy
00120 DIV_SAT temp.b, temp.r, temp.g;
00121 LRP pos, temp.b, pos, prevPos;
00122
00123 # Determine gradient at corrected position (same as above)
00124 TXL grad.rgb, pos, texture[0], 3D;
00125
00126 # Determine scale factor for gradient
00127 SNE temp.rgb, grad, 0.0;
00128 DP3_SAT temp.r, temp, 1.0;
00129
00130 # Reconstruct the gradient
00131 MAD_SSAT grad.rgb, grad, 2.0, -1.0;
00132 NRM_SSAT grad.rgb, grad;
00133
00134 # Set gradient to zero if it was zero
00135 MUL grad.rgb, grad, temp.r;
00136
00137 # Re-orient the gradient to get a
00138 # correct surface normal
00139 MOV temp, 0.0;
00140 DP3 temp.r, -geomDir, grad;
00141 NRM temp, temp;
00142 MUL grad.rgb, grad, temp.r;
00143
00144 # Perform the lighting calculation (yes, we know LIT!)
00145
00146 # Ambient term
00147 MUL localDst, state.light[0].ambient, isColor1;
00148
00149 # Diffuse term
00150 SUB lightVec.rgb, state.light[0].position, pos;
00151 MOV lightVec.a, 0.0;
00152 NRM lightVec, lightVec;
00153 DP3_SAT temp.a, lightVec, grad;
00154 MUL temp, isColor1, temp.a;
00155 MAD localDst, state.light[0].diffuse, temp, localDst;
00156
00157 # Specular term
00158 # First calculate halfway vector
00159 ADD halfway, lightVec, -dir;
00160 MOV halfway.a, 0.0;
00161 NRM halfway, halfway;
00162 # Specular Lighting
00163 SUB eyeVec.rgb, camera, pos;
00164 MOV eyeVec.a, 0.0;
00165 NRM eyeVec, eyeVec;
00166 DP3_SAT temp.a, halfway, grad;
00167 POW temp.a, temp.a, 100.0;
00168 MAD localDst, state.light[0].specular, temp.a, localDst;
00169
00170 # Perform the blending
00171 SUB texblen.g, 1.0, dst.a;
00172 MAD dst, localDst, texblen.g, dst;
00173
00174 # Set the new position (otherwise the isosurface would be
00175 # intersected again -- we don't know why -- and it actually would
00176 # be better to just restore the original position)
00177 #MUL temp, params.r, dir;
00178 MOV pos, origPos;
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 # Already outside volume, skip the rest
00195 BRK (EQ.x);
00196
00197 ENDREP;
00198
00199 # Compute the normal of the background plane (this is just the negative view
00200 # direction which initially is (0, 0, -1))
00201 MOV normal, state.matrix.modelview.row[2];
00202
00203 # Compute the plane constant (we want the plane to be located in the volume
00204 # center)
00205 DP3 temp1.r, normal, center;
00206
00207 # Move the plane behind the volume: d' = <n,(x - l n)> = <n, x> - l <n, n>;
00208 # l = 0.71 is chosen since it is greater than half the cube diagonal
00209 DP3 temp1.g, normal, normal;
00210 MAD temp1.r, temp1.g, -.71, temp1.r;
00211
00212 # Compute ray parameter
00213 DP3 temp1.g, normal, geomPos;
00214 SUB temp1.g, temp1.r, temp1.g;
00215 DP3 temp1.b, normal, geomDir;
00216 DIV temp.r, temp1.g, temp1.b;
00217
00218 # Compute ray/plane intersection
00219 MAD temp.rgb, temp.r, geomDir, geomPos;
00220
00221 # Compute the difference vector
00222 SUB diffVec, temp, center;
00223
00224 # Compute the texture coordinates
00225 DP3 temp.r, diffVec, state.matrix.modelview.row[0];
00226 DP3 temp.g, diffVec, state.matrix.modelview.row[1];
00227 MUL temp.rg, temp, params2.r;
00228
00229 # Center background image
00230 ADD temp.rg, temp, .5;
00231
00232 # Look up the texel value
00233 TEX temp.rgb, temp, texture[1], 2D;
00234 MOV temp.a, 1.0;
00235
00236 # Blend the background pixel
00237 SUB texblen, 1.0, dst.a;
00238 MAD dst, temp, texblen.x, dst;
00239
00240 # Write the output color
00241 MOV result.color, dst;
00242
00243 END