Im not too sure on texturing at the moment - will look into it. thats something more that would like to get my head round.
But for the time being, here are a few more functions that could be helpful
Memblock Exporter to an external file - named of your choice
// Project: meshimages
// Created: 2018-06-16
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "meshimages" )
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#
global startx#
startx#=1024/2
global starty#
starty#=768/2
// setup the types - each entity may have multiple of entities to keep track of, these can be stored as Types
type _textures
id // texture store
r,g,b // colors
dr,dg,db // depths (because minecraft tiles is built with rectangular different shades of the color)
texturessize
endtype
// textures
global textures as _textures[50] // used to store textures (not ready yet)
definetextures()
sleepwithsync("Exporting a cube to test.agkobj....", 1500)
cube = CreateObjectBox(5,5,5)
exportobject(cube, "test.agkobj")
DeleteObject(cube)
do
print ("Exported to test.agkobj... Use the load function to bring it back to life")
Print( ScreenFPS() )
Sync()
loop
// Imports an external object created with the export function here
//
// objid = object to export
// filename = resulting file that it creates
function importobject(filename as string)
objectfile= OpenToRead(filename)
size = ReadInteger(objectfile)
chk = CreateMemblock(size)
SetMemblockInt(chk,0, ReadInteger(objectfile))
SetMemblockInt(chk,4, ReadInteger(objectfile))
SetMemblockInt(chk,8, ReadInteger(objectfile))
SetMemblockInt(chk,12, ReadInteger(objectfile))
VertexSize = ReadInteger(objectfile)
SetMemblockInt(chk,16, VertexSize)
SetMemblockInt(chk,20, ReadInteger(objectfile))
SetMemblockInt(chk,24, ReadInteger(objectfile))
SetMemblockstring(chk,28, ReadString(objectfile))
SetMemblockInt(chk,40, ReadInteger(objectfile))
SetMemblockString(chk,44, ReadString(objectfile))
SetMemblockInt(chk,52, ReadInteger(objectfile))
SetMemblockString(chk,56, ReadString(objectfile))
for a=VertexSize to size-VertexSize
SetMemblockInt(chk,a,ReadInteger(objectfile))
next
CloseFile(objectfile)
mainobject = CreateObjectFromMeshMemblock(chk)
endfunction mainobject
// Creates an external file with all the object data
//
// objid = object to export
// filename = resulting file that it creates
function exportobject(objid, filename as string)
objectfile= OpenToWrite(filename)
objmem = CreateMemblockFromObjectMesh(objid,1)
VertexSize = GetMemblockInt(objmem,16)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
WriteInteger(objectfile, ObjectDataSize+VertexSize) // Object Data Size
WriteInteger(objectfile,GetMemblockInt(objmem,0)) // Vertex Count
WriteInteger(objectfile, GetMemblockInt(objmem,4)) // Indices
WriteInteger(objectfile, GetMemblockInt(objmem,8)) // Attribues
WriteInteger(objectfile, GetMemblockInt(objmem,12)) // Vertex Offset
WriteInteger(objectfile, GetMemblockInt(objmem,16)) // Vertex Size
WriteInteger(objectfile, GetMemblockInt(objmem,20)) // Object Size
WriteInteger(objectfile, GetMemblockInt(objmem,24)) // PositionData
WriteString(objectfile, GetMemblockString(objmem,28,8)) // Position Tag
WriteInteger(objectfile, GetMemblockInt(objmem,40)) //Normal Data
WriteString(objectfile, GetMemblockString(objmem,44,6)) // Normal Tag
WriteInteger(objectfile, GetMemblockInt(objmem,52)) // UV Data
WriteString(objectfile, GetMemblockString(objmem,56,2)) // UV Tag
for a=VertexSize to ObjectDataSize
WriteInteger(objectfile, GetMemblockInt(objmem,a)) // Data itself
next
CloseFile(objectfile)
endfunction
// Creates a copy of an object with debug information and a delay
//
// objid = the object you wish to make a copy of
// debuginfo = 1 then prints info of the object going into the function
//
function makememblock(objid, debuginfo)
objmem = CreateMemblockFromObjectMesh(objid,1)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
VertexSize = GetMemblockInt(objmem,16)
chk=CreateMemblock(ObjectDataSize + VertexSize)
SetMemblockInt(chk,0,GetMemblockInt(objmem,0)) // Vertice Count
SetMemblockInt(chk,4,GetMemblockInt(objmem,4)) // Indices
SetMemblockInt(chk,8,GetMemblockInt(objmem,8))
SetMemblockInt(chk,12,GetMemblockInt(objmem,12)) // Vertice Off
SetMemblockInt(chk,16,GetMemblockInt(objmem,16)) // Vertice size
SetMemblockInt(chk,20,GetMemblockInt(objmem,20))
SetMemblockInt(chk,24,GetMemblockInt(objmem,24))
SetMemblockString(chk,28,GetMemblockstring(objmem,28,8))
SetMemblockInt(chk,40,GetMemblockInt(objmem,40)) //same as position, but for normals
SetMemblockString(chk,44,GetMemblockString(objmem,44,6))
SetMemblockInt(chk,52,GetMemblockInt(objmem,52)) //For color we have byte, 4 components, normalize data
SetMemblockString(chk,56,GetMemblockString(objmem,56,2))
for a=VertexSize to ObjectDataSize
SetMemblockInt(chk,a,GetMemblockInt(objmem,a))
next
if (debuginfo = 1)
repeat
print("Vertice Count " + str(GetMemblockInt(objmem,0)))
print("Vertice Indices " + str(GetMemblockInt(objmem,4)))
print("Vertice Attributes " + str(GetMemblockInt(objmem,8)))
print("Vertice Offset " + str(GetMemblockInt(objmem,12)))
print("Vertice Size " + str(GetMemblockInt(objmem,16)))
print("Object Size " + str(GetMemblockInt(objmem,20)))
print("Position / Components / Normalisation / Position Data " + str(GetMemblockInt(objmem,24)))
print("Shader Tag " + GetMemblockString(objmem,28,8))
print("Normalisation / Position Data " + str(GetMemblockInt(objmem,40)))
print("Shader Tag " + GetMemblockString(objmem,44,6))
print("UV Data " + str(GetMemblockInt(objmem,52)))
print("Shader Tag" + GetMemblockString(objmem,56,2))
print("Press Space to Continue")
sync()
until GetRawKeyPressed(32)
endif
MainObject = CreateObjectFromMeshMemblock(chk)
endfunction MainObject
// AddObjectToMeshMemblockWithPosition
function AddObjectToMeshMemblockWithPosition( MainObject, ObjID, MeshIndex, X, Y, Z )
// Get the meshes
/* if GetObjectNumMeshes(MainObject)<>MeshIndex
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
AddObjectMeshFromMemblock(MainObject,OriginalObjectMemory)
else
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
endif
*/
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
NewObjectMemory = CreateMemblockFromObjectMesh(Objid, MeshIndex)
// Move the object on the mesh
// now need to ammend the vertcount by adding the vert count of the object coming in + vertices of the original object
VertCountOriginal = GetMemblockInt(OriginalObjectMemory,0)
VertCountNew = GetMemblockInt(NewObjectMemory,0)
VertSize = GetMemblockInt(NewObjectMemory,12)
VertOffset = GetMemblockInt(NewObjectMemory, 16)
DestOffset = GetMemblockInt(OriginalObjectMemory, 16)
print (str(VertcountOriginal + VertCountNew))
print (str(VertcountOriginal))
print (str(VertCountNew))
print ("Vert Size - " + str(VertSize))
print ("Vert Offset - " + str(VertOffset))
sleepwithsync("",1000)
// SetMemblockInt(Originalobjectmemory,16,GetMemblockInt(OriginalObjectMemory,16) + VertOffset)
setobjectpositiononmesh(MainObject, OriginalObjectMemory,MeshIndex,x,y,z)
// Memblock Header = 36 bytes
// CopyMemblock(NewObjectmemory, OriginalObjectMemory, VertOffset+1, Vertsize, Vertsize)
MainObject = CreateMemblockFromObjectMesh(OriginalObjectMemory, MeshIndex)
endfunction MainObject
function sleepwithsync(txt as string, num)
print(txt)
sync()
sleep(num)
endfunction
// AddObjectToMeshMemblockWithTexture
function AddObjectToMeshMemblockWithTexture( MainObject, ObjID, MeshIndex, TextureID, X, Y, Z )
meshtemp = CreateMemblockFromObjectMesh(ObjID,1)
AddObjectMeshFromMemblock(MainObject,meshtemp)
SetObjectMeshImage(MainObject, MeshIndex, TextureID, 0)
DeleteObject(ObjID)
objid = setobjectpositiononmesh(mainobject, meshtemp, MeshIndex, X, Y , Z)
DeleteMemblock(meshtemp)
endfunction objid
function setobjectpositiononmesh(objid, meshid, meshindex, x, y, z)
// Position the object on the mesh
vertcount=GetMemblockInt(meshid,0)
for a = 0 to vertcount-1
SetMeshMemblockVertexPosition(meshid,a,GetMeshMemblockVertexX(meshid,a)+x,GetMeshMemblockVertexY(meshid,a)+y,GetMeshMemblockVertexZ(meshid,a)+z)
next
SetObjectMeshFromMemblock(objid,meshindex,meshid)
endfunction objid
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
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
function definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=100 :textures[0].dg=50
textures[0].b=0 :textures[0].db=50
//dirt
textures[1].r=139 :textures[1].dr=50
textures[1].g=69:textures[1].dg=50
textures[1].b=19:textures[1].db=50
//goldgold=createtexture(8,8,255,215,100,1)
textures[2].r=255 :textures[2].dr=50
textures[2].g=215 :textures[2].dg=50
textures[2].b=100 :textures[2].db=50
// water=createtexture(8,8,0,0,100,1)
textures[3].r=0 :textures[3].dr=50
textures[3].g=0 :textures[3].dg=50
textures[3].b=100 :textures[3].db=50
// lightwater=createtexture(8,8,39,89,45,1)
textures[4].r=0 :textures[4].dr=50
textures[4].g=100 :textures[4].dg=50
textures[4].b=100 :textures[4].db=50
// =createtexture(8,8,39,89,45,1)
textures[5].r=194 :textures[5].dr=50
textures[5].g=178 :textures[5].dg=50
textures[5].b=128 :textures[5].db=50
// create the texturs
textures[0].id = CreateTextures(8,8,1,0,0) // top
textures[1].id = CreateTextures(8,8,1,1,1) // bottom
textures[2].id = CreateTextures(8,8,1,2,2) // sides
textures[3].id = CreateTextures(8,8,1,3,3) // sides
textures[4].id = CreateTextures(8,8,1,4,4) // sides
textures[5].id = CreateTextures(8,8,1,5,5) // sides
endfunction
function createtextures(sizex# as float, sizey# as float,density, toptexture, bottomtexture)
if bottomtexture=0
//do a full block of the same texture
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
else
// do an half block with a different texture at bottom
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey#/3 step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
texturecount=bottomtexture
for a=0 to sizex# step density
for b=sizey#/3 to sizey# step density
rr=random(0,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
endif
endfunction textures[texturecount].id
Memblock Imported to bring back from the external file (use export to create the file first
// Project: meshimages
// Created: 2018-06-16
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "meshimages" )
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#
global startx#
startx#=1024/2
global starty#
starty#=768/2
// setup the types - each entity may have multiple of entities to keep track of, these can be stored as Types
type _textures
id // texture store
r,g,b // colors
dr,dg,db // depths (because minecraft tiles is built with rectangular different shades of the color)
texturessize
endtype
// textures
global textures as _textures[50] // used to store textures (not ready yet)
definetextures()
sleepwithsync("Importing test.agkobj....", 1500)
mainobject = importobject("test.agkobj")
sleepwithsync("Importing test.agkobj....", 1500)
SetObjectImage(mainobject,textures[1].id,0)
do
movecamerawithmouse()
RotateObjectLocalX(mainobject,1)
RotateObjectLocalZ(mainobject,1)
print ("Move your mouse")
Print( ScreenFPS() )
Sync()
loop
// Imports an external object created with the export function here
//
// objid = object to export
// filename = resulting file that it creates
function importobject(filename as string)
objectfile= OpenToRead(filename)
size = ReadInteger(objectfile)
chk = CreateMemblock(size)
SetMemblockInt(chk,0, ReadInteger(objectfile))
SetMemblockInt(chk,4, ReadInteger(objectfile))
SetMemblockInt(chk,8, ReadInteger(objectfile))
SetMemblockInt(chk,12, ReadInteger(objectfile))
VertexSize = ReadInteger(objectfile)
SetMemblockInt(chk,16, VertexSize)
SetMemblockInt(chk,20, ReadInteger(objectfile))
SetMemblockInt(chk,24, ReadInteger(objectfile))
SetMemblockstring(chk,28, ReadString(objectfile))
SetMemblockInt(chk,40, ReadInteger(objectfile))
SetMemblockString(chk,44, ReadString(objectfile))
SetMemblockInt(chk,52, ReadInteger(objectfile))
SetMemblockString(chk,56, ReadString(objectfile))
for a=VertexSize to size-VertexSize
SetMemblockInt(chk,a,ReadInteger(objectfile))
next
CloseFile(objectfile)
mainobject = CreateObjectFromMeshMemblock(chk)
endfunction mainobject
// Creates an external file with all the object data
//
// objid = object to export
// filename = resulting file that it creates
function exportobject(objid, filename as string)
objectfile= OpenToWrite(filename)
objmem = CreateMemblockFromObjectMesh(objid,1)
VertexSize = GetMemblockInt(objmem,16)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
WriteInteger(objectfile, ObjectDataSize+VertexSize) // Object Data Size
WriteInteger(objectfile,GetMemblockInt(objmem,0)) // Vertex Count
WriteInteger(objectfile, GetMemblockInt(objmem,4)) // Indices
WriteInteger(objectfile, GetMemblockInt(objmem,8)) // Attribues
WriteInteger(objectfile, GetMemblockInt(objmem,12)) // Vertex Offset
WriteInteger(objectfile, GetMemblockInt(objmem,16)) // Vertex Size
WriteInteger(objectfile, GetMemblockInt(objmem,20)) // Object Size
WriteInteger(objectfile, GetMemblockInt(objmem,24)) // PositionData
WriteString(objectfile, GetMemblockString(objmem,28,8)) // Position Tag
WriteInteger(objectfile, GetMemblockInt(objmem,40)) //Normal Data
WriteString(objectfile, GetMemblockString(objmem,44,6)) // Normal Tag
WriteInteger(objectfile, GetMemblockInt(objmem,52)) // UV Data
WriteString(objectfile, GetMemblockString(objmem,56,2)) // UV Tag
for a=VertexSize to ObjectDataSize
WriteInteger(objectfile, GetMemblockInt(objmem,a)) // Data itself
next
CloseFile(objectfile)
endfunction
// Creates a copy of an object with debug information and a delay
//
// objid = the object you wish to make a copy of
// debuginfo = 1 then prints info of the object going into the function
//
function makememblock(objid, debuginfo)
objmem = CreateMemblockFromObjectMesh(objid,1)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
VertexSize = GetMemblockInt(objmem,16)
chk=CreateMemblock(ObjectDataSize + VertexSize)
SetMemblockInt(chk,0,GetMemblockInt(objmem,0)) // Vertice Count
SetMemblockInt(chk,4,GetMemblockInt(objmem,4)) // Indices
SetMemblockInt(chk,8,GetMemblockInt(objmem,8))
SetMemblockInt(chk,12,GetMemblockInt(objmem,12)) // Vertice Off
SetMemblockInt(chk,16,GetMemblockInt(objmem,16)) // Vertice size
SetMemblockInt(chk,20,GetMemblockInt(objmem,20))
SetMemblockInt(chk,24,GetMemblockInt(objmem,24))
SetMemblockString(chk,28,GetMemblockstring(objmem,28,8))
SetMemblockInt(chk,40,GetMemblockInt(objmem,40)) //same as position, but for normals
SetMemblockString(chk,44,GetMemblockString(objmem,44,6))
SetMemblockInt(chk,52,GetMemblockInt(objmem,52)) //For color we have byte, 4 components, normalize data
SetMemblockString(chk,56,GetMemblockString(objmem,56,2))
for a=VertexSize to ObjectDataSize
SetMemblockInt(chk,a,GetMemblockInt(objmem,a))
next
if (debuginfo = 1)
repeat
print("Vertice Count " + str(GetMemblockInt(objmem,0)))
print("Vertice Indices " + str(GetMemblockInt(objmem,4)))
print("Vertice Attributes " + str(GetMemblockInt(objmem,8)))
print("Vertice Offset " + str(GetMemblockInt(objmem,12)))
print("Vertice Size " + str(GetMemblockInt(objmem,16)))
print("Object Size " + str(GetMemblockInt(objmem,20)))
print("Position / Components / Normalisation / Position Data " + str(GetMemblockInt(objmem,24)))
print("Shader Tag " + GetMemblockString(objmem,28,8))
print("Normalisation / Position Data " + str(GetMemblockInt(objmem,40)))
print("Shader Tag " + GetMemblockString(objmem,44,6))
print("UV Data " + str(GetMemblockInt(objmem,52)))
print("Shader Tag" + GetMemblockString(objmem,56,2))
print("Press Space to Continue")
sync()
until GetRawKeyPressed(32)
endif
MainObject = CreateObjectFromMeshMemblock(chk)
endfunction MainObject
// AddObjectToMeshMemblockWithPosition
function AddObjectToMeshMemblockWithPosition( MainObject, ObjID, MeshIndex, X, Y, Z )
// Get the meshes
/* if GetObjectNumMeshes(MainObject)<>MeshIndex
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
AddObjectMeshFromMemblock(MainObject,OriginalObjectMemory)
else
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
endif
*/
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
NewObjectMemory = CreateMemblockFromObjectMesh(Objid, MeshIndex)
// Move the object on the mesh
// now need to ammend the vertcount by adding the vert count of the object coming in + vertices of the original object
VertCountOriginal = GetMemblockInt(OriginalObjectMemory,0)
VertCountNew = GetMemblockInt(NewObjectMemory,0)
VertSize = GetMemblockInt(NewObjectMemory,12)
VertOffset = GetMemblockInt(NewObjectMemory, 16)
DestOffset = GetMemblockInt(OriginalObjectMemory, 16)
print (str(VertcountOriginal + VertCountNew))
print (str(VertcountOriginal))
print (str(VertCountNew))
print ("Vert Size - " + str(VertSize))
print ("Vert Offset - " + str(VertOffset))
sleepwithsync("",1000)
// SetMemblockInt(Originalobjectmemory,16,GetMemblockInt(OriginalObjectMemory,16) + VertOffset)
setobjectpositiononmesh(MainObject, OriginalObjectMemory,MeshIndex,x,y,z)
// Memblock Header = 36 bytes
// CopyMemblock(NewObjectmemory, OriginalObjectMemory, VertOffset+1, Vertsize, Vertsize)
MainObject = CreateMemblockFromObjectMesh(OriginalObjectMemory, MeshIndex)
endfunction MainObject
function sleepwithsync(txt as string, num)
print(txt)
sync()
sleep(num)
endfunction
// AddObjectToMeshMemblockWithTexture
function AddObjectToMeshMemblockWithTexture( MainObject, ObjID, MeshIndex, TextureID, X, Y, Z )
meshtemp = CreateMemblockFromObjectMesh(ObjID,1)
AddObjectMeshFromMemblock(MainObject,meshtemp)
SetObjectMeshImage(MainObject, MeshIndex, TextureID, 0)
DeleteObject(ObjID)
objid = setobjectpositiononmesh(mainobject, meshtemp, MeshIndex, X, Y , Z)
DeleteMemblock(meshtemp)
endfunction objid
function setobjectpositiononmesh(objid, meshid, meshindex, x, y, z)
// Position the object on the mesh
vertcount=GetMemblockInt(meshid,0)
for a = 0 to vertcount-1
SetMeshMemblockVertexPosition(meshid,a,GetMeshMemblockVertexX(meshid,a)+x,GetMeshMemblockVertexY(meshid,a)+y,GetMeshMemblockVertexZ(meshid,a)+z)
next
SetObjectMeshFromMemblock(objid,meshindex,meshid)
endfunction objid
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
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
function definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=100 :textures[0].dg=50
textures[0].b=0 :textures[0].db=50
//dirt
textures[1].r=139 :textures[1].dr=50
textures[1].g=69:textures[1].dg=50
textures[1].b=19:textures[1].db=50
//goldgold=createtexture(8,8,255,215,100,1)
textures[2].r=255 :textures[2].dr=50
textures[2].g=215 :textures[2].dg=50
textures[2].b=100 :textures[2].db=50
// water=createtexture(8,8,0,0,100,1)
textures[3].r=0 :textures[3].dr=50
textures[3].g=0 :textures[3].dg=50
textures[3].b=100 :textures[3].db=50
// lightwater=createtexture(8,8,39,89,45,1)
textures[4].r=0 :textures[4].dr=50
textures[4].g=100 :textures[4].dg=50
textures[4].b=100 :textures[4].db=50
// =createtexture(8,8,39,89,45,1)
textures[5].r=194 :textures[5].dr=50
textures[5].g=178 :textures[5].dg=50
textures[5].b=128 :textures[5].db=50
// create the texturs
textures[0].id = CreateTextures(8,8,1,0,0) // top
textures[1].id = CreateTextures(8,8,1,1,1) // bottom
textures[2].id = CreateTextures(8,8,1,2,2) // sides
textures[3].id = CreateTextures(8,8,1,3,3) // sides
textures[4].id = CreateTextures(8,8,1,4,4) // sides
textures[5].id = CreateTextures(8,8,1,5,5) // sides
endfunction
function createtextures(sizex# as float, sizey# as float,density, toptexture, bottomtexture)
if bottomtexture=0
//do a full block of the same texture
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
else
// do an half block with a different texture at bottom
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey#/3 step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
texturecount=bottomtexture
for a=0 to sizex# step density
for b=sizey#/3 to sizey# step density
rr=random(0,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
endif
endfunction textures[texturecount].id
A nice copy object to a new object instance - could be similar to copymemblock perhaps but this has debug info
// Project: meshimages
// Created: 2018-06-16
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "meshimages" )
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#
global startx#
startx#=1024/2
global starty#
starty#=768/2
// setup the types - each entity may have multiple of entities to keep track of, these can be stored as Types
type _textures
id // texture store
r,g,b // colors
dr,dg,db // depths (because minecraft tiles is built with rectangular different shades of the color)
texturessize
endtype
// textures
global textures as _textures[50] // used to store textures (not ready yet)
definetextures()
cube = CreateObjectBox(5,5,5)
newcube = makememblock(cube,1)
SetObjectPosition(cube,10,0,0)
do
movecamerawithmouse()
print ("move mouse")
Print( ScreenFPS() )
Sync()
loop
// Imports an external object created with the export function here
//
// objid = object to export
// filename = resulting file that it creates
function importobject(filename as string)
objectfile= OpenToRead(filename)
size = ReadInteger(objectfile)
sleepwithsync(str(size),2000)
chk = CreateMemblock(size)
SetMemblockInt(chk,0, ReadInteger(objectfile))
SetMemblockInt(chk,4, ReadInteger(objectfile))
SetMemblockInt(chk,8, ReadInteger(objectfile))
SetMemblockInt(chk,12, ReadInteger(objectfile))
VertexSize = ReadInteger(objectfile)
SetMemblockInt(chk,16, VertexSize)
SetMemblockInt(chk,20, ReadInteger(objectfile))
SetMemblockInt(chk,24, ReadInteger(objectfile))
SetMemblockstring(chk,28, ReadString(objectfile))
SetMemblockInt(chk,40, ReadInteger(objectfile))
SetMemblockString(chk,44, ReadString(objectfile))
SetMemblockInt(chk,52, ReadInteger(objectfile))
SetMemblockString(chk,56, ReadString(objectfile))
for a=VertexSize to size-VertexSize
SetMemblockInt(chk,a,ReadInteger(objectfile))
next
CloseFile(objectfile)
mainobject = CreateObjectFromMeshMemblock(chk)
endfunction mainobject
// Creates an external file with all the object data
//
// objid = object to export
// filename = resulting file that it creates
function exportobject(objid, filename as string)
objectfile= OpenToWrite(filename)
objmem = CreateMemblockFromObjectMesh(objid,1)
VertexSize = GetMemblockInt(objmem,16)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
sleepwithsync(str(ObjectDataSize),1000)
print("Exporting")
WriteInteger(objectfile, ObjectDataSize+VertexSize) // Object Data Size
WriteInteger(objectfile,GetMemblockInt(objmem,0)) // Vertex Count
WriteInteger(objectfile, GetMemblockInt(objmem,4)) // Indices
WriteInteger(objectfile, GetMemblockInt(objmem,8)) // Attribues
WriteInteger(objectfile, GetMemblockInt(objmem,12)) // Vertex Offset
WriteInteger(objectfile, GetMemblockInt(objmem,16)) // Vertex Size
WriteInteger(objectfile, GetMemblockInt(objmem,20)) // Object Size
WriteInteger(objectfile, GetMemblockInt(objmem,24)) // PositionData
WriteString(objectfile, GetMemblockString(objmem,28,8)) // Position Tag
WriteInteger(objectfile, GetMemblockInt(objmem,40)) //Normal Data
WriteString(objectfile, GetMemblockString(objmem,44,6)) // Normal Tag
WriteInteger(objectfile, GetMemblockInt(objmem,52)) // UV Data
WriteString(objectfile, GetMemblockString(objmem,56,2)) // UV Tag
for a=VertexSize to ObjectDataSize
WriteInteger(objectfile, GetMemblockInt(objmem,a)) // Data itself
next
CloseFile(objectfile)
endfunction
// Creates a copy of an object with debug information and a delay
//
// objid = the object you wish to make a copy of
// debuginfo = 1 then prints info of the object going into the function
//
function makememblock(objid, debuginfo)
objmem = CreateMemblockFromObjectMesh(objid,1)
ObjectDataSize = (((GetMemblockInt(objmem,4) * 4) + GetMemblockInt(objmem,20)) - 4)
VertexSize = GetMemblockInt(objmem,16)
chk=CreateMemblock(ObjectDataSize + VertexSize)
SetMemblockInt(chk,0,GetMemblockInt(objmem,0)) // Vertice Count
SetMemblockInt(chk,4,GetMemblockInt(objmem,4)) // Indices
SetMemblockInt(chk,8,GetMemblockInt(objmem,8))
SetMemblockInt(chk,12,GetMemblockInt(objmem,12)) // Vertice Off
SetMemblockInt(chk,16,GetMemblockInt(objmem,16)) // Vertice size
SetMemblockInt(chk,20,GetMemblockInt(objmem,20))
SetMemblockInt(chk,24,GetMemblockInt(objmem,24))
SetMemblockString(chk,28,GetMemblockstring(objmem,28,8))
SetMemblockInt(chk,40,GetMemblockInt(objmem,40)) //same as position, but for normals
SetMemblockString(chk,44,GetMemblockString(objmem,44,6))
SetMemblockInt(chk,52,GetMemblockInt(objmem,52)) //For color we have byte, 4 components, normalize data
SetMemblockString(chk,56,GetMemblockString(objmem,56,2))
for a=VertexSize to ObjectDataSize
SetMemblockInt(chk,a,GetMemblockInt(objmem,a))
next
if (debuginfo = 1)
repeat
print("Vertice Count " + str(GetMemblockInt(objmem,0)))
print("Vertice Indices " + str(GetMemblockInt(objmem,4)))
print("Vertice Attributes " + str(GetMemblockInt(objmem,8)))
print("Vertice Offset " + str(GetMemblockInt(objmem,12)))
print("Vertice Size " + str(GetMemblockInt(objmem,16)))
print("Object Size " + str(GetMemblockInt(objmem,20)))
print("Position / Components / Normalisation / Position Data " + str(GetMemblockInt(objmem,24)))
print("Shader Tag " + GetMemblockString(objmem,28,8))
print("Normalisation / Position Data " + str(GetMemblockInt(objmem,40)))
print("Shader Tag " + GetMemblockString(objmem,44,6))
print("UV Data " + str(GetMemblockInt(objmem,52)))
print("Shader Tag" + GetMemblockString(objmem,56,2))
print("Press Space to Continue")
sync()
until GetRawKeyPressed(32)
endif
MainObject = CreateObjectFromMeshMemblock(chk)
endfunction MainObject
// AddObjectToMeshMemblockWithPosition
function AddObjectToMeshMemblockWithPosition( MainObject, ObjID, MeshIndex, X, Y, Z )
// Get the meshes
/* if GetObjectNumMeshes(MainObject)<>MeshIndex
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
AddObjectMeshFromMemblock(MainObject,OriginalObjectMemory)
else
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
endif
*/
OriginalObjectMemory = CreateMemblockFromObjectMesh(MainObject, MeshIndex)
NewObjectMemory = CreateMemblockFromObjectMesh(Objid, MeshIndex)
// Move the object on the mesh
// now need to ammend the vertcount by adding the vert count of the object coming in + vertices of the original object
VertCountOriginal = GetMemblockInt(OriginalObjectMemory,0)
VertCountNew = GetMemblockInt(NewObjectMemory,0)
VertSize = GetMemblockInt(NewObjectMemory,12)
VertOffset = GetMemblockInt(NewObjectMemory, 16)
DestOffset = GetMemblockInt(OriginalObjectMemory, 16)
print (str(VertcountOriginal + VertCountNew))
print (str(VertcountOriginal))
print (str(VertCountNew))
print ("Vert Size - " + str(VertSize))
print ("Vert Offset - " + str(VertOffset))
sleepwithsync("",1000)
// SetMemblockInt(Originalobjectmemory,16,GetMemblockInt(OriginalObjectMemory,16) + VertOffset)
setobjectpositiononmesh(MainObject, OriginalObjectMemory,MeshIndex,x,y,z)
// Memblock Header = 36 bytes
// CopyMemblock(NewObjectmemory, OriginalObjectMemory, VertOffset+1, Vertsize, Vertsize)
MainObject = CreateMemblockFromObjectMesh(OriginalObjectMemory, MeshIndex)
endfunction MainObject
function sleepwithsync(txt as string, num)
print(txt)
sync()
sleep(num)
endfunction
// AddObjectToMeshMemblockWithTexture
function AddObjectToMeshMemblockWithTexture( MainObject, ObjID, MeshIndex, TextureID, X, Y, Z )
meshtemp = CreateMemblockFromObjectMesh(ObjID,1)
AddObjectMeshFromMemblock(MainObject,meshtemp)
SetObjectMeshImage(MainObject, MeshIndex, TextureID, 0)
DeleteObject(ObjID)
objid = setobjectpositiononmesh(mainobject, meshtemp, MeshIndex, X, Y , Z)
DeleteMemblock(meshtemp)
endfunction objid
function setobjectpositiononmesh(objid, meshid, meshindex, x, y, z)
// Position the object on the mesh
vertcount=GetMemblockInt(meshid,0)
for a = 0 to vertcount-1
SetMeshMemblockVertexPosition(meshid,a,GetMeshMemblockVertexX(meshid,a)+x,GetMeshMemblockVertexY(meshid,a)+y,GetMeshMemblockVertexZ(meshid,a)+z)
next
SetObjectMeshFromMemblock(objid,meshindex,meshid)
endfunction objid
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
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
function definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=100 :textures[0].dg=50
textures[0].b=0 :textures[0].db=50
//dirt
textures[1].r=139 :textures[1].dr=50
textures[1].g=69:textures[1].dg=50
textures[1].b=19:textures[1].db=50
//goldgold=createtexture(8,8,255,215,100,1)
textures[2].r=255 :textures[2].dr=50
textures[2].g=215 :textures[2].dg=50
textures[2].b=100 :textures[2].db=50
// water=createtexture(8,8,0,0,100,1)
textures[3].r=0 :textures[3].dr=50
textures[3].g=0 :textures[3].dg=50
textures[3].b=100 :textures[3].db=50
// lightwater=createtexture(8,8,39,89,45,1)
textures[4].r=0 :textures[4].dr=50
textures[4].g=100 :textures[4].dg=50
textures[4].b=100 :textures[4].db=50
// =createtexture(8,8,39,89,45,1)
textures[5].r=194 :textures[5].dr=50
textures[5].g=178 :textures[5].dg=50
textures[5].b=128 :textures[5].db=50
// create the texturs
textures[0].id = CreateTextures(8,8,1,0,0) // top
textures[1].id = CreateTextures(8,8,1,1,1) // bottom
textures[2].id = CreateTextures(8,8,1,2,2) // sides
textures[3].id = CreateTextures(8,8,1,3,3) // sides
textures[4].id = CreateTextures(8,8,1,4,4) // sides
textures[5].id = CreateTextures(8,8,1,5,5) // sides
endfunction
function createtextures(sizex# as float, sizey# as float,density, toptexture, bottomtexture)
if bottomtexture=0
//do a full block of the same texture
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
else
// do an half block with a different texture at bottom
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey#/3 step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
texturecount=bottomtexture
for a=0 to sizex# step density
for b=sizey#/3 to sizey# step density
rr=random(0,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
endif
endfunction textures[texturecount].id
I have uploaded a test object that was created using the export function
Shall crack on with more....