Author Topic: MD3 Model Loader (I need help!!!)  (Read 2874 times)

0 Members and 1 Guest are viewing this topic.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
MD3 Model Loader (I need help!!!)
« on: June 14, 2020, 11:58:16 am »
Hi folks,

So as i've got .MDL (Quake) and .MD2 (Quake II) model loaders I figured i'd move on up and do Quake III...it was all going well until i tried to use my triangle indices! I got a "Subscript out of range" error and quite rightly too as i dont think there are 10 BILLION vertices! (In my current model there is about 700'ish).

So, as i've tried everything i can think off and to no avail i need a fresh set of eyes...

Code: QB64: [Select]
  1. '// MD3 Model Loader v.01
  2. '// By John Onyon a.k.a Unseen Machine
  3.  
  4. CONST True = -1, False = 0 '// Generic valuations
  5.  
  6. DIM SHARED InitGL AS _BYTE, AllowGL AS _BYTE '// Generic OpenGL control variables.
  7. DIM Debug AS _BYTE '// Debug mode
  8.  
  9. '// Set up screen
  10. SCREEN _NEWIMAGE(800, 600, 32)
  11. _FPS 30
  12.  
  13. '// Flags
  14. InitGL = True
  15. Debug = True
  16.  
  17.  
  18. '// Model file information
  19. File$ = "3d programs\md3\Vehicle.md3"
  20.  
  21. '// Create the model header
  22. DIM Model AS MD3
  23.  
  24. '// Get model header data - Gives sizes of needed arrays.
  25. MD3_Load Model, File$
  26.  
  27. '// DIM containers
  28. DIM MDL_Frame(Model.NumFrames - 1) AS MD3_Frame
  29. DIM MDL_Surface(Model.NumSurfaces - 1) AS MD3_Surface
  30.  
  31. '// Open the file
  32. FFile% = FREEFILE
  33. OPEN File$ FOR BINARY AS #FFile%
  34.  
  35. '// Load frame data
  36. BytePos& = Model.FrameOffset + 1
  37. FOR i% = 0 TO Model.NumFrames - 1
  38.  GET #FFile%, BytePos&, MDL_Frame(i%)
  39.  BytePos& = BytePos& + LEN(MDL_Frame(0))
  40.  
  41. '// Load surface (mesh) data
  42. BytePos& = Model.SurfaceOffset + 1
  43. FOR i% = 0 TO Model.NumSurfaces - 1
  44.  GET #FFile%, BytePos&, MDL_Surface(i%)
  45.  NumTris& = NumTris& + MDL_Surface(i%).Triangles '// Count how many triangles in total
  46.  BytePos& = BytePos& + MDL_Surface(i%).EndOffset
  47.  
  48. '// Load triangle data - each surface has it's own set of triangles!!! Arghh!!!
  49. '// Not actual coordinates, these are indexes into the Vertex array.
  50. DIM MDL_Tris(NumTris& - 1) AS MD3_Triangle '// Triangle index array
  51. BytePos& = Model.SurfaceOffset + MDL_Surface(0).TriangleOffset + 1
  52. FOR i% = 0 TO Model.NumSurfaces - 1
  53.  FOR j& = 0 TO MDL_Surface(i%).Triangles - 1
  54.   GET #FFile%, BytePos&, MDL_Tris(Tcnt&)
  55.   BytePos& = BytePos& + LEN(MDL_Tris(0))
  56.   Tcnt& = Tcnt& + 1
  57.  
  58. '// Load vertex data
  59. FOR i% = 0 TO Model.NumSurfaces - 1
  60.  TotalVerts& = TotalVerts& + (MDL_Surface(i%).Frames * MDL_Surface(i%).Verts) '// Calculate total array size needed
  61. DIM MDL_Verts(TotalVerts& - 1) AS MD3_Vertex '// Vertex array (due to scaling this need dividing by 64)
  62. BytePos& = Model.SurfaceOffset + MDL_Surface(0).VertOffset + 1
  63. FOR i& = 0 TO TotalVerts& - 1
  64.  GET #FFile%, BytePos&, MDL_Verts(i&)
  65.  BytePos& = BytePos& + LEN(MDL_Verts(0))
  66.  
  67. '// Load texture coordinates
  68.  
  69.  
  70. '// All data should be loaded so close the model file.
  71. CLOSE #FFile%
  72.  
  73. '// Calculate all array sizes and create _MEM block
  74.  
  75.  
  76.  
  77.  
  78. '/////// Main Loop \\\\\\\
  79.  _LIMIT 30
  80.  CLS
  81.  
  82.  IF Debug THEN
  83.   PRINT Model.ID, Model.Ident
  84.   PRINT Model.QPath
  85.   PRINT Model.NumFrames, Model.NumTags, Model.NumSurfaces, Model.NumSkins
  86.   PRINT Model.FrameOffset, Model.TagsOffset, Model.SurfaceOffset
  87.   FOR i% = 0 TO Model.NumFrames - 1
  88.    PRINT "Frame : ", i%
  89.    PRINT MDL_Frame(i%).Min_Bounds.X, MDL_Frame(i%).Min_Bounds.Y, MDL_Frame(i%).Min_Bounds.Z
  90.    PRINT MDL_Frame(i%).Max_Bounds.X, MDL_Frame(i%).Max_Bounds.Y, MDL_Frame(i%).Max_Bounds.Z
  91.    PRINT "Name : ", MDL_Frame(i%).Name
  92.   NEXT
  93.   PRINT "Surfaces : "; Model.NumSurfaces
  94.   FOR i% = 0 TO Model.NumSurfaces - 1
  95.    PRINT MDL_Surface(i%).ID, MDL_Surface(i%).Name, MDL_Surface(i%).Triangles
  96.    PRINT
  97.   NEXT
  98.   PRINT "Total triangles : "; Tcnt&
  99.   PRINT "Total vertices : "; TotalVerts&
  100.  
  101.   FOR j& = 0 TO Tcnt& - 1
  102.    'PRINT MDL_Tris(j&).Vert1, MDL_Tris(j&).Vert2, MDL_Tris(j&).Vert3
  103.   NEXT
  104.  
  105.   FOR i& = 0 TO TotalVerts& - 1
  106.    ' PRINT MDL_Verts(i&).X / 64, MDL_Verts(i&).Y / 64, MDL_Verts(i&).Z / 64
  107.    ' IF MDL_Verts(i&).Y THEN PRINT MDL_Verts(i&).Y / 64
  108.   NEXT
  109.  
  110.  
  111.  
  112. '//////////////////////////////////
  113.  
  114. TYPE MD3
  115.  ID AS STRING * 4
  116.  Ident AS LONG
  117.  QPath AS STRING * 64
  118.  Flags AS LONG
  119.  NumFrames AS LONG
  120.  NumTags AS LONG
  121.  NumSurfaces AS LONG
  122.  NumSkins AS LONG
  123.  FrameOffset AS LONG
  124.  TagsOffset AS LONG
  125.  SurfaceOffset AS LONG
  126.  EOFOffset AS LONG
  127.  
  128. TYPE MD3_Vector
  129.  
  130. TYPE MD3_Frame
  131.  Min_Bounds AS MD3_Vector
  132.  Max_Bounds AS MD3_Vector
  133.  Local_Origin AS MD3_Vector
  134.  Radius AS _FLOAT
  135.  Name AS STRING * 16
  136.  
  137. TYPE MD3_Tag
  138.  Name AS STRING * 64
  139.  Origin AS MD3_Vector
  140.  Axis1 AS MD3_Vector
  141.  Axis2 AS MD3_Vector
  142.  Axis3 AS MD3_Vector
  143.  
  144. TYPE MD3_Surface
  145.  ID AS STRING * 4
  146.  Name AS STRING * 64
  147.  Flags AS LONG
  148.  Frames AS LONG
  149.  Shaders AS LONG
  150.  Verts AS LONG
  151.  Triangles AS LONG
  152.  TriangleOffset AS LONG
  153.  ShaderOffset AS LONG
  154.  TextureOffset AS LONG
  155.  VertOffset AS LONG
  156.  EndOffset AS LONG
  157.  
  158. TYPE MD3_Vertex
  159.  Normal AS INTEGER
  160.  
  161. TYPE MD3_TexCoord
  162.  
  163. TYPE MD3_Triangle
  164.  Vert1 AS LONG
  165.  Vert2 AS LONG
  166.  Vert3 AS LONG
  167.  
  168. TYPE MD3_Shader
  169.  Name AS STRING * 64
  170.  Index AS LONG
  171.  
  172. '//////////////////////////////////
  173.  
  174. SUB MD3_Load (MDL AS MD3, File$)
  175.  FFile% = FREEFILE
  176.  OPEN File$ FOR BINARY AS #FFile%
  177.  GET #FFile%, 1, MDL
  178.  CLOSE FFile%
  179.  
  180. '//////////////////////////////////
  181.  
  182. SUB _GL
  183.  IF InitGL THEN
  184.   InitGL = False
  185.   AllowGL = True
  186.   IF AllowGL THEN
  187.    '//RENDER
  188.   END IF
  189.  
  190.  

Please note the File$ reference at the start of the code (change to where you put the model).

Any ideas on where i might be going wrong would be great!

Thanks

Unseen


Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: MD3 Model Loader (I (no longer) need help!!!)
« Reply #1 on: June 14, 2020, 04:37:33 pm »
I've sorted it, seems the md3 file i was using was a bit weird, thanks though forlks.

@Admin - You can delete this thread...

Unseen