Just thought try heights with this method
// Project: led board
// Created: 2018-12-27
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "led board" )
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
global objects as integer[]
Type Vertex
x as float
y as float
z as float
nx as float
ny as float
nz as float
u as float
v as float
color as integer
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Mesh
VertexList as Vertex[]
TriangleList as Triangle[]
endtype
global MeshID as Mesh
global numofcubes,img
swap()
for x=0 to 100
for y=0 to 100
DrawBox(x,y,x+1,y+1,MakeColor(random(0,255),random(0,255),random(0,255)),MakeColor(random(0,255),random(0,255),random(0,255)),MakeColor(random(0,255),random(0,255),random(0,255)),MakeColor(random(0,255),random(0,255),random(0,255)),1)
next
next
render()
img = GetImage(0,0,100,100)
spr = CreateSprite(img)
grabinfo(img,20,100.0)
setupskybox()
camerax#=100
cameray#=800
cameraz#=-190
SetCameraRange(1,.01,2000)
do
print("This will create a map and a OBJ file based on this image")
SetSpritePosition(spr,100,100)
if GetRawKeyState(38) then inc cameraz#
if GetRawKeyState(40) then dec cameraz#
if GetRawKeyState(39) then inc camerax#
if GetRawKeyState(37) then dec camerax#
if GetRawKeyState(87) then inc cameray#
if GetRawKeyState(83) then dec cameray#
print(numofcubes)
SetCameraPosition(1,camerax#,cameray#,cameraz#)
Print( ScreenFPS() )
Sync()
loop
function setupskybox()
SetSkyBoxHorizonColor(0,0,70)
SetSkyBoxSkyColor(0,200,200)
SetSkyBoxVisible(1)
endfunction
function grabinfo(img,size,h)
imgmemblock = CreateMemblockFromImage(img)
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
objnumber=0
for x=width to 0 step -1
for z=height-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)
//DrawBox(x,y,x+1,y+1,MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),1)
if r<255 and g<255 and b<255 and a>0
heighty#=(r/h)+(g/h)+(b/h)
buildcube(x*(size*2),heighty#*(size*2),z*(size*2),r,g,b,size)
inc numofcubes
endif
next
next
obj = CreateObjectFromMeshWithUVTexturingandColor(meshid,img)
AGM_SaveObject(obj,1,"Map", "raw:c:\data\","map.obj","map.mtl", "",0)
endfunction
Function AddVertex(m ref as Mesh, x as float, y as float, z as float, nx as float, ny as float, nz as float, u as float, v as float, color as integer)
vert as vertex
vert.x = x
vert.y = y
vert.z = z
vert.nx = nx
vert.ny = ny
vert.nz = nz
vert.u = u
vert.v = v
vert.color = color
m.VertexList.Insert(vert)
endfunction
Function CreateObjectFromMeshWithUVTexturing(m ref as mesh, texture)
DeleteMemblock(memblock)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 2
IndexOffset = 60 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,3)
SetMemblockInt(memblock,12,32) // no color - 36 if color
SetmemblockInt(memblock,16,60)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
//SetMemblockInt(memblock,60,0x08010401) // maybe one day or year in 2019 lol
//SetMemblockString(memblock,64,"color") // maybe one day or year in 2019 lol
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,60+i*32,m.VertexList[i].x)
SetMemblockFloat(memblock,64+i*32,m.VertexList[i].y)
SetMemblockFloat(memblock,68+i*32,m.VertexList[i].z)
SetMemblockFloat(memblock,72+i*32,m.VertexList[i].nx)
SetMemblockFloat(memblock,76+i*32,m.VertexList[i].ny)
SetMemblockFloat(memblock,80+i*32,m.VertexList[i].nz)
SetMemblockFloat(memblock,84+i*32,m.VertexList[i].u)
SetMemblockFloat(memblock,88+i*32,m.VertexList[i].v)
//SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
// for i = 0 to m.TriangleList.Length
// SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
// SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
// SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
// next
DeleteObject(id)
id = CreateObjectFromMeshMemblock(memblock)
SetObjectImage(id,texture,0)
// if mode=1 // creates the world or it creates for other small things - like explosion blocks
// we need to keep the world chunk in memory so we can keep updating the vertices
meshmemory = memblock
// endif
// DeleteMemblock(memblock)
endfunction id
Function CreateObjectFromMeshWithColor(m ref as mesh)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 2
IndexOffset = 72 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,4)
SetMemblockInt(memblock,12,36) // no color - 36 if color
SetmemblockInt(memblock,16,72)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
SetMemblockInt(memblock,60,0x08010401) // maybe one day or year in 2019 lol
SetMemblockString(memblock,64,"color") // maybe one day or year in 2019 lol
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,72+i*36,m.VertexList[i].x)
SetMemblockFloat(memblock,76+i*36,m.VertexList[i].y)
SetMemblockFloat(memblock,80+i*36,m.VertexList[i].z)
SetMemblockFloat(memblock,84+i*36,m.VertexList[i].nx)
SetMemblockFloat(memblock,88+i*36,m.VertexList[i].ny)
SetMemblockFloat(memblock,92+i*36,m.VertexList[i].nz)
SetMemblockFloat(memblock,96+i*36,m.VertexList[i].u)
SetMemblockFloat(memblock,100+i*36,m.VertexList[i].v)
SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
for i = 0 to m.TriangleList.Length
SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
next
id = CreateObjectFromMeshMemblock(memblock)
// DeleteMemblock(memblock)
endfunction id
function buildcube(x,heighty#,z,r,g,b,size)
// face1 top
AddVertex(MeshID, (x-size),heighty#+size,z+size,0,1,0,0,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z+size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z+size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z-size,0,1,0,1,1,MakeColor(r,g,b))
// face2 bottom
// AddVertex(MeshID, (x-size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x-size),heighty#-size,z-size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#-size,z-size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x-size),heighty#-size,z-size,0,1,0,0,0,MakeColor(r,g,b))
// face 3- left
// AddVertex(MeshID, (x-size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x-size),heighty#+size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x-size),heighty#+size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// AddVertex(MeshID, (x+size),heighty#+size,z+size,0,1,0,0,0,MakeColor(r,g,b))
// face 4 - right
AddVertex(MeshID, (x-size),heighty#-size,z-size,0,1,0,0,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#-size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z-size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#-size,z-size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z-size,0,1,0,1,1,MakeColor(r,g,b))
// face 5 - front
AddVertex(MeshID, (x-size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#-size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z+size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z+size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#-size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x-size),heighty#+size,z-size,0,1,0,1,1,MakeColor(r,g,b))
// face 5 - back
AddVertex(MeshID, (x+size),heighty#-size,z+size,0,1,0,0,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z+size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#-size,z-size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z+size,0,1,0,0,1,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#+size,z-size,0,1,0,1,0,MakeColor(r,g,b))
AddVertex(MeshID, (x+size),heighty#-size,z-size,0,1,0,1,1,MakeColor(r,g,b))
endfunction
Function CreateObjectFromMeshWithUVTexturingandColor(m ref as mesh, diffuse)
DeleteMemblock(memblock)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 2
IndexOffset = 72 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,4)
SetMemblockInt(memblock,12,36) // no color - 36 if color
SetmemblockInt(memblock,16,72)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
SetMemblockInt(memblock,60,0x08010401) // maybe one day or year in 2019 lol
SetMemblockString(memblock,64,"color") // maybe one day or year in 2019 lol
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,72+i*36,m.VertexList[i].x)
SetMemblockFloat(memblock,76+i*36,m.VertexList[i].y)
SetMemblockFloat(memblock,80+i*36,m.VertexList[i].z)
SetMemblockFloat(memblock,84+i*36,m.VertexList[i].nx)
SetMemblockFloat(memblock,88+i*36,m.VertexList[i].ny)
SetMemblockFloat(memblock,92+i*36,m.VertexList[i].nz)
SetMemblockFloat(memblock,96+i*36,m.VertexList[i].u)
SetMemblockFloat(memblock,100+i*36,m.VertexList[i].v)
SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
DeleteObject(id)
id = CreateObjectFromMeshMemblock(memblock)
// diffuse
SetObjectImage(id,diffuse,0)
// normal / ambient
// SetObjectImage(id,ambient,1)
// specular
// SetObjectImage(id,specular,2)
// if mode=1 // creates the world or it creates for other small things - like explosion blocks
// we need to keep the world chunk in memory so we can keep updating the vertices
meshmemory = memblock
// endif
// DeleteMemblock(memblock)
endfunction id
Function AGM_SaveObject(objid, meshindex, group$, folder$, filename$, material$, texture$, debug)
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 ColorName$=""
colorData=0
ColorName$="No color Data"
endif
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
fw = OpenToWrite(folder$+filename$)
WriteLine(fw,"#AGM Object - " + folder$ + filename$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
WriteLine(fw,"mtllib " + material$)
WriteLine(fw,"")
WriteLine(fw,"g " + group$)
WriteLine(fw,"#Vertices")
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
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Normals")
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
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Textures UV's")
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
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Faces")
WriteLine(fw,"usemtl Material")
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
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)
// DeleteObject(objID)
DeleteMemblock(memblock)
endfunction
Move around cursors and WD