This will create an .obj file from a model in memory.
It will only create verts, textures and normals. No bones or animation. Should be suitable for a static model with a texture
The example will load "track.obj" from the Documents directory and create "newtrack.obj" in the Documents directory. You can substitute your own model names
I think there is an issue with some verts not being welded. I think it has something to do with they way the verts are presented in the mem block.
So there are only two options; 1) as is or 2) nearest vert welded. The code is an as is solution.
As usual i have done very little testing so it's prolly a mine field of bugs lol.
// Project: test113
// Created: 20-02-22
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "test113" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
SetCameraRange( 1, 1, 50000 )
type point
x as float
y as float
z as float
endtype
SetCameraPosition(1, 0, 1024, 0)
SetCameraLookAt(1, 0, 0, 0.1, 0)
global msg as string
id as integer
id = LoadObject("raw:"+GetDocumentsPath()+"/track.obj")
SaveToObj(id, 1, GetDocumentsPath()+"/newtrack.obj")
do
Sync()
loop
function SaveToObj(model as integer, mesh as integer, filename as string)
id as integer
mem as integer
i as integer
m as integer
all as tVertex[]
c as integer
v as integer
verts as tVertex[]
vertex as tVertex
poly as tPoly
offset as integer
p as integer
f as integer
n as integer
name as string
line as string
indicies as integer
verticies as integer
v1 as string
v2 as string
v3 as string
ext as string
s as string
id = CloneObject(model)
FixObjectPivot(id)
mem = CreateMemblockFromObjectMesh(id, mesh)
indicies = GetIndiciesFromMemblock(mem) - 1
verticies = GetVerticiesFromMemblock(mem) - 1
all.length = -1
for v=0 to verticies
vertex.pos.x = GetMeshMemblockVertexX(mem, v)
vertex.pos.y = GetMeshMemblockVertexY(mem, v)
vertex.pos.z = GetMeshMemblockVertexZ(mem, v)
vertex.normal.x = GetMeshMemblockVertexNormalX(mem, v)
vertex.normal.y = GetMeshMemblockVertexNormalY(mem, v)
vertex.normal.z = GetMeshMemblockVertexNormalZ(mem, v)
vertex.uv.u = GetMeshMemblockVertexU(mem, v)
vertex.uv.v = GetMeshMemblockVertexV(mem, v)
all.insert(vertex)
next
name = "raw:"+filename
ext = GetStringToken(name, ".", CountStringTokens(name, "."))
if ext <> "obj"
name = name + ".obj"
endif
f = OpenToWrite(name)
for i=0 to all.length
vertex = all[i]
WriteLine(f, "v "+str(vertex.pos.x)+" "+str(vertex.pos.y)+" "+str(-vertex.pos.z)+" ")
next
for i=0 to all.length
vertex = all[i]
WriteLine(f, "vn "+str(vertex.normal.x)+" "+str(vertex.normal.y)+" "+str(vertex.normal.z)+" ")
next
for i=0 to all.length
vertex = all[i]
WriteLine(f, "vt "+str(vertex.uv.u)+" "+str(1 - vertex.uv.v))
next
c = GetPolysFromMemblock(mem) - 1
for i=0 to c
poly = GetPolyFromMemblock(mem, i)
line = "f "
for v=poly.vertex.length to 0 step -1
vertex = poly.vertex[v]
line = line + str(vertex.no+1) + "/" + str(vertex.no+1) + "/" +str(vertex.no+1) + " "
next
WriteLine(f, line)
next
CloseFile(f)
DeleteObject(id)
DeleteMemblock(mem)
endfunction
/**************************************************************************************************
3D memblock functions
integer GetPolysFromMemBlock(id as integer)
id = memblock id
integer = The number of polys in this mesh
tPoly = GetPolyFromMemblock(id as integer, n as integer)
id = memblock id
n = poly (from 0 through (GetPolysFromMemBlock() - 1))
tPoly = tPoly.vertex is table containing information for the 3 vertex that make up the poly
pos - Position of vertex x, y, z
normal - Vertex normal x, y, z
uv - Vertex uv u, v
color - Color of vertex (if supplied)
**************************************************************************************************/
type tUV
u as float
v as float
endtype
type tVertex
pos as point
normal as point
uv as tUV
color as integer
no as integer
endtype
type tPoly
vertex as tVertex[]
endtype
function GetVerticiesFromMemBlock(id as integer)
polys as integer
verticies as integer
verticies = GetMemblockInt(id, 0)
endfunction verticies
function GetIndiciesFromMemBlock(id as integer)
polys as integer
indicies as integer
indicies = GetMemblockInt(id, 4)
endfunction indicies
function GetPolysFromMemBlock(id as integer)
polys as integer
indicies as integer
indicies = GetMemblockInt(id, 4)
if indicies = 0
polys = GetMemblockInt(id, 0) / 3
else
polys = indicies / 3
endif
endfunction polys
function GetPolyFromMemblock(id as integer, n as integer)
poly as tPoly
point as point
vertex as tVertex
verts as integer
indicies as integer
i as integer
v as integer
ioffset as integer
verts = GetMemblockInt(id, 0)
indicies = GetMemblockInt(id, 4)
SetErrorMode(0)
if indicies = 0
for i=(n * 3) to (n * 3)+2
vertex.no = i
vertex.pos.x = GetMeshMemblockVertexX(id, i)
vertex.pos.y = GetMeshMemblockVertexY(id, i)
vertex.pos.z = GetMeshMemblockVertexZ(id, i)
vertex.normal.x = GetMeshMemblockVertexNormalX(id, i)
vertex.normal.y = GetMeshMemblockVertexNormalY(id, i)
vertex.normal.z = GetMeshMemblockVertexNormalZ(id, i)
vertex.uv.u = GetMeshMemblockVertexU(id, i)
vertex.uv.v = GetMeshMemblockVertexV(id, i)
vertex.color = MakeColor(GetMeshMemblockVertexRed(id, i), GetMeshMemblockVertexGreen(id, i), GetMeshMemblockVertexBlue(id, i), GetMeshMemblockVertexAlpha(id, i))
poly.vertex.insert(vertex)
next
else
ioffset = GetMemblockInt(id, 20) + (n * 12)
for i=0 to 8 step 4
v = GetMemblockInt(id, ioffset + i)
vertex.no = v
vertex.pos.x = GetMeshMemblockVertexX(id, v)
vertex.pos.y = GetMeshMemblockVertexY(id, v)
vertex.pos.z = GetMeshMemblockVertexZ(id, v)
vertex.normal.x = GetMeshMemblockVertexNormalX(id, v)
vertex.normal.y = GetMeshMemblockVertexNormalY(id, v)
vertex.normal.z = GetMeshMemblockVertexNormalZ(id, v)
vertex.uv.u = GetMeshMemblockVertexU(id, v)
vertex.uv.v = GetMeshMemblockVertexV(id, v)
vertex.color = MakeColor(GetMeshMemblockVertexRed(id, v), GetMeshMemblockVertexGreen(id, v), GetMeshMemblockVertexBlue(id, v), GetMeshMemblockVertexAlpha(id, v))
poly.vertex.insert(vertex)
next
endif
SetErrorMode(2)
endfunction poly
function DeleteObjectMemblock(id as integer)
if GetMemblockExists(id)
DeleteMemblock(id)
endif
DeleteObject(id)
endfunction