New update
Now we can save the OBJ with UV so we can load the object and apply our texture . We could save in the Obj files orders to take the texture automatically in other programs (Not Appgamekit) just unrem line // Writeline ( 1, "mtllib texture.mtl" ) and line //Writeline ( 1,"usemtl Material1") .
I didn't save the .mtl file automatically but we can just make one in Notepad with just two lines . Where map_Kd xxxxxx wlill be the image to load.
Quote: "
newmtl Material1
map_Kd my_image.jpg
"
// Project: icosahedron
// Created: 2018-12-09
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "icosaedro" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 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
`
`Creating 12 vertex
for i= 1 to 12
createobjectbox( i,0.5,0.5,0.5) :setobjectcolor(i,255,0,0,0)
setobjectrotation(i,0,72*i,0)
moveobjectlocaly(i,9)
if i>5 then moveobjectlocaly(i,-9)
if i>5 then setobjectrotation(i,0,(72*i)+72/2,0)
moveobjectlocalz(i,8)
createtext(i,str(i))
settextsize(i,40)
next
`top and bottom objects
setobjectposition(11,0,14,0)
setobjectposition(12,0,-5,0)
tri(20,11,1,2)
tri(21,11,2,3)
tri(22,11,3,4)
tri(23,11,4,5)
tri(24,11,5,1)
tri(25,1,6,2)
tri(26,2,7,3)
tri(27,3,8,4)
tri(28,5,4,9)
tri(29,1,5,10)
tri(30,1,10,6)
tri(31,2,6,7)
tri(32,3,7,8)
tri(33,4,8,9)
tri(34,5,9,10)
tri(35,6,10,12)
tri(36,10,9,12)
tri(37,9,8,12)
tri(38,8,7,12)
tri(39,7,6,12)
`unifying all objects
createobjectbox(100,0,0,0)
for i= 20 to 39
setobjectposition(i,0,-5,0) `giving at every triangle new pivot
FixObjectToObject(i,100)
next
gosub save_obj
setcameraposition(1,0,0,-50)
SetCameraLookAt(1,0,0,0,0)
do
`hiding vertex
for i= 1 to 12
if ( GetRawKeyState( 32 ) ) then SetObjectVisible(i,1) else SetObjectVisible(i,0)
next
`Showing vertex numbers
for i= 1 to 12
x#=getobjectx(i)
y#=getobjecty(i)
z#=getobjectz(i)
px = GetScreenXFrom3D( x#,y#,z#)
py = GetScreenYFrom3D( x#,y#,z#)
if ( GetRawKeyState( 32 ) ) then settextposition(i,px,py) else settextposition(i,-30,-30)
next
gosub camara
g=g+6
setobjectrotation(100,g,g,g)
print("Use Cursor to fly")
print("Press Spacebar to see vertex")
Sync()
loop
function tri(n,v1,v2,v3)
createobjectplane(n,5,5)
SetObjectCullMode(n,0)
setobjectcolor(n,random(50,255),random(50,255),random(50,255),0)
mesh = CreateMemblockFromObjectMesh(n,1)
`getting vertex quantity
verts= GetMemblockInt( mesh, 0 )
`first of all , setting all vertex to 0,0,0 position
for t= 0 to verts-1
SetMeshMemblockVertexPosition(mesh,t,0,0,0)
next
`making triangle in three vertex ....setting plane vertex in three object coords.
x1#=Getobjectx(v1)
y1#=Getobjecty(v1)
z1#=Getobjectz(v1)
x2#=Getobjectx(v2)
y2#=Getobjecty(v2)
z2#=Getobjectz(v2)
x3#=Getobjectx(v3)
y3#=Getobjecty(v3)
z3#=Getobjectz(v3)
SetMeshMemblockVertexPosition(mesh,0, x1#,y1#,z1#)
SetMeshMemblockVertexPosition(mesh,1, x2#,y2#,z2#)
SetMeshMemblockVertexPosition(mesh,2, x3#,y3#,z3#)
SetMeshMemblockVertexUV(mesh,1,0.5,0.5)
SetMeshMemblockVertexUV(mesh,2,-0.5,0.5)
DeleteMemblock(1)
`fixing mesh
SetObjectMeshFromMemblock(n,1,mesh)
endfunction
camara:
if ( GetRawKeyState( 38 ) ) then MoveCameraLocalZ( 1, 1 ) // cursor up
if ( GetRawKeyState( 40 ) ) then MoveCameraLocalZ( 1, -1 ) // cursor down
if ( GetRawKeyState( 37 ) ) then MoveCameraLocalX( 1, -1 ) // cursor iz
if ( GetRawKeyState( 39 ) ) then MoveCameraLocalX( 1, 1 ) // cursor derecha
if ( GetPointerPressed() )
startx# = GetPointerX()
starty# = GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
pressed = 1
endif
if ( GetPointerState() = 1 )
fDiffX# = (GetPointerX() - startx#)/1.0
fDiffY# = (GetPointerY() - starty#)/1.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endif
return
save_obj:
OpenToWrite ( 1, "raw:d:media\icosahedron.obj", 0 )
// Writeline ( 1, "mtllib texture.mtl" ) // unrem this line if you wanna load texture automatically in other programs
for t= 20 to 39
mesh = CreateMemblockFromObjectMesh(t,1)
`get object vertex quantity
verts= GetMemblockInt( mesh, 0 )
`VERTEX
for i= 0 to 2
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexy(mesh,i)
z#=GetMeshMemblockVertexz(mesh,i)
Writeline ( 1, " v " + str(x#)+ " "+str(z#)+ " "+str(y#))
next i
`NORMALS
for i= 0 to 2
nx#=GetMeshMemblockVertexNormalX( mesh, i )
ny#=GetMeshMemblockVertexNormalY( mesh, i )
nz#=GetMeshMemblockVertexNormalz( mesh, i )
writeline(1, " vn "+str(nx#)+" "+str(ny#)+" "+str(nz#))
next
`TEXTURE VERTICES
for i= 0 to 2
UU#=GetMeshMemblockVertexU( mesh, i )
VV#=GetMeshMemblockVertexV( mesh, i )
writeline(1, " vt "+str(UU#)+" "+str(VV#))
next
//Writeline ( 1,"usemtl Material1") // unrem this line if you wanna load texture automatically in other programs
`FACES
s=0 `reseting variable s
for i= 0 to 2
inc s,1
if s=2
n=n+1
writeline (1, " f "+ str(n)+"/"+str(n)+" "+str(n+2)+"/"+str(n+2)+" "+str(n+1)+"/"+str(n+1))
s=0
n=n+2
endif
next
DeleteMemblock(1)
next
CloseFile ( 1 )
return
remstart
The content of Mtl file just this two lines. Just put the name of your texture behind map_kd
newmtl Material1
map_Kd my_image.jpg
remend
I hope you can find it useful .
Cheers.
I'm not a grumpy grandpa
