NVlabs / nvdiffrast

Nvdiffrast - Modular Primitives for High-Performance Differentiable Rendering
Other
1.35k stars 144 forks source link

What is pos? #31

Closed alexsax closed 3 years ago

alexsax commented 3 years ago

Thanks for releasing this code--if I can get it to work, then it should be quite helpful :)

The function nvdiffrast.torch.rasterize(glctx, pos, tri, resolution, ranges=None, grad_db=True) takes an argument, pos which I assume is supposed to be the positions of vertices in a mesh. The documentation seems to indicate that this should be a 3-coord dimension: "x/w, y/w, z/w". However, the dimension needs to be [num_verts, 4] which doesn't correspond to a 3D coordinate encoding.

I tried playing around with values in the triangle.py example, but couldn't figure it out.

Could you clarify what this argument is supposed to be?

s-laine commented 3 years ago

Hi @alexsax! The contents of pos should specify vertices in homogeneous coordinates (x, y, z, w) after any 3D/projective transformations. Looking at the first diagram here, that would be step 4 (clip space). The OpenGL documentation gives further information. This is so that all the transformations, including perspective projection, can be performed outside nvdiffrast and there is no need to consider camera parameters etc. inside the library.

In practice, you first convert your 3D vertices to the homogeneous form by appending w=1, do any necessary transformations via 4x4 matrix multiplications, and feed the resulting vectors to nvdiffrast in pos.

alexsax commented 3 years ago

Thanks @s-laine! That was a very helpful pointer to NDC space. I got it working for my use case.

In case this ends up being useful for others, I ended up getting the projection matrix from Pytorch3D, here and using nvdiffrast for the rasterization, which is ~1000x faster than the Pytorch3D rasterization.

I'll close the issue now :)