+ Code Snippet// Project: MeshTemplate
// Created: 2018-09-02
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Mesh Template" )
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( 30, 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#, startx#, starty#, camerax#,cameray#,cameraz#,memblock, numberofcubes, inventorycubecount
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
size#=5
bladesize#=.2
heaviness#=1 // higher the more fuller
// Heavy Grass
y#=0
for x#=0 to size# step bladesize#
for z#=0 to size# step bladesize#
CreateMeshObjectGrassBladeWithColor(x#,0,z#,bladesize#,random2(0,heaviness#),MakeColor(0,random(100-50,100),0))
next
next
object = CreateObjectFromMeshWithColor(MeshID)
grass as integer[]
i=0
for wx#=-20 to 20
for wz#=-20 to 20
grass.insert(InstanceObject(object))
SetObjectPosition(grass[i],wx# * size#, 0, wz# * size#)
inc i
next
next
do
// RotateObjectLocalX(object,1)
// RotateObjectLocalY(object,1)
// RotateObjectLocalZ(object,1)
movecamerawithmouse()
Print( ScreenFPS() )
Sync()
loop
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
function CreateMeshObjectGrassBladeWithColor(x#, y#, z#, Width#, Height#, Color)
top=random(-1,1) // add a movement
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y#+height#+top ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y# ,z#-width#/2+top , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y# ,z#+width#/2+top , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y#+height#+top ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y# ,z#-width#/2+top , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y#+height#+top ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width#/2+top ,y# ,z#+width#/2+top , 0,1,0, 0,0,Color)
endfunction MeshID
function CreateMeshObjectBoxWithColor(x#, y#, z#, Width#, Height#, Depth#, Color)
//Front Face
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z# , 0,1,0, 0,0,Color)
// Left
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
// Right
AddVertex(MeshID, x#+width# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z#-depth# , 0,1,0, 0,0,Color)
//back
AddVertex(MeshID, x# ,y# ,z#-depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z#-depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z#-depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z#-depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z#-depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z#-depth#, 0,1,0, 0,0,Color)
//top
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y# ,z#-depth# , 0,1,0, 0,0,Color)
// bottom
AddVertex(MeshID, x# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x#+width# ,y#+height# ,z#-depth# , 0,1,0, 0,0,Color)
AddVertex(MeshID, x# ,y#+height# ,z#-depth# , 0,1,0, 0,0,Color)
endfunction MeshID
function addcube(meshID ref as mesh, cubesize#, tilenumberx, tilenumbery, normx#,normy#,normz#,x#,y#,z#)
// calculate the UVs - YIKES here we go
cubex#=((tilenumberx-1) * .5) + tilenumberx //
cubey#=(tilenumbery * 0.5)+ 0.5
maxx# = ((100/15.00)/100) * cubex#
minx# = (maxx#-(100/15.00)/100)
halfx# = (maxx#/2 + minx# / 2)
bottommaxx# = ((100/15.00)/100) * (cubex#+.5)
bottomminx# = (bottommaxx#-((100/15.00)/100))
bottomhalfx# = (bottommaxx#/2 + bottomminx# / 2)
maxy# = ((100/11.00)/100) * cubey#
miny# = (maxy#-(100/11.00)/100)
halfy# = (maxy#/2 + miny# / 2)
// create the cube
AddVertex(meshID,x# ,y#,z# ,0,1,0, maxx#,halfy#,MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize#,z# ,0,1,0, maxx#,miny#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#,z# ,0,1,0, halfx#,halfy#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z# ,0,1,0, halfx#,halfy#,MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize#,z# ,0,1,0, maxx#,miny#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize#,z# ,0,1,0, halfx#,miny#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize#,z# ,0,1,0 ,maxx# ,miny#, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize#,z#+-cubesize# ,0,1,0 ,halfx#,miny#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z# ,0,1,0 ,maxx#,halfy#, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z# ,0,1,0 ,maxx# ,halfy#, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize#,z#+-cubesize# ,0,1,0 ,halfx# ,miny#,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z#+-cubesize# ,0,1,0 ,halfx# ,halfy#, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z#+-cubesize# ,0,1,0 ,bottommaxx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z# ,0,1,0 ,bottomhalfx#,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z# ,0,1,0 ,bottommaxx#,halfy#, MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z# ,0,1,0 ,bottomhalfx#,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z#+-cubesize# ,0,1,0 ,bottommaxx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z#+-cubesize# ,0,1,0 ,bottomhalfx# ,miny# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z#+-cubesize# ,0,1,0 ,halfx# ,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z#+-cubesize# ,0,1,0 ,maxx# ,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize#,z#+-cubesize# ,0,1,0 ,halfx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize#,z#+-cubesize# ,0,1,0 ,maxx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize#,z#+-cubesize# ,0,1,0 ,halfx# ,miny# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y# ,z#+-cubesize# ,0,1,0 ,maxx# ,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#,y# ,z#+-cubesize# ,0,1,0 ,maxx# ,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize# ,z# ,0,1,0 ,halfx# ,miny# , MakeColor(255,0,0))
AddVertex(meshID,x#,y# ,z# ,0,1,0 ,halfx#,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize# ,z# ,0,1,0 ,halfx# ,miny# , MakeColor(255,0,0))
AddVertex(meshID,x#,y# ,z#+-cubesize# ,0,1,0 ,maxx# , halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize# ,z#+-cubesize# ,0,1,0 ,maxx# , miny# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize# ,z#+-cubesize# ,0,1,0 ,halfx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize# ,z# ,0,1,0 ,minx#,miny#, MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize# ,z# ,0,1,0 ,minx#,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize# ,z#+-cubesize# ,0,1,0 ,halfx# ,halfy# , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize#,y#+cubesize# ,z#+-cubesize# ,0,1,0 ,halfx#,miny# , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize# ,z# ,0,1,0 ,minx#,halfy# , MakeColor(255,0,0))
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, mode)
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)
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