Finding 3Dworld position of texture pixel (texel)?

Blitz3D Forums/Blitz3D Programming/Finding 3Dworld position of texture pixel (texel)?

Beaker(Posted 2006) [#1]
I can probably work this out, but has anyone already created a function to calculate the 3D world position of a texture pixel on a particular triangle/surface?

Any help would be appreciated. Thanks.


Mustang(Posted 2006) [#2]
This should do it, more or less:

http://www.blitzbasic.com/codearcs/codearcs.php?code=515

[edit]

Ooops, or maybe not... if I understud the question correctly after second reading.


Baystep Productions(Posted 2006) [#3]
As in a radar map?

I did it as a 2D image for the hud...

And I as I remember it's easy math, just get the pixel cordinate x&y subtract half the width&height (if you want the center to be 0) devide it by the whole width& height and multiply the result by the scale of the map.

So it would be something like...
x#=(Pixel_X-(ImageWidth(map)/2))/ImageWidth(map)
z#=(Pixel_Y-(ImageHeight(map)/2))/ImageHeight(map)
PositionEntity(obj,x#*Scale#,0,z#=*Scale#)


Bobysait(Posted 2006) [#4]
use LinePick (or CameraPick )
- Surf=pickedsurface()
- Tri=PickedTriangle()
- get the 3 vertices of the triangle :
V0=Trianglevertex(surf,0)
V1=Trianglevertex(surf,1)
V2=Trianglevertex(surf,2)

VU0#=vertexU(surf,V0)
VV0#=vertexV(surf,V0)

VU1#=vertexU(surf,V1)
VV1#=vertexV(surf,V1)

VU2#=vertexU(surf,V2)
VV2#=vertexV(surf,V2)

=> And get the coord :
U#=(VU0+VU1+VU2)/3
V#=(VV0+VV1+VV2)/3

Then convert the UV with texturecoord using:
=> GetSurfaceBrush => getBrushTexture()
And finally => texturewidth() , TextureHeight()
=> Pixel_I=U*TextureWidth()
=> Pixel_J=V*TextureHeight()


Jeppe Nielsen(Posted 2006) [#5]
This will do what you want, I think :-)

Kind of the reverse of the code Mustang showed.

Look at the TFormTexel function:

;this will calculate the world coordinates of a texel, at the texture pixel coordinates specified
;results are grabbed with TFormedX(),TFormedY() and TFormedZ()
TFormTexel(mesh,texture,pixelx,pixely)




Bobysait(Posted 2006) [#6]
good work. But maybe parsing all triangles of the mesh to get one coord could be long .

Maybe using a preloading system to store triangles datas should get it work faster no ? But it should also use bigs array ...


Beaker(Posted 2006) [#7]
Thanks guys, particularly Jeppe. Just what I needed.


RemiD(Posted 2016) [#8]
Thanks Jeppe, very useful code !