Save Mesh?

Blitz3D Forums/Blitz3D Programming/Save Mesh?

John Blackledge(Posted 2003) [#1]
I've created a complicated structure ('manually') out of loaded individual entities, a town made out of buildings. Now I'd like to save it out of Blitz as a .X file. (Format doesn't matter so long as I can load it into a modeller and continue work on it.)

I've tried the code archives '3D Graphics/DirectX Exporter' (multi-surface exporter) in combination with Mark's AddMesh example, but it appears to create a mesh with zero triangles, zero surfaces etc.... Doh!

Anyone have any _reliable_ code which can do this?


Stevie G(Posted 2003) [#2]
I use these when making my own meshes - you would need to call the function recursively is you wanted to save any child objects though.

Function save_mesh(mesh,mesh_name$)
UpdateNormals mesh
file=WriteFile(mesh_name)
WriteInt(file,CountSurfaces(mesh))
For surface_num=1 To CountSurfaces(mesh)
temp_surface=GetSurface(mesh,surface_num)
WriteInt(file,CountVertices(temp_surface))
For vertex_num=0 To CountVertices(temp_surface)-1
WriteFloat(file,VertexX#(temp_surface,vertex_num))
WriteFloat(file,VertexY#(temp_surface,vertex_num))
WriteFloat(file,VertexZ#(temp_surface,vertex_num))
WriteFloat(file,VertexNX#(temp_surface,vertex_num))
WriteFloat(file,VertexNY#(temp_surface,vertex_num))
WriteFloat(file,VertexNZ#(temp_surface,vertex_num))
WriteFloat(file,VertexRed#(temp_surface,vertex_num))
WriteFloat(file,VertexGreen#(temp_surface,vertex_num))
WriteFloat(file,VertexBlue#(temp_surface,vertex_num))
WriteFloat(file,VertexU#(temp_surface,vertex_num))
WriteFloat(file,VertexV#(temp_surface,vertex_num))
WriteFloat(file,VertexW#(temp_surface,vertex_num))
Next
WriteInt(file,CountTriangles(temp_surface))
For triangle_num=0 To CountTriangles(temp_surface)-1
WriteInt(file,TriangleVertex(temp_surface,triangle_num,0))
WriteInt(file,TriangleVertex(temp_surface,triangle_num,1))
WriteInt(file,TriangleVertex(temp_surface,triangle_num,2))
Next
Next
CloseFile file
End Function




Function load_mesh(mesh_name$)

file=ReadFile(mesh_name$)

mesh=CreateMesh():surfaces=ReadInt(file)

For surface_num=1 To surfaces

temp_surface=CreateSurface(mesh):FreeBrush brush
vertices=ReadInt(file)
For vertex_num=0 To vertices-1
x#=ReadFloat(file):y#=ReadFloat(file):z#=ReadFloat(file)
temp_vertex=AddVertex(temp_surface,x,y,z)
nx#=ReadFloat(file):ny#=ReadFloat(file):nz#=ReadFloat(file)
VertexNormal temp_surface,temp_vertex,nx,ny,nz
r#=ReadFloat(file):g#=ReadFloat(file):b#=ReadFloat(file)
VertexColor temp_surface,temp_vertex,r,g,b
u#=ReadFloat(file):v#=ReadFloat(file):w#=ReadFloat(file)
VertexTexCoords temp_surface,temp_vertex,u,v,w
Next

triangles=ReadInt(file)
For triangle_num=0 To triangles-1
v0=ReadInt(file):v1=ReadInt(file):v2=ReadInt(file)
AddTriangle(temp_surface,v0,v1,v2)
Next
Next

CloseFile file

Return mesh
End Function


John Blackledge(Posted 2003) [#3]
Thanks for the code example, but can we just go over the basics? Suppose I have 3 houses, I want to do something like...

town = CreateMesh()
AddMesh house1, town
AddMesh house2, town
AddMesh house3, town
save_mesh(town,"C;\town.x")

... does the new mesh not contain all the surfaces that your function needs?


Neo Genesis10(Posted 2003) [#4]
John, in the case of your example it would be exported and loaded without problems. The only REAL difficulty I can see is that Stevie's code will NOT save texture information. See the GetSurfaceBrush and GetBrushTexture commands for details on how to write that information into your save and load routines.


John Blackledge(Posted 2003) [#5]
Great - I don't mind retexturing. But what did Stevie mean by 'you would need to call the function recursively is you wanted to save any child objects'?
Stevie?


jfk EO-11110(Posted 2003) [#6]
If the Houses are Children of a Parent Mesh or Pivot, so the whole Scene would contain several Meshes you would have to handle seperately.

But if you use Addmesh first to add everything to one Mesh (you should check if it looks right) then you don't need to check for children.

Have a look at the recuriseve Mesh Hierarchy displaying Examples from the 3D Code Archive.


John Blackledge(Posted 2003) [#7]
Err... Stevie G, I just took another look at your code - I don't see any definitions that look like any of the proprietory formats (.X or .3DS).
The whole point of this question was 'how can I save out an AddMesh'd entity that I can then load into an external editor?'


Perturbatio(Posted 2003) [#8]
You would need to know the specific format and customize the functions accordingly.

Here's one page that details the .x format - dated 1999 but still useful nonetheless.

*EDIT*
And another at microsoft.com (DX8.0)
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dx8_c/directx_cpp/graphics_xof_format_1h2e.asp

*EDIT*
This might also help, although it's in C, but it's DirectX 7 stuff
http://www.gamedev.net/reference/programming/features/d3d7im4/page2.asp


John Blackledge(Posted 2003) [#9]
Wonderful ;)

So can I just repeat the question one more time -
Does ANYBODY have a mesh > .X or mesh > .3DS file saver that actually works?
Cheers.


Perturbatio(Posted 2003) [#10]
Have you tried the .X exporter in the code archives?

here


John Blackledge(Posted 2003) [#11]
There's:
'Saving meshes x file format'
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=111
which only saves single sirfaces, and
'DirectX Exporter'
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=631
which as I said appears to save zero triangles, zero faces etc. Ho-hum.


Perturbatio(Posted 2003) [#12]
I should learn to read all of people's posts :)


John Blackledge(Posted 2003) [#13]
Also, just tried:
'Save mesh to .asc file format'
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=280
which only saves the first entity in a mesh.
Anybody???????


Shambler(Posted 2003) [#14]
I've just posted a tidied up version of my directX file exporter in the code archives.

@John if you have a problem with the function please send me the file it saves and I'll see if its something I can fix. email jm.keay@...


Stevie G(Posted 2003) [#15]
John,

Perhaps I should read your question in future. You probably wouldn't be able to load this format into an editor. I just use it to create my own meshes to load into my own B3d programs.

Sorry..


John Blackledge(Posted 2003) [#16]
@Stevie G - understandable, but I have created a complex shape using Blitz primitives.
@Shambler - no change on the new code. Zip on its way to you (Mark's Addmesh example + your code with textures defs removed.)
Thanks.