Posted: 19th Apr 2021 13:04
Hi there. I'm still around ... the covid19 has not been able to defeat me .

Lately I've been fiddling with the directx again . To export an object , if we are going to use only vertices color , we don't need the normals or texture coordinates but the position of each vertex x,y,z and its colors... this reduces the final file quite a bit. I have made a small example showing how we can export (write) .x files from AppGameKit . I have the same example exporting boxes that I use in my voxel creator and it works like a charm . I hope this example can be useful and inspiring for someone.


+ Code Snippet
// Project: Export vertex color 
// Created: 2021-04-19

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Export vertex color" )
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

n_cajas=110       `num of planes
n_verts=4         `vertex per object

type caja
  x as float
  y as float
  z as float
  t as float
  r as float
  g as float
  b as float
endtype

dim ball[n_cajas] as caja

SetRandomSeed( 1 )   `crea siempre el mismo patron
for i=1 to n_cajas
   ball[i].x=(Random(0,30)*10)
   ball[i].y=(Random(0,30)*10)
   ball[i].z=0
   ball[i].t=10
   ball[i].r=random(0,255.0)/255.0     ` DirectX color works from 0 to 1 , and AGK  rgb works from 1 to 255
   ball[i].g=random(0,255.0)/255.0     ` something like directX_col = AGK_col/255    Red = 120/255 = 0.470
   ball[i].b=random(0,255.0)/255.0
next

`hold vertex vx,vy,vz
dim vx[4]
dim vy[4]
dim vz[4]

`Time so write to file
OpenToWrite (1,"raw:d:media\planes.x", 0 )
writeline(1,"xof 0303txt 0032")
writeline(1,"")  `space
writeline(1,"#  Saving planes from scratch to .x by Chafari ")
writeline(1,"#  Made in AGK2 April 2021")
writeline(1,"")  `space

for i= 1 to n_cajas
    writeline(1,"")  `space
    writeline(1,"Mesh {")
    writeline(1,"  "+ str(n_verts) + ";")
    
    for a= 1 to n_verts
      c=makecolor(255,0,0)
     
      `holding vertex x,y,z of every plane  ...this time we put them all in same Z position
      vx[1]=ball[i].x-ball[i].t/2
      vx[2]=ball[i].x+ball[i].t/2
      vx[3]=ball[i].x+ball[i].t/2
      vx[4]=ball[i].x-ball[i].t/2

      vy[1]=ball[i].y-ball[i].t/2
      vy[2]=ball[i].y-ball[i].t/2
      vy[3]=ball[i].y+ball[i].t/2
      vy[4]=ball[i].y+ball[i].t/2

      vz[1]=ball[i].z
      vz[2]=ball[i].z
      vz[3]=ball[i].z
      vz[4]=ball[i].z
      
      if a < n_verts  then app$ = ";," else app$ = ";;"
      writeline(1,"   "+ str(vx[a], 6) + "; " + str(vy[a], 6) + "; " + str(vz[a], 6) + app$)
     next
    
    writeline(1,"")             rem space
    writeline(1,"   2;")
    writeline(1,"   3;2,1,0;,")
    writeline(1,"   3;0,3,2;;")
    writeline(1,"" )            rem space
    writeline(1,"   MeshVertexColors {")
    writeline(1,"   4;")
    writeline(1,"   0;"+str(ball[i].r, 6)+";"+str(ball[i].g, 6)+";"+str(ball[i].b, 6)+";1.000000;;,")
    writeline(1,"   1;"+str(ball[i].r, 6)+";"+str(ball[i].g, 6)+";"+str(ball[i].b, 6)+";1.000000;;,")
    writeline(1,"   2;"+str(ball[i].r, 6)+";"+str(ball[i].g, 6)+";"+str(ball[i].b, 6)+";1.000000;;,")
    writeline(1,"   3;"+str(ball[i].r, 6)+";"+str(ball[i].g, 6)+";"+str(ball[i].b, 6)+";1.000000;;;")
    writeline(1,"   }")
    writeline(1,"}")
     
next
 CloseFile ( 1 )
 
 
 loadobject(1,"planes.x")
 SetObjectCullMode(1,0)
 SetObjectLightMode(1,0)
  
  
`sky	
createobjectsphere(1200,3000,10,10)
SetObjectColorEmissive(1200,0,0,150)
SetObjectCullMode(1200,0)	
  
 setcameraposition(1,100,200,-600)
setcameralookat(1,100,200,0,0)
SetCameraRange(1,1,3000)
do
  g=g+2
  setobjectrotation(1,g,g,g)
  Print( ScreenFPS() )
  Sync()
loop



More to come if I feel myself motivated .
Posted: 19th Apr 2021 13:20
great stuff chafari as always
i changed line 51 to this OpenToWrite (1,"raw:"+GetReadPath()+"media\planes.x", 0 )

have you got a universal direct .x exporter with the normals etc

I think that could be modified to suit a plane mesh thats had its vertices modified
Posted: 19th Apr 2021 14:52
Thanks fubarpk.....yeah....I made my own .x exporter long ago when puzzler was here. I have an exporter that works great from Dbpro and I translated to Agk and you can export normals , textures , colors and now vertex colors. This last example was made with the purpose of export .x from any 2D engine. We really do not need a 3D program to write Obj or .x and even your own format based in vertex and triangles. Let me know what you need .

Cheers
Edit. The x exporter I made can only export static meshes...not animation .
Posted: 19th Apr 2021 22:31
If it can do static meshes with normals that would be great
Posted: 19th Apr 2021 22:55
Yeah ....in Dbpro it was so easy to split the whole object and limbs into triangles to read better index data and save it to .x or Obj. When I translated my exporter, I made some test in Agk and I have exported correctly normals and texture coordinates ....even animation that I had to do on the fly. What I didn't sucsses was loading an animated mesh and export to animated .x . When we study the object, and we know what to export, I'm sure that can be posible .

Cheers.