Haven't tested with moving camera so may not be quite there yet. A lot of stuff in UpdateWireMesh could be precalculated in wire type if wires are not too dynamic.
; wired.bb
; 3d wire frame system
; wires are lines in 3D space with start and end widths
; CreateWireMesh creates single surface mesh with quad per wire
; UpdateWireMesh positions wire quad so as to face the camera
Type wire
Field x0#,y0#,z0#,w0#
Field x1#,y1#,z1#,w1#
End Type
Function CreateWire.wire(x0#,y0#,z0#,w0#,x1#,y1#,z1#,w1#)
w.wire=New wire
w\x0=x0
w\y0=y0
w\z0=z0
w\w0=w0
w\x1=x1
w\y1=y1
w\z1=z1
w\w1=w1
End Function
Function CreateWireMesh()
m=CreateMesh()
EntityFX m,16 ;double sided flag so no tricky flip testing required
s=CreateSurface(m,b)
For w.wire=Each wire
AddVertex s,0,0,0,0,0
AddVertex s,0,0,0,1,0
AddVertex s,0,0,0,1,1
AddVertex s,0,0,0,0,1
AddTriangle s,v+0,v+1,v+2
AddTriangle s,v+0,v+2,v+3
v=v+4
Next
Return m
End Function
Function UpdateWireMesh(cam,mesh)
s=GetSurface(mesh,1)
camx#=EntityX(cam)
camy#=EntityY(cam)
camz#=EntityZ(cam)
For w.wire=Each wire
px#=w\x1-w\x0
py#=w\y1-w\y0
pz#=w\z1-w\z0
l#=1.0/Sqr(px*px+py*py+pz*pz)
px=px*l
py=py*l
pz=pz*l
cx#=w\x0-camx
cy#=w\y0-camy
cz#=w\z0-camz
l#=1.0/Sqr(cx*cx+cy*cy+cz*cz)
cx=cx*l
cy=cy*l
cz=cz*l
l=w\w0
ex#=l*(py*cz-pz*cy)
ey#=l*(-px*cz+pz*cx)
ez#=l*(py*cx+px*cy)
VertexCoords s,v+0,w\x0+ex,w\y0+ey,w\z0+ez
VertexCoords s,v+3,w\x0-ex,w\y0-ey,w\z0-ez
cx#=w\x1-camx
cy#=w\y1-camy
cz#=w\z1-camz
l#=1.0/Sqr(cx*cx+cy*cy+cz*cz)
cx=cx*l
cy=cy*l
cz=cz*l
l=w\w1
ex=l*(py*cz-pz*cy)
ey=l*(-px*cz+pz*cx)
ez=l*(py*cx+px*cy)
VertexCoords s,v+1,w\x1+ex,w\y1+ey,w\z1+ez
VertexCoords s,v+2,w\x1-ex,w\y1-ey,w\z1-ez
v=v+4
Next
End Function
Graphics3D 1024,768
; front square
CreateWire(-100,100,200,1, 100,100,200,1)
CreateWire(-100,-100,200,1, 100,-100,200,1)
CreateWire(-100,-100,200,1, -100,100,200,1)
CreateWire(100,100,200,1, 100,-100,200,1)
; side lines
CreateWire(-100,-100,200,1, -100,-100,-200,1)
CreateWire(100,-100,200,1, 100,-100,-200,1)
CreateWire(100,100,200,1, 100,100,-200,1)
CreateWire(-100,100,200,1, -100,100,-200,1)
; back square
CreateWire(-100,100,-200,1, 100,100,-200,1)
CreateWire(-100,-100,-200,1, 100,-100,-200,1)
CreateWire(-100,-100,-200,1, -100,100,-200,1)
CreateWire(100,100,-200,1, 100,-100,-200,1)
mesh=CreateWireMesh()
cam=CreateCamera()
While Not KeyHit(1)
TurnEntity cam,0,1,.3
UpdateWireMesh(cam,mesh)
UpdateWorld
RenderWorld
Flip
Wend
End