Problem with Decorator

Community Forums/Developer Stations/Problem with Decorator

jfk EO-11110(Posted 2003) [#1]
Hi Terabit!

I had the following Problem: I was working with a Maplet Level that used the oldbric.bmp. This worked fine, and it even used to load the Texture from its original Location while I was texturing the Level in Decorator. But then I saved the Level and ended Decorator. Some time later I started it again and wanted to continue the Work, but now this Texture has disappeared. It was the second Texture, right after the Lightmap in the B3D File.

I checked the File, and it seems there is no longer the String "oldbric.bmp" in the File, instead there is an empty String ("") where it should be.

The problem is when I try to load it in Blitz it shows up without Lightmap. If I reload it in Decorator and save it after reassigning the Lightmap using F3, it produces an illegal Memory error in Blitz.

I tried to manually insert the "oldbric.bmp" in the File, but I wasn't successful for some reason.

Now I have a simple Question: how are the Textures stored in the File? Is there a way to replace a Texture with another Texturename? (In this case replace "" by "oldbric.bmp") ?

jfk EO-11110(Posted 2003) [#2]
Ok, I was able to solve the Problem myself. if anybody wonders how I did it, here is a B3D Mesh-Save Routine that will allow to replace a missing texturepath (as described above) with any name and explicitly define a lightmap texture, then save it includng Lightmap. Decorator will then be capable of loading it.

Of course, this Source can also be used to save any Mesh in B3D Format, as long as it has no Children. ATM it expects the Presence of a Lightmap and a 2nd Texture Coordinates Set, but this can be modified relatively easy.

ATM it uses Texture Index 0 for the Lightmap and 1 to n for the other Textures. The Index of the Brushes (1 to n) is similar to the index of the first Layer Textures (not the Lightmap)

Graphics3D 640,480,32,2
SetBuffer BackBuffer()

Include ""

MoveEntity cam,0,0,-50

EntityFX mesh,1

Global c_surfs=CountSurfaces(mesh)

Print "Nr. of Surfaces: "+ c_surfs

Dim c_surf(1000)
Dim c_brush(1000)
Dim c_tex(1000)
Dim c_tex_name$(1000)

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) )
 te$=TextureName$( c_tex(i) )
 ; this is used to replace an "empty" Path that prouced me Errors...
 If te$="" Then te$=CurrentDir$()+"oldbric.bmp"
 ; now cut off the path and leave the filename only
 While Mid$(te$,wo,1)<>"\" And wo >1
 Print c_tex_name$(i)

TextureCoords li,1
EntityTexture mesh,li,0,1




WriteBB3D( "temp2.b3d",mesh )


Function WriteBB3D( f_name$,mesh )

	file=WriteFile( f_name$ )

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

		b3dBeginChunk( "TEXS" )
		For i=0 To c_surfs
			b3dWriteString( c_tex_name$(i) ) ; textur nr 0
			If i<>0 Then
			b3dWriteInt( 1 )					;flags
			b3dWriteInt( 1 Or 65536)					;flags
			b3dWriteInt( 2 )					;blend
			b3dWriteFloat( 0 )					;x in tex 0
			b3dWriteFloat( 0 )					;y in tex 0
			b3dWriteFloat( 1 )					;x scale 1
			b3dWriteFloat( 1 )					;y scale 1
			b3dWriteFloat( 0 )					;rotation 0
		b3dEndChunk()	;end of TEXS chunk

		For i=1 To c_surfs
			b3dBeginChunk( "BRUS" )
			b3dWriteInt( 2 )					;textures per brush ; eg 2 with lightmap
			b3dWriteString( "brush"+(i) )		;name
			b3dWriteFloat( 1 )					;red
			b3dWriteFloat( 1 )					;green
			b3dWriteFloat( 1 )					;blue
			b3dWriteFloat( 1 )					;alpha
			b3dWriteFloat( 0 )				;shininess
			b3dWriteInt( 2 )					;blend
			b3dWriteInt( 1 )					;FX
			b3dWriteInt( i )					;texture index ?
			b3dWriteInt( 0 )					;lightmap texture index ?

			b3dEndChunk()	;end of BRUS chunk
		b3dBeginChunk( "NODE" )
			b3dWriteString( "entity_name_here!" )
			b3dWriteFloat( 0 )	;x_pos
			b3dWriteFloat( 0 )	;y_pos
			b3dWriteFloat( 0 )	;y_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( 2 )			;number of tex_coord sets (eg: 2 with lightmap)
			b3dWriteInt( 2 )			;coords per set (u,v,w?) 2 in uv, 3 in 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 ) )
		b3dEndChunk()	;end of VRTS chunk
		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 ) )
			b3dEndChunk()	;end of TRIS chunk
			first_vert=first_vert+CountVertices( surf )
	b3dEndChunk()	;end of MESH chunk
End Function


And this is the include File, taken from the B3D Format SDK:

;b3d file utils to be included

Dim b3d_stack(100)
Global b3d_file,b3d_tos

Function b3dSetFile( file )
End Function

;***** functions for reading from B3D files *****

Function b3dReadByte()
	Return ReadByte( b3d_file )
End Function

Function b3dReadInt()
	Return ReadInt( b3d_file )
End Function

Function b3dReadFloat#()
	Return ReadFloat( b3d_file )
End Function

Function b3dReadString$()
		If ch=0 Return t$
End Function

Function b3dReadChunk$()
	For k=1 To 4
	sz=ReadInt( b3d_file )
	b3d_stack(b3d_tos)=FilePos( b3d_file )+sz
	Return tag$
End Function

Function b3dExitChunk()
	SeekFile b3d_file,b3d_stack(b3d_tos)
End Function

Function b3dChunkSize()
	Return b3d_stack(b3d_tos)-FilePos( b3d_file )
End Function

;***** Functions for writing to B3D files *****

Function b3dWriteByte( n )
	WriteByte( b3d_file,n )
End Function

Function b3dWriteInt( n )
	WriteInt( b3d_file,n )
End Function

Function b3dWriteFloat( n# )
	WriteFloat( b3d_file,n )
End Function

Function b3dWriteString( t$ )
	For k=1 To Len( t$ )
		If ch=0 Return
	b3dWriteByte( 0 )
End Function

Function b3dBeginChunk( tag$ )
	For k=1 To 4
		b3dWriteByte(Asc(Mid$( tag$,k,1 )))
	b3dWriteInt( 0 )
	b3d_stack(b3d_tos)=FilePos( b3d_file )
End Function

Function b3dEndChunk()
	n=FilePos( b3d_file )
	SeekFile b3d_file,b3d_stack(b3d_tos)-4
	b3dWriteInt( n-b3d_stack(b3d_tos) )
	SeekFile b3d_file,n
End Function