Quake MD2 Files

Advanced game related topics
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Quake MD2 Files

Post by DarkDragon »

Hello,

I started to load a MD2File, but I get wrong values in the Triangle(maybe) and in the Vertexdata:

Code: Select all

;1 short = 2bytes = 1 word

Structure tMd2Header
   magic.l;               // This is used to identify the file
   version.l;            // The version number of the file (Must be 8)
   skinWidth.l;            // The skin width in pixels
   skinHeight.l;            // The skin height in pixels
   frameSize.l;            // The size in bytes the frames are
   numSkins.l;            // The number of skins associated with the model
   numVertices.l;            // The number of vertices (constant for each frame)
   numTexCoords.l;         // The number of texture coordinates
   numTriangles.l;         // The number of faces (polygons)
   numGlCommands.l;         // The number of gl commands
   numFrames.l;            // The number of animation frames
   offsetSkins.l;            // The offset in the file for the skin data
   offsetTexCoords.l;         // The offset in the file for the texture data
   offsetTriangles.l;         // The offset in the file for the face data
   offsetFrames.l;         // The offset in the file for the frames data
   offsetGlCommands.l;            // The offset in the file for the gl commands data
   offsetEnd.l;            // The end of the file offset
EndStructure

Structure Triangles
  vert.w[3]
  tex.w[3]
EndStructure

Structure dtrivertx_t
         v.b[3]
         lightnormalindex.b
EndStructure

Structure aliasframe
         scale.f[3]
         translate.f[3]
         name.s[16]
         verts.dtrivertx_t[1]
EndStructure

Global m_Header.tMd2Header, m_Tri.Triangles, out.aliasframe

If OpenFile(0, "cube.MD2")
  ReadData(@m_Header, SizeOf(tMd2Header))
  FileSeek(m_Header\offsetSkins)
  Skin = AllocateMemory(0, 64000)
  ReadData(Skin, m_Header\numSkins * 64)
  Debug "Skin: "+PeekS(Skin)
  Dim TextureCoords.l(m_Header\numTexCoords)
  ReadData(@TextureCoords(), m_Header\numTexCoords * 4)
  Debug " "
  Debug " "
  Debug "Texturkoordinaten:"
  For i=0 To m_Header\numTexCoords
    Debug PeekW(@TextureCoords(i))
    Debug PeekW(@TextureCoords(i)+2)
    Debug "---"
  Next
  FileSeek(m_Header\offsetTriangles)
  Debug " "
  Debug " "
  Debug "Triangles:"
  For i = 0 To m_Header\numTriangles-1
    ReadData(m_Tri, SizeOf(Triangles))
    Debug m_Tri\vert[0]
    Debug m_Tri\vert[1]
    Debug m_Tri\vert[2]
    Debug "---"
  Next
  FileSeek(m_Header\offsetFrames)
  Debug " "
  Debug " "
  Debug "Vertexdata:"
  For i=0 To m_Header\numVertices
    ReadData(out, m_Header\frameSize)
    Debug out\scale[0]
    Debug out\verts\v[0]
    Debug out\verts\v[1]
    Debug out\verts\v[2]
    Debug "---"
  Next
  CloseFile(0)
EndIf
Can you help me?

PS: Test it with a democube :wink:
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Not that familiar with the file format but does the format of triangle data in the file match the structure as defined? Eg, maybe the file does not contain 3 sets of tex coords. I could be wrong and the file padded for additional (non-existent) data.
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Uhm one on the german board helped me a little bit, but it doesn't work yet.

Code: Select all

;-Strukturen
Structure MD2Header
   magic.l;               // This is used to identify the file
   version.l;            // The version number of the file (Must be 8)
   skinWidth.l;            // The skin width in pixels
   skinHeight.l;            // The skin height in pixels
   frameSize.l;            // The size in bytes the frames are
   numSkins.l;            // The number of skins associated with the model
   numVertices.l;            // The number of vertices (constant for each frame)
   numTexCoords.l;         // The number of texture coordinates
   numTriangles.l;         // The number of faces (polygons)
   numGlCommands.l;         // The number of gl commands
   numFrames.l;            // The number of animation frames
   offsetSkins.l;            // The offset in the file for the skin data
   offsetTexCoords.l;         // The offset in the file for the texture data
   offsetTriangles.l;         // The offset in the file for the face data
   offsetFrames.l;         // The offset in the file for the frames data
   offsetGlCommands.l;            // The offset in the file for the gl commands data
   offsetEnd.l;            // The end of the file offset
EndStructure


Structure MD2_vertex
    v.b[3]                                    ; Vertex 
    normal.b                                  ; Index of normal (into some lookup table. I can't find any info on this table, so normals And lighting aren't currently implemented.) 
EndStructure

Structure MD2_triangle
    xyz.w[3]                            ; Index of triangle vertices 
    st.w[3]                             ; Index of texture coordinates 
EndStructure

Structure MD2_frame
    scale.f[3]
    translate.f[3]                          ; Scaling And translation
    name.s                                  ; Display name
EndStructure


;-Listen und Globale Variablen
Global m_Header.MD2Header, m_Frame.MD2_frame
NewList m_Vertices.MD2_vertex()
NewList m_Tris.MD2_triangle()


;-Lesen der Datei
If OpenFile(0, "cube.MD2")

  ;-Header
  ReadData(@m_Header, SizeOf(MD2Header)) ;Header lesen
  
  
  ;-Skins
  FileSeek(m_Header\offsetSkins) ;Zu den Skins gehen
  Skin = AllocateMemory(0, 64000)
  ReadData(Skin, m_Header\numSkins * 64)
  Debug "Skin: "+PeekS(Skin)     ;Einen Skinname brauch ich nur
  
  ;-Texturkoordinaten
  Dim TextureCoords.l(m_Header\numTexCoords) ;Texturcoords lesen
  ReadData(@TextureCoords(), m_Header\numTexCoords * 2)
  Debug " "
  Debug " "
  Debug "Texturkoordinaten:"
  For i=0 To m_Header\numTexCoords
    Debug PeekW(@TextureCoords(i))
    Debug PeekW(@TextureCoords(i)+2)
    Debug "---"
  Next
  
  
  ;-Tris
  FileSeek(m_Header\offsetTriangles) ;Zu den Triangles gehen
  Debug " "
  Debug " "
  Debug "Triangles:"
  For i = 0 To m_Header\numTriangles-1
    AddElement(m_Tris())
    ReadData(m_Tris(), SizeOf(MD2_triangle)) ;Triangles in eine LinkedList speichern
    Debug m_Tris()\xyz[0] ;Vertexnummern
    Debug m_Tris()\xyz[1]
    Debug m_Tris()\xyz[2]
    Debug m_Tris()\st[0]  ;Texturkoordinatennummern
    Debug m_Tris()\st[1]
    Debug m_Tris()\st[2]
    Debug "---"
  Next
  
  
  ;-Frames(Verticekoordinaten)
  FileSeek(m_Header\offsetFrames) ;Zu den Frames gehen(darin werden die Vertexkoordinaten gespeichert)
  
  m_Frame\scale[0] = ReadFloat() ;Größe
  m_Frame\scale[1] = ReadFloat()
  m_Frame\scale[2] = ReadFloat()
  
  m_Frame\translate[0] = ReadFloat() ;Position
  m_Frame\translate[1] = ReadFloat()
  m_Frame\translate[2] = ReadFloat()
  
  m_Frame\name = Space(16)
  ReadData(m_Frame\name, 16) ;Framename lesen
  Debug " ";VertexDaten anzeigen
  Debug " "
  Debug "Vertexdata:"
  Debug "Framename = "+m_Frame\name
  Debug "Größe     = "+StrF(scale)
  Debug "Position  = "+StrF(translate)
  For i=0 To m_Header\numVertices
    For i1=0 To 2
      AddElement(m_Vertices())
      m_Vertices()\v[0] = ReadByte()
      m_Vertices()\v[1] = ReadByte()
      m_Vertices()\v[2] = ReadByte()
    Next
    m_Vertices()\normal = ReadByte()
    Debug m_Vertices()\v[0]
    Debug m_Vertices()\v[1]
    Debug m_Vertices()\v[2]
    Debug m_Vertices()\normal
    Debug "---"
  Next
  
  
  ;-Schließen der Datei
  CloseFile(0) ;Datei schließen
EndIf


XIncludeFile "GL.pbi"

hWnd = OpenWindow(0, 100, 100, 600, 480, #PB_Window_SystemMenu, "Test")

pfd.PIXELFORMATDESCRIPTOR   ;OpenGL starten
hDC = GetDC_(hWnd)
pfd\nSize        = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion     = 1
pfd\dwFlags      = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType   = #PFD_MAIN_PLANE
pfd\iPixelType   = #PFD_TYPE_RGBA
pfd\cColorBits   = 24
pfd\cDepthBits   = 32
pixformat = ChoosePixelFormat_(hDC, pfd)
SetPixelFormat_(hDC, pixformat, pfd)
hrc = wglCreateContext_(hDC)
wglMakeCurrent_(hDC, hrc)

hDC = GetDC_(hWnd)

AngleX.f
AngleY.f
AngleZ.f
X.f
Y.f
Z.f
piover180.f = (ATan(1)*4)/180

glEnable_(#GL_CULL_FACE)
glEnable_(#GL_DEPTH_TEST)

;-Hauptschleife
Repeat
  If AngleY <= 0
    AngleY + 360
  ElseIf AngleY > 360
    AngleY - 360
  EndIf
  glMatrixMode_(5889)
  glLoadIdentity_()
  gluPerspective__(FOV, WindowWidth()/WindowHeight(), 0.01, 20.0)
  glRotatef_(AngleX, 1.0, 0.0, 0.0)
  glRotatef_(AngleY, 0.0, 1.0, 0.0)
  glRotatef_(AngleZ, 0.0, 0.0, 1.0)
  glTranslatef_(X, Y, Z)
  glMatrixMode_(5888)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glLoadIdentity_()
  glScalef_(m_Frame\scale[0], m_Frame\scale[1], m_Frame\scale[2])
  glTranslatef_(m_Frame\translate[0], m_Frame\translate[1], m_Frame\translate[2])
  
  glBegin_(#GL_TRIANGLES)
  ResetList(m_Tris())
  While NextElement(m_Tris())
    SelectElement(m_Vertices(), m_Tris()\xyz[0])
    glVertex3f_(ValF(StrF(m_Vertices()\v[0])), ValF(StrF(m_Vertices()\v[1])), ValF(StrF(m_Vertices()\v[2])))
    SelectElement(m_Vertices(), m_Tris()\xyz[1])
    glVertex3f_(ValF(StrF(m_Vertices()\v[0])), ValF(StrF(m_Vertices()\v[1])), ValF(StrF(m_Vertices()\v[2])))
    SelectElement(m_Vertices(), m_Tris()\xyz[2])
    glVertex3f_(ValF(StrF(m_Vertices()\v[0])), ValF(StrF(m_Vertices()\v[1])), ValF(StrF(m_Vertices()\v[2])))
  Wend
  glEnd_()
  
  SwapBuffers_(hDC)
  Event = WindowEvent()
  
  Select Event
    Case #WM_KEYDOWN
      Select EventwParam()
        Case #VK_NEXT
          AngleX + 1
        Case #VK_PRIOR
          AngleX - 1
        Case #VK_UP
          Z + Cos(AngleY*piover180)*0.125
          X - Sin(AngleY*piover180)*0.125
        Case #VK_DOWN
          Z - Cos(AngleY*piover180)*0.125
          X + Sin(AngleY*piover180)*0.125
        Case #VK_RIGHT
          AngleY + 1
        Case #VK_LEFT
          AngleY - 1
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow
End
Post Reply