as usual thanks in advance..
I'm using pixel bender to generate lighting effects for a heightmap based terrain. I'd like to make a normal map of the terrain and from there dot each normal against a given sun direction. Pretty standard stuff really, but pixel bender isn't playing nicely. I have this code:
void
evaluatePixel()
{
float2 pt = outCoord();
pixel3 a = sampleNearest(src,pt);
pixel3 b = sampleNearest(src,float2(pt.x + 1.0, pt.y));
pixel3 c = sampleNearest(src,float2(pt.x, pt.y + 1.0));
float3 v1 = float3(1.0, 0.0, b.r - a.r);
float3 v2 = float3(0.0, 1.0, c.r - a.r);
normalize(v1);
normalize(v2);
float3 n = cross(v1,v2);
dst = pixel3(n.x,n.y,n.z);
}
I would expect this to produce a normal map. To test, I assumed that the light was pointing straight down and just use n.z as the output colour. This produces a solid colour. If you take the above code and run it, you'll see that whilst there is variation in red and green, blue is always full at 255. Why is this? I'd expect that considering v1 and v2 are normalized that this shouldnt always output full blue?
What am I doing wrong?!?
I actually did something very similar here, with my triangle-strip heightmaps.
This works amazingly well for me. Runs through 1.8M vertices in less than 100ms. Just to clarify, I define my triangles as the columns between vertices. This means there are two triangles for every 4 vertices. I identify the two triangles per quad with "quadIdx".
Note: You are normalizing and then calculating the cross - this is backwards, you need to normalize after. Also, I actually believe I found a bug in normalize(). It was creating NaN's where I did not expect. I implemented my own normalization with length() and it all works great!