Thanks all for the replies. I have hacked together a cheesy example code with Floyds bit in. The results are not what I expected. I unweld a sphere, centre all the triangles and want them to align. So I make an entity per triangle and rotate them as per the earlier formula, but This is what I get...
Type TRIS
Field x0#
Field y0#
Field z0#
Field u0#
Field v0#
Field U20#
Field V20#
Field x1#
Field y1#
Field z1#
Field u1#
Field v1#
Field U21#
Field V21#
Field x2#
Field y2#
Field z2#
Field u2#
Field v2#
Field U22#
Field V22#
Field surface
End Type
Dim txv(3)
Global MFNx#, MFNy#, MFNz#, tmp
Graphics3D 800,600,32,2
Local Cam = CreateCamera()
CameraRange cam,0.1,50
PositionEntity cam,0,0,-4
Local Ball = CreateSphere(16)
WireFrame True
Unweld(Ball)
AlignTriangles(Ball)
While Not KeyDown(1)
RenderWorld
If EntityZ(cam) < -0.3 Then MoveEntity cam,0,0,0.01
Flip False
Wend
EndGraphics
End
Function AlignTriangles(Mesh)
Local X1#,Y1#,Z1#
Local X2#,Y2#,Z2#
Local X3#,Y3#,Z3#
Local X4#,Y4#,Z4#
For nsurf = 1 To CountSurfaces(mesh)
su=GetSurface(mesh,nsurf)
For tq = 0 To CountTriangles(su)-1
X1 = VertexX(Su,TriangleVertex(su,tq,0))
Y1 = VertexY(Su,TriangleVertex(su,tq,0))
Z1 = VertexZ(Su,TriangleVertex(su,tq,0))
X2 = VertexX(Su,TriangleVertex(su,tq,1))
Y2 = VertexY(Su,TriangleVertex(su,tq,1))
Z2 = VertexZ(Su,TriangleVertex(su,tq,1))
X3 = VertexX(Su,TriangleVertex(su,tq,2))
Y3 = VertexY(Su,TriangleVertex(su,tq,2))
Z3 = VertexZ(Su,TriangleVertex(su,tq,2))
X4 = (X1 + X2 + X3) / Float(3)
Y4 = (Y1 + Y2 + Y3) / Float(3)
Z4 = (Z1 + Z2 + Z3) / Float(3)
Tmp = CreateMesh()
Local sfs = CreateSurface(tmp)
AddVertex sfs, X1 - X4, Y1-Y4, Z1 - Z4
AddVertex sfs, X2 - X4, Y2-Y4, Z2 - Z4
AddVertex sfs, X3 - X4, Y3-Y4, Z3 - Z4
AddTriangle sfs,0,1,2
gfn(sfs,0)
DebugLog "NX=" + MFNx
DebugLog "NY=" + MFNy
DebugLog "NZ=" + MFNz
pvt = CreatePivot()
AlignToVector pvt, mfny, -mfnx, 0, 2 ; point y-axis at N crossed with (0,0,1)
TurnEntity pvt, 0, ACos(mfnz), 0 ; pivot has orientation desired for triangle
RotateMesh tmp, EntityPitch(pvt), EntityYaw(pvt), EntityRoll(pvt)
;RotateMesh tmp, -mfnx*180, -mfny*180,-mfnz*180
;VertexCoords su,TriangleVertex(su,tq,0),X1 - X4, Y1-Y4, Z1 - Z4
;VertexCoords su,TriangleVertex(su,tq,1),X2 - X4, Y2-Y4, Z2 - Z4
;VertexCoords su,TriangleVertex(su,tq,2),X3 - X4, Y3-Y4, Z3 - Z4
Next
Next
End Function
Function unWeld(mish)
For nsurf = 1 To CountSurfaces(mish)
su=GetSurface(mish,nsurf)
For tq = 0 To CountTriangles(su)-1
txv(0) = TriangleVertex(su,tq,0)
txv(1) = TriangleVertex(su,tq,1)
txv(2) = TriangleVertex(su,tq,2)
vq.TRIS = New TRIS
vq\x0# = VertexX(su,txv(0))
vq\y0# = VertexY(su,txv(0))
vq\z0# = VertexZ(su,txv(0))
vq\u0# = VertexU(su,txv(0),0)
vq\v0# = VertexV(su,txv(0),0)
vq\u20# = VertexU(su,txv(0),1)
vq\v20# = VertexV(su,txv(0),1)
vq\x1# = VertexX(su,txv(1))
vq\y1# = VertexY(su,txv(1))
vq\z1# = VertexZ(su,txv(1))
vq\u1# = VertexU(su,txv(1),0)
vq\v1# = VertexV(su,txv(1),0)
vq\u21# = VertexU(su,txv(1),1)
vq\v21# = VertexV(su,txv(1),1)
vq\x2# = VertexX(su,txv(2))
vq\y2# = VertexY(su,txv(2))
vq\z2# = VertexZ(su,txv(2))
vq\u2# = VertexU(su,txv(2),0)
vq\v2# = VertexV(su,txv(2),0)
vq\u22# = VertexU(su,txv(2),1)
vq\v22# = VertexV(su,txv(2),1)
Next
ClearSurface su
For vq.tris = Each tris
AddVertex su,vq\x0#,vq\y0#,vq\z0#,vq\u0#,vq\v0#
VertexTexCoords su,mycount,vq\u20#,vq\v20#,0,1
mycount = mycount +1
AddVertex su,vq\x1#,vq\y1#,vq\z1#,vq\u1#,vq\v1#
VertexTexCoords su,mycount,vq\u21#,vq\v21#,0,1
mycount = mycount +1
AddVertex su,vq\x2#,vq\y2#,vq\z2#,vq\u2#,vq\v2#
VertexTexCoords su,mycount,vq\u22#,vq\v22#,0,1
mycount = mycount +1
AddTriangle su,mycount-3,mycount-2,mycount-1
Next
Delete Each tris
mycount=0
Next
End Function
Function GFN(SUR,TRI)
If sur = 0 Then Return
Local X1#,Y1#,Z1#
Local X2#,Y2#,Z2#
Local X3#,Y3#,Z3#
V0 = TriangleVertex(SUR,TRI,0)
V1 = TriangleVertex(SUR,TRI,1)
V2 = TriangleVertex(SUR,TRI,2)
X1# = VertexX(SUR,V0)
Y1# = VertexY(SUR,V0)
Z1# = VertexZ(SUR,V0)
X2# = VertexX(SUR,V1)
Y2# = VertexY(SUR,V1)
Z2# = VertexZ(SUR,V1)
X3# = VertexX(SUR,V2)
Y3# = VertexY(SUR,V2)
Z3# = VertexZ(SUR,V2)
; Calculate the normal of the plane which this triangle lies in, (A = Nx, B = Ny, C = Nz)
; and the distance of the plane (D) from the origin (0,0,0) at it's closest point To the Origin.
A# = Y1# * (Z2# - Z3#) + Y2# * (Z3# - Z1#) + Y3# * (Z1# - Z2#)
B# = Z1# * (X2# - X3#) + Z2# * (X3# - X1#) + Z3# * (X1# - X2#)
C# = X1# * (Y2# - Y3#) + X2# * (Y3# - Y1#) + X3# * (Y1# - Y2#)
;D# = -(X1# * (Y2# * Z3# - Y3# * Z2#) + X2# * (Y3# * Z1# - Y1# * Z3#) + X3# * (Y1# * Z2# - Y2# * Z1#))
; Normalize the plane equation.
; (Remove any scaling which will skew our results. Make the normal's length 1.)
Length# = Sqr(A#*A# + B#*B# + C#*C#)
A# = A# / Length#
B# = B# / Length#
C# = C# / Length#
;D# = D# / Length#
; Return the normal of the triangle.
MFNx# = A#
MFNy# = B#
MFNz# = C#
End Function