So I'm trying to load an MD2 model here, but for some reason the texture is severely warped. I have tried multiple .md2's to no avail. With certain models, I also get errors relating to array length. Taking a look at the data read from the header, some of the numbers are outright incorrect! I've been at this problem for a week at this point. Here's my code (Formatting might be messed up):
Const GAMEWIDTH:Int = 640
Const GAMEHEIGHT:Int = 480
AppTitle = "YOUR MOTHER WAS A HAMSTER AND YOUR FATHER SMELLS OF ELDERBERRIES!"
GLGraphics(GAMEWIDTH, GAMEHEIGHT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glViewport(0, 0, GAMEWIDTH, GAMEHEIGHT)
gluPerspective(75.0, Float(GAMEWIDTH)/Float(GAMEHEIGHT), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
Type TVector3
Field x:Float
Field y:Float
Field z:Float
Field cx:Byte
Field cy:Byte
Field cz:Byte
End Type
Type TVector2
Field x:Float
Field y:Float
End Type
Type TFace
Field vIndices:Int[]
Field tIndices:Int[]
End Type
Type MD2Frame
Field scale:Float[]
Field translate:Float[]
Field name:String
Field vertices:TVector3[]
End Type
Local mrb:Int[] = LoadMD2("mrb.md2")
Local mrb_texture:Int = LoadTexture("mr_b_texture.png")
Local rotation:Float = 0.0
While Not AppTerminate() And Not KeyDown(KEY_ESCAPE)
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
glLoadIdentity()
glRotatef rotation, 0, 1, 0
glTranslatef 0.0, -2.0, -16.0
rotation :+ KeyDown(KEY_D) - KeyDown(KEY_A)
glPushMatrix()
glRotatef -90.0, 1, 0, 0
glRotatef 90.0, 0, 0, 1
glTranslatef 0.0, 0.0, 0.0
glScalef 1.5, 1.5, 1.5
glBindTexture GL_TEXTURE_2D, mrb_texture
glCallList(mrb[0])
glLoadIdentity()
glPopMatrix()
glBegin(GL_QUADS)
glTexCoord2f 0.0, 1.0; glVertex3f 0.0, 0.0, 12.0
glTexCoord2f 1.0, 1.0; glVertex3f 1.0, 0.0, 12.0
glTexCoord2f 1.0, 0.0; glVertex3f 1.0, 1.0, 12.0
glTexCoord2f 0.0, 0.0; glVertex3f 0.0, 1.0, 12.0
glEnd()
Flip -1
Wend
Function LoadTexture:Int(f:String)
Local Texture:TPixmap = LoadPixmap(f)
Local TextureImage:Int = GLTexFromPixmap(Texture, False)
glBindTexture GL_TEXTURE_2D, TextureImage
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
Return TextureImage
End Function
Function LoadMD2:Int[](path:String)
Local file:TStream = ReadFile(path)
Local displayLists:Int[]
Local magicNumber:Int = ReadInt(file)
Local version:Int = ReadInt(file)
If magicNumber <> 844121161 Or version <> 8 Then
Print "YA DONE GOOFED, SON!"
Return Null
EndIf
'Header
Local skinWidth:Int = ReadInt(file)
Local skinHeight:Int = ReadInt(file)
Local frameSize:Int = ReadInt(file)
Local skinCount:Int = ReadInt(file)
Local vertexCount:Int = ReadInt(file)
Local texCoordCount:Int = ReadInt(file)
Local triangleCount:Int = ReadInt(file)
Local glCommandCount:Int = ReadInt(file)
Local frameCount:Int = ReadInt(file)
Local offsetSkins:Int = ReadInt(file)
Local offsetTexCoords:Int = ReadInt(file)
Local offsetTriangles:Int = ReadInt(file)
Local offsetFrames:Int = ReadInt(file)
Local offsetGLCommands:Int = ReadInt(file)
Local offsetEnd:Int = ReadInt(file)
Local texCoords:TVector2[] = New TVector2[texCoordCount]
Local frames:MD2Frame[] = New MD2Frame[frameCount]
Local triangles:TFace[] = New TFace[triangleCount]
SeekStream(file, offsetTexCoords)
For Local i:Int = 0 To texCoordCount - 1
Local s:Short = ReadShort(file)
Local t:Short = ReadShort(file)
Local tc:TVector2 = New TVector2
'tc.x = 0
'tc.y = 0
tc.x = Float(s)/Float(skinWidth)
tc.y = (Float(s)/Float(skinHeight))
texCoords[i] = tc
Next
SeekStream(file, offsetTriangles)
For i = 0 To triangleCount - 1
triangles[i] = New TFace
triangles[i].vIndices = [Int(ReadShort(file)), Int(ReadShort(file)), Int(ReadShort(file))]
triangles[i].tIndices = [Int(ReadShort(file)), Int(ReadShort(file)), Int(ReadShort(file))]
Next
SeekStream(file, offsetFrames)
For i = 0 To frameCount - 1
frames[i] = New MD2Frame
frames[i].scale = [ReadFloat(file), ReadFloat(file), ReadFloat(file)]
frames[i].translate = [ReadFloat(file), ReadFloat(file), ReadFloat(file)]
For Local j:Int = 0 To 15
ReadByte(file)
Next
frames[i].vertices = New TVector3[vertexCount]
For j = 0 To vertexCount - 1
frames[i].vertices[j] = New TVector3
Local cx:Byte = ReadByte(file)
Local cy:Byte = ReadByte(file)
Local cz:Byte = ReadByte(file)
frames[i].vertices[j].x = (Float(cx)*frames[i].scale[0]) + frames[i].translate[0]
frames[i].vertices[j].y = (Float(cy)*frames[i].scale[1]) + frames[i].translate[1]
frames[i].vertices[j].z = (Float(cz)*frames[i].scale[2]) + frames[i].translate[2]
ReadByte(file) 'Light normal index
Next
Next
CloseStream(file)
'Generate displaylists for each frame
displayLists = New Int[frameCount]
For i = 0 To frameCount - 1
Local dl:Int = glGenLists(1)
glNewList(dl, GL_COMPILE)
glBegin GL_TRIANGLES
For j = 0 To triangleCount - 1
For Local k:Int = 0 To 2
glTexCoord2f(texCoords[triangles[j].tIndices[2-k]].x, texCoords[triangles[j].tIndices[2-k]].y)
glVertex3f(frames[i].vertices[triangles[j].vIndices[2-k]].x, frames[i].vertices[triangles[j].vIndices[2-k]].y, frames[i].vertices[triangles[j].vIndices[2-k]].z)
Next
Next
glEnd
glEndList()
displayLists[i] = dl
Next
Return displayLists
End Function
I'd appreciate any sort of lead on this problem.
Click here to download my project w/ the model in question
|