# Optimal way to store coordinates of a random point in 3D space in a texture?

A quick question: there is a well-known approach to store 3d point in space in a texture as a distance between camera and an object (deferred rendering). But it implies that the object lies on a ray, cast from the camera towards that object in such and such section (UVs) of the texture. Is there any way to store a RANDOM 3d point in space in a texture with a single (mb 16-bit) depth value or less than 3 values?

-

Depth maps get away with using only a single coordinate because the other two are implicit in the location of the texel within the map (UV).

If you need to scatter each texel randomly in 3D space, then you'll need to encode all three degrees of freedom into the texel value.

If you don't have 3 channels to spare, you can quantize your positions and pack them into a smaller number of channels (as low as one channel encoding all three axes).

The optimal way to do this will depend heavily on your application. For instance, if your points are clumped it may work well to express them in polar coordinates. If one axis has greater range/variability than others, you may want to devote more bits to it...

As one example, if I have a 16-bit field to work with, I can encode x into the low 5 bits, y into the middle 5, and z into the top 6 bits. This only gives 32 unique positions along the x/y axes, and 64 on the z.

Texture values encoded this way will not interpolate correctly with bi/trilinear or anisotropic filtering, so ensure you're using point sampling and doing any interpolation you need manually in the shader.

-
I felt that coming, but hoped that I might have missed something. Indeed the only options I have are packing and bit-shifting, which are no options at all for me as: A) I need 16-bit precision for each of the 3 coordinates; B) I am using DX9 so I can't bitshift; C) I am working with floating point 16-bit textures (HalfVector) and all my previous attempts to pack a value in one of their channels failed, so I am not sure if it is possible at all. – cubrman Mar 7 '14 at 17:07

Basically what you're trying to do is called unprojecting. If you unproject a 2D point (calculated from an inverse projection matrix) you will get a ray. If you store the distance from the camera in the 16bit depth values you can quite accurately reconstruct the 3D world point.

Please see this question on StackOverflow http://stackoverflow.com/questions/3528114/whats-wrong-with-my-project-and-unproject-functions (Note that it really depends on the framework you are using if you need to transpose the matrix or not).

Another good resource is: http://myweb.lmu.edu/dondi/share/cg/unproject-explained.pdf

(Normally I would reproduce it here so that it would be available as long as this question exists but I'm not 100% sure if the author of the PDF would allow it)

-
I think the asker is looking for a way to store positions which do NOT necessarily lie on a known ray from a camera (ie. they have three degrees of freedom, not one). – DMGregory Mar 7 '14 at 15:53
DMGregory is right, I am looking for a way to store a completely random (not even shown on the screen) 3d point in one channel. – cubrman Mar 7 '14 at 17:04
Out of curiosity, why are you storing this data if I may ask? I mean what useful applications does storing this data have? – zehelvion Mar 7 '14 at 17:08
I'm sorry you're absolutely right. I totally missed that. Is the relation really 100% random, else there still might be a few tricks available. For example encoding it in a texture that was made with a camera that has a 360 degree view of the scene. If it is truly random then never forget that the location in the array itself is also extra data and it doesnt cost extra storage. – Roy T. Mar 7 '14 at 21:55