Here is a meshblock version - creates individual cubes to create ground,floor,sides
// Project: multitexturedcube
// Created: 2019-01-12
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "multitexturedcube" )
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
global angx#,angy#,startx#,starty#,cube
// meshmemblock types
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
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Mesh
VertexList as Vertex[]
endtype
global MeshID as Mesh
type _meshcubedata
vertexstart
endtype
global meshcubedata as _meshcubedata[]
mcd as _meshcubedata
// meshmemblock types
img=LoadImage("\media\texture.png")
//SetImageWrapU(img,1)
//SetImageWrapV(img,1)
SetImageMagFilter(img,0)
SetImageMinFilter(img,0)
size=60
for x=-size to size
for z=-size to size
//create a floor
AddCubeToMesh(x,random(0,0),z,1,1,1)
// create a roof
AddCubeToMesh(x,10,z,1,1,1)
// create a few blocks on the ground
if random(0,10)<2 then AddCubeToMesh(random2(-size+1,size-1),1,random2(-size+1,size-1),1,1,1)
// create a wall out outside
if (z=-size or z=size) or (x=-size or x=size)
for c=0 to 10
AddCubeToMesh(x,c,z,1,1,1)
next
endif
next
next
CreateObjectFromMeshWithUVTexturing(meshid,1,img)
//SetObjectScale(cube,scale,1,scale)
//SetObjectUVScale(cube,0,scale,scale)
startx#=screenwidth/2
starty#=screenheight/2
camerax#=0
cameray#=5
cameraz#=-10
SetRawMousePosition(startx#,starty#)
do
// RotateObjectLocalY(cube,.05)
movecamera()
if GetRawKeyState(37) then inc camerax#,.05
if GetRawKeyState(39) then dec camerax#,.05
if GetRawKeyState(38) then inc cameraz#,.05
if GetRawKeyState(40) then dec cameraz#,.05
if GetRawKeyState(83) then dec cameray#,.05
if GetRawKeyState(87) then inc cameray#,.05
SetCameraPosition(1,camerax#,cameray#,cameraz#)
Print( ScreenFPS() )
Sync()
loop
function movecamera()
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 )
endfunction
function addCubeToMesh(x#,y#,z#,sizex#,sizey#,sizez#)
c=MakeColor(255,255,255)
// TOP face - top left corner texture
// Normals, U Vs , Color
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, 0 ,0 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, 0 ,.5 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, .5 ,0 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, .5 ,0 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, 0 ,.5 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, .5 ,.5 , c)
// BOTTOM face - top right corner texture
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 0.5,0 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 1,0 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 0.5,0.5 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 1,0 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 1,0.5 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 0.5,0.5 , c)
// Left Side - bottom left corner texture
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, 0,0.5, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, 0.5,0.5, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 0,1, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0, 0.5,0.5, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 0.5,1, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0, 0,1, c)
// Right Side - bottom left corner texture
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, 0,0.5, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 0,1, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, 0.5,0.5, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0, 0.5,0.5, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 0,1, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0, 0.5,1, c)
// Front Side - bottom left corner texture
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0 , 0 , 0.5, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0 , 0 , 1 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , 1 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , 1, c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , .5 , c)
AddVertex(meshid, x#-(sizex#/2) ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0 , 0 , .5 , c)
// Back Side - bottom left corner texture
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0 , 0 , 0.5, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , 1 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2) , 0,1,0 , 0 , 1 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2)-sizey# ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , 1, c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2) , 0,1,0 , 0 , .5 , c)
AddVertex(meshid, x#-(sizex#/2)+sizex# ,y#+(sizey#/2) ,z#+(sizez#/2)-sizez# , 0,1,0 , .5 , .5 , c)
endfunction
function emptymesh()
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
m.VertexList.Insert(vert)
endfunction
Function CreateObjectFromMeshWithUVTexturing(m ref as mesh, MeshIndex,texture)
DeleteMemblock(chunkmesh)
VertexCount = m.VertexList.Length + 1
IndexCount = 0
IndexOffset = 60 + VertexCount*36
chunkmesh = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(chunkmesh,0,VertexCount)
SetMemblockInt(chunkmesh,4,IndexCount)
SetMemblockInt(chunkmesh,8,3)
SetMemblockInt(chunkmesh,12,32) // no color - 36 if color
SetmemblockInt(chunkmesh,16,60)
SetMemblockInt(chunkmesh,20,IndexOffset)
SetMemblockInt(chunkmesh,24,0x0c000300)
SetMemblockString(chunkmesh,28,"position")
SetMemblockInt(chunkmesh,40,0x08000300)
SetMemblockString(chunkmesh,44,"normal")
SetMemblockInt(chunkmesh,52,0x04000200)
SetMemblockString(chunkmesh,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(chunkmesh,60+i*32,m.VertexList[i].x)
SetMemblockFloat(chunkmesh,64+i*32,m.VertexList[i].y)
SetMemblockFloat(chunkmesh,68+i*32,m.VertexList[i].z)
SetMemblockFloat(chunkmesh,72+i*32,m.VertexList[i].nx)
SetMemblockFloat(chunkmesh,76+i*32,m.VertexList[i].ny)
SetMemblockFloat(chunkmesh,80+i*32,m.VertexList[i].nz)
SetMemblockFloat(chunkmesh,84+i*32,m.VertexList[i].u)
SetMemblockFloat(chunkmesh,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
DeleteObject(cube)
cube = CreateObjectFromMeshMemblock(chunkmesh)
SetObjectImage(cube,texture,0)
endfunction cube
Just need to nip out for a bit.
Later, Will do the same version but with AppGameKit objects/clone objects - but using the memblock function above to create a multitextured cube