Konichiwa, everyone! I found this great source code that apparently exports DirectX models which is exactly what I've been looking for - for a very long time now. But is there some way somebody can help me make this code also export an X file's textures as well as animations?
Link to original code:
http://forum.thegamecreators.com/?m=forum_view&t=123576&b=6
DirectX_XPorter.dbp:
sync on : sync rate 100
fromfile$ = _open_save("DirectX|*.X|3D studio|*.3DS", "Open", get dir$(), "X", 1)
if fromfile$ = "" then end
load object fromfile$, 1
tofile$ = _open_save("DirectX|*.X", "Save", get dir$(), "X", 0)
ExportDirectX(1, tofile$)
`Delete and load
delete object 1
load object tofile$, 1
do
yrotate object 1, wrapvalue(object angle y(1) + 0.2)
sync
loop
`ExportDirectX(obj, filename$)
`obj: object to export.
`filename$: file to export.
`Returns 1 if succeeded, 0 if file exist or filename$ is not a DirectX file.
function ExportDirectX(obj, filename$)
`Debug
if file exist(filename$) > 0 then exitfunction 0
if lower$(right$(filename$, 2)) <> ".x" then exitfunction 0
`Find name
split string replace all$(filename$, "/", "\"), "\"
name$ = get split word$(split count())
name$ = replace all$(replace all$(left$(name$, len(name$) - 2), " ", ""), ".", "")
`Start writing
f = find free file()
open to write f, filename$
`Write header data
write string f, "xof 0302txt 0032"
`Write some remark data
write string f, "//This exporter was made by Sven B"
write string f, "//Coded in DarkBasic Professional"
`Write root frame
write string f, "Frame " + name$ + "Root {"
`Write identity matrix for the root frame
write string f, "//Root frame"
write string f, "FrameTransformMatrix {"
write string f, "1.000000,0.000000,0.000000,0.000000,"
write string f, "0.000000,1.000000,0.000000,0.000000,"
write string f, "0.000000,0.000000,1.000000,0.000000,"
write string f, "0.000000,0.000000,0.000000,1.000000;;"
write string f, "}"
`Write limb frames
for limb = 0 to get limb count(obj)
`Lock vertex data
lock vertexdata for limb obj, limb
`Get information
vd = get vertexdata vertex count()
id = get vertexdata index count()
`Write frame
if vd > 0 and id > 0 or (limb position x(obj, limb) <> 0 or limb position y(obj, limb) <> 0 or limb position z(obj, limb) <> 0)
write string f, "//Child " + str$(limb)
if replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", "") <> "" and val(right$(replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", ""), 1)) > 0 and right$(replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", ""), 1) <> "0"
write string f, "Frame " + replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", "") + " {"
else
write string f, "Frame " + name$ + "Frame" + str$(limb) + " {"
endif
`Write translation matrix
write string f, "//Transformation matrix"
write string f, "FrameTransformMatrix {"
write string f, "1.000000,0.000000,0.000000,0.000000,"
write string f, "0.000000,1.000000,0.000000,0.000000,"
write string f, "0.000000,0.000000,1.000000,0.000000,"
write string f, str$(limb position x(obj, limb), 6) + "," + str$(limb position y(obj, limb), 6) + "," + str$(limb position z(obj, limb), 6) + ",1.000000;;"
write string f, "}"
write string f, "Mesh " + replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", "") + "Mesh {"
`VERTEX data
write string f, str$(vd) + ";"
`Positions
for v = 0 to vd - 1
`Appendix
if v < vd - 1 then app$ = ";," else app$ = ";;"
write string f, str$(get vertexdata position x(v), 6) + ";" + str$(get vertexdata position y(v), 6) + ";" + str$(get vertexdata position z(v), 6) + app$
next v
write string f, ""
`FACES
if id > 0
write string f, str$((id + 1)/3) + ";"
`Write indices
ind = 0
repeat
if ind => id - 3 then app$ = ";;" else app$ = ";,"
write string f, "3;" + str$(get indexdata(ind)) + "," + str$(get indexdata(ind+1)) + "," + str$(get indexdata(ind+2)) + app$
inc ind, 3
until app$ = ";;"
else
write string f, "0;"
endif
`MATERIAL
write string f, "MeshMaterialList {"
`One material
write string f, "1;"
if id > 0
write string f, str$((id + 1)/3) + ";"
`Write faces
ind = 0
repeat
if ind => id - 3 then app$ = ";;" else app$ = ","
write string f, "0" + app$
inc ind, 3
until app$ = ";;"
else
write string f, "0;"
endif
write string f, "Material " + replace all$(replace all$(limb name$(obj, limb), " ", ""), ".", "") + "Material {"
write string f, "0.800000;0.800000;0.800000;1.000000;;"
write string f, "0.000000;"
write string f, "0.000000;0.000000;0.000000;;"
write string f, "0.000000;0.000000;0.000000;;"
write string f, "}"
write string f, "}"
`NORMALS
write string f, "MeshNormals {"
`Write verteces
write string f, str$(vd) + ";"
for v = 0 to vd - 1
if v < vd - 1 then app$ = ";," else app$ = ";;"
write string f, str$(get vertexdata normals x(v), 6) + ";" + str$(get vertexdata normals y(v), 6) + ";" + str$(get vertexdata normals z(v), 6) + app$
next v
`Write indices
if id > 0
write string f, str$((id+1)/3) + ";"
ind = 0
repeat
if ind => id - 3 then app$ = ";;" else app$ = ";,"
write string f, "3;" + str$(get indexdata(ind)) + "," + str$(get indexdata(ind+1)) + "," + str$(get indexdata(ind+2)) + app$
inc ind, 3
until app$ = ";;"
else
write string f, "0;"
endif
write string f, "}"
`UV data
write string f, "MeshTextureCoords {"
`Write verteces
write string f, str$(vd) + ";"
for v = 0 to vd - 1
if v < vd - 1 then app$ = ";" else app$ = ";;"
write string f, str$(get vertexdata u(v), 6) + ";" + str$(get vertexdata v(v), 6) + app$
next v
write string f, "}"
write string f, "}"
write string f, "}"
endif
unlock vertexdata
next limb
write string f, "}"
close file f
endfunction 1
Function _open_save(filter As String,initdir As String,dtitle As String,defext As String,open As Boolean)
`Examples for parameters
`filter = "Text Documents ( *.txt )|*.txt|All Files ( *.* )|*.*|"
` each item in the filter must be terminated by a "|", the description and extension
` must also be seperated by a "|"
`initdir = "C:\"
`dtitle = "Open ~ Test"
`defext = "txt"
` This is the default extension appended to a file when saved if none is inputed by user
`open = 1 For Open Dialogue, 0 for save dialogue
`Get DLL numbers
comdlg32 As Integer
user32 As Integer
`Load in required DLL's
comdlg32 = _find_fee_dll()
Load DLL "comdlg32.dll",comdlg32
user32 = _find_fee_dll()
Load DLL "user32.dll",user32
`Get handle ( unique ID ) to the calling ( this ) window
hwnd As DWord
hwnd = Call DLL(user32,"GetActiveWindow")
`Get the Memblock Number
OPENFILENAME As Integer
OPENFILENAME = _find_free_mem()
`Make The Memblock containing the OPENFILENAME structure
Make MemBlock OPENFILENAME,76
`Get the pointer to the just created Structure
lpofn As DWord
lpofn = Get MemBlock Ptr(OPENFILENAME)
`Write all the info to the Structure for the API call to handle it.
RemStart : C++ Structure Lay-out
typedef struct tagOFN {
DWORD lStructSize;
HWND hwndOwner;
HINSTANCE hInstance;
LPCTSTR lpstrFilter;
LPTSTR lpstrCustomFilter;
DWORD nMaxCustFilter;
DWORD nFilterIndex;
LPTSTR lpstrFile;
DWORD nMaxFile;
LPTSTR lpstrFileTitle;
DWORD nMaxFileTitle;
LPCTSTR lpstrInitialDir;
LPCTSTR lpstrTitle;
DWORD Flags;
WORD nFileOffset;
WORD nFileExtension;
LPCTSTR lpstrDefExt;
LPARAM lCustData;
LPOFNHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
#if (_WIN32_WINNT >= 0x0500)
void * pvReserved;
DWORD dwReserved;
DWORD FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} OPENFILENAME, *LPOPENFILENAME;
RemEnd
`Declare temp variables to hold data for OPENFILENAME structure
size As Integer
filebuffer As String
filebufferptr As DWord
flags As DWord
`Fix up strings so that they are "NULL" terminated ( "|" is replaced with NULL )
filter = filter + "|"
initdir = initdir + "|"
dtitle = dtitle + "|"
defext = defext + "|"
`Set up internal parameters for the API call
filebuffer = "|" + Space$(255) + "|"
filebufferptr = _get_str_ptr(filebuffer)
flags = 0x00001000 || 0x00000004 || 0x00000002
size = 0
Write MemBlock DWord OPENFILENAME,0,76 : `lStructSize
Write MemBlock DWord OPENFILENAME,4,hwnd : `hwndOwner
`Write MemBlock DWord OPENFILENAME,8,NULL : `hInstance
Write MemBlock DWord OPENFILENAME,12,_get_str_ptr(filter) : `lpstrFilter
`Write MemBlock DWord OPENFILENAME,16,0 : `lpstrCustomFilter
`Write MemBlock DWord OPENFILENAME,20,NULL : `nMaxCustFilter
Write MemBlock DWord OPENFILENAME,24,1 : `nFilterIndex
Write MemBlock DWord OPENFILENAME,28,filebufferptr : `lpstrFile
Write MemBlock DWord OPENFILENAME,32,256 : `nMaxFile
`Write MemBlock DWord OPENFILENAME,36,0 : `lpstrFileTitle
`Write MemBlock DWord OPENFILENAME,40,NULL : `nMaxFileTitle
Write MemBlock DWord OPENFILENAME,44,_get_str_ptr(initdir) : `lpstrInitialDir
Write MemBlock DWord OPENFILENAME,48,_get_str_ptr(dtitle) : `lpstrTitle
Write MemBlock DWord OPENFILENAME,52,flags : `Flags
`Write MemBlock Word OPENFILENAME,56,NULL : `nFileOffset
`Write MemBlock Word OPENFILENAME,58,NULL : `nFileExtension
Write MemBlock DWord OPENFILENAME,60,_get_str_ptr(defext) : `lpstrDefExt
`Write MemBlock DWord OPENFILENAME,64,NULL : `lCustData
`Write MemBlock DWord OPENFILENAME,68,NULL : `lpfnHook
`Write MemBlock DWord OPENFILENAME,72,0 : `lpTemplateName
`Call the Command to open/save dialouge
retval As DWord
If open
retval = Call DLL(comdlg32,"GetOpenFileNameA",lpofn)
Else
retval = Call DLL(comdlg32,"GetSaveFileNameA",lpofn)
EndIf
`Check if it was sucecfull
If retval <> 0
code$ = _strip_space(1,_strip_space(2,_get_str(filebufferptr,256)))
Else
retval = Call DLL(comdlg32,"CommDlgExtendedError")
Select retval
Case 0xFFFF : code$ = "The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle." : EndCase
Case 0x0006 : code$ = "The common dialog box function failed to find a specified resource." : EndCase
Case 0x0004 : code$ = "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle." : EndCase
Case 0x0002 : code$ = "The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available." : EndCase
Case 0x000B : code$ = "The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure." : EndCase
Case 0x0008 : code$ = "The common dialog box function failed to lock a specified resource." : EndCase
Case 0x0003 : code$ = "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template." : EndCase
Case 0x0007 : code$ = "The common dialog box function failed to load a specified string." : EndCase
Case 0x0001 : code$ = "The lStructSize member of the initialization structure for the corresponding common dialog box is invalid." : EndCase
Case 0x0005 : code$ = "The common dialog box function failed to load a specified string." : EndCase
Case 0x3003 : code$ = "The buffer pointed to by the lpstrFile member of the OPENFILENAME structure is too small for the file name specified by the user. The first two bytes of the lpstrFile buffer contain an integer value specifying the size, in TCHARs, required to receive the full name." : EndCase
Case 0x0009 : code$ = "The common dialog box function was unable to allocate memory for internal structures." : EndCase
Case 0x3002 : code$ = "A file name is invalid." : EndCase
Case 0x000A : code$ = "The common dialog box function was unable to lock the memory associated with a handle." : EndCase
Case 0x3001 : code$ = "An attempt to subclass a list box failed because sufficient memory was not available." : EndCase
Case Default : code$ = "WHOOPS!" : EndCase
EndSelect
EndIF
Delete DLL comdlg32
Delete DLL user32
EndFunction code$
Function _get_str_ptr(pstr As String)
`pstr$ should be a "|" ( NULL ) seperated string.
memnum As Integer
strlen As Integer
char As Byte
memptr As DWord
strptr As DWord
memnum = _find_free_mem()
strlen = Len(pstr)
Make MemBlock memnum,strlen
For i = 1 To strlen
If Mid$(pstr,i) = "|"
char = 0
Else
char = Asc(Mid$(pstr,i))
EndIf
Write MemBlock Byte memnum,(i - 1),char
Next i
memptr = Get MemBlock Ptr(memnum)
strptr = Make Memory(strlen)
Copy Memory strptr,memptr,strlen
Delete MemBlock memnum
EndFunction strptr
Function _get_str(strptr As DWord,strsize As Integer)
`strptr is the pointer returned by _get_str_ptr()
`strsize is the Integer length of the string specified by the pointer
memnum As Integer
memptr As DWord
str As String
char As String
memnum = _find_free_mem()
Make MemBlock memnum,strsize
memptr = Get MemBlock Ptr(memnum)
Copy Memory memptr,strptr,strsize
For i = 1 To strsize
str = str + Chr$(MemBlock Byte(memnum,i - 1))
Next i
Delete MemBlock memnum
EndFunction str
Function _strip_space(part As Integer,sstr As String)
`str is the string to be striped.
`part can be 1, 2, 3 or 4.
`1 to strip space at the start of the string
`2 to strip space at the end of the string
`3 to strip all space in the string
strlen = Len(sstr)
tmpstr$ = ""
If part = 1
For i = 1 To strlen
If Mid$(sstr,i) <> " "
tmpstr$ = Right$(sstr,strlen - i + 1)
ExitFunction tmpstr$
EndIf
Next i
EndIf
If part = 2
For i = strlen To 1 Step -1
If Mid$(sstr,i) <> " "
tmpstr$ = Left$(sstr,i)
ExitFunction tmpstr$
EndIf
Next i
EndIf
If part = 3
For i = 1 To strlen
If Mid$(sstr,i) <> " "
tmpstr$ = tmpstr$ + Mid$(sstr,i)
EndIf
Next i
ExitFunction tmpstr$
EndIf
EndFunction "Error"
Function _find_fee_dll()
retval = 0
Repeat
Inc retval
Until DLL Exist(retval) = 0
EndFunction retval
Function _find_free_mem()
retval = 0
Repeat
Inc retval
Until MemBlock Exist(retval) = 0
EndFunction retval
Note that you'll need Ian M's plugins to make this work.
Thank you SO much for all your advice!
Have a great evening!
Sasuke
平和愛ユニティ