I have now added a boundary code so when your outside the whole object then it will increase its occulusions
and if inside the object it decreases the occlusions
// Project: greedy algorythm
// Created: 2019-01-02
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
#constant size=16
#constant near#=0
#constant far#=4
#constant gap=2
// 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
type _boxes
id
x#
y#
z#
endtype
global boxes as _boxes[size,size,size]
global box as _boxes
global cameraobject
cameraobject=CreateObjectCapsule(2,2,2)
camerax#=0
cameray#=-near#-1
cameraz#=0
SetObjectPosition(cameraobject,camerax#,cameray#,cameraz#)
a=0
pivot=CreateObjectBox(1,1,1)
for x=0 to size
for z=0 to size
for y=0 to size
boxes[x,y,z].id = CreateObjectBox(1,1,1)
boxes[x,y,z].x# = x * gap
boxes[x,y,z].y# = y * gap
boxes[x,y,z].z# = z * gap
SetObjectPosition(boxes[x,y,z].id,boxes[x,y,z].x#,boxes[x,y,z].y#,boxes[x,y,z].z#)
FixObjectToObject(boxes[x,y,z].id,pivot)
// buildAGKCubeIntoMesh(x#*gap,y#*gap,z#*gap, MakeColor(0,random(200,255),0))
inc nongreedy
inc a
next
next
next
SetCameraPosition(1,0,50,-100)
angx#=0
angy#=0
startx#=screenwidth/2
starty#=screenheight/2
cameraonobject=-1
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 GetRawKeyState(32)
for x=0 to size
for y=0 to size
for z=0 to size
SetObjectVisible(boxes[x,y,z].id,1)
next
next
next
else
for x=0 to size
for y=0 to size
for z=0 to size
// out of bounds of the full object then increase the near and far
if camerax#<0 or cameray#<0 or cameraz#<0 or camerax# > size * gap or cameray# > size * gap or cameraz# > size * gap
if distance#(boxes[x,y,z].x#,boxes[x,y,z].y#,boxes[x,y,z].z#,camerax#,cameray#,cameraz#) < far# * 5 and distance#(boxes[x,y,z].x#,boxes[x,y,z].y#,boxes[x,y,z].z#,camerax#,cameray#,cameraz#) > near#
SetObjectColor(boxes[x,y,z].id,255,0,0,255)
SetObjectVisible(boxes[x,y,z].id,1)
//z=size
else
SetObjectColor(boxes[x,y,z].id,255,255,0,255)
SetObjectVisible(boxes[x,y,z].id,0)
endif
else
// inside the object
if distance#(boxes[x,y,z].x#,boxes[x,y,z].y#,boxes[x,y,z].z#,camerax#,cameray#,cameraz#) < far# and distance#(boxes[x,y,z].x#,boxes[x,y,z].y#,boxes[x,y,z].z#,camerax#,cameray#,cameraz#) > near#
SetObjectColor(boxes[x,y,z].id,255,0,0,255)
SetObjectVisible(boxes[x,y,z].id,1)
//z=size
else
SetObjectColor(boxes[x,y,z].id,255,255,0,255)
SetObjectVisible(boxes[x,y,z].id,0)
endif
endif
next
next
next
endif
if GetRawKeyState(37) then dec camerax#,.1
if GetRawKeyState(39) then inc camerax# ,.1
if GetRawKeyState(38) then inc cameray# ,.1
if GetRawKeyState(40) then dec cameray# ,.1
if GetRawKeyState(87) then inc cameraz# ,.1
if GetRawKeyState(83) then dec cameraz# ,.1
if GetRawKeyState(82) // R= rotate
RotateObjectLocalX(pivot,.1)
RotateObjectLocaly(pivot,.1)
RotateObjectLocalz(pivot,.1)
endif
if GetRawKeyPressed(67) // C - Camera on cameraobject
cameraonobject=-cameraonobject
endif
if cameraonobject=1
SetCameraPosition(1,camerax#,cameray#,cameraz#)
else
SetCameraPosition(1,0,40,-40)
endif
print(GetRawLastKey())
SetObjectPosition(cameraobject,camerax#,cameray#,cameraz#)
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 chunk_raycast(objID1, objID2)
old_x# = GetObjectX(objID1)
old_y# = GetObjectY(objID1)
old_z# = GetObjectZ(objID1)
new_x# = GetObjectX(objID2)
new_y# = GetObjectY(objID2)
new_z# = GetObjectZ(objID2)
if ObjectRayCast(objID1,old_x#,old_y#,old_z# ,new_x#,new_y#,new_z#) = 1
hit=1
else
hit=0
endif
endfunction hit
function buildAGKCubeIntoMesh(x#,y#,z#,c)
cube = CreateObjectBox(1,1,1)
SetObjectPosition(cube,x#,y#,z#)
/*
oc.id = cube
oc.x#=x#
oc.y#=y#
oc.z#=z#
objectcolider.insert(oc)
*/
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 dispose()
for a=0 to boxes.length
DeleteAllObjects()
next
boxes.length=-1
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
function distance#(x1#,y1#,z1#,x2#,y2#,z2#)
value#=sqrt(((x2#-(x1#))^2)+((y2#-(y1#))^2)+((z2#-(z1#))^2))
endfunction value#
Have a great day...
Come back soon