4 ms·
I assume (though I'm certainly not as knowledgeable as Inigo) that he's suggesting most functionality that uses trigonometry during rendering in some way tends
by jmts 8y ago
I assume (though I'm certainly not as knowledgeable as Inigo) that he's suggesting most functionality that uses trigonometry during rendering in some way tends to be performed using dot- and cross-products - which are calculated using only basic arithmetic operations - and therefore don't require slow mathematical function calls. If the actual value of a cosine is needed in some way, it will likely be calculated via the dot-product of two vectors, where one or both has unit length.
As far as construction of rotation matrices goes, most of that will be baked before rendering a frame, so expensive trigonometric calculations and their conversion to rationals will occur ahead of time and therefore much less frequently than anything done per pixel, improving performance.
In computer graphics, because a great many calculations need to be done for each pixel to be displayed, use of expensive functions tends to be avoided. In the literature I recall reading a number of triangle-ray intersection algorithms where each breaks down precisely how many adds, multiplies, divisions, etc are performed because the goal is to minimize all these things. If you are intersecting 10 triangles per pixel at 2 million pixels per frame, a single multiply per intersection becomes 20 million multiplies per frame. By contrast, recalculating the camera matrix using expensive functions should only happen once per frame.
- jacobolus 8y agoEveryone should use vector methods and shy away from transcendental functions to the extent possible. It is usually (but not always) possible to reframe problems in such a way that exponentials (including sine/cosine/tangent) and logarithms (including arcsine, arctangent, ..) are unnecessary. Vector methods are much faster, have fewer weird edge cases, are less subject to rounding error, etc. As an example: if you want to store a planar vector, use a pair of coordinates instead of an angle measure and magnitude. If you want to store an 2d orientation or rotation, still use a pair of (x, y) coordinates on the unit circle instead of angle measure (which is a logarithmic quantity). If you want to reduce your pair of coordinates to a single number, use the stereographic projection, which only requires division.
- westoncb 8y agoWould you mind expanding on the idea that an angle measure is a logarithmic quantity and that sin/cos/tan are exponentials (or providing a phrase I could google)? I've never come across a formulation/characterization like that, and it sounds useful...
- jacobolus 8y agoYou can break the exponential function into the part acting on a scalar and the part acting on a bivector, and then furthermore break those into even and odd parts: exp(ρ + θi) = (cosh ρ + sinh ρ)(cos θ + i sin θ) The angle measure in the above is the θ part. The “linear space” expression of the orientation/rotation is a 2-dimensional quanitity cos θ + i sin θ. Or if you start with the 2-coordinate version x + iy, where x^2 + y^2 = 1, you can find the angle measure by taking the natural logarithm, imag(log(x + yi)). Rotations are naturally a multiplicative concept. If you have a rotation a + bi and a rotation c + di, then you can compose them like (a + bi)(c + di) = (ac - bd) + (ad + bc)i (you might recognize this as the the “angle sum” identities from high school trigonometry class). The idea of the logarithm is that you can convert a multiplicative structure into an additive one. So if you have rotations Q and R, you could normally compose them like QR, but as an alternative you can use a log-space representation (angle measure): QR = exp(log(Q) + log(R)). A rotation inherently comes with an orientation. The i (“imaginary unit”) is in my opinion properly thought of as a bivector, oriented like a plane. When we express something as a scalar-valued angle measure, we have stripped the orientation out.
- twic 8y agoThanks for this - i have studied very little maths, so even this perhaps quite basic stuff is like magic for me! I love the revelation that log/exp shifts between Cartesian and (log-)polar meanings of complex numbers. That's enormously satisfying for some reason. I note that the pattern in multiplying rotations also comes up if you do it with matrices: a -b c -d ac-bd -(ad+bc) b a . d c = ad+bc ac-bd Does something similar happen with quaternions / geometric products in 3D?
- 8y ago