Heres a really quick demo on how to do it easily using some helper functions.
// Project: MeshFromCode
// Created: 2019-06-04
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "MeshFromCode" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetVSync(1)
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// Create a memblock to hold your object mesh
mb = CreateMeshMemblock(4,6) // 4 vertices and 6 indexes
// Make a square (2 tri's with 4 vertexes)
SetMemblockVertex(mb,0, -5,-5,0, 0,0,-1, 0,1)
SetMemblockVertex(mb,1, 5,-5,0, 0,0,-1, 1,1)
SetMemblockVertex(mb,2, 5, 5,0, 0,0,-1, 1,0)
SetMemblockVertex(mb,3, -5, 5,0, 0,0,-1, 0,0)
SetMemblockTriangleIndexes(mb,0,0,1,2) // triangle 1 indices
SetMemblockTriangleIndexes(mb,1,0,2,3) // triangle 2 indices
// Create the object mesh
ob = CreateObjectFromMeshMemblock(mb)
// Load the AGK logo and apply it to the object
if GetFileExists("logo.jpg") = 1
im = LoadImage("logo.jpg") // comment this line out or add the image to your project
SetObjectImage(ob,im,0)
endif
// SetSun direction
SetSunDirection(0,-0.2,1.0)
SetObjectCullMode(ob,0) // turn off rear face culling so you can see the object from any direction
do
// rotate the object for fun around the Y axis
SetObjectRotation(ob,0,Timer()*80,0)
Sync()
loop
// Functions for easily creating an arbitrary mesh
// This creates and sets up a memblock for adding vertices to make a mesh model
// The mesh contains position, normals and uv coords
function CreateMeshMemblock( nvertices, nindices)
// Calculate the number o
NumVertices = nvertices
NumIndices = nindices
VertexSize = 32 // Size of a vertex = 8 floats x 4 bytes = 32 bytes x,y,z,nx,ny,nz,u,v
// calculate the size of memblock we will need
VertexOffset = 24 + 36
IndexOffset = VertexOffset + (NumVertices * VertexSize)
MemSize = IndexOffset + (NumIndices * 4)
// create the memblock
msblk = CreateMemblock( MemSize )
// Start filling it up with relevant info
SetMemblockInt( msblk, 0, NumVertices )
SetMemblockInt( msblk, 4, NumIndices )
SetMemblockInt( msblk, 8, 3 ) // number if attributes (3)
SetMemblockInt( msblk, 12, VertexSize ) // Bytes per vertex (32)
SetMemblockInt( msblk, 16, VertexOffset ) // should be (60)
SetMemblockInt( msblk, 20, IndexOffset ) // varies
// Set the attribute Data
// VERTEX ATTRIBUTES
SetMemblockByte( msblk, 24, 0 ) // float
SetMemblockByte( msblk, 25, 3 ) // component count
SetMemblockByte( msblk, 26, 0 ) // normalize
SetMemblockByte( msblk, 27, 12 ) // string length
SetMemblockString( msblk, 28, "position" )
// NORMALS ATTRIBUTES
SetMemblockByte( msblk, 40, 0 ) // float
SetMemblockByte( msblk, 41, 3 ) // component count
SetMemblockByte( msblk, 42, 0 ) // normalize
SetMemblockByte( msblk, 43, 8 ) // string length
SetMemblockString( msblk, 44, "normal" )
// UV ATTRIBUTES
SetMemblockByte( msblk, 52, 0 ) // float
SetMemblockByte( msblk, 53, 2 ) // component count
SetMemblockByte( msblk, 54, 0 ) // normalize
SetMemblockByte( msblk, 55, 4 ) // string length
SetMemblockString( msblk, 56, "uv" )
// Set the current number of indices and vertices
//SetMemblockInt( msblk, GetMemblockSize(msblk)-4, 0 ) // num indices used
//SetMemblockInt( msblk, GetMemblockSize(msblk)-8, 0 ) // num vertices used
endfunction msblk
// Set all the values of a vertex
function SetMemblockVertex(msblk as integer, vertex,x#,y#,z#,nx#,ny#,nz#,u#,v#)
vertexsize = GetMemblockInt(msblk,12)
vertexlocation = GetmemblockInt(msblk,16)
offset = vertexlocation + vertex*vertexsize
// Positions
SetMemblockFloat(msblk,offset,x#)
SetMemblockFloat(msblk,offset+4,y#)
SetMemblockFloat(msblk,offset+8,z#)
// Normals
SetMemblockFloat(msblk,offset+12,nx#)
SetMemblockFloat(msblk,offset+16,ny#)
SetMemblockFloat(msblk,offset+20,nz#)
// UV's
SetMemblockFloat(msblk,offset+24,u#)
SetMemblockFloat(msblk,offset+28,v#)
endfunction
// Set all the indices of a triangle
function SetMemblockTriangleIndexes(msblk as integer, Triangle as integer, v1 as integer,v2 as integer,v3 as integer)
offset = GetMemblockInt(msblk,20) // offset to index data
offset = offset + (Triangle*3*4)
SetMemblockInt(msblk,offset,v1)
SetMemblockInt(msblk,offset+4,v2)
SetMemblockInt(msblk,offset+8,v3)
endfunction
Its basically:
1) Create a memblock
2) Set Vertex and index values in it
3) Create an object mesh from the memblock
With the demo above...you can add an image to your media folder to put on the mesh too.