TOKRB_AddConvex()

Blitz3D Forums/Blitz3D Userlibs/TOKRB_AddConvex()

"BankSize% = Size of the bank holding vertex data."

I can't find how exactly you set the bank with the
vertex data ... I saw it anywhere but can't remember where...

Ideas?

thanks
Paolo.

Haha! Just did this :D

;APOE Physics Editor 0.1; output for Fighter1.3ds ;Hehe... Shameless :D
;Surfaces
;Object
Fighter1=TOKRB_Create()
TOKRB_SetPosition Fighter1,x#,y#,z#
TOKRB_SetRotation Fighter1,pitch#,yaw#,roll#
TOKRB_SetLinearDamping Fighter1,0.0
TOKRB_SetAngularDamping Fighter1,0.0
TOKRB_SetMass Fighter1,0.0
TOKRB_SetBoxInertiaTensor Fighter1,12.8823,3.73948,13.1859,0.0
cHull = LoadConvex("Fighter1_CHull_0.off")
Geom_1=TOKRB_AddConvex(Fighter1,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_1,-3.95875,0.0196976,-2.448,0.0,0.0,0.0
cHull = LoadConvex("Fighter1_CHull_1.off")
Geom_2=TOKRB_AddConvex(Fighter1,cHull,BankSize(cHull))
TOKGEOM_SetPositionAndRotation Geom_2,3.8196,0.0196976,-2.448,0.0,0.0,0.0
Function LoadConvex(file$)
 Convex = CreateBank(FileSize(file$))
 filein = ReadFile(file$)
 For i=0 To FileSize(file$)
  PokeByte Convex,i,ReadByte(filein)
 Next
 CloseFile filein
 Return Convex
End Function


Oh right... and code doesn't work (and don't try it) because I need to set some default values for stuff.

USe the LoadConvex() function to load your convex hull, then when calling TokRB_AddConvex use BankSize(cHull) to get (duh!) the bank size :D



And you probably saw it in the Convex example in the Tokamak wrapper folder.

Thanks Picklesworth,
but you didn't get me ... :)
What I need is how to create your ".off" file for a certain mesh.

The help for the static mesh is very clear, it says you need
16 bytes for each vertice, the first 12 are 3 floats representing X,Y,Z,
and 24 bytes for each triangle where the first 12 are 3 integers
representing the 3 corners, the next integer is the material index ...

I need the same information about how to set the bank's floats and integers for convex bodies...

Paolo.

I've the same problems. I think it would be usefull to have a function to convert ".x" or ".b3d" files to ".off" files.

thanks

I'm assuming that you have QHull and Adjacency.
This module for my program here (posted below) doesn't work on its own (at all) but maybe it will help you out a bit. Look at the writefile bits. The create_tok_hull function creates a .off file using qhull (this .off file is used to make a mesh out of Adjacency's output). Then (I got lazy, but hey, it works!) it creates a file very ridiculously similar to that which was sent to qhull, and sends it to Adjacency via the command line. Adjacency then creates the requested .bin file, which is loaded into Tokamak.

Obviously, this will all be made easier "Shortly". Right now, I have two stupid busy-work assignments to do for school.

Here's that code:
;11/06/2004 : Fixed a memory release bug in the dll source.

;-------------------------------------------------------------------------
;qhull1.dll is a modified and simplified version of qhull1.0
;By Tsiantas Elias 20 feb 2004.
;Full modified source is provided.
;-------------------------------------------------------------------------


;-------------------------------------------------------------------------
;
;Qhull1.0 is free software and hence this modification falls under
;the same license as qhull1.0.
;It may be obtained via anonymous ftp from geom.umn.edu
;Read the Copying_qhull.rtf for more.
;
;
;                     Copyright (c) 1993
;       The National Science And Technology Research Center For
;	     Computation And Visualization of Geometric Structures
;
;-------------------------------------------------------------------------








;YOU NEED THIS DECLS FILE

;.lib "qhull1.dll"
;qhull%(infile$,outfile$,delta#,pf%,fo%):"qh@20"





;*************************************************************************

;weld global stuff


;Edited (Deformed) by Dylan McCall, May 2005 for APOE Convex Hull Function

Dim txv(3)

Type TRIS
	Field x0#,y0#,z0#
	Field x1#,y1#,z1#
	Field x2#,y2#,z2#
End Type

Const GW_CHILD = 5




;*************************************************************************








;*************************************************************************
;The main function that calls the qhull dll
;It returns a welded convex hull of the model you send it.

Dim qvix#(0),qviy#(0),qviz#(0)


Function create_Tok_Hull(entity,file$,file2$,file3$,delta#,weld=1)


Local points%
Local vx#,vy#,vz#
Local k%,l%
Local count%,res%
Local v0%,v1%,v2%
Local vt0%,vt1%,vt2%
Local t_f$
Local rx#,ry#,rz#
Local px#,py#,pz#
Local su,hull


rx=EntityPitch(entity,1)
ry=EntityYaw(entity,1)
rz=EntityRoll(entity,1)
RotateEntity entity,0,0,0,1
px=EntityX(entity,1)
py=EntityY(entity,1)
pz=EntityZ(entity,1)
PositionEntity entity,0,0,0,1


Local surfs=CountSurfaces(entity)
If surfs=0 Return 0


For k=1 To surfs
	surf=GetSurface(entity,k)
	points=points+CountVertices(surf)
Next

Dim qvix#(points),qviy#(points),qviz#(points)


Local fil=WriteFile(file$)
WriteLine fil,"3";! dimension 3d
WriteLine fil,points


count=0

For k=1 To surfs
	surf=GetSurface(entity,k)
	
	For l=0 To CountVertices(surf)-1
		vx=VertexX(surf,l)
		vy=VertexY(surf,l)
		vz=VertexZ(surf,l)
		TFormPoint vx,vy,vz,entity,0
		WriteLine fil,TFormedX()+" "+TFormedY()+" "+TFormedZ()
		qvix(count)=TFormedX()
		qviy(count)=TFormedY()
		qviz(count)=TFormedZ()
		count=count+1
	Next

Next


CloseFile fil


;set it back to it's original rotation and position
RotateEntity entity,rx,ry,rz,1
PositionEntity entity,px,py,pz,1

;make sure the file is written before we call qhull1.dll
While FileType(file$)<>1
Delay 30
Wend


;Executing external file in order to prevent loss of data if the generator crashes
;---------------------------------
;force qhull1.dll to print out only
;face indices and force output
;hWnd = SystemProperty("AppHWND")
;Exec$=Chr$(34)+apppath$+"CHull_CrashShield.exe"+Chr$(34)+hWnd+" <> "+file$ + " > " + file2$ + " < " + delta
;ExecFile Exec$
res=qhull(file$,file2$,delta,1,1)

;---------------------------------

Local CShieldHWnd=api_GetWindow(hWnd,GW_CHILD)
If CShieldHWnd=0
    Repeat
        Delay 10
        CShieldHWnd=api_GetWindow(hWnd,GW_CHILD)
        If Not CShieldHWnd=0 Then DebugLog "1" : Exit
    Forever
EndIf
Repeat
    Delay 50
    If api_IsWindow(CShieldHWnd)=0 Then DebugLog "2" : Exit
Forever


For loop = 0 To 500
    Delay 50
    If FileType(file2$)=1 Then Exit
Next
If FileType(file2$)=0
    Dim qvix(0),qviy(0),qviz(0)
    ;Delete the Input file
    While FileType(file$)=1
        Delay 30
        DeleteFile file$
    Wend
    Return 0
EndIf


fil=ReadFile(file2$)

t_f$=ReadLine(fil)
If Lower(Left$(t_f$,1))<>"n"
    Dim qvix(0),qviy(0),qviz(0)
    ;Delete the Input file
    While FileType(file$)=1
        Delay 30
        DeleteFile file$
    Wend
    ;Delete the output file
    While FileType(file2$)=1
        Delay 30
        DeleteFile file2$
    Wend
    Return 0
EndIf

;create the hull mesh
hull=CreateMesh()
su=CreateSurface(hull)

While Not Eof(fil)
t_f$=ReadLine(fil)
	If t_f$<>""
		v0=get_string(t_f$,1)
		v1=get_string(t_f$,2)
		v2=get_string(t_f$,3)
			vt0=AddVertex ( su,qvix(v0),qviy(v0),qviz(v0) )
			vt1=AddVertex ( su,qvix(v1),qviy(v1),qviz(v1) )
			vt2=AddVertex ( su,qvix(v2),qviy(v2),qviz(v2) )
		
			AddTriangle (su,vt0,vt2,vt1)
	EndIf
Wend

CloseFile fil

;Adjacency input (replace QHull mesh generator input)

;Delete the original input file
While FileType(file$)=1
Delay 30
DeleteFile file$
Wend

While FileType(file$)=1
Delay 30
Wend

rx=EntityPitch(entity,1)
ry=EntityYaw(entity,1)
rz=EntityRoll(entity,1)
RotateEntity entity,0,0,0,1
px=EntityX(entity,1)
py=EntityY(entity,1)
pz=EntityZ(entity,1)
PositionEntity entity,0,0,0,1


surfs=CountSurfaces(entity)
If surfs=0 Return 0

points = 0


For k=1 To surfs
	surf=GetSurface(entity,k)
	points=points+CountVertices(surf)
Next

fil=WriteFile(file$)
WriteLine fil,points + " " + chr(10)


count=0

For k=1 To surfs
	surf=GetSurface(entity,k)

	For l=0 To CountVertices(surf)-1
		vx=VertexX(surf,l)
		vy=VertexY(surf,l)
		vz=VertexZ(surf,l)
		TFormPoint vx,vy,vz,entity,0
		If Not l = CountVertices(surf)-1 Then space$ = "  " + chr(10) Else space$ = ""
		WriteLine fil,TFormedX()+" "+TFormedY()+" "+TFormedZ() + space$
		count=count+1
	Next

Next


CloseFile fil


;set it back to it's original rotation and position
RotateEntity entity,rx,ry,rz,1
PositionEntity entity,px,py,pz,1

;make sure the file is written before we run Adjacency
While FileType(file$)<>1
Delay 30
Wend

Exec$=chr(34)+appPath$+"CHullData\adjacency.exe"+chr(34)+" "+replace(file$,filedir(file$),"")+" "+file3$
DebugLog exec$
ChangeDir appPath$+"CHullData"
ExecFile Exec$ ;chr(34)+"adjacency.exe"+chr(34)+" 0.txt 0.bin"

;make sure the file is written Before we continue
While FileType(file3$)<>1
Delay 30
Wend

;Delete the Input file
While FileType(file$)=1
Delay 30
DeleteFile file$
Wend

;Delete the output file
While FileType(file2$)=1
Delay 30
DeleteFile file2$
Wend

;If res<>1 Return 0;failure


Dim qvix(0),qviy(0),qviz(0)

;weld the hull
If weld weld3(hull)

UpdateNormals hull
Return hull

End Function



;*************************************************************************
;function needed to parse the output from qhull1.dll
Function get_string$(in$, num)
stemp$ = ""
numtemp = 0
For temp=1 To Len(in$)
If Mid(in$, temp, 1)<>" "
stemp$ = stemp$ + Mid(in$, temp, 1)
Else
numtemp = numtemp + 1
If numtemp = num
Return stemp$
Else
stemp$ = ""
EndIf
EndIf
Next
Return ""
End Function



;*************************************************************************
;
;Cutdown version of the weld routine from the code archives

Function Weld3(mesh,thresh#=0.001)
	Dim txv(3)

		Local su=GetSurface(mesh,1)

		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\x1# = VertexX(su,txv(1))
			vq\y1# = VertexY(su,txv(1))
			vq\z1# = VertexZ(su,txv(1))
			vq\x2# = VertexX(su,txv(2))
			vq\y2# = VertexY(su,txv(2))
			vq\z2# = VertexZ(su,txv(2))
		Next
		
		ClearSurface su
		
		For vq.tris = Each tris
		
				vt1=findvert3(su,vq\x0#,vq\y0#,vq\z0#,thresh#)
				
				If vt1=-1 Then

					vt1=AddVertex(su,vq\x0#,vq\y0#,vq\z0#)
					vt1 = mycount
					mycount = mycount +1
				EndIf
		
				vt2=findvert3(su,vq\x1#,vq\y1#,vq\z1#,thresh#)
				
				If Vt2=-1 Then

					vt2=AddVertex( su,vq\x1#,vq\y1#,vq\z1#)
					vt2 = mycount
					mycount = mycount +1
				EndIf
				
				vt3=findvert3(su,vq\x2#,vq\y2#,vq\z2#,thresh#)
				
				If vt3=-1 Then

					vt3=AddVertex(su,vq\x2#,vq\y2#,vq\z2#)
					vt3 = mycount
					mycount = mycount +1
				EndIf
		
			AddTriangle su,vt1,vt2,vt3
		
		Next
		
		Delete Each tris
		mycount=0

UpdateNormals(mesh)

End Function

	
Function findvert3(su,x2#,y2#,z2#,thresh#);
	
	For t=0 To CountVertices(su)-1;
		If Abs(VertexX(su,t)-x2#)<thresh# Then 
			If Abs(VertexY(su,t)-y2#)<thresh# Then 
				If Abs(VertexZ(su,t)-z2#)<thresh# Then 
							Return t
						EndIf
					EndIf
				EndIf
	Next
	Return -1
End Function




;*************************************************************************



Oh yah, and you may notice that I am suddenly using .bin files instead of .off files.
It turns out that the .off files are actually only for easily getting the shape of the convex hull (or something) and are recieved from qhull. These have nothing to do with the .bin file created by Adjacency (thankfully, using a nearly - possibly copmletely and I'm just dumb - identical input file). The .bin file is what Tokamak uses.

Errr, what is a convex hull?

Sorry to lower the standard of the post ;)

Rhy :)

A convex hull is a thing that many physics engines use to create more precise simulations.
Basically, it is a version of a mesh with no internal angles (it's convex), which can then recieve very precise collisions.

Ah, I think I understand. You mean there are no 'corners', everything is curved? But I see, its bascialy a more accurate representation of the mesh.

Cheers :)