Positional Spherical Mapping?
BlitzMax Forums/BlitzMax Programming/Positional Spherical Mapping?
| ||
I want to apply a sphere map to a flat 2d polygon, I think Positional sphere mapping will do the trick. Before I attempt to code something i'd like to know anyone has tried this before. Why use Positional Spherical Mapping? In some cases, using normals to calculate spherical texture coordinates may not be appropriate. For example, objects which use face normals would have the same coordinates applied to each vertex of a face. Also, regions that are relatively flat on one or more axis will cause the texture to appear stretched. An alternative to this is to project a vector from the center of the object outward through each vertex, and use the normalized X and Y coordinates to calculate the texture coordinates. This has the effect of projecting each vertex onto a theoretical sphere. Why do I want it? You've played GISH? I wan't a deformable 2d polygon that retains the texture. The polygons will be able to combine and split with other polygons very much like a 2D metaballs simulation. Ta, |
| ||
This should do it, it clamps it so that if a vertex is outside of the spherical range it peaks at -1,1. Remove the last four lines of the func to avoid this.Local U#,v# Local VertexX=120,VertexY sphereMap( 20,20,100,vertexX,vertexy,u,v ) Print "U:"+u Print "V:"+v WaitKey End Function SphereMap( SphereX#,SphereY#,SphereRadius#,X#,Y#,U:Float Var,V:Float Var ) Local xd# = X-SphereX Local yd# = Y-SphereY Local mag# = Sqr(xd*xd+yd*yd) xd = xd/mag yd = yd/mag Local vc# = mag/sphereradius u = xd*vc v = yd*vc if u<-1 u=-1 if u>1 u=1 if v<-1 v=-1 if v>1 v=1 End Function |
| ||
Thanks Ant, I got some C++ code, I think this will do the job. Tell me if I'm wrong. // determine extents D3DXVECTOR3 vMin,vMax; D3DXComputeBoundingBox(pVerts,numVerts,FVF_VERTEX,&vMin,&vMax); // calculate center D3DXVECTOR3 vCent; vCent=(vMax+vMin)*0.5f; // loop through the vertices for (i=0;i<numVerts;i++) { // calculate normalized offset from center D3DXVECTOR3 v; v=pVerts->pos-vCent; D3DXVec3Normalize(&v,&v); // calculate texture coordinates pVerts->tu=asinf(v.x)/D3DX_PI+0.5f; pVerts->tv=asinf(v.y)/D3DX_PI+0.5f; // go to next vertex pVerts++; } |
| ||
Ant, Your code did not work as I thought, I made some minor changes and not it works like a dream. There is still work to be done as I need to calculate the centre of mass before calculating the map. Code as is Function SphereMap( spX#,spY#,x#,y#,U:Float Var,V:Float Var ) xd# = x - spX yd# = y - spY mag# = Sqr(xd*xd + yd*yd) xd = xd / mag yd = yd / mag u = (xd/2) + 0.5 v = -(yd/2) + 0.5 End Function GISH type Tar blobs here I come. |