October 28, 2021, 12:13:28

Author Topic: [bb] VRML Mesh Exporter by Braneloc [ 1+ years ago ]  (Read 720 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] VRML Mesh Exporter by Braneloc [ 1+ years ago ]
« on: June 29, 2017, 00:28:42 »
Title : VRML Mesh Exporter
Author : Braneloc
Posted : 1+ years ago

Description : (Ver 1.1, updated Apr 04)
Takes a normal blitz3D mesh, and outputs it into the "standard" VRML format.  It can then be loaded into any other graphics programs that support VRML import - like 3D Studio Max.

My uses for it:
 Just to export custom blitz geometry in editable format..
 Or to convert a .B3D or .X into something editable...


Code :
Code: BlitzBasic
  1. ;
  2. ;               VRML Exporter
  3. ;               (c) Braneloc, Feb 2003
  4. ;                       Just to export mesh geometry into max
  5. ;
  6. ;               Usage:
  7. ;                       Feel free to use it !  Please send me any changes,
  8. ;                       don't claim you wrote it (!)
  9. ;                       Add as new file to project
  10. ;
  11. ;               Good bits:
  12. ;                       Correctly exports single and multi-surface static meshes
  13. ;                       Preserves UVs
  14. ;                       Well, it works !!
  15. ;
  16. ;               Limitations:
  17. ;                       No animation support
  18. ;              
  19. ;               History:
  20. ;                       1.0 Feb 2003
  21. ;                               First release to Blitz code archives.
  22. ;                               (Protean is my code editor of choice, check it out)
  23. ;                       1.0     July 2003
  24. ;                               First release to Protean code archives 
  25. ;                       1.1     Apr 2004
  26. ;                               Removed brush & texture information limitation :)
  27. ;                               Now exports correct filenames for textures.
  28. ;
  29. ;               Omissions:      (might add if there is sufficiant/any demand)
  30. ;                                       (it's just an exporter, not a world creator !)
  31. ;                       Vertex colouring
  32. ;                       normals
  33.  
  34. vrml_demo()
  35.  
  36. ;;; <summary>just create/load an object, and then export it.
  37. ;;; it should load nicely into 3D Studio Max and anything that
  38. ;;; can handle VRML files.</summary>
  39. ;;; <subsystem>vrml</subsystem>
  40. Function vrml_demo()
  41.         Graphics3D 640,480,0,2
  42.        
  43.         c=CreateCube()
  44.         NameEntity c,"Cube"
  45.         vrml_export("cube.wrl",c)
  46. End Function
  47.  
  48. ;;; <summary>Converts a mesh into a vrml file</summary>
  49. ;;; <param name="file">The file to export, presumably ending .wrl</param>
  50. ;;; <param name="mesh">Blitz's mesh handle</param>
  51. ;;; <remarks>Preserves as much as possible</remarks>
  52. ;;; <subsystem>vrml</subsystem>
  53. Function vrml_export(file$,mesh)
  54.         o=WriteFile(file$)
  55.        
  56.         WriteLine o,"#VRML V1.0 ascii"
  57.         WriteLine o,"# Blitz mesh to VRML converted on "+CurrentDate$()
  58.         WriteLine o,"# Braneloc's BlitzVRML Exporter 1.1 (Apr 2004)"
  59.         WriteLine o,"#"
  60.         If EntityName(mesh)
  61.                 WriteLine o,"# "+EntityName(mesh)
  62.         EndIf
  63.         For n=1 To CountSurfaces(mesh)
  64.                 ;WriteLine o,"DEF surface"+n
  65.                 WriteLine o,"Separator { # surface "+n
  66.                
  67.                 vrml_tex(o,vrml_surfacename$(mesh,n))   ; you have a better idea?
  68.                
  69.                 WriteLine o,"   Coordinate3 {"
  70.                 WriteLine o,"           point ["
  71.                 vrml_verts(o,mesh,n)
  72.                 WriteLine o,"           ]"
  73.                 WriteLine o,"   }"
  74.                
  75.                 WriteLine o,"   TextureCoordinate2 {"
  76.                 WriteLine o,"           point ["
  77.                 vrml_UVs(o,mesh,n)
  78.                 WriteLine o,"           ]"
  79.                 WriteLine o,"   }"
  80.                
  81.                 WriteLine o,"   IndexedFaceSet {"
  82.                 WriteLine o,"           coordIndex ["
  83.                 vrml_faces(o,mesh,n)
  84.                 WriteLine o,"           ]"
  85.                 WriteLine o,"   }"
  86.                
  87.                 WriteLine o,"}"
  88.         Next
  89.        
  90.         CloseFile o
  91. End Function
  92.  
  93. ;;; <summary>Get texture name of mesh surface</summary>
  94. ;;; <param name="mesh">Mesh to analyse</param>
  95. ;;; <param name="surfacenumber">Surface number in question</param>
  96. ;;; <returns>Texture name of specified surface on mesh</returns>
  97. ;;; <subsystem>vrml</subsystem>
  98. Function vrml_surfacename$(mesh,surfacenumber)
  99.         surf=GetSurface(mesh,surfacenumber)
  100.         br=GetSurfaceBrush(surf)
  101.         te=GetBrushTexture(br)
  102.        
  103.         tn$=vrml_StripPath(TextureName(te))
  104.         If tn$<>""
  105.                 Return tn$
  106.         Else
  107.                 Return surfacenumber+".bmp"
  108.         EndIf
  109. End Function
  110.  
  111. ;#Region vrml fragments
  112. ;;; <summary>Rips the path from a full filename</summary>
  113. ;;; <param name="file"></param>
  114. ;;; <remarks></remarks>
  115. ;;; <returns>File part of filename</returns>
  116. ;;; <subsystem>vrml</subsystem>
  117. ;;; <example></example>
  118. Function vrml_StripPath$(file$)
  119.         If Len(file$)>0
  120.                 For i=Len(file$) To 1 Step -1
  121.                         mi$=Mid$(file$,i,1)
  122.                         If mi$="" Or mi$="/" Then Return name$ Else name$=mi$+name$
  123.                 Next
  124.         EndIf
  125.         Return name$
  126. End Function
  127.  
  128. ;;; <summary>outputs vrml fragment for the vertices</summary>
  129. ;;; <param name="file"></param>
  130. ;;; <param name="mesh"></param>
  131. ;;; <param name="surf"></param>
  132. ;;; <remarks></remarks>
  133. ;;; <returns></returns>
  134. ;;; <subsystem>vrml</subsystem>
  135. ;;; <example></example>
  136. Function vrml_verts(file,mesh, surf=1)
  137.         s=GetSurface(mesh,surf)
  138.         For n=0 To CountVertices(s)-1
  139.                 WriteLine file,"                        "+VertexX(s,n)+" "+VertexY(s,n)+" "+VertexZ(s,n)+","
  140.         Next
  141. End Function
  142.  
  143. ;;; <summary>outputs vrml fragment for the triangles in the mesh</summary>
  144. ;;; <param name="file"></param>
  145. ;;; <param name="mesh"></param>
  146. ;;; <param name="surf"></param>
  147. ;;; <remarks></remarks>
  148. ;;; <returns></returns>
  149. ;;; <subsystem>vrml</subsystem>
  150. ;;; <example></example>
  151. Function vrml_faces(file,mesh,surf=1)
  152.         s=GetSurface(mesh,surf)
  153.         For n=0 To CountTriangles (s)-1
  154.                 a$="                    "
  155.                 For m=0 To 2
  156.                         a$=a$+TriangleVertex(s,n,m)+","
  157.                 Next
  158.                 a$=a$+"-1,"
  159.                 WriteLine file,a$
  160.         Next
  161. End Function
  162.  
  163. ;;; <summary>outputs vrml fragment for UV coordinates</summary>
  164. ;;; <param name="file"></param>
  165. ;;; <param name="mesh"></param>
  166. ;;; <param name="surf"></param>
  167. ;;; <remarks></remarks>
  168. ;;; <returns></returns>
  169. ;;; <subsystem>vrml</subsystem>
  170. ;;; <example></example>
  171. Function vrml_UVs(file,mesh,surf=1)
  172.         s=GetSurface(mesh,surf)
  173.         ; not sure how/if extra sets are handled.
  174.         For n=0 To CountVertices(s)-1
  175.                 WriteLine file,"                        "+VertexU(s,n)+" "+VertexV(s,n)+","
  176.         Next
  177. End Function
  178.  
  179.  
  180. ;;; <summary>outputs vrml fragment for textures</summary>
  181. ;;; <param name="file"></param>
  182. ;;; <param name="name"></param>
  183. ;;; <remarks></remarks>
  184. ;;; <returns></returns>
  185. ;;; <subsystem>vrml</subsystem>
  186. ;;; <example></example>
  187. Function vrml_tex(file,name$)
  188.         o=file
  189.         WriteLine o,"           Texture2 {"
  190.         WriteLine o,"                   filename "+Chr$(34)+name$+Chr$(34)
  191.         WriteLine o,"                   wrapS REPEAT"
  192.         WriteLine o,"                   wrapT REPEAT"
  193.         WriteLine o,"           }"
  194.         WriteLine o,"           Texture2Transform {"
  195.         WriteLine o,"                   rotation     0"
  196.         WriteLine o,"                   center       0 0"
  197.         WriteLine o,"                   translation  0 0"
  198.         WriteLine o,"                   scaleFactor  1 1"
  199.         WriteLine o,"           }"
  200. End Function
  201.  
  202. ;#End Region


Comments :


Mr Snidesmin(Posted 1+ years ago)

 This is genius. I use it loads :O)Thanks Braneloc! :O)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal