Damm this is not what i was expecting
// Project: greedy algorythm
// Created: 2019-01-02
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "greedy algorythm" )
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 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 nongreedy=0
global strings as string[]
type _boxes
id
x#
y#
z#
endtype
global boxes as _boxes[]
global box as _boxes
size= 16
for x#=-size to size
for y#=-size to size
for z#=-size to size
if random(0,1)=1
buildAGKCubeIntoMesh(x#,y#,z#, MakeColor(0,random(200,255),0))
inc nongreedy
endif
next
next
next
ncube = CreateObjectFromMeshWithColor(meshid)
ncube1 = CreateObjectFromMeshWithColor(meshid)
SetObjectCullMode(ncube,1)
SetObjectCullMode(ncube1,1)
FixObjectToObject(ncube1,ncube)
SetObjectPosition(ncube1,0,0,50)
SetObjectPosition(ncube,1,0,0)
SetCameraPosition(1,0,50,-100)
angx#=0
angy#=0
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
do
print("Polygons : " + str(GetPolygonsDrawn()))
print("greedy cubes " + str(boxes.length))
print("compared to non greedy cubes " + str(nongreedy))
print("Press lft mouse to create an OBJ")
if GetRawMouseLeftPressed()
// AGM_SaveObject(boxes,1,"boxes","raw:c:\database\","test.obj","test.mtl","",0)
endif
if GetRawKeyState(37) then MoveObjectLocalX(ncube1,-1)
if GetRawKeyState(39) then MoveObjectLocalX(ncube1,1)
if GetRawKeyPressed(27) then end
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 )
Print( ScreenFPS() )
Sync()
loop
function buildAGKCubeIntoMesh(x#,y#,z#,c)
cube = CreateObjectBox(1,1,1)
SetObjectPosition(cube,x#,y#,z#)
mesh = CreateMemblockFromObjectMesh(cube,1)
verts=GetMemblockInt(mesh,0)
for v=0 to verts-1 step 4
vx1# = GetMeshMemblockVertexX(mesh,v) + x#
vy1# = GetMeshMemblockVertexY(mesh,v) + y#
vz1# = GetMeshMemblockVertexZ(mesh,v) + z#
vnx1# = GetMeshMemblockVertexNormalX(mesh,v)
vny1# = GetMeshMemblockVertexNormalY(mesh,v)
vnz1# = GetMeshMemblockVertexNormalZ(mesh,v)
vx2# = GetMeshMemblockVertexX(mesh,v+1) + x#
vy2# = GetMeshMemblockVertexY(mesh,v+1) + y#
vz2# = GetMeshMemblockVertexZ(mesh,v+1) + z#
vnx2# = GetMeshMemblockVertexNormalX(mesh,v+1)
vny2# = GetMeshMemblockVertexNormalY(mesh,v+1)
vnz2# = GetMeshMemblockVertexNormalZ(mesh,v+1)
vx3# = GetMeshMemblockVertexX(mesh,v+2) + x#
vy3# = GetMeshMemblockVertexY(mesh,v+2) + y#
vz3# = GetMeshMemblockVertexZ(mesh,v+2) + z#
vnx3# = GetMeshMemblockVertexNormalX(mesh,v+2)
vny3# = GetMeshMemblockVertexNormalY(mesh,v+2)
vnz3# = GetMeshMemblockVertexNormalZ(mesh,v+2)
vx4# = GetMeshMemblockVertexX(mesh,v+3) + x#
vy4# = GetMeshMemblockVertexY(mesh,v+3) + y#
vz4# = GetMeshMemblockVertexZ(mesh,v+3) + z#
vnx4# = GetMeshMemblockVertexNormalX(mesh,v+3)
vny4# = GetMeshMemblockVertexNormalY(mesh,v+3)
vnz4# = GetMeshMemblockVertexNormalZ(mesh,v+3)
AddVertex(meshid,vx1#,vy1#,vz1#,vnx1#,vny1#,vnz1#,0,0,c)
AddVertex(meshid,vx2#,vy2#,vz2#,vnx2#,vny2#,vnz2#,0,0,c)
AddVertex(meshid,vx3#,vy3#,vz3#,vnx3#,vny3#,vnz3#,0,0,c)
AddVertex(meshid,vx3#,vy3#,vz3#,vnx3#,vny3#,vnz3#,0,0,c)
AddVertex(meshid,vx2#,vy2#,vz2#,vnx2#,vny2#,vnz2#,0,0,c)
AddVertex(meshid,vx4#,vy4#,vz4#,vnx4#,vny4#,vnz4#,0,0,c)
next
deleteobject (cube)
endfunction
function buildmap_Greedy(sizey)
box as _boxes
a=0
xcount=0
zcount=0
startedx=1
nongreedy=0
for z=0 to strings.length
for y=0 to sizey
posx=0
for x=1 to len(strings[z])
inc nongreedy
if mid(strings[z],x,1)="1"
inc xcount
if startedx=1 then posx=x+1
startedx=2
made=0
else
if made=0
box.id = CreateObjectBox(xcount,1,1) // +1 cause xcount starts at 0
box.x# = posx+xcount/2
box.y# = y
box.z# = z
boxes.insert(box)
SetObjectPosition(boxes[a].id,boxes[a].x#,boxes[a].y#,boxes[a].z#)
SetObjectColor(boxes[a].id,random(200,255),random(200,255),random(200,255),255)
inc a
startedx=1
xcount=0
else
made=1
endif
endif
next
// if made=0 // is the object been made yet for this row
// boxes.insert(CreateObjectBox(xcount+1,1,1)) // +1 cause xcount starts at 0
// SetObjectPosition(boxes[a],x,0,z)
// SetObjectColor(boxes[a],random(200,255),random(200,255),random(200,255),255)
// inc a
// xcount=0
// endif
next
next
endfunction
function dispose()
for a=0 to boxes.length
DeleteObject(boxes[a].id)
next
strings.length=-1
boxes.length=-1
endfunction
function makemap(sizex,sizey,sizez)
for z=0 to sizez
strings.insert("")
for x=0 to sizex
strings[z]=strings[z]+str(random(0,1))
next
strings[z]=strings[z]+"0"
next
endfunction
function emptymesh()
MeshID.VertexList.Length=-1
MeshID.TriangleList.Length=-1
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,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 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
Press the cursors < and > and moe mouse
Am I been totally been misunderstood in the theory..
My mind boggles sometimes
Shall continue researching and testing