Source Code gone bad

Blitz3D Forums/Blitz3D Beginners Area/Source Code gone bad

jigga619(Posted 2004) [#1]
I am attempting to make a level editor that can make a level using seperate level parts that can be combined. I got it working only at one object,room1.x, a x file. I added another x file, block.x and now I am totally confused. I tried to combine the code from the archives concerning saving meshes as b3d files. What am I doing wrong? How do I correctly add different x objects?


;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;


Graphics3D 800,600,16
SetBuffer BackBuffer()
camera=CreateCamera()
light=CreateLight()

Include "b3dfile.bb"


PositionEntity camera,0,20,-90

;by default this will be the loaded mesh
mesh=LoadMesh("room1.x")


Global c_surfs=CountSurfaces(mesh)
Dim c_surf(c_surfs)
Dim c_brush(c_surfs)
Dim c_tex(c_surfs)
Dim c_tex_name$(c_surfs)










Function WriteBB3D( f_name$,mesh )

file=WriteFile( f_name$ )

b3dSetFile( file )

b3dBeginChunk( "BB3D" )
b3dWriteInt( 1 ) ;version

b3dBeginChunk( "TEXS" ) ; list all textures used by the mesh
For i=1 To c_surfs
b3dWriteString( c_tex_name$(i) ) ;texture file
b3dWriteInt( 1 ) ;flags
b3dWriteInt( 2 ) ;blend
b3dWriteFloat( 0 ) ;x in tex 0 (hu?)
b3dWriteFloat( 0 ) ;y in tex 0
b3dWriteFloat( 1 ) ;x scale 1
b3dWriteFloat( 1 ) ;y scale 1
b3dWriteFloat( 0 ) ;rotation 0

Next
b3dEndChunk() ;end of TEXS chunk


For i=1 To c_surfs
b3dBeginChunk( "BRUS" ) ; describe all brushes used by the mesh

b3dWriteInt( 1 ) ;number of textures per brush ; (eg 2 with lightmap)
b3dWriteString( "brush"+(i-1) ) ;brushname
b3dWriteFloat( 1 ) ;red
b3dWriteFloat( 1 ) ;green
b3dWriteFloat( 1 ) ;blue
b3dWriteFloat( 1 ) ;alpha
b3dWriteFloat( 0 ) ;shininess
b3dWriteInt( 1 ) ;blendmode
b3dWriteInt( 0 ) ;FX
b3dWriteInt( i-1 ) ;used texture index
; b3dWriteInt( ? ) ;additional texture index (eg lightmap), but here we only use 1 (see above)

b3dEndChunk() ;end of BRUS chunk
Next

b3dBeginChunk( "NODE" )
b3dWriteString( "entity_name_here!" )
b3dWriteFloat( 0 ) ;x_pos
b3dWriteFloat( 0 ) ;y_pos
b3dWriteFloat( 0 ) ;z_pos
b3dWriteFloat( 1 ) ;x_scale
b3dWriteFloat( 1 ) ;y_scale
b3dWriteFloat( 1 ) ;z_scale
b3dWriteFloat( 1 ) ;rot_w
b3dWriteFloat( 0 ) ;rot_x
b3dWriteFloat( 0 ) ;rot_y
b3dWriteFloat( 0 ) ;rot_z
WriteMESH( mesh )
b3dEndChunk() ;end of NODE chunk

b3dEndChunk() ;end of BB3D chunk

CloseFile file
End Function

Function WriteMESH( mesh )

n_surfs=CountSurfaces( mesh )

b3dBeginChunk( "MESH" )
b3dWriteInt( -1 ) ;no 'entity' brush -1

b3dBeginChunk( "VRTS" )
b3dWriteInt( 0 ) ;flags - 0=no normal/color
b3dWriteInt( 1 ) ;number of tex_coord sets (eg: 2 with lightmap)
b3dWriteInt( 2 ) ;coords per set (u,v,w?) 2 with uv, 3 with uvw

For k=1 To n_surfs
surf=GetSurface( mesh,k )
n_verts=CountVertices( surf )-1

For j=0 To n_verts
b3dWriteFloat( VertexX( surf,j ) )
b3dWriteFloat( VertexY( surf,j ) )
b3dWriteFloat( VertexZ( surf,j ) )
b3dWriteFloat( VertexU#( surf,j,0 ) )
b3dWriteFloat( VertexV#( surf,j,0 ) )
; b3dWriteFloat( VertexW#( surf,j,0 ) )
;; b3dWriteFloat( VertexU#( surf,j,1 ) ) ; lightmap uv
;; b3dWriteFloat( VertexV#( surf,j,1 ) ) ; lightmap uv
; b3dWriteFloat( VertexW#( surf,j,1 ) )
Next
Next
b3dEndChunk() ;end of VRTS chunk

first_vert=0
For k=1 To n_surfs
surf=GetSurface( mesh,k )
n_tris=CountTriangles( surf )-1

b3dBeginChunk( "TRIS" )
b3dWriteInt( k-1 ) ;brush for these triangles (surf -1 !!!)

For j=0 To n_tris
b3dWriteInt( first_vert+TriangleVertex( surf,j,0 ) )
b3dWriteInt( first_vert+TriangleVertex( surf,j,1 ) )
b3dWriteInt( first_vert+TriangleVertex( surf,j,2 ) )
Next

b3dEndChunk() ;end of TRIS chunk

first_vert=first_vert+CountVertices( surf )

Next

b3dEndChunk() ;end of MESH chunk

End Function







While Not KeyDown(1)




;models that will be used in program
If model#=0
meshname$="room1.x"
EndIf


If model#=1
meshname$="block.x"
EndIf




If KeyDown(200) And done#=1
tre#=1
c#=c#+1
MoveEntity mesh2,0,0,tre#
;PositionMesh mesh,a#,b#,c#

EndIf


If KeyDown(208) And done#=1
tre#=-1
c#=c#-1
MoveEntity mesh2,0,0,tre#
;PositionMesh mesh,a#,b#,c#
EndIf




If KeyHit(50) Then model#=model#+1





;do this section for each different model

If model#=0 And KeyHit(28)
done#=1
mesh2=LoadMesh("room1.x")


PositionMesh mesh2,a#,b#,c#


AddMesh mesh2,mesh



End If


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If model#=1
mesh=LoadMesh("block.x")
c_surfs=CountSurfaces(mesh)

For i=1 To c_surfs
c_surf(i)= GetSurface(mesh,i)
c_brush(i)=GetSurfaceBrush( c_surf(i) )
c_tex(i)=GetBrushTexture( c_brush(i) )
c_tex_name$(i)=Lower$(TextureName$( c_tex(i))) ; Full (!) Texture Path
curdir$=Lower$(CurrentDir$())
c_tex_name$(i)= Replace$(c_tex_name$(i),curdir$,"")
Next

PositionMesh mesh,a#,b#,c#

mesh5=CopyMesh(mesh)


EndIf









If KeyHit(57);save entire level


c_surfs=CountSurfaces(mesh)

Print "Mesh "+meshname$+" has "+ c_surfs+" Surfaces, using the following textures:"



; track down used textures (thanks Mark!)
For i=1 To c_surfs
c_surf(i)= GetSurface(mesh,i)
c_brush(i)=GetSurfaceBrush( c_surf(i) )
c_tex(i)=GetBrushTexture( c_brush(i) )
c_tex_name$(i)=Lower$(TextureName$( c_tex(i))) ; Full (!) Texture Path
curdir$=Lower$(CurrentDir$())
c_tex_name$(i)= Replace$(c_tex_name$(i),curdir$,"") ;<<<<<<<<<<<<<<<<<<<
Print c_tex_name$(i)
If c_tex_name$(i)="" Then Print "Error: Surface No."+i+" has no Texture"
If FileType(c_tex_name$(i))<>1 Then Print "Warning: Surface No."+i+" uses nonexistant Texture ("+c_tex_name$(i)+")."
Next

Print "Press any key to save this Mesh as TEMP.B3D"


WaitKey()

; end

WriteBB3D( "temp.b3d",mesh )


; test if it worked...
FreeEntity mesh
mesh2=LoadMesh("temp.b3d")

















End
EndIf





RenderWorld
UpdateWorld


Text 30,20,"Use arrow keys to move model and Press Enter to place"
Text 30,40,"Press Space to save entire level and exit"
Text 30,60,"Press M to change model number"
Text 30,80,"Model# = "+model#
Text 30,120,"z # is "+z#


Flip
Wend
End


jfk EO-11110(Posted 2004) [#2]
Eigther study the B3D Format and learn how to nest NODES to save multiple Child-Meshes inside one File, or, maybe easier, use the AddMEsh Command to add multiple MEshes together before you save it.

x1=loadmesh("chair.x")
x2=loadmesh("table.x")

x3=createmesh()

positionmesh x2,213,34,anywhere

addmesh x1,x3
addmesh x2,x3

freeentity x1
freeentity x2

now start with the saveB3D code to save x3