Search code examples
mathglslraytracingnurbs

Do anyone know how to raytrace NURBS of degree 2?


I'm programming a GLSL raytrace since a while and I done some improvement, but since a view days I think that it would be much faster to raytrace curved surfaces instead of many triangles so I came across NURBS. If I write the equation (extended --> only +, -, *, /, sqrt and square) down I can't see any way to get an intersection point with a ray.

Does any one of you know how to raytrace a NURBS of degree 2?

This is my equation (no real NURBS equation):

given :
(A to I are 3d vectors)
A
B
C
D
E
F
G
H
I

a = 2(B-A)
b = 2B-A-C
c = 2(E-D)
d = 2E-D-F
e = 2(H-G)
f =  2H-G-I

(a to f are defined to have the equation a bit shorter later)

o
r
(o and r are 3d vectors again)

searched :
u, v (, t)

to solve :

(A+au-bu²) + ((D+cu-du²)-(A+au+bu²))2v - (2(D+cu-bu²)-(A+au-bu²)-(G+eu-fu²))v² = o+rt
(NURB) = (LINE)

Solution

  • There is quite a bit of literature on the subject for example https://www.researchgate.net/publication/232644373_Direct_and_fast_ray_tracing_of_NURBS_surfaces . This is for general NURBS. Not sure if you can simplify things for quadratic NURBS.

    The basic idea is to think of your ray as the intersection of two planes N . r = a, M . r = b. With N, M normal vectors to the planes, a, b constants. If r = R(u,v) is you NURB function. This gives you two equations in two variables to solve.

    This is where I'm a little unsure. I think for quadratics NURBS you can represent the function as a quotient of two quadratic polynomials R(u,v) = P(u,v) / Q(u,v), where P is vector valued and Q is just a 1D polynomial. If so the equation you want to solve are

    N . P(u,v) = a Q(u,v) M . P(u,v) = b Q(u,v)

    that is two quadratics in two variables. You can use a variety numerical methods like Newton's method or gradient descent and as the equations are quadratics it should converge relatively quickly.

    You will need to consider each patch separately (0 < u < 1/3, 0 < v < 1/3 etc.) to cope with the piecewise nature of the functions.