The 'Stencil shadow' thread

Blitz3D Forums/Blitz3D Programming/The 'Stencil shadow' thread

After playing around with the .md2 format I came up with a little program that accesses a single vertex. I checked it against the .md2 model that I created and it is accurate. The code can be adapted to read ALL of the vertices and triangles. Pardon the mess as I left in unused types (they WILL be used, however, if you are to read the entire .md2 file). The file has only minimal error checking and you have to provide your own model (I have the filename hard coded)


Graphics 800,600

Type model_t 
   Field magic; must be 844121161
   Field version; must be 8
   Field skinWidth; 
   Field skinHeight; 
   Field frameSize; 
   Field numSkins; 
   Field numVertices; number of vertices per frame
   Field numTexCoords; 
   Field numTriangles; 
   Field numGlCommands; 
   Field numFrames; 
   Field offsetSkins; 
   Field offsetTexCoords; 
   Field offsetTriangles; the position in bytes from the beginning of the file to the triangle data
   Field offsetFrames; 	the position in bytes from the beginning of the file to the frames data
   Field offsetGlCommands; 
   Field offsetEnd; the position in bytes from the beginning of the file to the end of the file
End Type

Type vertex
	Field x,y,z
End Type

Type triangleVertex_t
   Field vertex;
   Field lightNormalIndex;
End Type

Type scale
	Field x#,y#,z#
End Type

Type translate
	Field x#,y#,z#
End Type


Type frame_t
    Field  scale
    Field translate
    Field name$
    Field vertices.triangleVertex_t
End Type

Type vertexIndices
	Field x#,y#,z#
End Type

Type textureIndices
	Field x#,y#,z#
End Type

Type triangle_t
	Field vertexIndices
   	Field textureIndices
End Type

Type textureCoordinate_t
	Field s
	Field t
End Type

Type glCommandVertex_t
	Field s#
	Field t#
	Field vertexIndex
End Type

Type md2_skin_t
	Field name$
End Type

; dim vert#(3)
; vert(i)=(triangleVertex_t.vertex(i)*frame_t.scale(i))+frame_t.translate(i)
; The above is how to convert the scaled vertex position into it's real position


ReadMd2Model("temp.md2",model_t)
WaitKey()
End

Function ReadMD2Model(filename$,model_t)
file=OpenFile(filename$)
If Not file
	End
End If
header=CreateBank(15308*10) ; the 15308 is what I found in offsetEnd
amount=ReadBytes(header,file,0,15308)
If amount=0 Then
	End
End If
CloseFile file

skins_offset=PeekInt(header,44) ; offset for skins
num_skins=PeekInt(header,20) ; number of skins
num_tris=PeekInt(header,32) ; number of triangles
num_verts=PeekInt(header,24) ; number of vertices
tris_offset=PeekInt(header,52) ; offset for triangles
frame_offset=PeekInt(header,56) ; offset to frame start
num_frames=PeekInt(header,40) ; number of frames
frame_size=PeekInt(header,16) ; frame size
end_file=PeekInt(header,64) ; offset for end of file
scaled_x=PeekByte(header,frame_offset+24)
scaled_y=PeekByte(header,frame_offset+28)
scaled_z=PeekByte(header,frame_offset+32)
x_scale=PeekFloat(header,frame_offset)
y_scale=PeekFloat(header,frame_offset+4)
z_scale=PeekFloat(header,frame_offset+8)
x_trans=PeekFloat(header,frame_offset+12)
y_trans=PeekFloat(header,frame_offset+16)
z_trans=PeekFloat(header,frame_offset+20)
real_x=scaled_x*x_scale+x_trans
real_y=scaled_y*y_scale+y_trans
real_z=scaled_z*z_scale+z_trans
Print "Number of Vertices per frame: "+num_verts
Print "Size of each frame: "+frame_size
Print "X Scale: "+PeekFloat(header,frame_offset)
Print "Y Scale: "+PeekFloat(header,frame_offset+4)
Print "Z Scale: "+PeekFloat(header,frame_offset+8)
Print "X Translate: "+PeekFloat(header,frame_offset+12)
Print "Y Translate: "+PeekFloat(header,frame_offset+16)
Print "Z Translate: "+PeekFloat(header,frame_offset+20)
Print "First Vertex X Scaled Position: "+PeekByte(header,frame_offset+24)
Print "First Vertex Y Scaled Position: "+PeekByte(header,frame_offset+28)
Print "First Vertex Z Scaled Position: "+PeekByte(header,frame_offset+32)
Print "Real Position of First Vertex is: "+real_x+","+real_y+","+real_z
freeBank header
End Function


**** EDIT *******
Looking back at my code I see some minor errors such as not making some variables "float". The principle, however, remains the same. This code is free for any to use, by the way.
Kevin