Problems loading a created mesh

Blitz3D Forums/Blitz3D Programming/Problems loading a created mesh

I'm fairly new to blitz3d but have programmed in a few other langauges (apple basic,atari basic, graforth, 6502 asm). I have written a program which takes a single surface .3ds mesh( created from a heightfield by cybermesh) and rebuilds it, creating surfaces based on elevation. then fetching tris from the source and assigning them to the proper surface. the program then saves the mesh using the b3dfile.bb functions, however when blitz loads the mesh it is not visible, I have tried everything I can think of (flipmesh, updatenormals) even retexturing the mesh to no avail....any ideas ?

Here is the created mesh prior to saving (quite visible lol)



I take it you've created a camera?

Post some code and I'm sure someone will take a look.

Glad to GfK, have not posted here before and am at a loss as to how to include html in my post etc.

I can't really tell what you're trying to do, from your description. We really need to see some code. However, you could try scaling the mesh up as the original .3ds mesh may be using a tiny scale.

tried scaling just prior to this reply...no luck, heres the source

;Init Window
Graphics3D 800,600
AppTitle "Mega Terrain 1.0"

Include "includes/b3dfile.bb"

Global number_brushes=8

Dim brush(number_brushes-1),my_surf(number_brushes-1),vx#(2),vy#(2),vz#(2)
Global ground_mesh
Global my_mesh
Global water,water_texture
Global ground_texture
Global sky
Global sky_image


my_mesh = CreateMesh ()

camera = CreateCamera ()
PositionEntity camera,0,1,0
CameraRange camera,.1,3000

;fog
	;CameraFogMode camera,1
	;CameraFogRange camera,10,2600
	;CameraFogColor camera,100,100,100


light = CreateLight()
PositionEntity light,0,6,0
AmbientLight 255,255,255


radius#=1500
altitude#=20
speed#=-0.1
oangle#=90

Load_Ground_Mesh ("meshes/ground.3ds")
Load_Ground_Texture ("gfx/ground.bmp",number_brushes,128)
Paint_Ground_Mesh ()
Load_Water()
Load_Sky()

Gosub saveit

sun = LoadSprite ("gfx/sun.bmp")
PositionEntity sun,0,1,2000
ScaleSprite sun,1000,1000
EntityFX sun,1
EntityShininess sun, 1

moon = LoadSprite ("gfx/moon.bmp")
PositionEntity moon,0,1,-2000
ScaleSprite moon,100,100

;Main Loop
While Not KeyDown(1)

 If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0
 If KeyDown( 203 )=True Then TurnEntity camera,0,1,0
 If KeyDown( 208 )=True Then MoveEntity camera,0,0,-.1
 If KeyDown( 200 )=True Then MoveEntity camera,0,0,.1
y#=0
If linepick(EntityX# (camera),200,EntityZ# (camera),0,-201,0)
y#=PickedY# ()+1-EntityY# (camera)
End If
 TranslateEntity camera,0,y#,0

;Update Water
EntityTexture water,water_texture,index
index=index+1:if index>14 Then index=0

;Update Clouds
PositionTexture sky_image,u_position#,v_position#
u_position#=u_position#+.0001

;Update Sun
  oy#=radius#*cos(oangle#)
  oz#=radius#*sin(oangle#)
  oangle#=oangle#+speed#
  oangle#= oangle# Mod 360
  PositionEntity sun,altitude#,oy#,oz#
  
  oy#=radius#*cos(oangle#+180)
  oz#=radius#*sin(oangle#+180)
  PositionEntity moon,altitude#,oy#,oz#

;Show It

UpdateWorld
RenderWorld
Flip

Wend

Function Load_Ground_Texture(File$,Frames,FrameSize)
	ground_texture=LoadAnimTexture ( File$,1,FrameSize,FrameSize,0,Frames )
	For f=0 To Frames-1
		brush(f)=CreateBrush ( 255,255,255 )
		BrushTexture brush(f),ground_texture,f
		my_surf(f) = CreateSurface ( my_mesh,brush(f))
		DebugLog "creating Brush : "+f
	Next
End Function

Function Load_Ground_Mesh(File$)
	ground_mesh=LoadMesh ( File$)
End Function

Function Paint_Ground_Mesh()
mesh_Height#=MeshHeight# (ground_mesh )
factor#=number_brushes/mesh_Height#
	For f=1 To CountSurfaces ( ground_mesh )
		surface=GetSurface ( ground_mesh, f )
		For g=0 To CountTriangles ( surface )-1
			average#=0
			For vertex=0 To 2
				vertex_index = TriangleVertex ( surface,g,vertex )
				vx#(vertex)=VertexX# ( surface,vertex_index )
				vy#(vertex)=VertexY# ( surface,vertex_index ):average#=average#+vy#(vertex)
				vz#(vertex)=VertexZ# ( surface,vertex_index )
			Next
			index=average#/3*factor#
			index=index+rnd(-1,1)
			If index<0 Then index=0
			If index>number_brushes-1 Then index=number_brushes-1
			If even
			v0 = AddVertex (my_surf(index), vx#(0),vy#(0),vz#(0), 1, 0) 
			v1 = AddVertex (my_surf(index), vx#(1),vy#(1),vz#(1), 1 ,1) 
			v2 = AddVertex (my_surf(index), vx#(2),vy#(2),vz#(2), 0 ,0)
			Else
			v0 = AddVertex (my_surf(index), vx#(0),vy#(0),vz#(0), 0 ,1) 
			v1 = AddVertex (my_surf(index), vx#(1),vy#(1),vz#(1), 0, 0) 
			v2 = AddVertex (my_surf(index), vx#(2),vy#(2),vz#(2), 1, 1)
			End If
			tri = AddTriangle (my_surf(index),v0,v2,v1)
			even=not even
			DebugLog "triangle : "+g+" brush = "+index+"  "+v0+", "+v2+" ,"+v1
		Next
	Next
	FreeEntity ground_mesh
	UpdateNormals my_mesh
	PositionEntity my_mesh,0,0,0
	EntityPickMode my_mesh,2
	EntityAutoFade my_mesh,100,300
End Function

Function Load_Water()
	water_texture=LoadAnimTexture ( "gfx/water.bmp",1,128,128,0,15 )
	water_texture2=LoadTexture ( "gfx/seafloor.bmp")
	ScaleTexture water_texture2,1000,1000
	water= CreatePlane ()
	water2= CreatePlane ()
	EntityTexture water2,water_texture2,0,0

	EntityFX water2,1
	PositionEntity water2,0,-2,0
	EntityTexture water,water_texture,0,1
		EntityAlpha water,.75
	EntityShininess water, 1
End Function

Function Load_Sky()
	sky=CreateSphere(32)
	ScaleEntity sky,2500,2500,2500
	sky_image=LoadTexture( "gfx/sky0.bmp" )
	EntityTexture sky,sky_image
	FlipMesh sky
End Function

.saveit

Global c_surfs=CountSurfaces(my_mesh)

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

; track down used textures (thanks Mark!)
For i=1 To c_surfs
 c_surf(i)= GetSurface(my_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$,"") ;<<<<<<<<<<<<<<<<<<<
 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


WriteBB3D( "meshes/wip.b3d",my_mesh )

For i=1 To c_surfs
 FreeBrush c_brush(i); release memory
 FreeTexture c_tex(i)
Next
Return



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 

			b3dEndChunk()	;end of BRUS chunk
		Next
		
		b3dBeginChunk( "NODE" )
			b3dWriteString( "my_mesh" )
			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 ) )
				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


I don't know what is causing your problems, but I do have some tips for you:

1 - Looking at this:
CameraRange camera,.1,3000
I suspect you will have problems later with z-fighting. Find out more here:
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

2 - Your sun, moon & sky don't actually have to be that far away as long as you control your render order (using EntityOrder or multiple renders):
Render sky first.
Then Sun.
Then Moon.
Then clouds (If you have any).
Everything else can be left as normal.

3 - does shininess on a sprite actually do anything?

4 - you can post pictures and code here using the forum tags:
http://www.blitzbasic.com/faq/faq_entry.php?id=2

Thanks Beaker,
1. camera range doesn't cause problems with my hardware at this point and this program is intended to be a mesh terrain editor for my own use but steve's z-buffer page is very informative.

2. sun/moon as well as the skysphere just experimental play, have only had blitz for a couple of weeks

3. nope shininess has absolutely no effect ... see 2 lol

4. appreciate the faq link, I looked all over the forums but rarely hit the home page

I have dumped the model to the debug log but everything looks right (To my inexperienced eye at least)

You might try examining your B3D model with this...

http://www.blitzbasic.com/codearcs/codearcs.php?code=665