I have put back a for loop to go through all the objects that are in the mesh
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
#constant KEY_LEFT 37
#constant KEY_UP 38
#constant KEY_RIGHT 39
#constant KEY_DOWN 40
#constant KEY_A 65
#constant KEY_Z 90
#constant KEY_M 77
#constant KEY_S 83
// set window properties
SetWindowTitle( "map" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( screenrate, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
type objectType
objID as integer
memID as integer
endtype
global width,depth
global objnumber
//size=abs(width*depth)
global objects as objectType[4096]
centreObjId=normalHeightFunction("\media\map3.png")
//centreObjId=smoothHeightFunction("\media\map.png")
SetCameraLookAt(1,GetObjectX(centreObjId),GetObjectY(centreObjId),GetObjectZ(centreObjId),0)
deletedObjects=dohousekeeping()
camerax#=32
cameray#=20
cameraz#=-85
do
//if getObjectExists(final_object) then MoveObjectLocalX(final_object,.05)
if GetRawKeyState(KEY_LEFT) then dec camerax#,.5
if GetRawKeyState(KEY_RIGHT) then inc camerax#,.5
if GetRawKeyState(KEY_UP) then dec cameray#,.5
if GetRawKeyState(KEY_DOWN) then inc cameray#,.5
if GetRawKeyState(KEY_A) then inc cameraz#,.5
if GetRawKeyState(KEY_Z) then dec cameraz#,.5
if GetRawKeyPressed(KEY_M)
final_object=mergememblocks()
if getObjectExists(final_object) then setobjectColor(final_object,0,200,0,255)
endif
if GetRawKeyPressed(KEY_S)
if getObjectExists(final_object)
AGM_SaveObject(final_object,1,"mesh", "raw:c:\database\","final.obj","final.mtl", "map2.png",0)
//AGM_SaveObject(objects[1].objID,1,"cube", "\media\","final.obj","final.mtl", "map2.png",1)
endif
endif
if getObjectExists(final_object) then print(GetObjectNumMeshes(final_object))
Print ("Started with 4096 objects")
print ("Deleted objects="+str(deletedObjects))
Print ("Now have "+str(4096-deletedObjects))
print("Press m to merge objects")
print("Camera X "+str(getCameraX(1)))
print("Camera Y "+str(getCameraY(1)))
print("Camera Z "+str(getCameraZ(1)))
Print("FPS "+str( ScreenFPS()) )
SetCameraPosition(1,camerax#,cameray#,cameraz#)
Sync()
loop
function smoothHeightFunction(filename$)
//top left front 0,9,18
//top right front 2,4,11
//top left back 8,14,16
//top right back 6,10,12
vertexLF as integer[2] = [0,9,18]
vertexRF as integer[2] = [2,4,11]
vertexLB as integer[2] = [8,14,16]
vertexRB as integer[2] = [6,10,12]
//bottom left front 1,19,20
//bottom right front 3,5,22
//bottom left back 15,17,21,24,25,26,27
//bottom right back 7,13,23,
img = LoadImage(filename$)
imgmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
objnumber=0:lastheight#=0
for z=depth-1 to 0 step -1
for x= width-1 to 0 step -1
//check the x plane
xx=x+1:if xx>width-1 then xx=width-1
prevOffset = (12+((z * width) + xx) * 4) - 4
r=GetMemblockByte(imgmemblock,prevOffset)
g=GetMemblockByte(imgmemblock,prevOffset+1)
b=GetMemblockByte(imgmemblock,prevOffset+2)
a=GetMemblockByte(imgmemblock,prevOffset+3)
color#=(r+g+b)
prevHeight#=color#/100.0
xx=x-1:if xx<0 then xx=0
nextOffset = (12+((z * width) + xx) * 4) - 4
r=GetMemblockByte(imgmemblock,nextOffset)
g=GetMemblockByte(imgmemblock,nextOffset+1)
b=GetMemblockByte(imgmemblock,nextOffset+2)
a=GetMemblockByte(imgmemblock,nextOffset+3)
color#=(r+g+b)
nextHeight#=color#/100.0
objects[objnumber].objID=CreateObjectBox(1,1,1)
SetObjectPosition(objects[objnumber].objID,x, 0 ,-z)
SetObjectColor(objects[objnumber].objID,r,g,b,a)
objects[objnumber].memID=CreateMemblockFromObjectMesh(objects[objnumber].objID,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRF[i])+prevHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRB[i])+prevHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLF[i])+nextHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLB[i])+nextHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(objects[objnumber].objID,1,objects[objnumber].memID)
//DeleteMemblock(memID)
//check the z plane
zz=z+1:if zz>depth-1 then zz=depth-1
prevOffset = (12+((zz * width) + x) * 4) - 4
r=GetMemblockByte(imgmemblock,prevOffset)
g=GetMemblockByte(imgmemblock,prevOffset+1)
b=GetMemblockByte(imgmemblock,prevOffset+2)
a=GetMemblockByte(imgmemblock,prevOffset+3)
color#=(r+g+b)
prevHeight#=color#/100.0
zz=z-1:if zz<0 then zz=0
nextOffset = (12+((zz * width) + xx) * 4) - 4
r=GetMemblockByte(imgmemblock,nextOffset)
g=GetMemblockByte(imgmemblock,nextOffset+1)
b=GetMemblockByte(imgmemblock,nextOffset+2)
a=GetMemblockByte(imgmemblock,nextOffset+3)
color#=(r+g+b)
nextHeight#=color#/100.0
//objects[objnumber].objID=(CreateObjectBox(1,1,1))
SetObjectPosition(objects[objnumber].objID,x, 0 ,-z)
SetObjectColor(objects[objnumber].objID,r,g,b,a)
//objects[objnumber].memID=CreateMemblockFromObjectMesh(objects[objnumber].objID,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRF[i])+prevHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRB[i])+nextHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLF[i])+prevHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLB[i])+nextHeight#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(objects[objnumber].objID,1,objects[objnumber].memID)
//DeleteMemblock(memID)
if (width-1)/2=x and (depth-1)/2=z then centreObjId=objects[objnumber].objID
if (width-1)/2=x and (depth-1)=z then SetCameraPosition(1,getObjectX(objects[objnumber].objID),getObjectY(objects[objnumber].objID)+20,getObjectz(objects[objnumber].objID)-20)
inc objnumber
next x
next z
endfunction centreObjID
function normalHeightFunction(filename$)
//top left front 0,9,18
//top right front 2,4,11
//top left back 8,14,16
//top right back 6,10,12
vertexLF as integer[2] = [0,9,18]
vertexRF as integer[2] = [2,4,11]
vertexLB as integer[2] = [8,14,16]
vertexRB as integer[2] = [6,10,12]
//bottom left front 1,19,20
//bottom right front 3,5,22
//bottom left back 15,17,21,24,25,26,27
//bottom right back 7,13,23,
img = LoadImage(filename$)
imgmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
objnumber=0
for z=depth-1 to 0 step -1
for x= width-1 to 0 step -1
Offset = (12+((z * width) + x) * 4) - 4
r=GetMemblockByte(imgmemblock,Offset)
g=GetMemblockByte(imgmemblock,Offset+1)
b=GetMemblockByte(imgmemblock,Offset+2)
a=GetMemblockByte(imgmemblock,Offset+3)
color#=(r+g+b)
Height#=color#/100.0
objects[objnumber].objID=CreateObjectBox(1,1,1)
SetObjectPosition(objects[objnumber].objID,x, 0 ,-z)
SetObjectColor(objects[objnumber].objID,r,g,b,a)
objects[objnumber].memID=CreateMemblockFromObjectMesh(objects[objnumber].objID,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRF[i])+Height#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexRB[i])+Height#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLF[i])+Height#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(objects[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(objects[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(objects[objnumber].memID,vertexLB[i])+Height#,GetMeshMemblockVertexZ(objects[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(objects[objnumber].objID,1,objects[objnumber].memID)
//DeleteMemblock(memID)
if (width-1)/2=x and (depth-1)/2=z then centreObjId=objects[objnumber].objID
if (width-1)/2=x and (depth-1)=z then SetCameraPosition(1,getObjectX(objects[objnumber].objID),getObjectY(objects[objnumber].objID)+20,getObjectz(objects[objnumber].objID)-20)
inc objnumber
next x
next z
endfunction centreObjId
function mergememblocks()
for i = 0 to objects.length
if GetMemblockExists(objects[i].memID)
if i = 0
numvert = GetMemblockInt(objects[i].memID,0)
for n = 0 to numvert-1
x# = GetMeshMemblockVertexX( objects[i].memID, n )+GetObjectX( objects[i].objID )
y# = GetMeshMemblockVertexY( objects[i].memID, n )+GetObjectY( objects[i].objID )
z# = GetMeshMemblockVertexZ( objects[i].memID, n )+GetObjectZ( objects[i].objID )
SetMeshMemblockVertexPosition(objects[i].memID, n, x#, y#, z# )
next n
big_object = CreateObjectFromMeshMemblock(objects[0].memID)
DeleteMemblock(objects[i].memID )
else
for n = 0 to numvert-1
x# = GetMeshMemblockVertexX( objects[i].memID, n )+GetObjectX( objects[i].objID )
y# = GetMeshMemblockVertexY( objects[i].memID, n )+GetObjectY( objects[i].objID )
z# = GetMeshMemblockVertexZ( objects[i].memID, n )+GetObjectZ( objects[i].objID )
SetMeshMemblockVertexPosition(objects[i].memID, n, x#, y#, z# )
next n
AddObjectMeshFromMemblock( big_object, objects[i].memID )
DeleteMemblock( objects[i].memID )
endif
endif
next i
for i = 0 to objects.length
// DeleteObject( objects[i].objID )
next i
endfunction big_object
function dohousekeeping()
DeletedObjects=0
for num = objects.length-1 to 0 step -1
if GetObjectExists(objects[num].objID)
r=GetObjectColorRed(objects[num].objID)
b=GetObjectColorBLue(objects[num].objID)
g=GetObjectColorGreen(objects[num].objID)
if r+b+g<1
DeleteObject(objects[num].objID)
DeleteMemblock(objects[num].memID)
objects.remove(num)
inc DeletedObjects
endif
endif
next num
endfunction DeletedObjects
Function AGM_SaveObject(objid,meshIndex, group$, folder$, filename$, material$, texture$, debug)
fw = OpenToWrite(folder$+filename$)
for num = objects.length-1 to 0 step -1
objid=objects[num].objID
if getObjectExists(objId)
if texture$<>"" then SetObjectImage(objID,LoadImage(folder$+texture$),0)
memblock=CreateMemblockFromObjectMesh(objID,meshindex)
VC = GetMemblockInt(memblock,0)
IC = GetMemblockInt(memblock,4)
Attributes = GetMemblockInt(memblock,8)
counter = GetMemblockInt(memblock,12)
DataOffset = GetMemblockInt(memblock,16)
IndexOffset = GetMemblockInt(memblock,20)
PositionData = GetMemblockInt(memblock,24)
PositionName$ = GetMemblockString(memblock,28,8)
NormalData = GetMemblockInt(memblock,40)
NormalName$ = GetMemblockString(memblock,44,8)
CData = GetMemblockInt(memblock,52)
CName$ = GetMemblockString(memblock,56,8)
ColorData = GetMemblockInt(memblock,60)
ColorName$ = GetMemblockString(memblock,64,8)
if debug=1
repeat
print("Object " + group$)
print("Vertex Count - " + str(vc))
print("Index Count - " + str(IC))
print("Atribute Count - " + str(Attributes))
print("Data Counter - " + str(counter))
print("Data Offset - " + str(DataOffset))
print("Index Offset - " + str(IndexOffset))
print("Position Data - " + str(PositionData))
print("Position String " + positionName$)
print("Normal Data - " + str(NormalData))
print("Normal String " + NormalName$)
print("UV Data - " + str(CData))
print("UV String " + CName$)
print("Color Data - " + str(ColorData))
print("Color String " + ColorName$)
print("Press Space to continue")
print(VC)
print(IC)
print(IC/3)
print(IC/4)
sync()
until GetRawKeyPressed(32)
endif
if ColorName$=""
colorData=0
ColorName$="No color Data"
endif
//print("saving meshindex "+str(num)+ " of " +str(GetObjectNumMeshes( objid ) ) )
sync()
WriteLine(fw,"#AGM Object - " + folder$ + filename$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
WriteLine(fw,"mtllib " + material$)
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"g " + group$+str(index))
//WriteLine(fw,"o Cube." + str(index))
WriteLine(fw,"#Vertices")
for index =1 to IC
for v=0 to vc * counter-1 step counter
WriteLine(fw,"v " + str(GetMemblockFloat(memblock,dataoffset+v)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+4)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+8))) // Vertex X,Y,Z
next
next index
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Normals")
for index =1 to IC
for v=0 to vc * counter-1 step counter
WriteLine(fw,"vn " + str(GetMemblockFloat(memblock,dataoffset+v+12)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+16)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+20))) // Normal x,y,z
next
next index
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Textures UV's")
for index =1 to IC
for v=0 to vc * counter-1 step counter
WriteLine(fw,"vt " + str(GetMemblockFloat(memblock,dataoffset+v+24)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+28)))
next
next index
WriteLine(fw,"")
WriteLine(fw,"")
for index =1 to IC
WriteLine(fw,"g " + group$+str(index))
//WriteLine(fw,"#Faces")
WriteLine(fw,"")
WriteLine(fw,"usemtl Material")
//WriteLine(fw,"s off")
if IC<>0 // Index Couner > 0 so this model as index data at the end to work out the order of faces
for i=0 to (IC / 3)-1
index1=GetMemblockByte(memblock,(indexoffset+(i*12)))+1
index2=GetMemblockByte(memblock,(indexoffset+(i*12)+4))+1
index3=GetMemblockByte(memblock,(indexoffset+(i*12)+8))+1
WriteLine(fw,"f " + str(index1) + "/" + str(facecount+1) + "/" + str(facecount+1) +" " + str(index2) + "/" + str(facecount) + "/" + str(facecount) + " " + str(index3) + "/" + str(facecount+2) +"/" + str(facecount+2))
next
else
for facecount=1 to vc-3 // VC Vertex Count and no Index count - so the Vertices are straight as is.
WriteLine(fw,"f " + str(facecount+1) + "/"+str(facecount+1)+"/" + str(facecount+1) + " " + str(facecount) + "/"+str(facecount)+"/" + str(facecount)+ " " + str(facecount+2) + "/"+str(facecount+2)+"/" + str(facecount+2))
next
endif
next index
DeleteMemblock(memblock)
endif
next num
CloseFile(fw)
// write the MTL file
fw = OpenToWrite(folder$ + material$)
WriteLine(fw,"#AGM Material - " + material$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
// red#=GetObjectColorRed(objid)
// green#=GetObjectColorGreen(objid)
// blue#=GetObjectColorBlue(objid)
WriteLine(fw,"newmtl Material")
// WriteLine(fw,"Kd " + str(red#/255.0) + " " + str(green#/255.0) + " " + str(blue#/255.0))
if texture$<>""
writeLine(fw,"map_Kd "+texture$)
endif
CloseFile(fw)
sync()
//DeleteObject(objID)
endfunction
at least now it brings in all the Objects into the OBJ.
Is this something you was looking to do too?
EDIT - and this version now imports into my blender (i think its just a huge scaling issue on your last attempt)