In the
Minecraft clone thread for example I posted code to create separate faces for cubes from memblock it can also be used to generate all sorts of models.
This is the important part:
function CreateFace(CubeX,CubeY,CubeZ,Direction)
if Cube[CubeX,CubeY,CubeZ].Face[Direction]=0
Size#=0.5
MemblockID=CreateMeshMemblock()
select Direction
case DUP:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX-Size#,CubeY+Size#,CubeZ+Size#) // Vertex
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,0,1,0) // Normals
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0) // UV
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX-Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,0,1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX+Size#,CubeY+Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,0,1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX+Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,0,1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,1,1)
endcase
case DDOWN:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX-Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,0,-1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX-Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,0,-1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX+Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,0,-1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX+Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,0,-1,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,1,1)
endcase
case DRIGHT:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX+Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,1,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX+Size#,CubeY+Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX+Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX+Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,0,0)
endcase
case DLEFT:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX-Size#,CubeY+Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,-1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX-Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,-1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX-Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,-1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX-Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,-1,0,0)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,1,1)
endcase
case DFRONT:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX-Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,0,0,-1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX-Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,0,0,-1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX+Size#,CubeY+Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,0,0,-1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX+Size#,CubeY-Size#,CubeZ-Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,0,0,-1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,1,1)
endcase
case DBACK:
SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,CubeX+Size#,CubeY+Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,0,0,1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+8*4,CubeX+Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+11*4,0,0,1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+14*4,0,1)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+16*4,CubeX-Size#,CubeY+Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+19*4,0,0,1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+22*4,1,0)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+24*4,CubeX-Size#,CubeY-Size#,CubeZ+Size#)
SetMemblockVec3(MemblockID,VERTEX_OFFSET+27*4,0,0,1)
SetMemblockVec2(MemblockID,VERTEX_OFFSET+30*4,1,1)
endcase
endselect
//~ Indices
SetMemblockInt(MemblockID,IndexOffset+0*4,0)
SetMemblockInt(MemblockID,IndexOffset+1*4,1)
SetMemblockInt(MemblockID,IndexOffset+2*4,2)
SetMemblockInt(MemblockID,IndexOffset+3*4,2)
SetMemblockInt(MemblockID,IndexOffset+4*4,1)
SetMemblockInt(MemblockID,IndexOffset+5*4,3)
Cube[CubeX,CubeY,CubeZ].Face[Direction]=BuildMeshMemblock(MemblockID)
SetFaceTexture(CubeX,CubeY,CubeZ,Direction)
endif
endfunction
// here starts the memblock manipulation functions
function CreateMeshMemblock()
VertexCount=4
IndexCount=6
Attributes=3
VertexSize=3*4+3*4+2*4
IndexOffset=VERTEX_OFFSET+(VertexCount*VertexSize)
MemblockID=Creatememblock(VERTEX_OFFSET+(VertexCount*VertexSize)+(IndexCount*4))
SetMemblockInt(MemblockID,0,VertexCount)
SetMemblockInt(MemblockID,4,IndexCount)
SetMemblockInt(MemblockID,8,Attributes)
SetMemblockInt(MemblockID,12,VertexSize)
SetMemblockInt(MemblockID,16,VERTEX_OFFSET)
SetMemblockInt(MemblockID,20,IndexOffset)
AttributeOffset=24
SetMemblockByte(MemblockID,AttributeOffset,0) // Float type component
SetMemblockByte(MemblockID,AttributeOffset+1,3) // number of components
SetMemblockByte(MemblockID,AttributeOffset+2,0) // normalize flag
SetMemblockByte(MemblockID,AttributeOffset+3,12) // string length
SetMemblockString(MemblockID,AttributeOffset+4,"position"+chr(0)) // string
inc AttributeOffset,16
SetMemblockByte(MemblockID,AttributeOffset,0) // Float type component
SetMemblockByte(MemblockID,AttributeOffset+1,3) // number of components
SetMemblockByte(MemblockID,AttributeOffset+2,0) // normalize flag
SetMemblockByte(MemblockID,AttributeOffset+3,8) // string length
SetMemblockString(MemblockID,AttributeOffset+4,"normal"+chr(0)) // string
inc AttributeOffset,12
SetMemblockByte(MemblockID,AttributeOffset,0) // Float type component
SetMemblockByte(MemblockID,AttributeOffset+1,2) // number of components
SetMemblockByte(MemblockID,AttributeOffset+2,0) // normalize flag
SetMemblockByte(MemblockID,AttributeOffset+3,4) // string length
SetMemblockString(MemblockID,AttributeOffset+4,"uv"+chr(0)) // string
endfunction MemblockID
function BuildMeshMemblock(MemblockID)
ObjectID=CreateObjectFromMeshMemblock(MemblockID)
DeleteMemblock(MemblockID)
endfunction ObjectID
function SetMemblockVec3(MemblockID,Offset,x#,y#,z#)
SetMemblockFloat(MemblockID,Offset,x#)
SetMemblockFloat(MemblockID,Offset+4,y#)
SetMemblockFloat(MemblockID,Offset+8,z#)
endfunction
function SetMemblockVec2(MemblockID,Offset,u#,v#)
SetMemblockFloat(MemblockID,Offset,u#)
SetMemblockFloat(MemblockID,Offset+4,v#)
endfunction
In the CreateFace() function I set the vertex position, normal an uv coordinate for every vertex of one side of the cube.
You need these steps:
1. MemblockID=CreateMeshMemblock() // generate the mesh
2.
- SetMemblockVec3(MemblockID,VERTEX_OFFSET+0,0,0,0) // set Vertex position (you can also use SetMeshMemblockVertexPosition() here)
- SetMemblockVec3(MemblockID,VERTEX_OFFSET+3*4,0,1,0) // Normals
- SetMemblockVec2(MemblockID,VERTEX_OFFSET+6*4,0,0) // UV
3. SetMemblockInt(MemblockID,IndexOffset+0*4,0) // (optional) set the indices
4. ObjectID=BuildMeshMemblock(MemblockID) // finish and return the object ID
[Edit] I wonder how we are supposed to remove a mesh from an object previously added with AddObjectMeshFromMemblock
[/Edit]