3d format

Blitz3D Forums/Blitz3D Programming/3d format

David819(Posted 2004) [#1]
Hi i have been working on a 3d format but have got stuck on a section i want the code to store the order which the triangle is make but do not know how, can anyone please help me, here is my code so far...
Function save3d(Name$,Obj$)

file=WriteFile(Name$+".3d")
surf=GetSurface(Obj$,0)
For v=1 To CountVertices(surf)
VX#=VertexX(surf,v) : VY#=VertexY(surf,v)
VZ#=VertexZ(surf,v) : VU#=VertexU(surf,v)
VV#=VertexV(surf,v)
WriteLine(file," "+VX#+" "+VY#+" "+VZ#+" "+VU#+" "+VV#)
Next

For tv=1 To CountTriangles(surf)
V0=TriangleVertex(surf,tv,0)
V1=TriangleVertex(surf,tv,1)
V2=TriangleVertex(surf,tv,2)
WriteLine(file," "+v0+" "+v1+" "+v2)
Next


End Function


big10p(Posted 2004) [#2]
Have a look at the CountTriangles() and TriangleVertex() commands. Also, this:

For v=1 To CountVertices(surf)


should be changed to this:

For v=0 To CountVertices(surf)-1


as vertex indices start at 0, not 1.


David819(Posted 2004) [#3]
ok but how to you get the order of the joining of the vertixes?


jfk EO-11110(Posted 2004) [#4]
Best would be to first write all vertices, number zero to the last one. Then parse all triangles. Now only ask, which vertex each corner of the triangle is using, this will give you the vertex index, anumber between zero and the-latest-vertex in the list. This is also called indexed Vertices. So first you have a list withvertices: x,y,z,u,v, then follows a list with triangles: three numbers, indicating which Verticdes from the above list are used.

TriangleVertex will return the number of the vertex that is used with a specific corner of a triangle. So you don't need any order, simply store all vertices, then all triangles. This method will also allow to store socalled "shared vertices", these are corners which are used by more than one triangle.


David819(Posted 2004) [#5]
thanks


Stevie G(Posted 2004) [#6]
I use this to save my own meshes ...

Function save_mesh(mesh,mesh_name$)
        mesh_name$=mesh_name$+".my3d"
	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


and this to load them ..

Function load_mesh(mesh_name$,blend,fx,parent,show)

	DebugLog mesh_name$

	mesh_name=mesh_name+".my3d"

	file=ReadFile(mesh_name$)
	
	If file=0 RuntimeError mesh_name+": MY3D not found !!"
	
	mesh=CreateMesh(parent):surfaces=ReadInt(file)
	
	For surface_num=1 To surfaces
					
		temp_surface=CreateSurface(mesh) 
		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
	
	EntityBlend mesh,blend
	EntityFX mesh,fx
	If Not show HideEntity mesh
	
	Return mesh
End Function	


May be of some use to use.


David819(Posted 2004) [#7]
thanks i should be able to do it now.