Thank you
Using the same obj and png
how about a bit of shake rock and roll before the big smash
// Project: Mesh Objects
// Created: 2018-08-04
// TO DO - Save Primative Objects to OBJ so can model them in blender and such like
// show all errors
SetErrorMode(2)
#constant PI# = 3.14159265359
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
// set window properties
SetWindowTitle( "Mesh Objects" )
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( 1200, 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#, fDiffX#,newX#,startx#, starty#, camerax#, cameray#, cameraz#
// *************************************************
// ** Mesh Memblock Types - please dont upset these variables
// *************************************************
global Cubed, VertexCount, MaxObjects, object, memblock
global peices as integer[]
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
type _vector
x#
y#
z#
polygon // 1 = yes - 0 = no
vindex
nindex
tindex
u#
v#
endtype
type _object
v as integer[] // the actual vertex on the mesh which need to save so can use SetMeshMemblockVertexPosition commands for example at a later time
vertex as _vector[] // eg v 1.276477 0.200934 -0.387665
normals as _vector[] // eg vn vn -0.5346 0.3304 -0.7779
faces as _vector[] // eg f 5//9 21//9 29//9
textures as _vector[] // eg vt 0.500 1 [0]
polygon as integer // 1=Yes , 0 = No
endtype
global objectmesh as _object[10] // upto 10 objects per app
type _meshmemblockvertexes
id
vfrom
vto
endtype
global meshmemblockvertexes as _meshmemblockvertexes[] // this will store the true vertex positions on a meshmemblock
type _farworlds
objID
x#
y#
z#
endtype
global farworlds as _farworlds[]
global objects as integer[]
global size=16
global wsize=0
global coned
coned=LoadImage("\media\cube.png")
loadobjects()
global objs as integer[4]
Create3DPhysicsWorld()
size=9
maxobjects=0
// Creates some individual objects of the OBJ
InstanceMeshObjectShatter(0, 0, -3,0,0, 1,1,1, MakeColor(255,255,255), 1)
// Create the object onto the mesh in its entirity
InstanceMeshObject(0, 0, -3,0,0, .01,.01,.01, MakeColor(255,255,255), 1)
object=CreateObjectFromMeshWithUVTexturing(meshid, coned)
objectmesh.length=-1
setupcamera()
explodesize=3:explodetime=300:gravity=4:delay=2:angle#=0:explodedelay=10000:alpha=255
do
if GetObjectExists(object) then RotateObjectLocalY(object,.1)
movecamerawithmouse()
if GetRawKeyPressed(13)
active=1
endif
print(angle#)
if active=1
//lets create some of own physics for the object that create a shatter for
DeleteObject(object)
min = random(0,peices.length-1)
max = peices.length-1
for a=0 to max
if GetObjectExists(peices[a])
inc angle#,.01
if angle#<explodedelay
x#=(angle#/1000.0) * sin(angle#)/1000.0
z#=(angle#/1000.0) * cos(angle#)/1000.0
else
x#=random2(-explodesize, explodesize)/100.0
y#=random2(-explodesize, 0)/100.0
z#=random2(-explodesize, explodesize)/100.0
endif
// SetObjectPosition(peices[a],getobjectx(peices[a])+random2(-explodesize,explodesize)/100.0,getobjecty(peices[a])+random2(-gravity,0)/100.0,getobjectz(peices[a])+random2(-explodesize,explodesize)/100.0)
SetObjectRotation(peices[a],GetObjectAngleX(peices[a])+random2(-explodesize,explodesize)/100.0,GetObjectAngleY(peices[a])+random2(-explodesize,explodesize)/100.0,GetObjectAngleZ(peices[a])+random2(-explodesize,explodesize)/100.0)
SetObjectPosition(peices[a],getobjectx(peices[a])+x#,getobjecty(peices[a])+y#,getobjectz(peices[a])+z#)
/// Create3DPhysicsDynamicBody(peices[a])
// SetObjectRotation(peices[a],GetObjectAngleX(peices[a])+random2(-explodesize,explodesize)/100.0,GetObjectAngleY(peices[a])+random2(-explodesize,explodesize)/100.0,GetObjectAngleZ(peices[a])+random2(-explodesize,explodesize)/100.0)
//SetObjectColor(peices[a],a,a,a,alpha)
// if explodetime<0 then DeleteObject(peices[a])
endif
dec alpha
next
dec explodetime
inc peicecount
if peicecount>=peices.length-1 then peicecount=peices.length-1
endif
if GetRawKeyState(87) then inc cameray#,2
if GetRawKeyState(83) then dec cameray#,2
SetCameraPosition(1,camerax#,cameray#,cameraz#)
print("FPS " + str(ScreenFPS()) + " - Last Key" + str(GetRawLastKey()) + " Press enter to shatter")
Step3DPhysicsWorld()
Sync()
loop
function loadobjects()
// 0 cube, 1 - tree
/* LoadObjectIntoMesh(0,"\media\fb.obj", "cube.png",0,1) // 1= saves the V/VN/VTs for been able// to instance it
// LoadObjectIntoMesh(2,"\media\tree.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(0,"\media\uvsphere.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(0,"\media\torus.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(0,"\media\ico.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(0,"\media\metaball.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
//LoadObjectIntoMesh(0,"\media\testmix.obj", "",1,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(0,"\media\texturedsphere.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
*/
// LoadObjectIntoMesh(0,"\media\skydome.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
//
LoadObjectIntoMesh(0,"\media\torus.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
// LoadObjectIntoMesh(1,"\media\cube.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
endfunction
function setupcamera()
camerax#=0
cameray#=4
cameraz#=-5
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
SetCameraRange(1,0.1,90000)
endfunction
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
// Instance a loaded object - requires to "save" the data when load an object for this to work
function InstanceMeshObjectShatter(id, ObjID, x#, y#, z#, scalex#,scaley#,scalez#, color, inverted)
mv as _meshmemblockvertexes
mv.id = ObjID
mv.vfrom = vertexcount
indices=0
peicescount=0
if objectmesh[id].polygon=0
for a=0 to objectmesh[id].faces.length-2 step 2
// calculate the normals
nnx#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].x#
nny#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].y#
nnz#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].z# * -1
length# = sqrt((nnx# * nnx#) + (nny# * nny#) + (nnz# * nnz#))
if length#>31
length#=1/length#
else
nny#=1
length#=1
endif
nx# = nnx# * length#
ny# = nny# * length#
nz# = nnz# * length#
// apply the polygon referencing indexes from the OBJect
// does the object have a texture assigned
if objectmesh[id].faces[a].tindex <> -1
// yes
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,Color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,Color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,Color)
inc indices,2
else
// no
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 0,0,Color)
inc indices,1
endif
// add the next vertex position so we know which vertexs to change later when using setmeshmemblockvertexposition etc
next
else
//polygon
shat=0
// polygon construction
for a=0 to objectmesh[id].faces.length step 4
//calculate the normals
//sleep(10000)
nnx#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].x#
nny#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].y#
nnz#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].z# * -1
length# = sqrt((nnx# * nnx#) + (nny# * nny#) + (nnz# * nnz#))
if length#>31
length#=1/length#
else
nny#=1
length#=1
endif
nx# = nnx# * length#
ny# = nny# * length#
nz# = nnz# * length#
// apply the polygon referencing indexes from the OBJect
// does the object have a texture assigned
if objectmesh[id].faces[a].tindex <> -1
// yes
if inverted = 0
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
else
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].v#,color)
peices.insert(CreateObjectFromMeshWithUVTexturing(meshid,coned))
SetObjectTransparency(peices[peicescount],1)
// SetObjectPosition(peices[peicescount],x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#,y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#,z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez#)
inc peicescount
emptymesh()
endif
else
// no
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 1,1 ,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#,1,0,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#,0,0,color)
peices.insert(CreateObjectFromMeshWithUVTexturing(meshid,coned))
emptymesh()
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, 0,0,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, 0,1,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 1,1,color)
peices.insert(CreateObjectFromMeshWithUVTexturing(meshid,coned))
SetObjectTransparency(peices[peicescount],1)
// SetObjectPosition(peices[peicescount],x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#,y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#,z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez#)
inc peicescount
emptymesh()
endif
// add the next 4 vertex position so we know which vertexs to change later when using setmeshmemblockvertexposition etc
// we need to add 4 of these cause its a polygon structure
// inc vertexcount,1
inc indices,6
next
endif
mv.vto = vertexcount + indices-1
meshmemblockvertexes.insert(mv)
inc vertexcount,indices
endfunction
// LoadObjectIntoMeshfunction
// filename$ - What is the model name
// debug - 0 to not have any - 1 to have all the info displayed
// save - 0 to not save any Vs/VNs/VTs/Fs in global memory or 1 to keep those in memory to change at a later time in the app if so wish
function LoadObjectIntoMesh(id, filename$, material$, debug, save)
v as _vector // create an empty type so can inert data
// with face elements these are index references of the above array indexes
//
// f v1 v2 v3 // This will be the indexes of the Vertex 1 2 and 3 in the vertex array
// f v1/vt1 v2/vt2 v3/vt3 // This one has texture / UV assigned to the object and the first is the index in the vertex array and the 2nd is the texture array
// f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 // This one has texture / UV assigned and Normals to the object and the first is the index in the vertex array and the 2nd is the texture array and the index in normals arra
//
// The f data is normally at the end of the OBJ file, so
// What we would have to do first is grab the V into the Vertex array
// then the VNs into the Normal array
// then the VTs into the texture array
// then use the F(aces) data to pick out which vertex to work with the AddVertex command
// firstly empty the mesh for a new object to be loaded
objectmesh[id].vertex.length=-1
objectmesh[id].normals.length=-1
objectmesh[id].faces.length=-1
objectmesh[id].textures.length=-1
f = OpenToRead(filename$)
vindex=0
while not FileEOF(f)
line$=ReadLine(f)
if (mid(line$,1,2) = "v ")
line$=mid(line$,3,len(line$))
v.x# = ValFloat(GetStringToken(line$," ",1))
v.y#= ValFloat(GetStringToken(line$," ",2))
v.z#= ValFloat(GetStringToken(line$," ",3)) * -1
objectmesh[id].vertex.insert(v)
endif
if (mid(line$,1,3) = "vn ")
line$=mid(line$,4,len(line$))
v.x# = ValFloat(GetStringToken(line$," ",1))
v.y#= ValFloat(GetStringToken(line$," ",2))
v.z#= ValFloat(GetStringToken(line$," ",3)) * -1
objectmesh[id].normals.insert(v)
endif
if (mid(line$,1,3) = "vt ")
line$=mid(line$,4,len(line$))
v.u# = ValFloat(GetStringToken(line$," ",1))
v.v#= ValFloat(GetStringToken(line$," ",2))
objectmesh[id].textures.insert(v)
endif
if (mid(line$,1,2) = "f ")
line$=mid(line$,3,len(line$))
face1$ = GetStringToken(line$," ",1)
face2$ = GetStringToken(line$," ",2)
face3$ = GetStringToken(line$," ",3)
face4$ = GetStringToken(line$," ",4)
// eg constucted F 1//1 14//1 13//1 = Vertex // Normal VertexIndex//NormalIndex VertexIndex//NormalIndex
// eg constucted F 1//1 14//1 13//1 8//1 = Polygon construction // Normal VertexIndex//NormalIndex VertexIndex//NormalIndex
v.vindex = ValFloat(GetStringToken(face1$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face1$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face1$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
//sync()
//sleep(3000)
v.vindex = ValFloat(GetStringToken(face2$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face2$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face2$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
//sync()
//sleep(1000)
// Y#
// Z#
v.vindex = ValFloat(GetStringToken(face3$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face3$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face3$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
//sync()
//sleep(1000)
// 4 faces = polygon so needs a polygon - 4 vertices command rather than a addvertex (3 points)
// a polygon will require 6xvertices. 1st/2nd/3rd points is indeed makes the first triangle
// then the other 3 needs to be - 2 x shared vertices from the 2 of the previous and an extra one making the
// final outcome of 4 vertices polygon object
if len(face4$)>1
v.vindex = ValFloat(GetStringToken(face4$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face4$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face4$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
print(v.vindex)
print(v.tindex)
print(v.nindex)
print(face4$)
//sync()
//sleep(1)
objectmesh[id].polygon=1
else
sleep(1)
if len(face3$)>1
v.vindex = ValFloat(GetStringToken(face3$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face3$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face3$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
else
v.vindex = ValFloat(GetStringToken(face2$,"//",1))-1
v.tindex = ValFloat(GetStringToken(face2$,"//",2))-1
v.nindex = ValFloat(GetStringToken(face2$,"//",3))-1
if v.nindex = -1
v.nindex = v.tindex
v.tindex = -1
endif
objectmesh[id].faces.insert(v)
endif
if objectmesh[id].polygon<>1
objectmesh[id].polygon=0
endif
endif
endif
endwhile
if debug=1
print(objectmesh[id].Vertex.length)
for a=0 to objectmesh[id].vertex.length
print("V " + str(objectmesh[id].vertex[a].x#) + " " + str(objectmesh[id].vertex[a].y#) + " " + str(objectmesh[id].vertex[a].z#))
next
sync()
sleep(2000)
print(objectmesh[id].Normals.length)
for a=0 to objectmesh[id].normals.length
print("VN " + str(objectmesh[id].normals[a].x#) + " " + str(objectmesh[id].normals[a].y#) + " " + str(objectmesh[id].normals[a].z#))
next
sync()
sleep(2000)
print(objectmesh[id].Faces.length)
for a=0 to objectmesh[id].faces.length
print("F " + str(objectmesh[id].faces[a].x#) + " " + str(objectmesh[id].faces[a].y#) + " " + str(objectmesh[id].faces[a].z#))
next
sync()
sleep(2000)
endif
if debug=1
sync()
sleep(1000)
endif
if save=0
objectmesh[id].vertex.length=-1
objectmesh[id].normals.length=-1
objectmesh[id].faces.length=-1
objectmesh[id].textures.length=-1
endif
CloseFile(f)
endfunction
function emptymesh()
MeshID.VertexList.Length=-1
MeshID.TriangleList.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
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,36) // 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, texture)
DeleteMemblock(memblock)
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)
SetObjectImage(id,texture,0)
// 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
// Instance a loaded object - requires to "save" the data when load an object for this to work
function InstanceMeshObject(id, ObjID, x#, y#, z#, scalex#,scaley#,scalez#, color, inverted)
mv as _meshmemblockvertexes
mv.id = ObjID
mv.vfrom = vertexcount
indices=0
if objectmesh[id].polygon=0
for a=0 to objectmesh[id].faces.length-2 step 2
// calculate the normals
nnx#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].x#
nny#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].y#
nnz#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].z# * -1
length# = sqrt((nnx# * nnx#) + (nny# * nny#) + (nnz# * nnz#))
if length#>31
length#=1/length#
else
nny#=1
length#=1
endif
nx# = nnx# * length#
ny# = nny# * length#
nz# = nnz# * length#
// apply the polygon referencing indexes from the OBJect
// does the object have a texture assigned
if objectmesh[id].faces[a].tindex <> -1
// yes
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,Color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,Color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,Color)
inc indices,2
else
// no
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 0,0,Color)
inc indices,1
endif
// add the next vertex position so we know which vertexs to change later when using setmeshmemblockvertexposition etc
next
else
//polygon
// polygon construction
for a=0 to objectmesh[id].faces.length step 4
//calculate the normals
//sleep(10000)
nnx#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].x#
nny#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].y#
nnz#=objectmesh[id].normals[objectmesh[id].faces[a].nindex].z# * -1
length# = sqrt((nnx# * nnx#) + (nny# * nny#) + (nnz# * nnz#))
if length#>31
length#=1/length#
else
nny#=1
length#=1
endif
nx# = nnx# * length#
ny# = nny# * length#
nz# = nnz# * length#
// apply the polygon referencing indexes from the OBJect
// does the object have a texture assigned
if objectmesh[id].faces[a].tindex <> -1
// yes
if inverted = 0
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
else
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+1].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+2].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a].tindex].v#,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].u# ,objectmesh[id].textures[objectmesh[id].faces[a+3].tindex].v#,color)
endif
else
// no
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 1,1 ,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+1].vindex].z# * scalez# ,nx#,ny#,nz#,1,0,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#,0,0,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+2].vindex].z# * scalez# ,nx#,ny#,nz#, 0,0,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a+3].vindex].z# * scalez# ,nx#,ny#,nz#, 0,1,color)
AddVertex(MeshID, x#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].x# * scalex#, y#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].y# * scaley#, z#+objectmesh[id].vertex[objectmesh[id].faces[a].vindex].z# * scalez# ,nx#,ny#,nz#, 1,1,color)
endif
// add the next 4 vertex position so we know which vertexs to change later when using setmeshmemblockvertexposition etc
// we need to add 4 of these cause its a polygon structure
// inc vertexcount,1
inc indices,6
next
endif
mv.vto = vertexcount + indices-1
meshmemblockvertexes.insert(mv)
inc vertexcount,indices
endfunction
endfunction
Enjoy!