[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pixel Location in 3D Space?



HandyGM wrote:

Imagine taking a balloon in the shape of the 3D object, 'painting' the
texture map on it, then popping the balloon, leaving the paint hanging
where it was in 3D space when the model existed. For each 'dot of paint'
(pixel in the texture map), I want to know where it is in 3D space.

Basically, I want to map all the points in a texture map, to their
corresponding points in 3D space, given a standard 3D object of
arbitrary-sized polygons. Most of the time, the model is composed of
triangles and quadrangles, but sometimes there are polygons with more sides.


Does anyone have an idea on how this might be accomplished?

* textures are mapped linearly across a triangle o All polygons can be reduced to triangles during tessellation o The corner of each triangle has a texture coordinate, again, often generated during tessellation o For any point on the surface of the triangle, the pixel displayed is a linear blending of the texture coordinates of the 3 points of the triangle, you'll find an equation in the OpenGL spec for how that math works * textures are applied (normally) to large numbers of triangles, and can be repeated even within a single triangle (texture coordinates with differences > 1), so for each pixel in the texture you have up to NUMTRIANGLES 3D points for that pixel o So, for each pixel + for each tessellated triangle # for imagewidth in texture coordinate triangle width: * for imageheight in texture coordinate triangle height: o test if imagewidth+pixel,imageheight+pixel is within coordinate triangle + if it is: # take a linear blend of the 3D points of the triangle with the weights for the texture coordinates

However, that's a fairly generalised approach, if you've got X standard objects and you just want to work with those you can likely simplify some assumptions considerably. I've also not spent much time on how to optimise it, there's likely a better way to do the "does this pixel in any repetition show up within the triangle" test.

HTH,
Mike

--
________________________________________________
 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com