Reputation: 23
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?
Upvotes: 0
Views: 1143
Reputation: 446
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?
Upvotes: 1