# Integrating ray casting equation

In some ray casting algorithm I want to integrate the following integral:

$\int_{0}^{Z} \frac{c^x}{\|(\vec{o}+x \vec{d})-\vec{l}\|^3} dx$

• $Z$ is a constant until which I want to integrate
• $\vec{o}$ is the origin of the ray.
• $\vec{d}$ is the direction of the ray.
• $\vec{o} + x \vec{d}$ is the position of the ray at time $x$.
• $\vec{l}$ is the position of a light.
• $\|(\vec{o}+x \vec{d})-\vec{l}\|$ is the distance between the current position and the light.
• $c^x$ is a weighting factor which I want to apply. When I move further away from the camera by increasing x, I want the weight to decrease ($c$ < 1).
• $c$ is a constant value (like 0.9).

Solving it via a Riemann sum is not efficient enough on the Xbox. Therefore, I want to solve it analytically. However, as there is a norm of vectors involved together with some other ugly parts, I don't really know where to start solving this.

• Is it possible to solve this integral analytically? (If yes, how?)
• Is there a way to approximate this integral by using non-uniform step-sizes using some Riemann-style integration to increase efficiency of my current implementation?
• Maybe some other solutions? It doesn't have to be exact; as long as it is not instable for small changes to $Z$ and it somehow approximates the integral, it should be fine since it is used in a game I am working on (where importance of performance is bigger than importance of correctness).
-
The question whether this integral can be solved analytically isn't well-defined unless you specify $c^x$. – joriki Apr 21 '11 at 14:13
c is a constant factor, (like 0.9) and x is the integration variable. – Etan Apr 21 '11 at 14:15
I see. Then you've got "some" and "a" exchanged. It makes sense to say either "$c$ is a constant value" or "$c$ is some constant value", but it doesn't make sense to say "$c^x$ is some weighting factor", since you've actually specified it (namely $c^x$). – joriki Apr 21 '11 at 14:31
Ok, now it should be "mathematically correct" ;-) – Etan Apr 21 '11 at 21:36

In general, this integral won't have an elementary 'closed-form' solution; the denominator is some value of the form $(ax^2+bx+c)^{3/2}$ and a few moments with the integrator at Wolfram Alpha suggests that there's no closed form for such an integral with the exponential weighting you want. I think you have the right idea with numerical evaluation, but Riemann sums aren't the best way of evaluating integrals; you should look at things like Simpson's Rule as a starting point, or go to a reference like Numerical Recipes for more adaptive mechanisms. In practice, though, you're going to have a hard time getting an accurate value for an integral like this with less than dozens of steps and evaluations, especially for rays that come close to the light where the denominator can grow large and varies rapidly.
What about changing the denominator to $(ax^2 + bx + c)$ - changing the exponent from 3 to 2? Ok, it's not physically correct, but the basic visual result will be acceptable. Or maybe changing the nominator to something like $\frac{1}{x+1}$. An analytical result is really necessary since a dozens of steps are just impossible in real time. So, approximations are really okay :-) – Etan Apr 21 '11 at 22:56