Mesh Extrude Tool V1.9
This tool will extrude an object from a selected image, you have two types of extrusion normal extrude which
is a voxel style of extrusion the second uses the same method but smooths it and only creates half the boxes
once you have extruded the object it will save the object deleting everything in memory and reload and color
it red to show it worked. I recommend small image sizes (ie 32*32) as the objects can quickly become quite
large but most should still load just be patient if the file is likely to be large. Another thing where there are
black areas (RGB 0,0,0) it will not create a voxel).
// 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_D 68
#constant KEY_E 69
#constant KEY_Z 90
#constant KEY_M 77
#constant KEY_S 83
#constant KEY_L 76
#constant KEY_C 67
#constant KEY_HOME 36
#constant KEY_PLUS 107
#constant KEY_SUBTRACT 109
#constant KEY_SHIFT 16
// set window properties
SetWindowTitle( "Mesh Creator from an image 1.9" )
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 _boxes
id
memID
x#
y#
z#
endtype
final_object as integer
global objnumber,width
global boxes as _boxes[]
camerax#=32
cameray#=20
cameraz#=-85
centreObjId=0
image=0
extrude=100:direction=1
final_object=0
do
if GetRawKeyState(KEY_LEFT) then dec camerax#,.25
if GetRawKeyState(KEY_RIGHT) then inc camerax#,.25
if GetRawKeyState(KEY_UP) then dec cameray#,.25
if GetRawKeyState(KEY_DOWN) then inc cameray#,.25
if GetRawKeyState(KEY_A) then inc cameraz#,.25
if GetRawKeyState(KEY_Z) then dec cameraz#,.25
if GetRawKeyPressed(KEY_L)
ret$=ChooseRawFile("*.jpg;*.png",1)
if ret$<>""
image=loadImage("raw:"+ret$)
imgmemblock = CreateMemblockFromImage(image)
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
if mod(width,2)<>0 or mod(depth,2)<>0
Message("The image wasnt suitable. It needs to be an even number width and height")
ret$="":DeleteImage(image):DeleteMemblock(imgmemblock)
endif
endif
endif
if GetRawKeyPressed(KEY_E)
if ret$<>""
if GetRawKeyState(KEY_SHIFT)
centreObjId=smoothHeightFunction(256-extrude,direction,"raw:"+ret$)
deletedObjects=dohousekeeping()
else
centreObjId=normalHeightFunction(256-extrude,direction,"raw:"+ret$)
deletedObjects=dohousekeeping()
endif
AGM_SaveObject("raw:"+left(ret$,len(ret$)-4)+".obj","raw:"+left(ret$,len(ret$)-4)+".mtl")
final_object=LoadObject("raw:"+left(ret$,len(ret$)-4)+".obj")
SetObjectColor(final_object,200,0,0,255)
SetObjectPosition(final_object,0,0,0)
RotateObjectLocaly(final_object,-180)
camerax#=-63
cameray#=25
cameraz#=-50
endif
endif
if GetRawKeyPressed(KEY_C)
for num = boxes.length to 0 step -1
DeleteObject(boxes[num].id)
boxes.remove(num)
next num
DeleteAllObjects()
final_object=0
endif
if GetRawKeyPressed(KEY_D)
if direction=1
direction=2
else
direction=1
endif
endif
if GetRawKeyPressed(KEY_PLUS) and extrude <255
inc extrude
endif
if GetRawKeyPressed(KEY_SUBTRACT) and extrude >2
dec extrude
endif
if GetRawKeyPressed(KEY_HOME)
if getObjectExists(centreObjID)
xx#=GetObjectX(centreObjID)
yy#=GetObjectY(centreObjID)
zz#=GetObjectZ(centreObjID)
for num = boxes.length to 0 step -1
if GetObjectExists(boxes[num].id)
SetObjectPosition(boxes[num].id,getObjectX(boxes[num].id)-xx#,0,getObjectZ(boxes[num].id)-zz#)
endif
next num
else
If GetObjectExists(final_object)
SetObjectPosition(final_object,0,0,0)
endif
endif
camerax#=0
cameray#=25
cameraz#=-50
endif
print("Mesh Extrude Tool V1.9")
if ret$=""
print("No image Loaded please load an image to extrude")
else
print(left(ret$,len(ret$)-4))
endif
if boxes.length<0
print("No blocks in memory please use one of the extrude functions")
else
print("Number of blocks in memory "+str(boxes.length))
endif
print("Press Home to center")
print("Press C to clear")
print("Press L to load image")
if direction=1
print("Press D to toggle direction=(1 way)")
else
print("Press D to toggle direction=(2 way)")
endif
print("Press E to Extrude image and save")
print("Press SHIFT + E Smooth Extrude")
print("Press - less extrude value before extruding")
print("Press + more extrude value before extruding")
print("Extrude value (> value is > extrude ie 100) "+str(extrude))
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 normalHeightFunction(extrudeDivisor as integer,direction as integer, 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]
vertexBLF as integer[2] = [1,19,20]
vertexBRF as integer[2] = [3,5,22]
vertexBLB as integer[2] = [15,17,21]
vertexBRB as integer[2] = [7,13,23]
//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,
box as _boxes
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#/extrudeDivisor
box.id=CreateObjectBox(1,1,1)
boxes.insert(box)
SetObjectPosition(boxes[objnumber].id,-x, 0 ,-z)
SetObjectColor(boxes[objnumber].id,r,g,b,a)
boxes[objnumber].memID=CreateMemblockFromObjectMesh(boxes[objnumber].id,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRF[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRB[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLF[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLB[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLB[i]))
next i
if direction=2
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBRF[i])-Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBRB[i])-Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBLF[i])-Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBLF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBLB[i])-Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBLB[i]))
next i
endif
SetObjectMeshFromMemblock(boxes[objnumber].id,1,boxes[objnumber].memID)
//DeleteMemblock(memID)
if (width-1)/2=x and (depth-1)/2=z then centreObjId=boxes[objnumber].id
if (width-1)/2=x and (depth-1)=z then SetCameraPosition(1,getObjectX(boxes[objnumber].id),getObjectY(boxes[objnumber].id)+20,getObjectz(boxes[objnumber].id)-20)
DeleteMemblock(boxes[objnumber].memID)
inc objnumber:rem if objnumber =width*depth then exitfunction centreObjID
next x
next z
endfunction centreObjId
function smoothHeightFunction(extrudeDivisor as integer,direction as integer, 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]
vertexBLF as integer[2] = [1,19,20]
vertexBRF as integer[2] = [3,5,22]
vertexBLB as integer[2] = [15,17,21]
vertexBRB as integer[2] = [7,13,23]
//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,
box as _boxes
img = LoadImage(filename$)
imgmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
objnumber=0
rem adjust the heights for all the objects smoothly
counter=1:objnumber=0
for z=depth-1 to 2 step -2
for x= width-1 to 2 step -1
//in order rightfront,leftfront,rightback,leftback
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)
HeightRF#=color#/extrudeDivisor
Offset = (12+((z * width) + (x-2)) * 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)
HeightLF#=color#/extrudeDivisor
if color#<1.0
Offset = (12+((z * width) + (x-1)) * 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)
HeightLF#=color#/extrudeDivisor
endif
Offset = (12+(((z-2) * 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)
HeightRB#=color#/extrudeDivisor
if color#<1.0
Offset = (12+(((z-1) * 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)
HeightRB#=color#/extrudeDivisor
endif
Offset = (12+(((z-2) * width) + (x-2)) * 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)
HeightLB#=color#/extrudeDivisor
if color#<1.0
Offset = (12+(((z-1) * width) + (x-1)) * 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)
HeightLB#=color#/extrudeDivisor
endif
box.id=CreateObjectBox(2,1,2)
SetObjectPosition(box.id,-x, 0 ,-z)
boxes.insert(box)
//if GetObjectExists(boxes[objnumber].id)
SetObjectColor(boxes[objnumber].id,r,g,b,a)
boxes[objnumber].memID=CreateMemblockFromObjectMesh(boxes[objnumber].id,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRB[i])+HeightRB#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLB[i])+HeightLB#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRF[i])+HeightRF#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLF[i])+HeightLF#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLF[i]))
next i
if direction=2
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBRB[i])-HeightRB#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBLB[i])-HeightLB#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBLB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBRF[i])-HeightRF#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexBLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexBLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexBLF[i])-HeightLF#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexBLF[i]))
next i
endif
SetObjectMeshFromMemblock(boxes[objnumber].id,1,boxes[objnumber].memID)
DeleteMemblock(boxes[objnumber].memID)
//endif
if (width-1)/2=x and (depth-1)/2=z then centreObjId=boxes[objnumber].id
//if mod(x,2)<>0 and mod(z,2)
inc objnumber
//endif
next x
next z
endfunction centreObjId
function dohousekeeping()
DeletedObjects=0
for num = boxes.length to 0 step -1
if GetObjectExists(boxes[num].id)
r=GetObjectColorRed(boxes[num].id)
b=GetObjectColorBLue(boxes[num].id)
g=GetObjectColorGreen(boxes[num].id)
if r+b+g<1
DeleteObject(boxes[num].id)
boxes.remove(num)
inc DeletedObjects
endif
endif
next num
endfunction DeletedObjects
Function AGM_SaveObject(filename$ as string,material$ as string)
fw = OpenToWrite(filename$)
WriteLine(fw,"#AGM Object - " + filename$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
WriteLine(fw,"mtllib " + trimPath(material$))
incrementalcounter=0
`number of material
m=0
for t= boxes.length to 0 step-1
if GetObjectExists(boxes[t].id)
mesh = CreateMemblockFromObjectMesh(boxes[t].id,1)
` get object vertex quantity
verts= GetMemblockInt( mesh, 0 )
writeline(fw,"g ") //GROUP
`VERTEX
for i= 0 to verts -1
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexy(mesh,i)
z#=GetMeshMemblockVertexz(mesh,i)
ox#=(width-1)-GetObjectWorldX(boxes[t].id)
oy#=GetObjectWorldY(boxes[t].id)
oz#=GetObjectWorldZ(boxes[t].id)
Writeline ( fw, " v " + str(x#+ox#)+ " "+str(y#+oy#)+ " "+str(z#+oz#))
next i
`TEXTURE VEERTICES
for i= 1 to verts
uu#=GetMeshMemblockVertexU( mesh, i )
vv#=GetMeshMemblockVertexV( mesh, i )
writeline(fw, " vt "+str(uu#)+" "+str(vv#))
next
` NORMALS
for i= 0 to verts -1
x#=GetMeshMemblockVertexnormalX(mesh,i)
y#=GetMeshMemblockVertexnormaly(mesh,i)
z#=GetMeshMemblockVertexnormalz(mesh,i)
Writeline ( fw, " vn " + str(x#)+ " "+str(y#)+ " "+str(z#))
next i
`FACES
inc m,1
//Writeline ( fw,"usemtl Material.00"+str(m))
Writeline ( fw,"usemtl Material.00")
s=0 `reseting variable s
for i= 1 to verts-4
inc s,1
if s=3
n=n+1
nor=nor+4
writeline (fw, " f "+ str(n+1)+"/"+str(n+1)+"/"+str(nor) +" "+str(n) +"/"+str(n+2)+"/"+str(nor) +" "+str(n+2)+"/"+str(n)+"/"+str(nor) +" "+str(n+3)+"/"+str(n+3)+"/"+str(nor))
s=0
n=n+3
endif
next
DeleteMemblock(mesh)
endif
next t
CloseFile ( fw )
/*
fw=OpenToWrite(material$)
for t= boxes.length to 0 step-1
if GetObjectExists(boxes[t].id)
//Writeline ( fw,"newmtl Material.00"+str(t))
Writeline ( fw,"newmtl Material.00")
WriteLine(fw,"Kd " + str(GetObjectColorRed(boxes[t].id)/255.0) + " " + str(GetObjectColorGreen(boxes[t].id)/255.0) + " " + str(GetObjectColorBlue(boxes[t].id)/255.0))
DeleteObject(boxes[t].id)
DeleteMemblock(boxes[t].memID)
boxes.remove(t)
endif
next t
CloseFile(fw)
*/
for num = boxes.length to 0 step -1
DeleteObject(boxes[num].id)
DeleteMemblock(boxes[num].memID)
boxes.remove(num)
next num
DeleteAllObjects()
endfunction
function trimPath(str$ as string)
num=len(str$)
if num<1 then exitfunction ""
repeat
dec num
until mid(str$,num,1)="\"
for num2=(num+1) to len(Str$)
trimStr$=trimStr$+mid(str$,num2,1)
next
endfunction trimStr$
or a
much older version This is the original version which does allow you to draw within the program that was the path
I was going to take but as you can see the later version above has many improvements
// 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_E 69
#constant KEY_Z 90
#constant KEY_M 77
#constant KEY_S 83
#constant KEY_L 76
#constant KEY_C 67
// 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 _boxes
id
memID
x#
y#
z#
endtype
final_object as integer
global objnumber
colorWheelImg=createGreyWheelImage(128)
colorWheelSpr=CreateSprite(colorWheelImg)
SetSpritePosition(colorWheelSpr,screenwidth-128,128)
drawImg=createDrawImage(128)
drawSpr=CreateSprite(drawImg)
SetSpritePosition(drawSpr,screenWidth-128,0)
global boxes as _boxes[]
//centreObjId=normalHeightFunction("\media\map.png")
//centreObjId=smoothHeightFunction("\media\map.png")
//SetCameraLookAt(1,GetObjectX(centreObjId),GetObjectY(centreObjId),GetObjectZ(centreObjId),1)
//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)=0
AGM_SaveObject(boxes,"final.obj","final.mtl")
endif
endif
if GetRawKeyPressed(KEY_L)
if GetObjectExists(final_object)=0
final_object=LoadObject("final.obj")
SetObjectColor(final_object,random(1,255),random(1,255),random(1,255),255)
camerax#=32
cameray#=20
cameraz#=-19.5
endif
endif
if GetRawKeyPressed(KEY_E)
saveImage(drawImg,"map.png")
centreObjId=normalHeightFunction("map.png")
deletedObjects=dohousekeeping()
//SetCameraLookAt(1,GetObjectX(centreObjId),GetObjectY(centreObjId),GetObjectZ(centreObjId),1)
endif
if GetRawKeyPressed(KEY_C)
for num = boxes.length to 0 step -1
DeleteObject(boxes[num].id)
boxes.remove(num)
next num
DeleteAllObjects()
endif
hit=GetSpriteHit(getPointerX(),getPointerY())
rem pick a colour from the screen
if getPointerState()>0 and hit=colorWheelSpr
col = pickColor(getPointerX(),getPointerY())
alpha = (col && 0xFF000000) >> 24
red = (col && 0x00FF0000) >> 16
green = (col && 0x0000FF00) >> 8
blue = col && 0x000000FF
endif
print("color=R" +str(red)+", G"+str(green)+", B"+str(blue)+", A"+str(alpha))
if getPointerState()>0 and hit=drawSpr
drawToImage(drawImg,getPointerX()-(screenwidth-128),128-getPointerY(),col)
endif
print(boxes.length)
print("Press C to clear")
print("Press E to Extrude")
print("Press S to save objects as one")
print("Press L to load object")
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,
box as _boxes
img = LoadImage(filename$)
imgmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
size=abs(width*depth)
for num=1 to size
boxes.insert(box)
next num
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
boxes[objnumber].id=CreateObjectBox(1,1,1)
SetObjectPosition(boxes[objnumber].id,x, 0 ,-z)
SetObjectColor(boxes[objnumber].id,r,g,b,a)
boxes[objnumber].memID=CreateMemblockFromObjectMesh(boxes[objnumber].id,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRF[i])+prevHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRB[i])+prevHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLF[i])+nextHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLB[i])+nextHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(boxes[objnumber].id,1,boxes[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].id=(CreateObjectBox(1,1,1))
SetObjectPosition(boxes[objnumber].id,x, 0 ,-z)
SetObjectColor(boxes[objnumber].id,r,g,b,a)
//boxes[objnumber].memID=CreateMemblockFromObjectMesh(boxes[objnumber].id,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRF[i])+prevHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRB[i])+nextHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLF[i])+prevHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLB[i])+nextHeight#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(boxes[objnumber].id,1,boxes[objnumber].memID)
//DeleteMemblock(memID)
if (width-1)/2=x and (depth-1)/2=z then centreObjId=boxes[objnumber].id
if (width-1)/2=x and (depth-1)=z then SetCameraPosition(1,getObjectX(boxes[objnumber].id),getObjectY(boxes[objnumber].id)+20,getObjectz(boxes[objnumber].id)-20)
inc objnumber:if objnumber =4095 then exitfunction centreObjID
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,
box as _boxes
img = LoadImage(filename$)
imgmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
depth = GetMemblockInt(imgmemblock,4)
size=abs(width*depth)
for num=1 to size
boxes.insert(box)
next num
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
boxes[objnumber].id=CreateObjectBox(1,1,1)
SetObjectPosition(boxes[objnumber].id,x, 0 ,-z)
SetObjectColor(boxes[objnumber].id,r,g,b,a)
boxes[objnumber].memID=CreateMemblockFromObjectMesh(boxes[objnumber].id,1)
for i = 0 to 2
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRF[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexRB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexRB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexRB[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexRB[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLF[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLF[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLF[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLF[i]))
SetMeshMemblockVertexPosition(boxes[objnumber].memID,vertexLB[i],GetMeshMemblockVertexX(boxes[objnumber].memID,vertexLB[i]),GetMeshMemblockVertexY(boxes[objnumber].memID,vertexLB[i])+Height#,GetMeshMemblockVertexZ(boxes[objnumber].memID,vertexLB[i]))
next i
SetObjectMeshFromMemblock(boxes[objnumber].id,1,boxes[objnumber].memID)
//DeleteMemblock(memID)
if (width-1)/2=x and (depth-1)/2=z then centreObjId=boxes[objnumber].id
if (width-1)/2=x and (depth-1)=z then SetCameraPosition(1,getObjectX(boxes[objnumber].id),getObjectY(boxes[objnumber].id)+20,getObjectz(boxes[objnumber].id)-20)
inc objnumber:if objnumber =4095 then exitfunction centreObjID
next x
next z
endfunction centreObjId
function dohousekeeping()
DeletedObjects=0
for num = boxes.length to 0 step -1
if GetObjectExists(boxes[num].id)
r=GetObjectColorRed(boxes[num].id)
b=GetObjectColorBLue(boxes[num].id)
g=GetObjectColorGreen(boxes[num].id)
if r+b+g<1
DeleteObject(boxes[num].id)
boxes.remove(num)
inc DeletedObjects
endif
endif
next num
endfunction DeletedObjects
Function AGM_SaveObject(boxes as _boxes[],filename$ as string,material$ as string)
fw = OpenToWrite(filename$)
WriteLine(fw,"#AGM Object - " + filename$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
WriteLine(fw,"mtllib " + material$)
incrementalcounter=0
`number of material
m=0
//nor=0
//n=0
for t= boxes.length to 0 step-1
if GetObjectExists(boxes[t].id)
mesh = CreateMemblockFromObjectMesh(boxes[t].id,1)
` get object vertex quantity
verts= GetMemblockInt( mesh, 0 )
writeline(fw,"g ") //GROUP
`VERTEX
for i= 0 to verts -1
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexy(mesh,i)
z#=GetMeshMemblockVertexz(mesh,i)
ox#=GetObjectWorldX(boxes[t].id)
oy#=GetObjectWorldY(boxes[t].id)
oz#=GetObjectWorldZ(boxes[t].id)
Writeline ( fw, " v " + str(x#+ox#)+ " "+str(y#+oy#)+ " "+str(z#+oz#))
next i
`TEXTURE VEERTICES
for i= 1 to verts
uu#=GetMeshMemblockVertexU( mesh, i )
vv#=GetMeshMemblockVertexV( mesh, i )
writeline(fw, " vt "+str(uu#)+" "+str(vv#))
next
` NORMALS
for i= 0 to verts -1
x#=GetMeshMemblockVertexnormalX(mesh,i)
y#=GetMeshMemblockVertexnormaly(mesh,i)
z#=GetMeshMemblockVertexnormalz(mesh,i)
Writeline ( fw, " vn " + str(x#)+ " "+str(y#)+ " "+str(z#))
next i
`FACES
inc m,1
Writeline ( fw,"usemtl Material.00"+str(m))
s=0 `reseting variable s
for i= 1 to verts-4
inc s,1
if s=3
n=n+1
nor=nor+4
writeline (fw, " f "+ str(n+1)+"/"+str(n+1)+"/"+str(nor) +" "+str(n) +"/"+str(n+2)+"/"+str(nor) +" "+str(n+2)+"/"+str(n)+"/"+str(nor) +" "+str(n+3)+"/"+str(n+3)+"/"+str(nor))
s=0
n=n+3
endif
next
DeleteObject(boxes[t].id)
DeleteMemblock(boxes[t].memID)
boxes.remove(t)
DeleteMemblock(mesh)
endif
next t
CloseFile ( fw )
endfunction
function createColorWheelImage(size#)
rem set up data
w = size#
cx = w/2
cy = w/2
rem create image
clearScreen()
setScissor(0,0,w,w)
`
spr = createSprite(0)
setSpriteSize(spr,1,1)
setSpriteAngle(spr,45)
`
ang# = 0
in# = 1.0/(size#/20.0)
repeat
rem set red value
r# = 0.0
v# = abs(wrap180(360-ang#))
if v#<120
if v#<=60
r# = 255.0
else
r# = (1.0-((v#-60)/60.0))*255
endif
endif
rem set green value
g# = 0.0
v# = abs(wrap180(120-ang#))
if v#<120
if v#<=60
g# = 255.0
else
g# = (1.0-((v#-60)/60.0))*255
endif
endif
rem set blue value
b# = 0.0
v# = abs(wrap180(240-ang#))
if v#<120
if v#<=60
b# = 255.0
else
b# = (1.0-((v#-60)/60.0))*255
endif
endif
rem paint colour spoke
fw# = w
tot = w*0.5 - 2
for i=1 to tot
rem set lightness
fi# = i
f# = fi#/(fw#*0.5)
if f#>0.5
f# = 1.0 - (f#-0.5)/0.5
thisR# = f#*r#
thisG# = f#*g#
thisB# = f#*b#
else
f# = 1.0 - f#/0.5
thisR# = r# + (255.0-r#)*f#
thisG# = g# + (255.0-g#)*f#
thisB# = b# + (255.0-b#)*f#
endif
rem paint sprite
setSpriteColor(spr,thisR#,thisG#,thisB#,255)
setSpritePositionByOffset(spr,cx+1+sin(ang#)*fi#,cy+1-cos(ang#)*fi#)
drawSprite(spr)
next
ang# = ang# + in#
until ang#>360.0
`
deleteSprite(spr)
`
img = getImage(0,0,w,w)
`
setScissor(0,0,getDeviceWidth(),getDeviceHeight())
clearScreen()
endfunction img
Function pickColor(X,Y)
rem prepare image grab area
clearScreen()
setScissor(X,Y,X+1,Y+1)
render()
rem get image
img = getImage(x,y,1,1)
rem create memblock
mem = createMemblockfromImage(img)
rem get memblock data
r = getMemblockbyte(mem,12)
g = getMemblockbyte(mem,13)
b = getMemblockbyte(mem,14)
rem tidy up
deletememblock(mem)
deleteimage(img)
setScissor(0,0,getDeviceWidth(),getDeviceHeight())
clearScreen()
color=makeColor(r,g,b)
endfunction color
function wrap180(v#)
while v#>180.0
v# = v# - 360.0
endwhile
while v#<-180.0
v# = v# + 360.0
endwhile
endfunction v#
function createDrawImage(size as integer)
rem create an ellipse that can be used as a sprite
SetClearColor(0,0,0)
ClearScreen()
Render()
DrawBox(0,0,size-1,size-1,MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),1)
DrawBox(0,0,size-1,size-1,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),0)
Swap()
img=getimage(0,0,size,size)
sync()
endfunction img
function createGreyWheelImage(size as integer)
rem create an ellipse that can be used as a sprite
SetClearColor(0,0,0)
ClearScreen()
Render()
DrawBox(0,0,size-1,size-1,MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(255,255,255),MakeColor(255,255,255),1)
DrawBox(0,0,size-1,size-1,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),0)
Swap()
img=getimage(0,0,size,size)
sync()
endfunction img
function drawToImage(image as integer,x as integer,y as integer,col)
memblock=CreateMemblockFromImage(image)
width = GetMemblockInt(memblock, 0)
height = GetMemblockInt(memblock, 4)
A = (col && 0xFF000000) >> 24
R = (col && 0x00FF0000) >> 16
G = (col && 0x0000FF00) >> 8
B = col && 0x000000FF
if x<0 or x>width-1 then exitfunction
if y<0 or y>height -1 then exitfunction
R =R+ GetMemblockByte(memblock,12+(x*4)+((height-y)*4*width))
G =G+ GetMemblockByte(memblock,13+(x*4)+((height-y)*4*width))
B =B+ GetMemblockByte(memblock,14+(x*4)+((height-y)*4*width))
A =A+ GetMemblockByte(memblock,15+(x*4)+((height-y)*4*width))
if R>255 then R=255
if G>255 then G=255
if B>255 then B=255
if A>255 then A=255
SetMemblockByte(memblock,12+(x*4)+((height-y)*4*width),R) // Write Red
SetMemblockByte(memblock,13+(x*4)+((height-y)*4*width),G) // Write Green
SetMemblockByte(memblock,14+(x*4)+((height-y)*4*width),B) // Write Blue
SetMemblockByte(memblock,15+(x*4)+((height-y)*4*width),A) // Write Alpha full opaque (255)
CreateImageFromMemblock(image,memblock )
deleteMemblock(memblock)
endfunction
Normal Extrusion using the 128*128 logo but you will need a texturing program to texture it
An Example of a 2d hill kind of ramp using smooth extrusion option
created from
and just made wider
An example of twoway extrusion smooth extraction
And
created from
PS:Puzzlers light board gave me the idea and inspiration, i just made it into an object extruder
and had help from puzzler,l chafari and blink to get the saving of the objects to work but here it
is I hope people find it usefull
IT WILL SAVE THE OBJ FILE IN THE SAME DIRECTORY THE FILE MANAGER LOADS THE IMAGE FROM
JUST ADDING .OBJ TO THE NAME
fubar