   Pressure Projection
  Hi,Righto, so after a few days of struggle, I'm still having trouble with pressure projection for fluid dynamics.  I'll begin with divergence calculation.  The result should be a scalar field, right?  Or am I missing something?  The code, found here, looks like:void divergence(half2 coords  : WPOS,   // grid coordinates   out half4 div : COLOR,  // divergence   uniform half halfrdx,   // 0.5 / gridscale   uniform samplerRECT w)  // vector field{  half4 wL = h4texRECT(w, coords - half2(1, 0));  half4 wR = h4texRECT(w, coords + half2(1, 0));  half4 wB = h4texRECT(w, coords - half2(0, 1));  half4 wT = h4texRECT(w, coords + half2(0, 1));  div = halfrdx * ((wR.x - wL.x) + (wT.y - wB.y));}This doesn't make sense to me.  In the event that the value is negative, then it will be clipped to 0.0.  I assume this isn't the correct behavior, or there's something special going on with Cg.  I've ported mine to GLSL, adding the final value to 0.5, so that values less than 0.5 signify negative values and values greater than 0.5 signify positive values.  So . . . it should be a scalar field, right?  And it should contain positive and negative values too, as derivatives are surely occasionally negative . . .Thanks,G   
  Here's my current setup.  For a non-zero divergence velocity field (left), the resultant scalar divergence field is shown at right.  Is that correct?;Quote: In the event that the value is negative, then it will be clipped to 0.0.   Err, are you sure? I dont see any clipping in the code posted..?   ;  I mean in that when written to a texture, textures only store values [0.0,1.0].   ;Quote:Original post by GeometrianThe code, found here, looks like:*** Source Snippet Removed ***This doesn't make sense to me.  In the event that the value is negative, then it will be clipped to 0.0.They might be assuming that you'll render into an FP16 texture, which supports negative value.   ;Quote:They might be assuming that you'll render into an FP16 texture, which supports negative value.Ah--I didn't know that!  À la here and here, FP32 ought to support negative/positive too.  Does all that mean that I can have my shaders write something like 1.784*1042 to textures and have no clipping?!  Neat!Well, in any case, I'd also like to support normal RGB8 textures as well, so I'm centering the numbers on 0.5.After some thought, I think that divergence really ought to be a vector.  Why the sample doesn't do it, I don't know.  Can someone please explain how to calculate divergence and why it is the way it is?Thanks,G   ;Quote:Original post by GeometrianAfter some thought, I think that divergence really ought to be a vector.  Why the sample doesn't do it, I don't know.  Can someone please explain how to calculate divergence and why it is the way it is?The divergence operator takes a vector field as input, and outputs a scalar field that tells you, for every point in space, how much "stuff" is flowing into or out from that point.  Divergence is a scalar field.Was this your source of confusion, or did you mean something else?   ;  Hi,I think the source of my confusion (with divergence being a scalar) was that I couldn't see how Jacobi iteration would work to get the zero-divergence velocity field.  Honestly, I've never used Jacobi iteration before, so I'm still new at this.So, I have the non-zero-divergence-velocity and the divergence (as in my second post).  I want to find a velocity field that has zero divergence.  The code, again from here:half4 xL = h4texRECT(x, coords - half2(1, 0));half4 xR = h4texRECT(x, coords + half2(1, 0));half4 xB = h4texRECT(x, coords - half2(0, 1));half4 xT = h4texRECT(x, coords + half2(0, 1));// b sample, from centerhalf4 bC = h4texRECT(b, coords);// evaluate Jacobi iterationxNew = (xL + xR + xB + xT + alpha * bC) * rBeta;//According to the article, for this particular Jacobi operation, alpha = -(dx*dx) and rBeta = 0.25Once again, my source is very similar.  So, in solving, I start with an initial guess for the pressure (texture x) initialized to 0.  b must be solution, so it is the divergence (texture b).  I think this setup is correct.  Pressure is then iteratively solved for, by running the operation multiple times, with divergence (texture b) remaining constant, but the previous value of pressure (texture x) being fed back into the system.  I've found that for dx = 1.0/num_x_squares = 0.0078125, the resultant pressure remains 0 (or at least extremely close to it).  I also tried alpha=1.0, rBeta=0.2, and the pressure seems to converge to the divergence itself, which isn't helpful.  So it seems I'm still doing something wrong . . .Thanks again,-G   