problems with x files?

Blitz3D Forums/Blitz3D Programming/problems with x files?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I looked in the code archives to try to find a way so that my program will create random meshes and save them together as a single x file. What happens is instead of saving them together, the program resaves the previous mesh as the newer x file, so all I get is one single object in the x file instead of all the meshed created. Here is the code.




Graphics3D 800,600

light=CreateLight()
camera=CreateCamera()
SetBuffer BackBuffer()


t#=1.0




t1=OpenFile(t# + ".txt")

;make a text file called bogus and place it in the directory

t2=OpenFile(t#+ ".txt")

t3=WriteFile("completelevel.x")

;;start off with a random mesh

SeedRnd(MilliSecs())






If Rnd(5)>-1 And Rnd(5)<2 Then mesh=CreateCube()



If Rnd(5)>1 And Rnd(5)<3 Then mesh=CreateSphere()



If Rnd(5)>2 And Rnd(5)<4 Then mesh=CreatePlane()





If Rnd(5)>3 And Rnd(5)<5 Then mesh=CreateCylinder()



If Rnd(5)>4 And Rnd(5)<6 Then mesh=CreateCone()









Function SaveMeshX(mesh,file$)
;single surface at the moment
cnt=CountSurfaces(mesh)

If cnt=0 Then Return

;TODO Multi surface support
surf=GetSurface(mesh,1)

out=WriteFile(file$)
WriteLine out,"xof 0302txt 0064"
WriteLine out,""
WriteLine out,"Header {"
WriteLine out," 1;"
WriteLine out," 0;"
WriteLine out," 1;"
WriteLine out,"}"
WriteLine out,""
WriteLine out,"Frame frm_Scene_Root {"
WriteLine out," FrameTransformMatrix {"
WriteLine out," 1.000000,0.000000,0.000000,0.000000,"
WriteLine out," 0.000000,1.000000,0.000000,0.000000,"
WriteLine out," 0.000000,0.000000,1.000000,0.000000,"
WriteLine out," 0.000000,0.000000,0.000000,1.000000;"
WriteLine out,"}"
WriteLine out,""
name$=EntityName$(mesh)
If name$="" Then name$="Unknown"
WriteLine out,"Frame frm_"+name$+" {"
WriteLine out," FrameTransformMatrix {"
WriteLine out," 1.000000,0.000000,0.000000,0.000000,"
WriteLine out," 0.000000,1.000000,0.000000,0.000000,"
WriteLine out," 0.000000,0.000000,1.000000,0.000000,"
WriteLine out," 0.000000,0.000000,0.000000,1.000000;"
WriteLine out,"}"
WriteLine out,""
WriteLine out," Mesh frm_"+name$+" {"
WriteLine out,CountVertices(surf)+";"
For a=0 To CountVertices(surf)-1
WriteLine out,VertexX(surf,a)+";"+VertexY(surf,a)+";"+VertexZ(surf,a)+";,"
Next
WriteLine out,""
WriteLine out," "+CountTriangles(surf)+";"
For a=0 To CountTriangles(surf)-1
in1=TriangleVertex(surf,a,0)
in2=TriangleVertex(surf,a,1)
in3=TriangleVertex(surf,a,2)
ln$=" 3;"+in1+","+in2+","+in3+";"
If a=CountTriangles(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
WriteLine out,ln$
Next
WriteLine out,""

WriteLine out,"MeshMaterialList {"
WriteLine out,"1;"
WriteLine out,"1;"
WriteLine out,"0;;"
WriteLine out,""
WriteLine out,"Material {"

WriteLine out," 1.000000,1.000000,1.000000,1.000000;;";rgba
WriteLine out," 1.000000;"
WriteLine out," 0.500000,0.500000,0.500000;;"
WriteLine out," 0.000000,0.000000,0.000000;;"
WriteLine out,"}"
WriteLine out,"}"
WriteLine out,""
WriteLine out,"MeshNormals {"
WriteLine out,CountVertices(surf)+";"
For a=0 To CountVertices(surf)-1
WriteLine out,VertexNX(surf,a)+";"+VertexNY(surf,a)+";"+VertexNZ(surf,a)+";,"
Next
WriteLine out," "+CountTriangles(surf)+";"
For a=0 To CountTriangles(surf)-1
in1=TriangleVertex(surf,a,0)
in2=TriangleVertex(surf,a,1)
in3=TriangleVertex(surf,a,2)
ln$=" 3;"+in1+","+in2+","+in3+";"
If a=CountTriangles(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
WriteLine out,ln$
Next
WriteLine out,"}"
WriteLine out,""

WriteLine out,"MeshTextureCoords {"
WriteLine out,CountVertices(surf)+";"
For a=0 To CountVertices(surf)-1
ln$=VertexU(surf,a)+";"+VertexV(surf,a)+";"
If a=CountVertices(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
WriteLine out,ln$
Next
WriteLine out," }"
WriteLine out," }"
WriteLine out," }"
WriteLine out,"}"
CloseFile out
End Function





While Not KeyDown(1)


file$=(t#+".txt")









If KeyDown(57) Then End



If KeyHit(28)

SaveMeshX(mesh,file$)
EndIf





If t1 And t2 And t3 Then
While Not Eof(t1):WriteLine(t3,ReadLine$(t1)):Wend
While Not Eof(t2):WriteLine(t3,ReadLine$(t2)):Wend
EndIf











RenderWorld
UpdateWorld





Text 300,200, "Press Enter to create another model"
Text 300,220, "Press Space to save and exit"

Text 300,240, "T#"
Text 340,240,t#


Flip
Wend
End