Frage

I am doing ray tracing with sphere. When we solve quadratic equation and get two roots then which value we need to take? My understanding is that we need to take smallest value. But with that i get wrong results when i put that value in calculating intersection point and further lighting calculations. When I take larger one it gives correct results. I saw some examples on the net and some of them are taking smaller one and others are taking larger. I am really confused which one is correct and why.

War es hilfreich?

Lösung

Suppose the origin of the ray is O and the direction is R (ideally a unit vector). Then the ray is defined by the parametric equation O + tR. Break this into components Ox + tRx, Oy + tRy and Oz + tRz. Substitute these into the equation for the circle x^2 + y^2 + z^2 = 0. This produces a quadratic equation with the variable t. Find the roots of the equation.

Now, if you have no real roots, the ray doesn't hit the sphere. If you have one real root then you hit the side of the sphere. If you have two real roots, then you hit the sphere twice. You want the closer root (the smaller t) unless t is negative in which case the intersection point is behind you and you don't care. If both roots are negative, then both intersection points are behind the origin of the ray and you can ignore them.

If the sphere is transformed away from the origin and you care about rotation and scaling of the sphere, then the equation of the sphere becomes more complex with xy, yz, xz, x, y and z terms. A general quadric shape is defined as Ax^2 + By^2 + Cy^2 + Dxy + Eyz + Fxz + Gx + Hy + Jz + K = 0. The principle above still applies. Substitute the parametric line equation in, solve for t, take the smallest non-negative root.

Your intersection point is O+tR where t is the smallest non-negative root. From there, I don't know why your lighting calculations would be off.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top