Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

StackOverflow https://stackoverflow.com/questions/18284382

Question

I am writing a shader which requires knowledge of the distance between a point and the origin. This is the line I had originally:

float distanceToOrigin = sqrt((pow(a_Position.x,2.0)+pow(a_Position.y,2.0)+pow(a_Position.z,2.0));

But doing that caused the vertices to dissappear in certain regions of the scene.

Replacing that line with this one for calculating Manhattan instead of Euclidean distance produces the results I would expect for Manhattan distance.

float distanceToOrigin = a_position.x + a_position.y + a_position.z;

Why might the first line produce unexpected results?

Was it helpful?

Solution

For the pow function the results are undefined for values of x<0 or if x=0 and y=0, not to mention it's a lot slower than plain squaring.

Why aren't you using length(a_Position) there?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top