Code: Select all
; MS3D ASCII Loader for PureBasic
Structure _Vertex
x.f
y.f
z.f
u.f
v.f
nx.f
ny.f
nz.f
EndStructure
Structure _TextureCoordinatesUV
u.f
v.f
EndStructure
Structure _Normals
x.f
y.f
z.f
EndStructure
Structure _Triangle
a.w
b.w
c.w
EndStructure
Structure MS3D_Mesh
vertices.l
Material.l
normals.l
triangles.l
EndStructure
Dim Meshes.MS3D_Mesh(1)
Global NewList LoadMS3D_Lines.s()
Procedure LoadMS3D(file$,start_mesh,scale.f)
Dim Meshdata._vertex(0)
Dim Meshtri._triangle(0)
ClearList(LoadMS3D_Lines())
file = ReadFile(#PB_Any,file$)
If file
Repeat
line$ = ReadString(file)
If line$ And Left(line$,2)<>"//"
AddElement(LoadMS3D_Lines())
LoadMS3D_Lines() = line$
EndIf
Until Eof(file)
CloseFile(file)
If FirstElement(LoadMS3D_Lines())=0
ProcedureReturn 0
EndIf
Frames = Val(StringField(LoadMS3D_Lines(),2," "))
Debug "Frames: "+Str(Frames)
NextElement(LoadMS3D_Lines())
Frame = Val(StringField(LoadMS3D_Lines(),2," "))
Debug "Frame: "+Str(Frame)
NextElement(LoadMS3D_Lines())
Meshes = Val(StringField(LoadMS3D_Lines(),2," "))
Debug "Meshes: "+Str(Meshes)
NextElement(LoadMS3D_Lines())
Dim MeshInfo.MS3D_Mesh(Meshes)
mesh=CreateMesh(#PB_Any)
For a = 1 To Meshes
AddSubMesh()
Name$ = StringField(LoadMS3D_Lines(),1," ")
Debug "Mesh: "+Name$
Flags = Val(StringField(LoadMS3D_Lines(),2," "))
Debug " Flags : "+Str(Flags)
MeshInfo(a)\Material = Val(StringField(LoadMS3D_Lines(),3," "))
Debug " MaterialIndex: "+Str(MeshInfo(a)\Material)
NextElement(LoadMS3D_Lines())
Vertices = Val(LoadMS3D_Lines())
MeshInfo(a)\vertices = Vertices
Debug " Vertices: "+Str(Vertices)
NextElement(LoadMS3D_Lines())
ReDim Meshdata(Vertices)
For b = 1 To Vertices
MeshData(b)\x = ValF(StringField(LoadMS3D_Lines(),2," "))
MeshData(b)\y = ValF(StringField(LoadMS3D_Lines(),3," "))
MeshData(b)\z = ValF(StringField(LoadMS3D_Lines(),4," "))
MeshData(b)\u = ValF(StringField(LoadMS3D_Lines(),5," "))
MeshData(b)\v = ValF(StringField(LoadMS3D_Lines(),6," "))
NextElement(LoadMS3D_Lines())
Next b
Normals = Val(LoadMS3D_Lines())
MeshInfo(a)\normals = Normals
Debug " Normals: "+Str(Normals)
NextElement(LoadMS3D_Lines())
For b = 1 To Normals
MeshData(b)\nx = ValF(StringField(LoadMS3D_Lines(),1," "))
MeshData(b)\ny = ValF(StringField(LoadMS3D_Lines(),2," "))
MeshData(b)\nz = ValF(StringField(LoadMS3D_Lines(),3," "))
NextElement(LoadMS3D_Lines())
;*normals + SizeOf(_Normals)
Next b
Triangles = Val(LoadMS3D_Lines())
MeshInfo(a)\triangles = Triangles
Debug " Triangles: "+Str(Triangles)
NextElement(LoadMS3D_Lines())
ReDim Meshtri._triangle(triangles)
For b = 1 To Triangles
Meshtri(b)\a = Val(StringField(LoadMS3D_Lines(),2," "))
Meshtri(b)\b = Val(StringField(LoadMS3D_Lines(),3," "))
Meshtri(b)\c = Val(StringField(LoadMS3D_Lines(),4," "))
NextElement(LoadMS3D_Lines())
;*triangles + SizeOf(_Triangle)
Next b
SetMeshData(mesh,smesh,MeshData(),#PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate , 1, ArraySize(MeshData()))
SetMeshData(mesh,smesh,Meshtri(),#PB_Mesh_Face , 1, ArraySize(Meshtri()))
smesh+1
Next a
Materials = Val(StringField(LoadMS3D_Lines(),2," "))
Debug "Materials: "+Str(Materials)
NextElement(LoadMS3D_Lines())
For a = 1 To Materials
mesh = start_mesh+a-1
; material name
Debug " loading: "+LoadMS3D_Lines()
NextElement(LoadMS3D_Lines())
ambient_r.f = ValF(StringField(LoadMS3D_Lines(),1," "))
ambient_g.f = ValF(StringField(LoadMS3D_Lines(),2," "))
ambient_b.f = ValF(StringField(LoadMS3D_Lines(),3," "))
NextElement(LoadMS3D_Lines())
diffuse_r.f = ValF(StringField(LoadMS3D_Lines(),1," "))
diffuse_g.f = ValF(StringField(LoadMS3D_Lines(),2," "))
diffuse_b.f = ValF(StringField(LoadMS3D_Lines(),3," "))
NextElement(LoadMS3D_Lines())
specular_r.f = ValF(StringField(LoadMS3D_Lines(),1," "))
specular_g.f = ValF(StringField(LoadMS3D_Lines(),2," "))
specular_b.f = ValF(StringField(LoadMS3D_Lines(),3," "))
NextElement(LoadMS3D_Lines())
; skip emmisive colors
NextElement(LoadMS3D_Lines())
; skip shininess
NextElement(LoadMS3D_Lines())
; skip transparency
NextElement(LoadMS3D_Lines())
; colormap (texture)
texture$ = Trim(LoadMS3D_Lines())
texture$ = Left (texture$,Len(texture$)-1) ; remove "
texture$ = Right(texture$,Len(texture$)-1) ; remove "
Debug " -> File: "+texture$
If LoadTexture(mesh,texture$)=0
Debug "Cant load texture "+texture$+" !!!"
If CreateTexture(mesh,64,64)
If StartDrawing(TextureOutput(mesh))
Box(0,0,64,64,$00FFFF)
Box(5,5,54,54,$FF0000)
StopDrawing()
EndIf
Else
ProcedureReturn 0
EndIf
EndIf
If CreateMaterial(mesh,TextureID(mesh))
MaterialShadingMode (mesh, #PB_Material_Flat)
SetMaterialColor(mesh,#PB_Material_DiffuseColor,RGB(diffuse_r *$FF,diffuse_g *$FF,diffuse_b *$FF))
SetMaterialColor (mesh,#PB_Material_AmbientColor, RGB(ambient_r *$FF,ambient_g *$FF,ambient_b *$FF))
SetMaterialColor(mesh, #PB_Material_SpecularColor,RGB(specular_r*$FF,specular_g*$FF,specular_b*$FF))
Else
ProcedureReturn 0
EndIf
NextElement(LoadMS3D_Lines())
; skip alphamap
NextElement(LoadMS3D_Lines())
Next a
;mesh=CreateMesh(#PB_Any)
For a = 1 To Meshes
mesh = start_mesh+a-1
If CreateEntity(mesh,MeshID(mesh),MaterialID(start_mesh+MeshInfo(a)\Material),0,0,0)=0
ProcedureReturn 0
EndIf
ScaleEntity(mesh, scale, scale, scale)
Next a
ClearList(LoadMS3D_Lines())
ProcedureReturn a-1
EndIf
ProcedureReturn 0
EndProcedure