a rocky Dragster racing track anyone?
// Project: ObjExporter
// Created: 2018-12-25
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "ObjExporter" )
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( screenrate, 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
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
size=4
y=0
red=255:green=255:blue=255
a=0
oldy=0
for z#=0 to 1000
r=random2(-2,2)
inc y,r
for x#=-size to size
AddVertex(meshid,(x#+-1)*size,y,(z#+ 1) * size , 0,1,0 ,0,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-1)*size,oldy,(z#+-1) * size , 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ 1)*size,y,(z#+ 1) * size , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ 1)*size,y,(z#+ 1) * size , 0,1,0 ,1,0, MakeColor(red,green,blue))
AddVertex(meshid,(x#+-1)*size,oldy,(z#+-1) * size , 0,1,0 ,0,1, MakeColor(red,green,blue))
AddVertex(meshid,(x#+ 1)*size,oldy,(z#+-1) * size , 0,1,0 ,1,1, MakeColor(red,green,blue))
next
oldy = y - r
next
obj = CreateObjectFromMeshWithUVTexturing(meshid,LoadImage("raw:c:\data\checkers.png"))
AGM_SaveObject(obj,1,"Multiple", "raw:c:\data\","multiple.obj","multiple.mtl", "checkers.png",1)
do
Print("Saved - look in the area where you saved them")
Print( ScreenFPS() )
Sync()
loop
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 AGM_SaveObject(objid, meshindex, group$, folder$, filename$, material$, texture$, debug)
if texture$<>"" then SetObjectImage(objID,LoadImage(folder$+texture$),0)
memblock=CreateMemblockFromObjectMesh(objID,meshindex)
VC = GetMemblockInt(memblock,0)
IC = GetMemblockInt(memblock,4)
Attributes = GetMemblockInt(memblock,8)
counter = GetMemblockInt(memblock,12)
DataOffset = GetMemblockInt(memblock,16)
IndexOffset = GetMemblockInt(memblock,20)
PositionData = GetMemblockInt(memblock,24)
PositionName$ = GetMemblockString(memblock,28,8)
NormalData = GetMemblockInt(memblock,40)
NormalName$ = GetMemblockString(memblock,44,8)
CData = GetMemblockInt(memblock,52)
CName$ = GetMemblockString(memblock,56,8)
ColorData = GetMemblockInt(memblock,60)
ColorName$ = GetMemblockString(memblock,64,8)
if ColorName$=""
colorData=0
ColorName$="No color Data"
endif
if debug=1
repeat
print("Object " + group$)
print("Vertex Count - " + str(vc))
print("Index Count - " + str(IC))
print("Atribute Count - " + str(Attributes))
print("Data Counter - " + str(counter))
print("Data Offset - " + str(DataOffset))
print("Index Offset - " + str(IndexOffset))
print("Position Data - " + str(PositionData))
print("Position String " + positionName$)
print("Normal Data - " + str(NormalData))
print("Normal String " + NormalName$)
print("UV Data - " + str(CData))
print("UV String " + CName$)
print("Color Data - " + str(ColorData))
print("Color String " + ColorName$)
print("Press Space to continue")
print(VC)
print(IC)
print(IC/3)
print(IC/4)
sync()
until GetRawKeyPressed(32)
endif
fw = OpenToWrite(folder$+filename$)
WriteLine(fw,"#AGM Object - " + folder$ + filename$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
WriteLine(fw,"mtllib " + material$)
WriteLine(fw,"")
WriteLine(fw,"g " + group$)
WriteLine(fw,"#Vertices")
for v=0 to vc * counter-1 step counter
WriteLine(fw,"v " + str(GetMemblockFloat(memblock,dataoffset+v)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+4)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+8))) // Vertex X,Y,Z
next
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Normals")
for v=0 to vc * counter-1 step counter
WriteLine(fw,"vn " + str(GetMemblockFloat(memblock,dataoffset+v+12)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+16)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+20))) // Normal x,y,z
next
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Textures UV's")
for v=0 to vc * counter-1 step counter
WriteLine(fw,"vt " + str(GetMemblockFloat(memblock,dataoffset+v+24)) + " " + str(GetMemblockFloat(memblock,dataoffset+v+28)))
next
WriteLine(fw,"")
WriteLine(fw,"")
WriteLine(fw,"#Faces")
WriteLine(fw,"usemtl Material")
if IC<>0 // Index Couner > 0 so this model as index data at the end to work out the order of faces
for i=0 to (IC / 3)-1
index1=GetMemblockByte(memblock,(indexoffset+(i*12)))+1
index2=GetMemblockByte(memblock,(indexoffset+(i*12)+4))+1
index3=GetMemblockByte(memblock,(indexoffset+(i*12)+8))+1
// WriteLine(fw,"f " + str(index1) + "/0/0 " + str(index2) + "/0/0 " + str(index3) + "/0/0")
WriteLine(fw,"f " + str(index1) + "/" + str(facecount+1) + "/" + str(facecount+1) +" " + str(index2) + "/" + str(facecount) + "/" + str(facecount) + " " + str(index3) + "/" + str(facecount+2) +"/" + str(facecount+2))
next
print(i)
sync()
sleep(1000)
else
for facecount=1 to vc-2 step 3// VC Vertex Count and no Index count - so the Vertices are straight as is.
WriteLine(fw,"f " + str(facecount) + "/"+str(facecount)+"/" + str(facecount) + " " + str(facecount+1) + "/"+str(facecount+1)+"/" + str(facecount+1)+ " " + str(facecount+2) + "/"+str(facecount+2)+"/" + str(facecount+2))
next
endif
CloseFile(fw)
// write the MTL file
fw = OpenToWrite(folder$ + material$)
WriteLine(fw,"#AGM Material - " + material$)
WriteLine(fw,"#Exported with AppGameKit")
WriteLine(fw,"")
red#=GetObjectColorRed(objid)
green#=GetObjectColorGreen(objid)
blue#=GetObjectColorBlue(objid)
WriteLine(fw,"newmtl Material")
WriteLine(fw,"Kd " + str(red#/255.0) + " " + str(green#/255.0) + " " + str(blue#/255.0))
if texture$<>""
writeLine(fw,"map_Kd "+texture$)
endif
CloseFile(fw)
sync()
sleep(1000)
DeleteObject(objID)
DeleteMemblock(memblock)
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")
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)
next
DeleteObject(id)
id = CreateObjectFromMeshMemblock(memblock)
SetObjectImage(id,texture,0)
meshmemory = memblock
endfunction id
Enough on this for now..