Here's code for a 3ds>CSM converter. If you need any more help, ask.
If Not Windowed3D() RuntimeError "You must change your desktop depth to 16-bit color to run this program."
Include "progs\functions\appendbyte.bb"
Include "progs\functions\appendint.bb"
Include "progs\functions\appendfloat.bb"
Include "progs\functions\appendshort.bb"
Include "progs\functions\appendstring.bb"
Include "progs\functions\pokestring.bb"
Include "light_progs\updatenormals.bb"
Const FLAG_SELECTED=0
Const FLAG_POLYGON=1
Const FLAG_NOSCALE=2
Const FLAG_AUTOMAP=3
Const FLAG_LIGHTMAPS=4
Const FLAG_VERTEXLIGHTS=5
Const FLAG_CASTLIGHTMAPSHADOWS=6
Const FLAG_CASTVERTEXSHADOWS=7
AppTitle "Cartography Shop model converter"
;file$=CommandLine()
;file=Remove(file,Chr(34))
;If file="" file$=RequestFile_("",CurrentDir(),"X or 3DS Model|*.*",0,0,0)
;If file="" End
patternposition=4
.loop
file$=getopenfile("Make prefab","","3D Studio file (*.3ds)|*.3ds|Microsoft DirectX Model (*.x)|*.x|Cartography Shop 3 map (*.csm)|*.csm|Blitz3D Model (*.b3d)|*.b3d|All files|*.*",patternposition)
If file="" End
patternposition=SelectedPattern()
If Lower(FileExtension(file))="csm"
convertcsm file
Else
convertmesh file
EndIf
If proceed("Convert more files?") Goto loop
End
Graphics3D 400,300,16,2
progpath$=CurrentDir()
ChangeDir progpath+"prefabs"
d=ReadDir(CurrentDir())
Repeat
file$=NextFile(d)
DebugLog file
Select FileType(file)
Case 0
Exit
Case 1
Case 2
If file<>"." And file<>".."
d2=ReadDir(file)
Repeat
file2$=NextFile(d2)
If file2="" Exit
DebugLog (file+"\"+file2)
Select FileType(file+"\"+file2)
Case 0
Exit
Case 1
ext$=Lower(FileExtension(file2))
If ext="3ds" Or ext="x" Or ext="b3d"
convertmesh file+"\"+file2
EndIf
Case 2
End Select
Forever
CloseDir d2
EndIf
End Select
Forever
CloseDir d
End
ChangeDir progpath+"meshes"
d2=ReadDir(CurrentDir())
If d2
Repeat
file2$=NextFile(d2)
If file2="" Exit
DebugLog (file2)
Select FileType(file2)
Case 0
Exit
Case 1
ext$=Lower(FileExtension(file2))
If ext="3ds" Or ext="x" Or ext="b3d"
convertmesh ".\"+file2
EndIf
Case 2
End Select
Forever
CloseDir d2
EndIf
Function convertcsm(file$)
f=ReadFile(file)
If Not f
notify "Failed to load "+Chr(34)+file+Chr(34)+"."
Return False
EndIf
groupbank=CreateBank()
lightmapbank=CreateBank()
meshbank=CreateBank()
entitybank=CreateBank()
version=ReadInt(f)
If version=2 Or version=4
notify Chr(34)+file+Chr(34)+" is not a Cartography Shop 3 map."
Return False
EndIf
SeekFile f,0
While Not Eof(f)
tag$=""
For n=0 To 3
tag=tag+Chr(ReadByte(f))
Next
size=ReadInt(f)
DebugLog " Loading "+tag+" - "+size+" bytes..."
endposition=size+FilePos(f)
Select tag
Case "LMAP"
width=ReadInt(f)
height=ReadInt(f)
;t=CreateTexture(width,height)
;TextureCoords t,1
;SetBuffer TextureBuffer(t)
;LockBuffer TextureBuffer(t)
appendInt lightmapbank,width
appendint lightmapbank,height
For x#=0 To width-1
For y#=0 To height-1
hue=ReadInt(f)
;appendbyte lightmapbank,Red(hue)
;appendbyte lightmapbank,Green(hue)
;appendbyte lightmapbank,Blue(hue)
appendint lightmapbank,hue
Next
Next
;UnlockBuffer TextureBuffer(t)
;SetBuffer BackBuffer()
clm=clm+1
;SaveBuffer TextureBuffer(t),"maps\"+FileName(file,False)+"_LM"+clm+".bmp"
;dynamicfile$="textures\specular\"+FileName(file,False)+clm+".bmp"
;If FileType(dynamicfile$)=1
; t2=LoadTexture(dynamicfile$)
; TextureCoords t2,1
; TextureBlend t2,3
; EndIf
;appendint lmapbank,t
; t2=CreateTexture(2048,2048)
; TextureBlend t2,2
;If FileType(dynamicfile$)=1
; t2=LoadTexture(dynamicfile$)
; TextureBlend t2,0
; TextureCoords t2,1
; sh.shader=New shader
; sh\texture=t
; sh\parameter[2]=t2
; sh\class=SHADER_DYNAMICLIGHT
; sh\image2=LoadImage("maps\"+FileName(file,False)+"_LM"+clm+".bmp")
; EndIf
;appendint addlmapbank,t2
;t2=0
Case "GRUP"
group=ReadInt(f)
r=ReadInt(f)
g=ReadInt(f)
b=ReadInt(f)
cgr=cgr+1
Case "BRUS"
flags=ReadInt(f)
hue=ReadInt(f)
group=ReadInt(f)
Properties$=ReadStringN(f)
surfcount=ReadInt(f)
appendint meshbank,(2^3+2^4+2^6+2^7)
appendint meshbank,group
appendstring meshbank,Properties$
appendint meshbank,0
appendint meshbank,0
appendint meshbank,0
appendfloat meshbank,0
appendfloat meshbank,0
appendfloat meshbank,0
appendint meshbank,surfcount
;mesh=CreateMesh(map)
;NameEntity mesh,Properties
;appendint entitybank,mesh
For s=1 To surfcount
nx#=ReadFloat(f)
ny#=ReadFloat(f)
nz#=ReadFloat(f)
d#=ReadFloat(f)
materialname$=readstringn(f)
lightmap=ReadInt(f)
uoffset#=ReadFloat(f)
voffset#=ReadFloat(f)
uscale#=ReadFloat(f)
vscale#=ReadFloat(f)
angle#=ReadFloat(f)
vertcount=ReadInt(f)
appendint meshbank,0
appendstring meshbank,materialname
appendint meshbank,lightmap
appendfloat meshbank,0
appendfloat meshbank,0
appendfloat meshbank,1
appendfloat meshbank,1
appendfloat meshbank,0
appendint meshbank,vertcount
appendint meshbank,vertcount-2
appendint meshbank,0
;skipface=False
;If Lower(FileName(materialname,False))="caulk" skipface=True
;If Not skipface
; surf=CreateSurface(mesh)
; b=retrievematerial("textures\"+materialname)
; If lightmap
; t=PeekInt(lmapbank,(lightmap-1)*4)
; BrushTexture b,t,0,LAYER_LIGHTMAP
; EndIf
; PaintSurface surf,b
; If lightmap reloadmaterial b;Get rid of lightmap texture
; EndIf
DebugLog vertcount
For v=0 To vertcount-1
x#=ReadFloat(f)
y#=ReadFloat(f)
z#=ReadFloat(f)
nx#=ReadFloat(f)
ny#=ReadFloat(f)
nz#=ReadFloat(f)
If nx<>0 RuntimeError nx
u0#=ReadFloat(f)
v0#=ReadFloat(f)
u1#=ReadFloat(f)
v1#=ReadFloat(f)
appendfloat meshbank,x
appendfloat meshbank,y
appendfloat meshbank,z
appendfloat meshbank,nx
appendfloat meshbank,ny
appendfloat meshbank,nz
appendint meshbank,128
appendint meshbank,128
appendint meshbank,128
appendfloat meshbank,u0
appendfloat meshbank,v0
appendfloat meshbank,0
appendfloat meshbank,u1
appendfloat meshbank,v1
appendfloat meshbank,0
;If Not skipface
; AddVertex surf,x,y,z,u0,v0
; VertexColor surf,v,255,255,255
; VertexNormal surf,v,nx,ny,nz
; VertexTexCoords surf,v,u1,v1,0,1
; If v>1 AddTriangle surf,v,v-1,0
; EndIf
Next
;triscount=ReadInt(f)
;DebugLog triscount
For v=2 To vertcount-1
appendint meshbank,v
appendint meshbank,0
appendint meshbank,v-1
Next
;appendint meshbank,0;no lines
Next
cmh=cmh+1
;UpdateNormals_ mesh
Case "ENTY"
flags=ReadInt(f)
x#=ReadFloat(f)
y#=ReadFloat(f)
z#=ReadFloat(f)
Properties$=ReadStringN(f)
group=ReadInt(f)
r=ReadInt(f)
g=ReadInt(f)
b=ReadInt(f)
width=ReadInt(f)
heigth=ReadInt(f)
cen=cen+1
appendint entitybank,0
appendint entitybank,0
appendstring entitybank,Properties$
appendfloat entitybank,x
appendfloat entitybank,y
appendfloat entitybank,z
;entity=CreateMesh(map)
;PositionEntity entity,x,y,z
;NameEntity entity,Properties
;appendint entitybank,entity
Case "MESH"
flags=ReadInt(f)
hue=ReadInt(f)
group=ReadInt(f)
Properties$=ReadStringN(f)
;If getproperty(Properties$,"classname")=""
; Properties$=Properties$+Chr(10)+Chr(34)+"classname"+Chr(34)+"="+Chr(34)+"staticmesh"+Chr(34)
; EndIf
materialname$=ReadStringN(f)
vertcount=ReadInt(f)
appendint meshbank,(2^5+2^6+2^7)
appendint meshbank,group
appendstring meshbank,Properties$
appendint meshbank,0
appendint meshbank,0
appendint meshbank,0
appendfloat meshbank,0
appendfloat meshbank,0
appendfloat meshbank,0
appendint meshbank,1; surface
appendint meshbank,0
appendstring meshbank,materialname
appendint meshbank,0
appendfloat meshbank,0
appendfloat meshbank,0
appendfloat meshbank,1
appendfloat meshbank,1
appendfloat meshbank,0
appendint meshbank,vertcount
pos=BankSize(meshbank)
appendint meshbank,0
appendint meshbank,0
;mesh=CreateMesh(map)
;NameEntity mesh,Properties
;surf=CreateSurface(mesh)
;appendint entitybank,mesh
;b=retrievematerial("textures\"+materialname)
;BrushShininess b,1
;PaintSurface surf,b
For v=0 To vertcount-1
x#=ReadFloat(f)
y#=ReadFloat(f)
z#=ReadFloat(f)
nx#=ReadFloat(f)
ny#=ReadFloat(f)
nz#=ReadFloat(f)
u0#=ReadFloat(f)
v0#=ReadFloat(f)
u1#=ReadFloat(f)
v1#=ReadFloat(f)
hue=ReadInt(f)
;AddVertex surf,x,y,z,u0,-v0
;VertexColor surf,v,Red(hue),Green(hue),Blue(hue)
;VertexNormal surf,v,nx,ny,nz
appendfloat meshbank,x
appendfloat meshbank,y
appendfloat meshbank,z
appendfloat meshbank,nx
appendfloat meshbank,ny
appendfloat meshbank,nz
appendint meshbank,128
appendint meshbank,128
appendint meshbank,128
appendfloat meshbank,u0
appendfloat meshbank,v0
appendfloat meshbank,0
appendfloat meshbank,u1
appendfloat meshbank,v1
appendfloat meshbank,0
Next
triscount=ReadInt(f)
PokeInt meshbank,pos,triscount
For t=0 To triscount-1
a=ReadInt(f)
b=ReadInt(f)
c=ReadInt(f)
appendint meshbank,b
appendint meshbank,a
appendint meshbank,c
Next
;UpdateNormals_ mesh
cmh=cmh+1
Default
DebugLog " Unknown chunk."
End Select
SeekFile f,endposition
Wend
CloseFile f
f=WriteFile(FileDir(file)+FileName(file,0)+"_v4.csm")
WriteInt f,4
WriteInt f,0
WriteInt f,0;clm
;For n=0 To BankSize(lightmapbank)-1
; WriteByte f,PeekByte(lightmapbank,n)
; Next
WriteInt f,cmh
For n=0 To BankSize(meshbank)-1
WriteByte f,PeekByte(meshbank,n)
Next
WriteInt f,cen
For n=0 To BankSize(entitybank)-1
WriteByte f,PeekByte(entitybank,n)
Next
WriteFloat f,0
WriteFloat f,500
WriteFloat f,-500
WriteFloat f,-45
WriteFloat f,0
CloseFile f
Return True
End Function
Function convertmesh(file$)
m=LoadAnimMesh(file)
If Not m
notify "Failed to load "+Chr(34)+file+Chr(34)
Return False
EndIf
outfile$=FileDir(file)+FileName(file,0)+".csm"
f=WriteFile(outfile)
If Not f
notify "Failed to write "+Chr(34)+outfile+Chr(34)
Return False
EndIf
WriteInt f,4;version
WriteInt f,0;groups
WriteInt f,0;lightmaps
WriteInt f,1;meshes
WriteInt f,2+2^FLAG_CASTLIGHTMAPSHADOWS+2^FLAG_VERTEXLIGHTS+2^FLAG_CASTVERTEXSHADOWS;flags
WriteInt f,1;group
WriteByte f,0;properties
WriteInt f,255
WriteInt f,255
WriteInt f,255
WriteFloat f,0
WriteFloat f,0
WriteFloat f,0
pos=FilePos(f)
WriteInt f,0
sum=writemesh(m,f)
WriteInt f,0;entities
WriteFloat f,0;x
WriteFloat f,500;y
WriteFloat f,-500;z
WriteFloat f,-45;pitch
WriteFloat f,0;yaw
SeekFile f,pos
WriteInt f,sum
CloseFile f
Return True
End Function
Function writemesh(m,f)
UpdateNormals m
LightMesh m,220,220,220,1000000,1000,1000,1000
For s=1 To CountSurfaces(m)
sum=sum+1
surf=GetSurface(m,s)
WriteInt f,0;flags
WriteByte f,0;texture
WriteInt f,0;lightmapindex
WriteFloat f,0
WriteFloat f,0
WriteFloat f,1
WriteFloat f,1
WriteFloat f,0
WriteInt f,CountVertices(surf)
WriteInt f,CountTriangles(surf)
WriteInt f,0;no lines
For v=0 To CountVertices(surf)-1
TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),m,0
WriteFloat f,TFormedX()
WriteFloat f,TFormedY()
WriteFloat f,TFormedZ()
TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),m,0
WriteFloat f,TFormedX()
WriteFloat f,TFormedY()
WriteFloat f,TFormedZ()
r=VertexRed(surf,v)
g=VertexGreen(surf,v)
b=VertexBlue(surf,v)
If r<50 r=50
If g<50 g=50
If b<50 b=50
WriteInt f,r
WriteInt f,g
WriteInt f,b
WriteFloat f,VertexU(surf,v)
WriteFloat f,-VertexV(surf,v)
WriteFloat f,VertexW(surf,v)
WriteFloat f,VertexU(surf,v,1)
WriteFloat f,VertexV(surf,v,1)
WriteFloat f,VertexW(surf,v,1)
Next
For t=0 To CountTriangles(surf)-1
WriteInt f,TriangleVertex(surf,t,1)
WriteInt f,TriangleVertex(surf,t,0)
WriteInt f,TriangleVertex(surf,t,2)
Next
Next
For c=1 To CountChildren(m)
sum=sum+writemesh(GetChild(m,c),f)
Next
Return sum
End Function
Function ReadStringN$(f,maxlength=0)
Repeat
ch=ReadByte(f)
If ch=0 Return t$
If maxlength
If Len(t$)=maxlength Return t$+Chr(ch)
EndIf
t$=t$+Chr$(ch)
Forever
End Function
Function GetOpenFile$(title$="",path$="",pattern$="All files|*.*",patternposition=0,multi=0)
Return RequestFile$(title$,path$,pattern$,patternposition,1,1)
End Function
|