Another terrain creator
This one may be suitable for those that want a low poly retro feel to a game
No media required
1> w a s d/shift move camera (camera needs work)
2> space saves
the file will save to a location of your choice but you meed a file that exists there first ie dummy.obj
the program will then save dummy.obj and dummy.mtl at that location
3> you may wish to bring into a program like blender to weld close verts etc effectively shrinking the file size
USE AT OWN RISK
// Project: heights
// Created: 2019-01-01
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "heights" )
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
Create3DPhysicsWorld()
#constant sizex=21 // change this to extend the map
#constant sizez=21 // change this to exeend the map
global angx#, angy#,startx#,starty#
startx#=screenwidth/2
starty#=screenheight/2
size=5
type _map
id
x#
height#
z#
colorred
colorgreen
colorblue
texture
endtype
global map as _map[sizex,sizez]
global vertexnumber
//textimages as integer[10]
//for loops=0 to 10
// textimages[loops]= createtexture(64,64,MakeColor(0,255,0),255)
//next
textimages as integer[10]
textimages[0]= createtexture(64,64,MakeColor(0,255,0),255)
textimages[1]= createtexture(64,64,MakeColor(255,255,255),255)
land = CreateObjectBox(.01,.01,.01)
colorgreen=150
for x=1 to sizex
for z=1 to sizez
map[x,z].id = CreateObjectBox(9,9,9)
map[x,z].x# = x * GetObjectSizeMaxX(map[x,z].id)*2 // Remove the * 2 to join the cubes together
map[x,z].height#=random(0,0)
map[x,z].z# = z * GetObjectSizeMaxZ(map[x,z].id)*2 // Remove the * 2 to join the cubes together
//map[x,z].colorgreen = random(150,200)
//SetObjectImage(map[x,z].id,textimages[0],0)
if colorgreen=150
colorgreen=200
else
colorgreen=150
endif
map[x,z].colorgreen =colorGreen
SetObjectImage(map[x,z].id,textimages[0],0)
Create3DPhysicsStaticBody(map[x,z].id)
SetObjectColor(map[x,z].id,0,map[x,z].colorgreen,0,255)
SetObjectPosition(map[x,z].id,map[x,z].x#,0,map[x,z].z#)
FixObjectToObject(map[x,z].id,land)
next
next
RotateObjectLocalY(land,-30)
prepareheights()
camerax#=20
cameray#=30
cameraz#=0
// frameImg=LoadImage("\media\frame2.png")
//FrameSpr=createSprite(frameImg)
collisioner=CreateObjectBox(10,10,10)
do
// SetSpriteDepth(frameSpr,100) //set the frame to behind all tiles to allow clicking to work
unit_x#=Get3DVectorXFromScreen(getpointerx(),getpointery())
unit_y#=Get3DVectorYFromScreen(getpointerx(),getpointery())
unit_z#=Get3DVectorZFromScreen(getpointerx(),getpointery())
// calculate the start of the ray cast, which is the unit vector + the camera position
start_x# = unit_x# + camerax#
start_y# = unit_y# + cameray#
start_z# = unit_z# - cameraz#
// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
end_x# = 800*unit_x# + camerax#
end_y# = 800*unit_y# + cameray#
end_z# = 800*unit_z# - cameraz#
// determine which object has been hit
object_hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
// if an object has been hit then turn it red
if object_hit <> 0
x=getobjectx(object_hit)/9
z=getobjectz(object_hit)/9
else
endif
if GetRawKeyState(13)
RotateObjectLocalY(land,.05)
endif
if GetRawMouseLeftState()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1
inc map[x,z].height#,.01
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
endif
endif
if GetRawMouseRightState()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1 and map[x,z].height#>0
dec map[x,z].height#,.01
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,-.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
endif
endif
if GetRawMouseMiddlePressed()
flag=1
if x<2 or x>sizex-1 then flag=0
if z<2 or z>sizez-1 then flag=0
if flag=1
//if map[x,z].height#>0
dec map[x,z].height#,10
SetObjectPosition(map[x,z].id,map[x,z].x#,map[x,z].height#,map[x,z].z#)
changeverts(x,z,-10.01)
Delete3DPhysicsBody(map[x,z].id)
Create3DPhysicsStaticBody(map[x,z].id)
//endif
//SetObjectColor(map[x,z].id,GetObjectColorRed(map[x,z].id)-50,GetObjectColorGreen(map[x,z].id)-50,GetObjectColorBlue(map[x,z].id)-50,255)
//SetObjectColor(map[x+1,z+1].id,GetObjectColorRed(map[x+1,z+1].id)-50,GetObjectColorGreen(map[x+1,z+1].id)-50,GetObjectColorBlue(map[x+1,z+1].id)-50,255)
//SetObjectColor(map[x-1,z+1].id,GetObjectColorRed(map[x-1,z+1].id)-50,GetObjectColorGreen(map[x-1,z+1].id)-50,GetObjectColorBlue(map[x-1,z+1].id)-50,255)
//SetObjectColor(map[x-1,z-1].id,GetObjectColorRed(map[x-1,z-1].id)-50,GetObjectColorGreen(map[x-1,z-1].id)-50,GetObjectColorBlue(map[x-1,z-1].id)-50,255)
//SetObjectColor(map[x+1,z].id,GetObjectColorRed(map[x+1,z].id)-50,GetObjectColorGreen(map[x+1,z].id)-50,GetObjectColorBlue(map[x+1,z].id)-50,255)
//SetObjectColor(map[x-1,z].id,GetObjectColorRed(map[x-1,z].id)-50,GetObjectColorGreen(map[x-1,z].id)-50,GetObjectColorBlue(map[x-1,z].id)-50,255)
//SetObjectColor(map[x,z+1].id,GetObjectColorRed(map[x,z+1].id)-50,GetObjectColorGreen(map[x,z+1].id)-50,GetObjectColorBlue(map[x,z+1].id)-50,255)
//SetObjectColor(map[x,z-1].id,GetObjectColorRed(map[x,z-1].id)-50,GetObjectColorGreen(map[x,z-1].id)-50,GetObjectColorBlue(map[x,z-1].id)-50,255)
SetObjectColor(map[x,z].id,100,100,100,255):SetObjectImage(map[x,z].id,textImages[1],0)
SetObjectColor(map[x+1,z+1].id,100,100,100,255):SetObjectImage(map[x+1,z+1].id,textImages[1],0)
SetObjectColor(map[x-1,z+1].id,100,100,100,255):SetObjectImage(map[x-1,z+1].id,textImages[1],0)
SetObjectColor(map[x-1,z-1].id,100,100,100,255):SetObjectImage(map[x-1,z-1].id,textImages[1],0)
SetObjectColor(map[x+1,z].id,100,100,100,255):SetObjectImage(map[x+1,z].id,textImages[1],0)
SetObjectColor(map[x-1,z].id,100,100,100,255):SetObjectImage(map[x-1,z].id,textImages[1],0)
SetObjectColor(map[x,z+1].id,100,100,100,255):SetObjectImage(map[x,z+1].id,textImages[1],0)
SetObjectColor(map[x,z-1].id,100,100,100,255):SetObjectImage(map[x,z-1].id,textImages[1],0)
endif
endif
if GetRawKeyState(68) then inc camerax#,.1
if GetRawKeyState(65) then dec camerax#,.1
if GetRawKeyState(83) then inc cameray#,.1
if GetRawKeyState(87) then dec cameray#,.1
movecamera()
if GetRawKeyPressed(32)
SetObjectRotation(land,0,0,0)
ret$=ChooseRawFile("*.obj;*.mtl",1)
if ret$<>""
print("Saving"):sync()
AGM_SaveObject("raw:"+left(ret$,len(ret$)-4)+".obj","raw:"+left(ret$,len(ret$)-4)+".mtl")
endif
endif
SetCameraPosition(1,camerax#,cameray#,cameraz#)
Print( ScreenFPS() )
Sync()
loop
function movecamera()
if GetRawKeyState(32)
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation(1, newX#, angy# + fDiffX#, 0 )
endif
endfunction
function prepareheights()
for x=2 to sizex-1
for z=2 to sizez-1
changeverts(x,z,map[x,z].height#)
next
next
endfunction
function changeverts(x,z,slope#)
CreateMemblockFromObjectMesh(2,map[x+1,z-1].id,1) // bottom right corner
CreateMemblockFromObjectMesh(4,map[x+1,z+1].id,1) // top right corner
CreateMemblockFromObjectMesh(5,map[x-1,z+1].id,1) // top left corner
CreateMemblockFromObjectMesh(6,map[x-1,z-1].id,1) // bottom left corner
CreateMemblockFromObjectMesh(3,map[x+1,z].id,1) // right edges
CreateMemblockFromObjectMesh(7,map[x-1,z].id,1) // left cube edges
CreateMemblockFromObjectMesh(8,map[x,z-1].id,1) // bottom cube edges
CreateMemblockFromObjectMesh(9,map[x,z+1].id,1) // top cube edges
for i=0 to 23
// bottom rightcorner
if i=8 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
if i=16 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
if i=14 then SetMeshMemblockVertexPosition(2,i,GetMeshMemblockVertexX(2,i),GetMeshMemblockVertexY(2,i)+slope#,GetMeshMemblockVertexZ(2,i))
// top rightcorner
if i=9 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
if i=0 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
if i=18 then SetMeshMemblockVertexPosition(4,i,GetMeshMemblockVertexX(4,i),GetMeshMemblockVertexY(4,i)+slope#,GetMeshMemblockVertexZ(4,i))
// top leftcorner
if i=11 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
if i=2 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
if i=4 then SetMeshMemblockVertexPosition(5,i,GetMeshMemblockVertexX(5,i),GetMeshMemblockVertexY(5,i)+slope#,GetMeshMemblockVertexZ(5,i))
// tbottom leftcorner
if i=10 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
if i=6 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
if i=12 then SetMeshMemblockVertexPosition(6,i,GetMeshMemblockVertexX(6,i),GetMeshMemblockVertexY(6,i)+slope#,GetMeshMemblockVertexZ(6,i))
// right cube
if i=8 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=9 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=16 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=18 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=0 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
if i=14 then SetMeshMemblockVertexPosition(3,i,GetMeshMemblockVertexX(3,i),GetMeshMemblockVertexY(3,i)+slope#,GetMeshMemblockVertexZ(3,i))
// left cube
if i=10 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=11 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=4 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=6 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=2 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
if i=12 then SetMeshMemblockVertexPosition(7,i,GetMeshMemblockVertexX(7,i),GetMeshMemblockVertexY(7,i)+slope#,GetMeshMemblockVertexZ(7,i))
// bottom cube
if i=8 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=10 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=6 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=12 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=14 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
if i=16 then SetMeshMemblockVertexPosition(8,i,GetMeshMemblockVertexX(8,i),GetMeshMemblockVertexY(8,i)+slope#,GetMeshMemblockVertexZ(8,i))
// top cube
if i=9 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=11 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=0 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=2 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=4 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
if i=18 then SetMeshMemblockVertexPosition(9,i,GetMeshMemblockVertexX(9,i),GetMeshMemblockVertexY(9,i)+slope#,GetMeshMemblockVertexZ(9,i))
next
SetObjectMeshFromMemblock(map[x+1,z-1].id,1,2)
SetObjectMeshFromMemblock(map[x+1,z].id,1,3)
SetObjectMeshFromMemblock(map[x+1,z+1].id,1,4)
SetObjectMeshFromMemblock(map[x-1,z+1].id,1,5)
SetObjectMeshFromMemblock(map[x-1,z-1].id,1,6)
SetObjectMeshFromMemblock(map[x-1,z].id,1,7)
SetObjectMeshFromMemblock(map[x,z-1].id,1,8)
SetObjectMeshFromMemblock(map[x,z+1].id,1,9)
DeleteMemblock(2)
DeleteMemblock(3)
DeleteMemblock(4)
DeleteMemblock(5)
DeleteMemblock(6)
DeleteMemblock(7)
DeleteMemblock(8)
DeleteMemblock(9)
endfunction
// Function to create a texture
//
// Inputs - Sizex - size of the texture to create - width
// Sizey - size of the texture to create - height
// Color - is the main color of the image
// Denisity - is a the depth of the texture - the lower the value, the more detail. higher value = no detail
//
// Returns the image for the resulting texture
//
// EG. CreateTexture ( 100, 100, makecolor(0,0,255), 100)
// This could create a DEEP water effect texture?
function createtexture(sizex# as float, sizey# as float, color, density as integer)
swap()
drawbox(0,0,sizex#, sizey#, color, color,color,color, 1)
render()
img = getimage(0,0,sizex#, sizey#)
memblockid = CreateMemblockFromImage (img)
imgwidth = GetMemblockInt(memblockid, 0)
imgheight = GetMemblockInt(memblockid, 4)
size=GetMemblockSize(memblockid)
for offset=12 to size-4 step 4
r=GetMemblockByte(memblockid, offset)
g=GetMemblockByte(memblockid, offset+1)
b=GetMemblockByte(memblockid, offset+2)
a=GetMemblockByte(memblockid, offset+3)
strength=random(1,density)
SetMemblockByte (memblockid, offset, r-strength)
SetMemblockByte (memblockid, offset+1, g-strength)
SetMemblockByte (memblockid, offset+2, b-strength )
SetMemblockByte (memblockid, offset+3, a-strength)
next
deleteimage (img)
img = CreateImageFromMemblock(memblockid)
DeleteMemblock(memblockid)
endfunction img
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
size=abs(sizex*sizez)
for x = 1 to sizex
for z = 1 to sizez
if GetObjectExists(map[x,z].id)
mesh = CreateMemblockFromObjectMesh(map[x,z].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#=(size-1)-GetObjectWorldX(map[x,z].id)
ox#=GetObjectWorldX(map[x,z].id)
oy#=GetObjectWorldY(map[x,z].id)
oz#=GetObjectWorldZ(map[x,z].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 z
next x
CloseFile ( fw )
fw=OpenToWrite(material$)
for z = 1 to sizex
for x = 1 to sizez
if GetObjectExists(map[x,z].id)
//Writeline ( fw,"newmtl Material.00"+str(t))
Writeline ( fw,"newmtl Material.00")
WriteLine(fw,"Kd " + str(GetObjectColorRed(map[x,z].id)/255.0) + " " + str(GetObjectColorGreen(map[x,z].id)/255.0) + " " + str(GetObjectColorBlue(map[x,z].id)/255.0))
//DeleteObject(map[x,z].id)
//DeleteMemblock(map[x,z].memID)
//map.remove(x,z)
endif
next x
next z
CloseFile(fw)
//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$