I really need to start working on a mesh collider work too- im falling behind on a lot of this
// Project: City Views
// Created: 2018-10-28
// 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( "City Views" )
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#
// *************************************************
// ** Perlin Setup - please dont upset these variables
// *************************************************
#constant perlinw = 1024 //power of 2
#constant perlinh = 1024 //power of 2
#constant perlinz = 0 // doesnt work yet - required 3D perlin noise - Yikes
#constant perlinheight=1000.0
// *************************************************
// ** Mesh Memblock Types - please dont upset these variables
// *************************************************
global Cubed, VertexCount, MaxObjects, 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
name$ as String
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[100]
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[]
// *************************************************
// ** END OF - Mesh Memblock Types - please dont upset these variables
// *************************************************
cube=LoadImage("\media\cube.png")
SetImageWrapU(cube,1)
SetImageWrapV(cube,1)
NoiseInit()
walls as integer[5]
LoadObjects()
//obj = CreateObjectFromHeightMap("\media\earth.png",1024,1,1024,0,5)
wsize=90
obj=0
for x#=-wsize to wsize
for z#=-wsize to wsize
InstanceMeshObject(0,obj,x#*2,floor(Noise2D(x#/120.0,z#/120.0))*2,z#*2,1,1,1,MakeColor(255,255,255),1)
inc obj
next
next
object = CreateObjectFromMeshWithUVTexturing(meshid,cube)
/*
walls[0] = InstanceObject(object)
SetObjectRotation(walls[0],90,90,0)
SetObjectPosition(walls[0],-wsize*2,wsize+wsize/2+10,0)
walls[0] = InstanceObject(object)
SetObjectRotation(walls[0],90,90,0)
SetObjectPosition(walls[0],wsize*2,wsize+wsize/2+10,0)
walls[0] = InstanceObject(object)
SetObjectRotation(walls[0],0,90,0)
SetObjectPosition(walls[0],0,wsize*4-10,0)
walls[0] = InstanceObject(object)
SetObjectRotation(walls[0],0,90,270)
SetObjectPosition(walls[0],0,wsize+wsize/2+10,-wsize*2)
walls[0] = InstanceObject(object)
SetObjectRotation(walls[0],0,90,270)
SetObjectPosition(walls[0],0,wsize+wsize/2+10,wsize*2)
*/
setupcamera()
do
RotateCameraLocalX(1,.05)
RotateCameraLocalY(1,.05)
RotateCameraLocalZ(1,.05)
movecamerawithmouse()
if GetRawKeyState(87) then inc cameray#,1
if GetRawKeyState(83) then dec cameray#,1
SetCameraPosition(1,camerax#,cameray#,cameraz#)
print("FPS " + str(ScreenFPS()) + " - Last Key" + str(GetRawLastKey()))
Sync()
loop
function createtentroof(size#,width#, depth#, wind#, offset#)
for x#=-size# to size# step width#
for z#=-size# to size# step depth#
// 4 corners and centre tile
if x#=-size# and z#=-size#
createMeshObjectJoin(x#,0,z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,Noise2D((x#+offset#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, 1, MakeColor(255,255,255))
else
if x#=size# and z#=-size#
createMeshObjectJoin(x#,Noise2D((x#+offset#)/wind#,(z#+offset#)/wind#),z#, x#+width#,0,z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,Noise2D((x#+offset#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, 1, MakeColor(255,255,255))
else
if x#=size# and z#=size#
createMeshObjectJoin(x#,Noise2D((x#+offset#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,0,z#+depth#, x#,Noise2D((x#+offset#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, 1, MakeColor(255,255,255))
else
if x#=-size# and z#=size#
createMeshObjectJoin(x#,Noise2D((x#+offset#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,0,z#+depth#, 1, MakeColor(255,255,255))
else
if x#=0 and z#=0
createMeshObjectJoin(x#,0,z#, x#+width#,0,z#, x#+width#,0,z#+depth#, x#,0,z#+depth#, 1, MakeColor(255,255,255))
else
createMeshObjectJoin(x#,Noise2D((x#+offset#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,Noise2D((x#+offset#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, 1, MakeColor(255,255,255))
endif
endif
endif
endif
endif
next
next
endfunction
function createflag(size#,width#, depth#, wind#, offset#)
// for x#=-width# to width# step 2
x#=-width#
z#=0
//for z#=-depth# to depth# step 1
if x#=-width#
// lock the right hand side
createMeshObjectJoin(x#,0,z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,0,z#+depth#, 1, MakeColor(255,255,255))
else
// move everything else
createMeshObjectJoin(x#,Noise2D((x#+offset#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,(z#+offset#)/wind#),z#, x#+width#,Noise2D(((x#+offset#)+width#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, x#,Noise2D((x#+offset#)/wind#,((z#+offset#)+depth#)/wind#),z#+depth#, 1, MakeColor(255,255,255))
endif
// next
//next
endfunction
// CreateMeshObjectJoin
// Creates an face object with 4 corners
// x#,y#, x1#, y1#, x2#, y2#, x3#, y3#
// ds = Double Sided object? - 1 Yes
// color = add the face color
function createMeshObjectJoin(x#,y#,z#,x1#,y1#,z1#,x2#,y2#,z2#,x3#,y3#,z3#, ds, color)
AddVertex(MeshID, x#,y#,z# , 0 ,1,0, 0,1,Color)
AddVertex(MeshID, x1#,y1#,z1# , 0 ,1,0, 1,1,Color)
AddVertex(MeshID, x2#,y2#,z2# , 0 ,1,0, 1,0,Color)
AddVertex(MeshID, x2#,y2#,z2# , 0 ,1,0, 1,0,Color)
AddVertex(MeshID, x3#,y3#,z3# , 0 ,1,0, 0,0,Color)
AddVertex(MeshID, x#,y#,z# , 0 ,1,0, 0,1,Color)
if ds=1
AddVertex(MeshID, x#,y#,z# , 0 ,1,0, 0,1,Color)
AddVertex(MeshID, x2#,y2#,z2# , 0 ,1,0, 1,0,Color)
AddVertex(MeshID, x1#,y1#,z1# , 0 ,1,0, 1,1,Color)
AddVertex(MeshID, x2#,y2#,z2# , 0 ,1,0, 1,0,Color)
AddVertex(MeshID, x#,y#,z# , 0 ,1,0, 0,1,Color)
AddVertex(MeshID, x3#,y3#,z3# , 0 ,1,0, 0,0,Color)
endif
endfunction
function loadobjects()
LoadObjectIntoMesh(0,"\media\cube.obj", "",0,1) // 1= saves the V/VN/VTs for been able to instance it
endfunction
function setupcamera()
camerax#=0
cameray#=100
cameraz#=0
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
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
// *************************************************
// ** Mesh Memblock Function
// *************************************************
// 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
// 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$)
loops=0
vindex=0
while not FileEOF(f)
inc loops
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 loops>1 and mid(line$,1,2) = "g "
print (line$)
print (id)
print (objectmesh[id].faces.length)
sync()
sleep(20)
// found a group so apply the name of it and increate the object count to create a new object for the next one in the group
objectmesh[id].name$ = line$
inc id
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.TriangleList.length=-1
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
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 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
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
////////////////////////////////////////////////////////////////////////////////////
///
/// Perlin Noise functions
///
////////////////////////////////////////////////////////////////////////////////////
#constant PN3DF2 = 0.5*(sqrt(3.0)-1.0)
#constant PN3DG2 = (3.0-sqrt(3.0))/6.0
Type sPNVECTOR
x as float
y as float
z as float
endtype
Global PNHash as integer[512]
Global PNGrad3 as sPNVECTOR[256]
Function NoiseInit()
Local n as integer, rn as integer
For n=0 To 255
PNHash[n] = n
Next n
For n=0 To 255
rn=Random(0, 255)
PNHash.swap(n,rn)
Next n
For n=0 To 255
PNHash[256 + n] = PNHash[n]
Next n
PNHash[511] = PNHash[0]
For n=0 To 15
PNGrad3[n * 16 + 0].x = 1 : PNGrad3[n * 16 + 0].y = 1 : PNGrad3[n * 16 + 0].z = 0
PNGrad3[n * 16 + 1].x = -1 : PNGrad3[n * 16 + 1].y = 1 : PNGrad3[n * 16 + 1].z = 0
PNGrad3[n * 16 + 2].x = 1 : PNGrad3[n * 16 + 2].y = -1 : PNGrad3[n * 16 + 2].z = 0
PNGrad3[n * 16 + 3].x = -1 : PNGrad3[n * 16 + 3].y = -1 : PNGrad3[n * 16 + 3].z = 0
PNGrad3[n * 16 + 4].x = 1 : PNGrad3[n * 16 + 4].y = 0 : PNGrad3[n * 16 + 4].z = 1
PNGrad3[n * 16 + 5].x = -1 : PNGrad3[n * 16 + 5].y = 0 : PNGrad3[n * 16 + 5].z = 1
PNGrad3[n * 16 + 6].x = 1 : PNGrad3[n * 16 + 6].y = 0 : PNGrad3[n * 16 + 6].z = -1
PNGrad3[n * 16 + 7].x = -1 : PNGrad3[n * 16 + 7].y = 0 : PNGrad3[n * 16 + 7].z = -1
PNGrad3[n * 16 + 8].x = 0 : PNGrad3[n * 16 + 8].y = 1 : PNGrad3[n * 16 + 8].z = 1
PNGrad3[n * 16 + 9].x = 0 : PNGrad3[n * 16 + 9].y = -1 : PNGrad3[n * 16 + 9].z = 1
PNGrad3[n * 16 + 10].x = 0 : PNGrad3[n * 16 + 10].y = 1 : PNGrad3[n * 16 + 10].z = -1
PNGrad3[n * 16 + 11].x = 0 : PNGrad3[n * 16 + 11].y = -1 : PNGrad3[n * 16 + 11].z = -1
PNGrad3[n * 16 + 12].x = 1 : PNGrad3[n * 16 + 12].y = 1 : PNGrad3[n * 16 + 12].z = 0
PNGrad3[n * 16 + 13].x = -1 : PNGrad3[n * 16 + 13].y = 1 : PNGrad3[n * 16 + 13].z = 0
PNGrad3[n * 16 + 14].x = 0 : PNGrad3[n * 16 + 14].y = -1 : PNGrad3[n * 16 + 14].z = 1
PNGrad3[n * 16 + 15].x = 0 : PNGrad3[n * 16 + 15].y = -1 : PNGrad3[n * 16 + 15].z = -1
Next n
endfunction
function Noise2D(xin as float, yin as float)
local n0 as float, n1 as float, n2 as float, s as float, t as float, x0 as float, y0 as float, xs as float, ys as float
local i as integer, j as integer, i1 as integer, j1 as integer, i2 as integer, j2 as integer, gi0 as integer, gi1 as integer, gi2 as integer
local x1 as float, y1 as float, x2 as float, y2 as float, x3 as float, y3 as float, t0 as float, t1 as float, t2 as float
s = (xin + yin) * PN3DF2
xs = xin + s
i = floor(xs)
ys = yin + s
j = floor(ys)
t = (i + j) * PN3DG2
x0 = xin - (i - t)
y0 = yin - (j - t)
if x0>y0
i1=1
j1=0
else
i1=0
j1=1
endif
x1 = x0 - i1 + PN3DG2
y1 = y0 - j1 + PN3DG2
x2 = x0 - 1.0 + 2.0 * PN3DG2
y2 = y0 - 1.0 + 2.0 * PN3DG2
i = i && 255
j = j && 255
gi0 = PNHash[i + PNHash[j]] && 15
gi1 = PNHash[i + i1 + PNHash[j + j1]] && 15
gi2 = PNHash[i + 1 + PNHash[j+ 1]] && 15
t0 = 0.5 - x0*x0-y0*y0
if t0<0
n0 = 0.0
else
t0 = t0 * t0
n0 = t0 * t0 * (PNGrad3[gi0].x * x0 + PNGrad3[gi0].y * y0)
endif
t1 = 0.5 - x1*x1-y1*y1
if t1<0
n1 = 0.0
else
t1 = t1 * t1
n1 = t1 * t1 * (PNGrad3[gi1].x * x1 + PNGrad3[gi1].y * y1)
endif
t2 = 0.5 - x2*x2-y2*y2
if t2<0
n2 = 0.0
else
t2 = t2 * t2
n2 = t2 * t2 * (PNGrad3[gi2].x * x2 + PNGrad3[gi2].y * y2)
endif
endfunction perlinheight * (n0 + n1 + n2)