Post by botbuilder on Feb 24, 2004 8:08:04 GMT
The general idea is to create some bb functions that use bb commands(Ie, things nearly impossible to do in C/C++).
Well, I thought someone else was going to do this, but eh. Here's what I got so far:
Additions welcome (I'll add them into this main post)
Sweenie - I mentioned this in the Tokamak demos, snippets and tutorials/Island Storm demo, but this would be a good place for a box-based cylinder function.
As a first use of my administrative powers , I move my own thread
Well, I thought someone else was going to do this, but eh. Here's what I got so far:
;The following by Bot_Builder
;************************
;Static Mesh commands.
;Use these to add a static mesh(a level,etc) to your tokamak scene.
;Call TOK_SetTextureMaterial to set a texture as a material. So, for instance if you
;had a metal.png texture you wanted to be assinged To material 4, you would call
;TOK_SetTextureMaterial("metal.png",4). Note that if you want a material to be set,
;you must call it before using Tok_AddMesh.
;Tok_AddMesh adds a blitz mesh to the tokamak staticmesh. Set recurse to 1 in order to
;recurse through a hierarchal mesh. Set mat to a value greater than -1 and texture
;assotiations are ignored, and the value of mat is used for the whole mesh. A value
;for mat of -2 and down means that the material will be used if a texture assotiations
;doesn't exist. The material that will be used in this case is -2-mat. So if mat is
;-5, and surface 1 doesn't have a material associated texture, the material index will
;be 3. Set Texture index to set which exture in a multi-textured mesh will determine
;the material properties.
;TOK_SetMesh finalizes mesh creation, sends it over to tokamak, and frees the banks.
;TOK_ReInitializeBanks re-creates the banks and sets the vars, so you can say, load
;a new level up.
;StripPath$ is a function I got from Peter Scheutz in "B3D text file format". You
;can use it in main proggie if you like.
;************************
Global vertices=CreateBank(0),triangles=CreateBank(0)
Global offsett,offsetv,tric,ttlvert,ttltris,cvt
Dim tname$(20),tMindex(20)
Global cur=0
Graphics3D 640,480
TOKSIM_CreateSimulator 0,-10,0
ni=LoadAnimMesh("ninja.b3d")
TOK_AddMesh(ni,1)
Function TOK_SetTextureMaterial(fil$,Mindex)
tname$(cur)=fil$
tMindex(cur)=Mindex
cur=cur+1
End Function
Function TOK_AddMesh(mesh,recurse=0,mat=-1,textureindex=0)
scount=CountSurfaces(mesh)
For ind=1 To scount
surface=GetSurface(mesh,ind)
ttltris=ttltris+CountTriangles(surface)
ttlvert=ttlvert+CountVertices(surface)
Next
ResizeBank triangles,ttltris*24
ResizeBank vertices,ttlvert*16
For ind=1 To scount
surface = GetSurface(mesh,ind)
bru=GetSurfaceBrush(surface)
tex=GetBrushTexture(bru,textureindex)
nam$=strippath$(TextureName$(tex))
fo=0
For a=0 To cur-1
If tname$(a)=nam$ Then mindex=tMindex(a):fo=1:Exit
Next
If fo=0 Then mindex=0
FreeBrush bru
FreeTexture tex
ctr=CountTriangles(surface)
tric=tric+cvt
cvt=CountVertices(surface)
;add vertices to bank
For v=0 To cvt-1
TFormPoint VertexX#(surface,v),VertexY#(surface,v),VertexZ#(surface,v),mesh,0
PokeFloat vertices,offsetv,TFormedX()
PokeFloat vertices,offsetv+4,TFormedY()
PokeFloat vertices,offsetv+8,TFormedZ()
PokeFloat vertices,offsetv+12,0.0
offsetv=offsetv+16
Next
;fill bank with triangles
For v=0 To ctr-1
PokeInt triangles,offsett,tric+TriangleVertex(surface,v,0)
PokeInt triangles,offsett+4,tric+TriangleVertex(surface,v,1)
PokeInt triangles,offsett+8,tric+TriangleVertex(surface,v,2)
If mat=-1 Then PokeInt triangles,offsett+12,mindex Else PokeInt triangles,offsett+12,mat
PokeInt triangles,offsett+16,0
PokeInt triangles,offsett+20,0
offsett=offsett+24
Next
Next
.rec
If recurse Then
children=CountChildren(mesh)
If children>0 Then
For childcount = 1 To children
child = GetChild(mesh,childcount)
If EntityClass$(child)="Mesh" Then TOK_AddMesh child,recurse,mat Else TOK_recurse child,mat
Next
EndIf
EndIf
End Function
Function TOK_recurse(mesh,mat)
children=CountChildren(mesh)
If children>0 Then
For childcount = 1 To children
child = GetChild(mesh,childcount)
If EntityClass$(child)="Mesh" Then TOK_AddMesh child,1,mat Else TOK_recurse child,mat
Next
EndIf
End Function
Function TOK_SetMesh()
;Hand over the terrain data to Tokamak
TOKSIM_SetStaticMesh vertices,ttlvert,triangles,ttltris
; Now we can free the banks as Tokamak has copied all data
FreeBank vertices
FreeBank triangles
End Function
Function StripPath$(file$)
If Len(file$)>0
For i=Len(file$) To 1 Step -1
mi$=Mid$(file$,i,1)
If mi$="\" Or mi$="/" Then Return name$ Else name$=mi$+name$
Next
EndIf
Return name$
End Function
Function TOK_ReInitializeBanks()
vertices=CreateBank(0)
triangles=CreateBank(0)
offsett=0
offsetv=0
tric=0
ttlvert=0
ttltris=0
End Function
;************************
;Geometry visualization commands
;With these, stay away from rotation as it introduces really wierd stuff. If
;someone knows how to fix this, please post on the Blitz-Tokamak forum,
;http://playerfactory.proboards25.com/index.cgi?board=tokamak
;************************
Function addbox(mesh,RigidBody,x#,y#,z#,Width#,Height#,Depth#,Pitch#=0,Yaw#=0,Roll#=0,alpha#=1)
TOKGEOM_SetPositionAndRotation TOKRB_AddBox(RigidBody,Width#,Height#,Depth#),x#,y#,z#,Pitch#,Yaw#,Roll#
If mesh Then c=CreateCube(mesh) Else c=CreateCube()
ScaleEntity c,Width#/2,Height#/2,Depth#/2,1
PositionEntity c,x#,y#,z#,1
RotateEntity c,Pitch#,Yaw#,Roll#,1
EntityAlpha c,alpha#
End Function
Function addcylinder(mesh,RigidBody,x#,y#,z#,Diameter#,Height#,Pitch#=0,Yaw#=0,Roll#=0,alpha#=1)
TOKGEOM_SetPositionAndRotation TOKRB_AddCylinder(RigidBody,Diameter#,Height#),x#,y#,z#,Pitch#,Yaw#,Roll#
If mesh Then c=CreateCylinder(8,True,mesh) Else c=CreateCylinder(8,True)
EntityAlpha c,alpha#
ScaleEntity c,Diameter#/2,Height#/2,Diameter#/2,1
sp1=CreateSphere(8,c)
ScaleEntity sp1,Diameter#/2,Diameter#/2,Diameter#/2,1
PositionEntity sp1,0,Height#/2,0,1
EntityAlpha sp1,alpha#
sp2=CreateSphere(8,c)
ScaleEntity sp2,Diameter#/2,Diameter#/2,Diameter#/2,1
PositionEntity sp2,0,Height#/-2,0,1
PositionEntity c,x#,y#,z#,1
RotateEntity c,Pitch#,Yaw#,Roll#,1
EntityAlpha sp2,alpha#
End Function
Function addsphere(mesh,RigidBody,x#,y#,z#,Diameter#,Pitch#=0,Yaw#=0,Roll#=0,alpha#=1)
TOKGEOM_SetPositionAndRotation TOKRB_AddSphere(RigidBody,Diameter#),x#,y#,z#,Pitch#,Yaw#,Roll#
If mesh Then c=CreateSphere(8,mesh) Else c=CreateSphere(8)
ScaleEntity c,Diameter#/2,Diameter#/2,Diameter#/2,1
PositionEntity c,x#,y#,z#,1
RotateEntity c,Pitch#,Yaw#,Roll#,1
EntityAlpha c,alpha#
End Function
;************************
;The unoficial format *.trb loader. Nothing actually produces this format
;yet, but my editer's getting there.
;************************
Function loadtrb(name$)
If Right$(name$,4)<>".trb" Then name$=name$+".trb"
fi=ReadFile(name$)
If ReadByte(fi)<>1 Then RuntimeError "Not trb file version 1"
rb=TOKRB_Create()
TOKRB_SetPosition rb,ReadFloat#(fi),ReadFloat#(fi),ReadFloat#(fi)
TOKRB_SetRotation rb,ReadFloat#(fi),ReadFloat#(fi),ReadFloat#(fi)
Select ReadByte(fi)
Case 1:
TOKRB_SetBoxInertiaTensor rb,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi)
Case 2:
TOKRB_SetSphereInertiaTensor rb,ReadFloat(fi),ReadFloat(fi)
Case 3:
TOKRB_SetCylinderInertiaTensor rb,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi)
End Select
TOKRB_SetMass rb,ReadFloat(fi)
TOKRB_SetLinearDamping rb,ReadFloat(fi)
TOKRB_SetAngularDamping rb,ReadFloat(fi)
TOKRB_CollideConnected rb,ReadByte(fi)
Co=ReadShort(fi)
For a=1 To Co
g=TOKRB_AddBox(rb,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi))
TOKGEOM_SetPositionAndRotation g,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi)
TOKGEOM_SetMaterialIndex g,ReadByte(fi)
Next
Co=ReadShort(fi)
For a=1 To Co
g=TOKRB_AddCylinder(rb,ReadFloat(fi),ReadFloat(fi))
TOKGEOM_SetPositionAndRotation g,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),EntityPitch#(ReadFloat(fi)),EntityYaw#(ReadFloat(fi)),EntityRoll#(ReadFloat(fi))
TOKGEOM_SetMaterialIndex g,ReadByte(fi)
Next
Co=ReadShort(fi)
For a=1 To Co
g=TOKRB_AddSphere(rb,ReadFloat(fi))
TOKGEOM_SetPositionAndRotation g,ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi),ReadFloat(fi)
TOKGEOM_SetMaterialIndex g,ReadByte(fi)
Next
End Function
;************************
;Create a "True tok cylinder"
;With this, you can make barrels, etc. In the future, I'd like to use
;multiple boxes for more accuraacy, but that's a complex prob.
;Mostly The Crow's code
;************************
Function CreateTokCylinder(crb,d#,h#)
Local s# = d# / Sqr(2)
TOKAB_AddCylinder crb,d#,h#
TOKRB_AddBox crb,s#,h#+d#,s#
End Function
Additions welcome (I'll add them into this main post)
Sweenie - I mentioned this in the Tokamak demos, snippets and tutorials/Island Storm demo, but this would be a good place for a box-based cylinder function.
As a first use of my administrative powers , I move my own thread