Ok cool, I think we have face resolvements now
// Project: City Views
// Created: 2018-10-28
// TO DO - Save Primative Objects to OBJ so can model them in blender and such like
// show all errors
SetErrorMode(2)
#constant PI# = 3.14159265359
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
// set window properties
SetWindowTitle( "City Views" )
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( 1200, 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 ang#, angx#, angy#, fDiffY#, fDiffX#,newX#,startx#, starty#, camerax#, cameray#, cameraz#
// *************************************************
// ** Perlin Setup - please dont upset these variables
// *************************************************
#constant perlinw = 1024 //power of 2
#constant perlinh = 1024 //power of 2
#constant perlinz = 0 // doesnt work yet - required 3D perlin noise - Yikes
#constant perlinheight=120.0
// *************************************************
// ** Mesh Memblock Types - please dont upset these variables
// *************************************************
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
type _vector
x#
y#
z#
polygon // 1 = yes - 0 = no
vindex
nindex
tindex
u#
v#
endtype
type _object
name$ as String
v as integer[] // the actual vertex on the mesh which need to save so can use SetMeshMemblockVertexPosition commands for example at a later time
vertex as _vector[] // eg v 1.276477 0.200934 -0.387665
normals as _vector[] // eg vn vn -0.5346 0.3304 -0.7779
faces as _vector[] // eg f 5//9 21//9 29//9
textures as _vector[] // eg vt 0.500 1 [0]
polygon as integer // 1=Yes , 0 = No
endtype
global objectmesh as _object[100]
type _meshmemblockvertexes
id
vfrom
vto
endtype
global meshmemblockvertexes as _meshmemblockvertexes[] // this will store the true vertex positions on a meshmemblock
type _farworlds
objID
x#
y#
z#
endtype
global farworlds as _farworlds[]
// *************************************************
// ** END OF - Mesh Memblock Types - please dont upset these variables
// *************************************************
global vertexcount
cube=LoadImage("\media\cube.png")
//LoadObjectIntoMesh(0,"\media\cube.obj","",0,1)
worldmeshes as integer[]
obj=0
wsize=32
cubesize=10
chunksize=50
noofcubes=0
chunks=0
worldsize=2
dim facechecks[chunksize,chunksize,chunksize]
for x=0 to chunksize
for y=0 to chunksize
for z=0 to chunksize
facechecks[x,y,z]=1 // 1=block, 0=air
facechecks[x,y,z]=random(0,1) // 1=block, 0=air --- UNcomment this line to see a different aspect
next
next
next
for x=0 to chunksize
for y=0 to chunksize
for z=0 to chunksize
t=1:b=0:l=0:r=0:f=0:ba=0
if x-1>=0
if facechecks[x-1,y,z]=0 then f=1
endif
if x+1<=chunksize
if facechecks[x+1,y,z]=0 then ba=1
endif
if z-1>=0
if facechecks[x,y,z-1]=0 then l=1
endif
if z+1<=chunksize
if facechecks[x,y,z+1]=0 then r=1
endif
if y+1<=chunksize
if facechecks[x,y+1,z]=0 then b=1
endif
if y-1<=0
if facechecks[x,y+1,z]=0 then t=1
endif
if facechecks[x,y,z]=1 then Create3DSingleSidedCube(x*cubesize*2,y*cubesize*2,z*cubesize*2,cubesize,cubesize,cubesize,t,b,l,r,f,ba)
inc noofcubes
next
next
next
cube= CreateObjectFromMeshWithUVTexturing(meshid,cube)
// next
//next
setupcamera()
do
print("Number of cubes " + str(noofcubes))
print("Number of Polys " + str(noofcubes*2))
print("Number of Actual Polys Drawn " + str( GetPolygonsDrawn()))
print("Number of chunks " + str(chunks))
print("Chunk size " + str(wsize) + " x " + str(wsize) + " x " + str(wsize))
print("World size " + str(worldsize) + " x " + str(worldsize) + " x " + str(worldsize))
RotateCameraLocalX(1,.05)
RotateCameraLocalY(1,.05)
RotateCameraLocalZ(1,.05)
movecamerawithmouse()
if GetRawKeyState(87) then inc cameray#,1
if GetRawKeyState(83) then dec cameray#,1
if GetRawKeyState(37) then inc camerax#,1
if GetRawKeyState(39) then dec camerax#,1
if GetRawKeyState(38) then inc cameraz#,1
if GetRawKeyState(40) then dec cameraz#,1
print("Use WASD and cursors to move camera")
SetCameraPosition(1,camerax#,cameray#,cameraz#)
print("FPS " + str(ScreenFPS()) + " - Last Key" + str(GetRawLastKey()))
Sync()
loop
function buildchunk(wsize,cubesize,texture)
// build a chunk
for x#=-wsize to wsize
for y#=0 to 0
for z#=-wsize to wsize
Create3DSingleSidedCube(x#*(cubesize*2),y#*(cubesize*2),z#*(cubesize*2),cubesize,cubesize,cubesize,1,0,0,0,0,0)
// InstanceMeshObject(0,obj,x#*(cubesize*2),y#*(cubesize*2),z#*(cubesize*2),cubesize,cubesize,cubesize,MakeColor(255,255,255),0)
// inc obj
next
next
next
// crate the chunk and join them together
chunk = CreateObjectFromMeshWithUVTexturing(meshid,texture)
emptymesh()
endfunction chunk
// X#,Y#,Z# - position of the cube
// SizeX, SizeY,SizeZ - size of the cube in all directions
// t - top - 1=yes-0-no
// b - bottom - 1=yes-0-no
// l - left - 1=yes-0-no
// r - right - 1=yes-0-no
// f - front - 1=yes-0-no
// ba - back - 1=yes-0-no
function Create3DSingleSidedCube(x#, y#, z#, sizex,sizey,sizez, t,b,l,r,f,ba)
red=MakeColor(255,255,255)
green=MakeColor(255,255,255)
blue=MakeColor(255,255,255)
//top
if t=1
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+ sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+ sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+ sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+-sizez), 0,1,0 ,1,1, MakeColor(red,green,blue))
endif
// bottom
if b=1
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+ sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex) ,y#-sizey,(z#+ sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex) ,y#-sizey,(z#+ sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex) ,y#-sizey,(z#+-sizez), 0,1,0 ,1,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
endif
// left
if l=1
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+-sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+-sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+-sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#-sizey,(z#+-sizez), 0,1,0 ,1,1, MakeColor(red,green,blue))
endif
// right
if r=1
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+sizez) , 0,1,0 ,1,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#+sizey,(z#+sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ sizex),y#-sizey,(z#+sizez), 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
endif
// front
if f=1
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#-sizez) , 0,1,0 ,1,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#+sizey,(z#+sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#+sizez), 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-sizex),y#-sizey,(z#-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
endif
// back
if ba=1
AddVertex(meshid,(x#+sizex),y#+sizey,(z#-sizez) , 0,1,0 ,1,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+sizex),y#-sizey,(z#-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+sizex),y#+sizey,(z#+sizez) , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+sizex),y#+sizey,(z#+sizez) , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+sizex),y#-sizey,(z#-sizez), 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+sizex),y#-sizey,(z#+sizez), 0,1,0 ,1,0, MakeColor(red,green,blue))
endif
endfunction
function setupcamera()
camerax#=0
cameray#=10
cameraz#=-20
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
SetCameraRange(1,0.1,3000)
endfunction
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.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
// *************************************************
// ** Mesh Memblock Function
// *************************************************
function emptymesh()
MeshID.TriangleList.length=-1
MeshID.VertexList.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 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 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
WASD or cursors to move about and change line
Uncomment liine 138 to see a differently