Advertisement
GenesisFan64

objtomars_sw

Apr 3rd, 2020
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.63 KB | None | 0 0
  1. # ======================================================================
  2. # OBJ TO MARS (for Star Wars Arcade 32X)
  3. # a copypaste of the script used in MdlRenderer-MARS
  4. #
  5. # Stable 3/Abr/2020
  6. #
  7. # use this script with this folder setup:
  8. # /mdl            | <-- your .obj models
  9. # objtomars_sw.py | <-- this script
  10. #
  11. # Usage:
  12. # objtomars_sw.py objname
  13. # ======================================================================
  14.  
  15. # example model data structure (for assembly)
  16. #       dc.l 0              ; $00 faces: 16-bit | $02 faces: 8-bit
  17. #       dc.l .vert|$02000000
  18. #       binclude "object/model_head.bin"
  19. #       dc.l .faces|$02000000
  20. #       dc.l 0
  21. #       dc.l .vert|$02000000
  22. #       binclude "object/model_head.bin"
  23. #       dc.l .faces|$02000000
  24. #       dc.w $FFF           ; Z distance
  25. #       dc.b 0
  26. #       dc.b 0
  27. #       dc.l 0,0,0
  28. #       dc.l .vert|$02000000
  29. #       dc.l .vert|$02000000
  30. #       align 4
  31. #.vert:
  32. #       binclude "object/model_vert.bin"
  33. #       align 4
  34. #.faces:
  35. #       binclude "object/model_face.bin"
  36. #       align 4
  37.        
  38. import sys
  39.  
  40. # ======================================================================
  41. # -------------------------------------------------
  42. # Settings
  43. # -------------------------------------------------
  44.  
  45. USE_WFACES = True       # face points are BYTES or WORDS (set 2 or 0 in the model header)
  46.  
  47. # ======================================================================
  48. # -------------------------------------------------
  49. # Init
  50. # -------------------------------------------------
  51.  
  52. SCALE_SIZE=0x100
  53. FROM_BLENDER=False #True
  54. img_width = 1
  55.  
  56. # normal names for textures
  57. TAG_NOMATERIAL    = "MARSNULL"      # random color mode
  58. TAG_MARSCOLOR     = "MARSINDX"      # set index color permanently
  59. TAG_MARSINDX_LIST = "MARSLIST"          # set index color in the material (for animated stuff)
  60.  
  61. # tag for texture data in assembly
  62. TAG_TEXTUR        = "Textr_"
  63.  
  64. # ======================================================================
  65. # -------------------------------------------------
  66. # Start
  67. # -------------------------------------------------
  68.  
  69. num_vert      = 0
  70. has_img       = False
  71. use_img       = False
  72.  
  73. projectname   = sys.argv[1]
  74. CONVERT_TEX=1
  75.  
  76. INCR_Y=0
  77. if len(sys.argv) == 3:
  78.   #CONVERT_TEX = sys.argv[2]
  79.   INCR_Y = sys.argv[2]
  80.  
  81. list_vertices = list()
  82. list_faces    = list()
  83. model_file    = open("mdl/"+projectname+".obj","r")
  84. material_file = open("mdl/"+projectname+".mtl","r") # CHECK BELOW
  85. out_vertices  = open(projectname+"_vert.bin","wb")  # vertices (points)
  86. out_faces     = open(projectname+"_face.bin","wb")  # faces
  87. #out_vertex    = open(projectname+"_vrtx.bin","wb") # texture vertex
  88. out_head      = open(projectname+"_head.bin","wb")  # header
  89. out_mtrl      = open(projectname+"_mtrl.asm","w")
  90.  
  91. used_triangles= 0
  92. used_quads    = 0
  93. solidcolor    = 1
  94. reading       = True
  95. vertex_list   = list()
  96.  
  97. random_mode   = True
  98. random_color  = 1
  99. indx_color    = 0
  100. mtrl_curr     = 0
  101. mtrl_index    = 0
  102.  
  103. # ======================================================================
  104. # -------------------------------------------------
  105. # Getting data
  106. # -------------------------------------------------
  107.  
  108. while reading:
  109.   text=model_file.readline()
  110.   if text=="":
  111.     reading=False
  112.  
  113.   ## ---------------------------
  114.   ## vertices
  115.   ## ---------------------------
  116.  
  117.   #if text.find("o") == False:
  118.       #print("OBJECT")
  119.  
  120.   # ---------------------------
  121.   # vertices
  122.   # ---------------------------
  123.  
  124.   if text.find("v") == False:
  125.     a = text[2:]
  126.     point = a.replace("\n","").split(" ")
  127.     if point[0] != "":
  128.       x=float(point[0])*SCALE_SIZE
  129.       y=(float(point[1])*SCALE_SIZE)
  130.       z=float(point[2])*SCALE_SIZE
  131.       mars_x=int(x)*-1
  132.       mars_z=int(z)
  133.       mars_y=int(y)+(int(INCR_Y)*-1)
  134.      
  135.       #print(mars_x,mars_y,mars_z)
  136.      
  137.       #if FROM_BLENDER == True:     # Y pos
  138.         #mars_y=(int(y*SCALE_SIZE)*-1)+int((SCALE_SIZE/2))
  139.       #else:
  140.         #mars_y=int(y*SCALE_SIZE)*-1
  141.  
  142.       # LONG
  143.       out_vertices.write( bytes([
  144.           #mars_x >> 24 & 0xFF,
  145.           #mars_x >> 16 & 0xFF,
  146.           mars_x >> 8 & 0xFF,
  147.           mars_x & 0xFF,
  148.           #mars_y >> 24 & 0xFF,
  149.           #mars_y >> 16 & 0xFF,
  150.           mars_y >> 8 & 0xFF,
  151.           mars_y & 0xFF,
  152.           #mars_z >> 24 & 0xFF,
  153.           #mars_z >> 16 & 0xFF,
  154.           mars_z >> 8 & 0xFF,
  155.           mars_z & 0xFF
  156.           ]) )
  157.       num_vert += 1
  158.    
  159.   # ---------------------------
  160.   # vertex
  161.   # ---------------------------
  162.  
  163.   if text.find("vt") == False:
  164.     a = text[2:]
  165.     point = a.replace("\n","").split(" ")
  166.    
  167.     b = float(point[2])-1
  168.     a = b - b - b
  169.     vertex_list.append(float(point[1]))
  170.     vertex_list.append(a)
  171.     vertex_list.append(0)
  172.     vertex_list.append(0)
  173.  
  174.     ## if needed later
  175.     #x=float(point[1])
  176.     #y=float(point[2])
  177.     #mars_x=int(x)
  178.     #mars_y=int(y)
  179.     #out_vertex.write( bytes([
  180.           #mars_x >> 24 & 0xFF,
  181.           #mars_x >> 16 & 0xFF,
  182.           #mars_x >> 8 & 0xFF,
  183.           #mars_x & 0xFF,
  184.           #mars_y >> 24 & 0xFF,
  185.           #mars_y >> 16 & 0xFF,
  186.           #mars_y >> 8 & 0xFF,
  187.           #mars_y & 0xFF,
  188.           #]) )
  189.  
  190.   # ---------------------------
  191.   # MATERIAL check
  192.   # ---------------------------
  193.  
  194.   if text.find("usemtl") == False:
  195.     material_file.seek(0)
  196.     mtlname = text[7:].rstrip('\r\n')
  197.    
  198.     a = mtlname[:8]
  199.    
  200.     if a == TAG_NOMATERIAL:
  201.       print("Material: Random")
  202.       has_img = False
  203.       use_img = False
  204.       random_mode = True
  205.      
  206.     # SOLID COLOR normal
  207.     elif a == TAG_MARSCOLOR:
  208.       a = mtlname.split("_")
  209.       #out_mtrl.write("\t dc.l "+str(a[1])+","+str(0)+"\n")  <-- if needed
  210.       #indx_color += 1
  211.       indx_color = int(a[1])
  212.       print("Material: Color ID",indx_color)
  213.       #img_width = 1
  214.       #img_height = 1
  215.       has_img = False
  216.       use_img = False
  217.       random_mode = False
  218.  
  219.     elif a == TAG_MARSINDX_LIST:
  220.       a = mtlname.split("_")
  221.       out_mtrl.write("\t dc.l "+str(a[1])+","+str(0)+"\n")
  222.       mtrl_curr = mtrl_index
  223.       mtrl_index += 1
  224.       print("Material: Color ID",indx_color)
  225.       #img_width = 1
  226.       #img_height = 1
  227.       has_img = False
  228.       use_img = False
  229.       random_mode = False
  230.  
  231.     # TEXTURE
  232.     else:
  233.       use_img = True
  234.       # MATERIAL FILE READ LOOP
  235.       mtlread = True
  236.       while mtlread:
  237.         mtltext=material_file.readline()
  238.         if mtltext=="":
  239.             mtlread=False
  240.      
  241.         # Grab material section
  242.         if mtltext.find("newmtl "+mtlname) == False:
  243.             i = True
  244.             while i:
  245.               b = material_file.readline()
  246.               if b=="":
  247.                   i=False
  248.                  
  249.               # filename
  250.               if b.find("map_Kd") == False:
  251.                   tex_fname = b[7:].rstrip('\r\n')
  252.                   tex_file = open(tex_fname,"rb")
  253.                   # COPYPASTED
  254.                   tex_file.seek(1)
  255.                   color_type = ord(tex_file.read(1))
  256.                   image_type = ord(tex_file.read(1))
  257.                  
  258.                   if color_type == 1:
  259.                     pal_start = ord(tex_file.read(1))
  260.                     pal_start += ord(tex_file.read(1)) << 8
  261.                     pal_len = ord(tex_file.read(1))
  262.                     pal_len += ord(tex_file.read(1)) << 8
  263.                     ignore_this = ord(tex_file.read(1))
  264.                     has_pal = True
  265.    
  266.                   if image_type == 1:
  267.                     img_xstart = ord(tex_file.read(1))
  268.                     img_xstart += ord(tex_file.read(1)) << 8
  269.                     img_ystart = ord(tex_file.read(1))
  270.                     img_ystart += ord(tex_file.read(1)) << 8
  271.                     img_width = ord(tex_file.read(1))
  272.                     img_width += ord(tex_file.read(1)) << 8
  273.                     img_height = ord(tex_file.read(1))
  274.                     img_height += ord(tex_file.read(1)) << 8
  275.    
  276.                     img_pixbits = ord(tex_file.read(1))
  277.                     img_type = ord(tex_file.read(1))
  278.                     if (img_type >> 5 & 1) == False:
  279.                         print("ERROR: TOP LEFT images only")
  280.                         tex_file.close()
  281.                         quit()
  282.                     has_img = True
  283.                     random_mode = False
  284.                    
  285.             # register name
  286.                     b = tex_fname.split("/")[-1:]
  287.                     a = b[0].split(".")
  288.                     outname = a[0]
  289.  
  290.                     if int(CONVERT_TEX) == True:
  291.                       print("Converting material:",mtlname)
  292.                       has_img = True
  293.  
  294.                       output_file = open("mtrl/"+outname+"_pal.bin","wb")
  295.                       d = pal_len
  296.                       while d:
  297.                         d -= 1
  298.  
  299.                         a = (ord(tex_file.read(1)) & 0xF8 ) << 7
  300.                         a |= (ord(tex_file.read(1)) & 0xF8 ) << 2
  301.                         a |= (ord(tex_file.read(1)) & 0xF8 ) >> 3
  302.                         output_file.write( bytes([ ((a>>8)&0xFF) , (a&0xFF) ]))
  303.                       output_file.close()
  304.                      
  305.                       art_file = open("mtrl/"+outname+"_art.bin","wb")
  306.                       b = img_height
  307.                       e = 0
  308.                       while b:
  309.                         c = img_width
  310.                         while c:
  311.                             a = ord(tex_file.read(1))
  312.                             art_file.write( bytes([a]) )
  313.                             c -= 1
  314.                         b -= 1
  315.                         e += 1
  316.                       art_file.close()
  317.  
  318.                   else:
  319.                       print("IMAGE TYPE NOT SUPPORTED:",hex(image_type))
  320.                       has_img = False
  321.                       random_mode = False
  322.  
  323.                   out_mtrl.write("\t dc.l "+str(TAG_TEXTUR)+str(mtlname)+","+str(img_width)+"\n")
  324.                   mtrl_curr = mtrl_index
  325.                   mtrl_index += 1
  326.                   tex_file.close()
  327.  
  328.   # ---------------------------
  329.   # Faces
  330.   # ---------------------------
  331.  
  332.   if text.find("f") == False:
  333.     a = text[2:]
  334.     point = a.split(" ")
  335.     if len(point) == 3:
  336.       x_curr=point[0].split("/")
  337.       y_curr=point[1].split("/")
  338.       z_curr=point[2].split("/")
  339.  
  340.       # Set material id and size
  341.       if use_img == True:
  342.         a = mtrl_curr
  343.         b = 0x8000|3
  344.       else:
  345.         if random_mode == True:
  346.           a = random_color
  347.           random_color += (1 & 0xFF)
  348.           if random_color == 0:
  349.             random_color = 1
  350.         else:
  351.           a = indx_color
  352.         b = 3
  353.  
  354.       #    +0 - normal
  355.       #    +8 - wireframe
  356.       # +0x10 - triangles
  357.       ftype = 0x10
  358.       out_faces.write( bytes([a&0xFF,ftype]) )
  359.       x=int(x_curr[0])-1
  360.       y=int(y_curr[0])-1
  361.       z=int(z_curr[0])-1
  362.       outx_l = x >> 8 & 0xFF
  363.       outx_r = x & 0xFF
  364.       outy_l = y >> 8 & 0xFF
  365.       outy_r = y & 0xFF
  366.       outz_l = z >> 8 & 0xFF
  367.       outz_r = z & 0xFF
  368.       if USE_WFACES == True:
  369.           out_faces.write(bytes([outx_l,outx_r,
  370.                   outy_l,outy_r,
  371.                   outz_l,outz_r,
  372.                   0,0
  373.                   ]))
  374.       else:
  375.           out_faces.write(bytes([outx_r,outy_r,outz_r,0]))
  376.  
  377.       used_triangles += 1
  378.      
  379.     # QUAD
  380.     if len(point) == 4:
  381.       x_curr=point[0].split("/")
  382.       y_curr=point[1].split("/")
  383.       z_curr=point[2].split("/")
  384.       q_curr=point[3].split("/")
  385.  
  386.       # Set material id and size
  387.       if has_img == True:
  388.         a = mtrl_curr
  389.         b = 0x8000|4
  390.       else:
  391.         if random_mode == True:
  392.           a = random_color
  393.           random_color += (1 & 0xFF)
  394.           if random_color == 0:
  395.             random_color = 1
  396.         else:
  397.           a = indx_color
  398.         b = 4
  399.      
  400.       #    +0 - normal
  401.       #    +8 - wireframe
  402.       # +0x10 - triangles
  403.       ftype = 0
  404.       out_faces.write( bytes([a&0xFF,ftype]) )
  405.       x=int(x_curr[0])-1
  406.       y=int(y_curr[0])-1
  407.       z=int(z_curr[0])-1
  408.       q=int(q_curr[0])-1
  409.       outx_l = x >> 8 & 0xFF
  410.       outx_r = x & 0xFF
  411.       outy_l = y >> 8 & 0xFF
  412.       outy_r = y & 0xFF
  413.       outz_l = z >> 8 & 0xFF
  414.       outz_r = z & 0xFF
  415.       outq_l = q >> 8 & 0xFF
  416.       outq_r = q & 0xFF
  417.       if USE_WFACES == True:
  418.           out_faces.write(bytes([outx_l,outx_r,
  419.                   outy_l,outy_r,
  420.                   outz_l,outz_r,
  421.                   outq_l,outq_r,
  422.                   ]))
  423.       else:
  424.           out_faces.write(bytes([outx_r,outy_r,outz_r,outq_r]))
  425.       used_quads += 1
  426.  
  427. #======================================================================
  428. # ----------------------------
  429. # Vertex convert
  430. # ----------------------------
  431.  
  432. cntr = len(vertex_list)
  433. if cntr != 0:
  434.   out_vertex = open(projectname+"_vrtx.bin","wb")   # texture vertex
  435.  
  436.   x_tx = 0
  437.   while cntr:
  438.     x_l = int(vertex_list[x_tx+2] * vertex_list[x_tx])
  439.     x_r = int(vertex_list[x_tx+3] * vertex_list[x_tx+1])-1
  440.     out_vertex.write( bytes([
  441.     x_l>>8&0xFF,x_l&0xFF,
  442.     x_r>>8&0xFF,x_r&0xFF]))
  443.     x_tx += 4
  444.     cntr -= 4
  445.  
  446.   # padding
  447.   b = out_vertex.tell()
  448.   a = b & 0xF
  449.   if a != 0:
  450.     a = 0x10 - a
  451.     out_vertex.write(bytes(a))
  452.   out_vertex.close()
  453.  
  454. # ----------------------------
  455. # face padding
  456. b = out_faces.tell()
  457. a = b & 0xF
  458. if a != 0:
  459.   a = (0x10 - a)
  460.   out_faces.write(bytes(a))
  461.    
  462. #======================================================================
  463. # ----------------------------
  464. # End
  465. # ----------------------------
  466.  
  467. out_head.write( bytes([
  468.     num_vert >> 8 & 0xFF,
  469.     num_vert & 0xFF,
  470.     used_triangles+used_quads >> 8 & 0xFF,
  471.     used_triangles+used_quads & 0xFF
  472.     ]) )
  473.  
  474. print("Vertices:",num_vert)
  475. print("   Faces:",used_triangles+used_quads)
  476. print("Polygons:",used_triangles)
  477. print("   Quads:",used_quads)
  478. #print("Done.")
  479.  
  480. #print(mtrl_tag)
  481.  
  482. model_file.close()
  483. material_file.close()
  484. out_vertices.close()
  485. out_faces.close()
  486. out_head.close()
  487. out_mtrl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement