Thanks
Here is a low poly style landscaping with this style of coding, just a few tweaks to do on the vertice colourings.
// Project: codingtrain-terrain
// Created: 2018-08-17
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "codingtrain-terrain" )
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( 0, 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 perlinheight=100
type _perlinmap
r,g, b, a, x, y
level
texture
noise
endtype
#constant perlinw = 512 //power of 2
#constant perlinh = 512 //power of 2
#constant perlinz = 0
global perlinmap as _perlinmap[perlinw,perlinh,perlinz]
global perlinx, perlinzz
global ang#, angx#, angy#, fDiffY#, fDiffX#,newX#,startx#, starty#, camerax#,cameray#,cameraz#,memblock, noiseterrain#,offsetx#,offsety#,meshobject
global landsizex#=300
global landsizez#=300
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
blocksimage = LoadImage("test.png")
NoiseInit()
setupskyandfog()
offsetx#=0:offsety#=0
//next
meshobject = buildlandscape(offsetx#,offsety#)
//SetObjectImage(meshobject,blocksimage,0)
//SetImageMagFilter(blocksimage,0)
//SetImageMinFilter(blocksimage,0)
//SetObjectUVScale(meshobject,0,2,2)
SetCameraRange(1,.01,10000)
//SetObjectRotation(meshobject,0,180,0)
camerax#=landsizex#/2
cameray#=2
cameraz#=landsizez#/2
playerspeed#=.005
SetCameraPosition(1,camerax#,cameray#,cameraz#)
// these work the landscape rolling - everytime move the player - it moves the camera
// and increases/decreases these values too.
// if they hit past the rebuildstep variable the landscape gets rebuilt and repositioned back to the origin
// of the players starting point - but the landscapes vertcies and colours get changed depending on
/// the map location
rebuildx#=0
rebuildy#=0
rebuildstep=8
SetRawMousePosition(1024/2,768/2)
do
if GetRawKeyState(32)
DeleteObject(meshobject)
meshobject = buildlandscape(offsetx#, offsety#)
inc offsetx#,1
inc offsety#,1
endif
//cameray#=getlandposition(landsizex#/2,landsizez#/2)+1
// cameray#=1
// RotateObjectLocalY(meshobject,.1)
//RotateObjectLocalX(meshobject,.1)
//RotateObjectLocalZ(meshobject,.1)
movecamerawithmouse()
if GetRawKeyState(38)
dec offsety#,playerspeed#
dec rebuildy#,playerspeed#
//dec cameraz#,playerspeed#
// set vertices for the landscape
// setverticesandcolor()
endif
if GetRawKeyState(40)
inc offsety#,playerspeed#
inc rebuildy#,playerspeed#
// inc cameraz#,playerspeed#
// set vertices for the landscape
// setverticesandcolor()
endif
if GetRawKeyState(37) then rotateCameraLocalY( 1, -playerspeed#*5 )
if GetRawKeyState(39) then rotateCameraLocalY( 1, playerspeed#*5 )
if GetRawKeyState(38) then MoveCameraLocalZ(1, playerspeed#)
if GetRawKeyState(40) then MoveCameraLocalZ(1,-playerspeed#)
if GetRawKeyState(39)
dec offsetx#,playerspeed#
dec rebuildx#,playerspeed#
// dec camerax#,playerspeed#
// set vertices for the landscape
// setverticesandcolor()
endif
if GetRawKeyState(87)
inc cameray#,playerspeed#
endif
print(GetRawLastKey())
if GetRawKeyState(83)
dec cameray#,playerspeed#
endif
if GetRawKeyState(37)
inc offsetx#,playerspeed#
inc rebuildx#,playerspeed#
//inc camerax#,playerspeed#
// set vertices for the landscape
// setverticesandcolor()
endif
if (rebuildx#>rebuildstep)
rebuildx#=0
meshobject = buildlandscape(offsetx#, offsety#)
endif
if (rebuildx#<-rebuildstep)
rebuildx#=0
meshobject = buildlandscape(offsetx#, offsety#)
endif
if (rebuildy#>rebuildstep)
rebuildy#=0
meshobject = buildlandscape(offsetx#, offsety#)
endif
if (rebuildy#<-rebuildstep)
rebuildy#=0
meshobject = buildlandscape(offsetx#, offsety#)
endif
print (GetMemblockSize(memblock))
print (GetMemblockInt(memblock,0))
Print( ScreenFPS() )
Sync()
loop
function setverticesandcolor()
col=255
scale#=1
col=255
scale#=1
noiseterrain#=100 // the higher this is the lower the hills and vica versa - 10-12 is a nice smooth effect
for i=1 to 600-1 // get num of vertices
SetMeshMemblockVertexPosition(memblock,i,GetMeshMemblockVertexX(memblock,i)+.01,GetMeshMemblockVertexY(memblock,i)+.01,GetMeshMemblockVertexZ(memblock,i)+.001)
// SetMeshMemblockVertexPosition(memblock,i,0,0,0)
SetMeshMemblockVertexColor(memblock,i,GetMeshMemblockVertexRed(memblock,i),GetMeshMemblockVertexGreen(memblock,i),GetMeshMemblockVertexBlue(memblock,i),255)
next
SetObjectMeshFromMemblock(meshobject,1,memblock)
endfunction
function getlandposition(x#,y#)
ret = noise2d((x#+offsetx#)/noiseterrain#, (y#+offsety#)/noiseterrain#)+1
endfunction ret
function buildlandscape(offsetx#, offsety#)
DeleteObject(meshobject)
meshID.TriangleList.length=-1
meshID.VertexList.length=-1
// build triangle strip
randomheight=3
col=255
scale#=1
uvx#=1/15
uvy#=1/11
print(uvx#)
print(uvy#)
perlinheight=random(70,100)
noiseterrain#=12 // the higher this is the lower the hills and vica versa - 10-12 is a nice smooth effect
for x#=1 to landsizex#
for y#=1 to landsizez#
//noise = 255.0*noise2d(x/120.0,y/120.0)
colvertice1 = MakeColor(0,noise2d((x#+offsetx#)/noiseterrain# ,(y#+offsety#)/noiseterrain#)*col,0)
colvertice2 = checkcolorlevels(noise2d(((x#+offsetx#)+1)/noiseterrain# ,((y#+offsety#)-1)/noiseterrain#)*col)
colvertice3 = checkcolorlevels(noise2d(((x#+offsetx#)+1)/noiseterrain# ,(y#+offsety#)/noiseterrain#)*col)
colvertice4 = checkcolorlevels(noise2d((x#+offsetx#)/noiseterrain# ,(y#+offsety#)/noiseterrain#)*col)
colvertice5 = checkcolorlevels(noise2d((x#+offsetx#)/noiseterrain# ,((y#+offsety#)-1)/noiseterrain#)*col)
colvertice6 = checkcolorlevels(Noise2D(((x#+offsetx#)+1)/noiseterrain# ,((y#+offsety#)-1)/noiseterrain#)*col)
r#=0
AddVertex(meshID,((x#)*scale#) ,noise2d((x#+offsetx#)/noiseterrain#+r#, (y#+offsety#)/noiseterrain#) ,y# *scale# ,0,1,0 , 0 ,0, colvertice1)
AddVertex(meshID,((x#)+scale#)*scale# ,Noise2D(((x#+offsetx#)+scale#)/noiseterrain#+r#, ((y#+offsety#)-scale#)/noiseterrain#) ,(y#-scale#)*scale# ,0,1,0 , 0.066667/2,0.090909/2, colvertice1)
AddVertex(meshID,((x#)+scale#)*scale# ,Noise2D(((x#+offsetx#)+scale#)/noiseterrain#+r#, ((y#+offsety#)/noiseterrain#)) ,y# *scale# ,0,1,0 , 0.066667/2,0, colvertice1)
AddVertex(meshID,((x#)*scale#) ,Noise2D((x#+offsetx#)/noiseterrain#+r#, (y#+offsety#)/noiseterrain#) ,y# * scale# ,0,1,0 , 0,0, colvertice1)
AddVertex(meshID,((x#)*scale#) ,Noise2D((x#+offsetx#)/noiseterrain#+r#, ((y#+offsety#)-scale#)/noiseterrain#) ,(y#-scale#)*scale# ,0,1,0 , 0,0.090909/2, colvertice1)
AddVertex(meshID,((x#)+scale#)*scale# ,Noise2D(((x#+offsetx#)+scale#)/noiseterrain#+r#, ((y#+offsety#)-scale#)/noiseterrain#) ,(y#-scale#)*scale# ,0,1,0 , 0.066667/2,0.090909/2, colvertice1)
next
next
meshobject = CreateObjectFromMeshWithColor(meshID)
// meshobject = CreateObjectFromMeshWithUVTexturing(meshID)
SetObjectPosition(meshobject,0,0,0)
camerax#=landsizex#/2
cameraz#=landsizez#/2
endfunction meshobject
function checkcolorlevels(formula)
//formula = abs(formula)
if (formula<=0) then colvertice1=MakeColor(46,139,87)
if (formula>-50) then colvertice1=MakeColor(100,100,0) // sand
if (formula<-300) then colvertice1=MakeColor(0,0,70) // water
if (formula>0) then colvertice1=MakeColor(0,100,0) //
if (formula>100) then colvertice1=MakeColor(0,200,0)
if (formula>400) then colvertice1=MakeColor(0,255,0)
endfunction colvertice1
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/1.0
fDiffY# = (GetPointerY() - starty#)/1.0
newX# = angx# + fDiffY#
// if ( newX# > 360 ) then newX# = 360
// if ( newX# < -360 ) then newX# = -360
// if we keep hold of the left mouse button then rotate the view otherwise the camera stays put
// so can concentrate on adding/removing blocks at that positon
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
function setupskyandfog()
// setup a skybox
SetSunActive(1)
SetSkyBoxHorizonSize(4.1,4)
SetSkyBoxHorizonColor(90,132,150)//nice horizon blue
SetSkyBoxSkyColor(0,122,193)//thunder horison thunderblue
SetSkyBoxVisible(1)
SetFogColor(155,155,155)
SetFogRange(1000,2000)
SetFogMode(0)
SetCameraRange(1,0.1,10000)
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 AddTriangle(m ref as Mesh, v1 as integer, v2 as integer, v3 as integer)
t as Triangle
t.v1 = v1
t.v2 = v2
t.v3 = v3
m.TriangleList.Insert(t)
endfunction
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,32) // 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 CreateObjectFromMeshWithUVTexturing(m ref as mesh)
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
id = CreateObjectFromMeshMemblock(memblock)
// DeleteMemblock(memblock)
endfunction id
// ***************************************************************************************************
// Ken Perlin's Simplex Noise 2D. AGK Version.
// Ported from Stefan Gustavson's Java implementation
// (http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf)
// 2015.02.03
// AGK reference https://forum.thegamecreators.com/thread/213532
// Thank you Thank you
#constant PN3DF2 = 0.5*(sqrt(3.0)-1.0)
#constant PN3DG2 = (3.0-sqrt(3.0))/6.0
Type sPNVECTOR
x as float
y as float
z as float
endtype
Global PNHash as integer[512]
Global PNGrad3 as sPNVECTOR[256]
Function NoiseInit()
Local n as integer, rn as integer
For n=0 To 255
PNHash[n] = n
Next n
For n=0 To 255
rn=Random(0, 255)
PNHash.swap(n,rn)
Next n
For n=0 To 255
PNHash[256 + n] = PNHash[n]
Next n
PNHash[511] = PNHash[0]
For n=0 To 15
PNGrad3[n * 16 + 0].x = 1 : PNGrad3[n * 16 + 0].y = 1 : PNGrad3[n * 16 + 0].z = 0
PNGrad3[n * 16 + 1].x = -1 : PNGrad3[n * 16 + 1].y = 1 : PNGrad3[n * 16 + 1].z = 0
PNGrad3[n * 16 + 2].x = 1 : PNGrad3[n * 16 + 2].y = -1 : PNGrad3[n * 16 + 2].z = 0
PNGrad3[n * 16 + 3].x = -1 : PNGrad3[n * 16 + 3].y = -1 : PNGrad3[n * 16 + 3].z = 0
PNGrad3[n * 16 + 4].x = 1 : PNGrad3[n * 16 + 4].y = 0 : PNGrad3[n * 16 + 4].z = 1
PNGrad3[n * 16 + 5].x = -1 : PNGrad3[n * 16 + 5].y = 0 : PNGrad3[n * 16 + 5].z = 1
PNGrad3[n * 16 + 6].x = 1 : PNGrad3[n * 16 + 6].y = 0 : PNGrad3[n * 16 + 6].z = -1
PNGrad3[n * 16 + 7].x = -1 : PNGrad3[n * 16 + 7].y = 0 : PNGrad3[n * 16 + 7].z = -1
PNGrad3[n * 16 + 8].x = 0 : PNGrad3[n * 16 + 8].y = 1 : PNGrad3[n * 16 + 8].z = 1
PNGrad3[n * 16 + 9].x = 0 : PNGrad3[n * 16 + 9].y = -1 : PNGrad3[n * 16 + 9].z = 1
PNGrad3[n * 16 + 10].x = 0 : PNGrad3[n * 16 + 10].y = 1 : PNGrad3[n * 16 + 10].z = -1
PNGrad3[n * 16 + 11].x = 0 : PNGrad3[n * 16 + 11].y = -1 : PNGrad3[n * 16 + 11].z = -1
PNGrad3[n * 16 + 12].x = 1 : PNGrad3[n * 16 + 12].y = 1 : PNGrad3[n * 16 + 12].z = 0
PNGrad3[n * 16 + 13].x = -1 : PNGrad3[n * 16 + 13].y = 1 : PNGrad3[n * 16 + 13].z = 0
PNGrad3[n * 16 + 14].x = 0 : PNGrad3[n * 16 + 14].y = -1 : PNGrad3[n * 16 + 14].z = 1
PNGrad3[n * 16 + 15].x = 0 : PNGrad3[n * 16 + 15].y = -1 : PNGrad3[n * 16 + 15].z = -1
Next n
endfunction
function Noise2D(xin as float, yin as float)
local n0 as float, n1 as float, n2 as float, s as float, t as float, x0 as float, y0 as float, xs as float, ys as float
local i as integer, j as integer, i1 as integer, j1 as integer, i2 as integer, j2 as integer, gi0 as integer, gi1 as integer, gi2 as integer
local x1 as float, y1 as float, x2 as float, y2 as float, x3 as float, y3 as float, t0 as float, t1 as float, t2 as float
s = (xin + yin) * PN3DF2
xs = xin + s
i = floor(xs)
ys = yin + s
j = floor(ys)
t = (i + j) * PN3DG2
x0 = xin - (i - t)
y0 = yin - (j - t)
if x0>y0
i1=1
j1=0
else
i1=0
j1=1
endif
x1 = x0 - i1 + PN3DG2
y1 = y0 - j1 + PN3DG2
x2 = x0 - 1.0 + 2.0 * PN3DG2
y2 = y0 - 1.0 + 2.0 * PN3DG2
i = i && 255
j = j && 255
gi0 = PNHash[i + PNHash[j]] && 15
gi1 = PNHash[i + i1 + PNHash[j + j1]] && 15
gi2 = PNHash[i + 1 + PNHash[j+ 1]] && 15
t0 = 0.5 - x0*x0-y0*y0
if t0<0
n0 = 0.0
else
t0 = t0 * t0
n0 = t0 * t0 * (PNGrad3[gi0].x * x0 + PNGrad3[gi0].y * y0)
endif
t1 = 0.5 - x1*x1-y1*y1
if t1<0
n1 = 0.0
else
t1 = t1 * t1
n1 = t1 * t1 * (PNGrad3[gi1].x * x1 + PNGrad3[gi1].y * y1)
endif
t2 = 0.5 - x2*x2-y2*y2
if t2<0
n2 = 0.0
else
t2 = t2 * t2
n2 = t2 * t2 * (PNGrad3[gi2].x * x2 + PNGrad3[gi2].y * y2)
endif
endfunction perlinheight * (n0 + n1 + n2)
/*
AddVertex(meshID,x# *scale# ,noise2d(x#,y#)*randomheight ,y# *scale# ,0,1,0, 0,0, MakeColor(0,noise2d(x#,y#)*col,0))
AddVertex(meshID,(x#+scale#)*scale# ,noise2d(x#+scale#,y#-scale#)*randomheight ,(y#-scale#)*scale# ,0,1,0, 0,0, MakeColor(0,noise2d(x#+1,y#-1)*col,0))
AddVertex(meshID,(x#+scale#)*scale# ,noise2d(x#+scale#,y#)*randomheight ,y# *scale# ,0,1,0, 0,0, MakeColor(0,noise2d(x#+1,y#)*col,0))
AddVertex(meshID,x#*scale# ,noise2d(x#,y#)*randomheight ,y# * scale# ,0,1,0, 0,0, MakeColor(0,noise2d(x#,y#)*col,0))
AddVertex(meshID,x#*scale# ,noise2d(x#,y#-1)*randomheight ,(y#-scale#)*scale# ,0,1,0, 0,0, MakeColor(0,noise2d(x#,y#-1)*col,0))
AddVertex(meshID,(x#+scale#)*scale# ,noise2d(x#+scale#,y#-scale#)*randomheight ,(y#-scale#)*scale# ,0,1,0, 0,0, MakeColor(0,Noise2D(x#+1,y#-1)*col,0))
*/
Added 2 functions
Function CreateObjectFromMeshWithColor(m ref as mesh) - this will build a mesh with colours
Function CreateObjectFromMeshWithUVTexturing(m ref as mesh) - this will build the mesh with texture an UV capabilities
Going to working on the rolling mapping it now
Long way to go yet