Small demo of Matrix/Bone Animation

Blitz3D Forums/Blitz3D Programming/Small demo of Matrix/Bone Animation

i have made a clear demo
Graphics3D 1024, 768, 32, 2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

;Camera
Global Cam = CreateCamera()
PositionEntity cam, 0, 1, 2
RotateEntity Cam, 0, 180, 0
CameraRange Cam, .1,1000
CameraClsColor Cam, 32, 64, 32

;Light
RotateEntity CreateLight(), 70, 20, 0



;Globs
Type mesh
	Field numverts, vert_bank
End Type
Type Local_verts
	Field bone
	Field loc_x#, loc_y#, loc_z#
End Type
Type tris
	Field vert1, vert2, vert3
End Type


f = ReadFile("gondor_spear.b3x")

n2$ = "skeleton_cloak.b3d"


mesh2 = LoadAnimMesh(n2$)
HideEntity mesh2
tex = LoadTexture(ReadString(f), ReadInt(f))
num_vert = ReadInt(f)
Dim verts#(num_vert, 5)
For aa = 0 To num_vert - 1
	verts(aa,1) = ReadFloat(f)
	verts(aa,2) = ReadFloat(f)
	verts(aa,3) = ReadFloat(f)
	verts(aa,4) = ReadFloat(f)
	verts(aa,5) = ReadFloat(f)
Next
num_tri = ReadInt(f)
Dim tris(num_tri, 3)
For aa = 0 To num_tri - 1
	tris(aa, 1) = ReadShort(f)
	tris(aa, 2) = ReadShort(f)
	tris(aa, 3) = ReadShort(f)
Next
mesh1 = CreateMesh()
surf1 = CreateSurface(mesh1)
For aa = 0 To num_vert - 1
	AddVertex(surf1, verts(aa, 1), verts(aa, 2), verts(aa, 3), verts(aa, 4), verts(aa, 5))
Next
For aa = 0 To num_tri - 1
	AddTriangle(surf1, tris(aa, 1), tris(aa, 2), tris(aa, 3))
Next
newmesh = CopyMesh(mesh1)
FreeEntity newmesh1
meshHeader = dllPeekInt(newmesh, 644)
surfListPtr = dllPeekInt(meshHeader, 80)
surf = dllPeekInt(surfListPtr, i * 4)
firstTriPtr = dllPeekInt(surf, 44)
triCount = (dllPeekInt(surf, 48) - firstTriPtr) / 6
EntityTexture newmesh, tex





Animate mesh2,1,1,1

frame_len# = AnimLength(mesh2) - 1

hip = FindChild(mesh2,"bone_pelvis ")

Global add






surf1 = GetSurface(mesh1, 1)
num_verts = CountVertices(surf1)


Dim pos#(num_verts, 3), loc_verts#(num_vert, 4), bones(50)
add = 0
While Not Eof(f)
	name2$ = ReadString(f)
	name = FindChild(mesh2, name2$)
	bones(add) = name
	add = add + 1
	num_weights = ReadShort(f)
	For vv = 1 To num_weights
		cc = ReadShort(f)
		loc_verts(cc, 0) = add - 1
	Next
Wend





nam.mesh = New mesh
For aa = 0 To tricount - 1
	pointer2.tris = New tris
	pointer2\vert1 = dllPeekShort(firstTriPtr,aa * 6)
	pointer2\vert2 = dllPeekShort(firstTriPtr,aa * 6 + 2)
	pointer2\vert3 = dllPeekShort(firstTriPtr,aa * 6 + 4)
Next
For aa = 0 To num_verts - 1
	vert.local_verts = New local_verts
	vert\bone = loc_verts(aa, 0)
	vert\loc_x = verts(aa, 1)
	vert\loc_y = verts(aa, 2)
	vert\loc_z = verts(aa, 3)
Next



Dim trans#(add, 16)



ClearSurface surf1

surf2 = GetSurface(newmesh, 1)

FreeEntity mesh1
anim = Default1


MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
FlushMouse()
FlushKeys()
While Not KeyHit(1)
	FreeLook(.025)
	SetAnimTime mesh2, MilliSecs() * .01, LoadAnimSeq(mesh2, "spear_idle3.b3d")
	For aaa = 0 To add - 1
		EntityY(bones(aaa), True)
	Next
	
	MatrixIdentity(mesh2)
	b = 0
	For a.local_verts = Each local_verts
		tt00# = trans(a\bone, 0)
		tt01# = trans(a\bone, 1)
		tt02# = trans(a\bone, 2)
		tt04# = trans(a\bone, 4)
		tt05# = trans(a\bone, 5)
		tt06# = trans(a\bone, 6)
		tt08# = trans(a\bone, 8)
		tt09# = trans(a\bone, 9)
		tt10# = trans(a\bone, 10)
		tt12# = trans(a\bone, 12)
		tt13# = trans(a\bone, 13)
		tt14# = trans(a\bone, 14)
		
		x# = a\loc_x * tt00# + a\loc_y * tt04# + a\loc_z * tt08# + tt12#
		y# = a\loc_x * tt01# + a\loc_y * tt05# + a\loc_z * tt09# + tt13#
		z# = a\loc_x * tt02# + a\loc_y * tt06# + a\loc_z * tt10# + tt14#
		VertexCoords surf2, b, x#, y#, z#
		b = b + 1
	Next
	pointer2.tris = First tris
	For i = 0 To num_tri - 1
		dllPokeShort(firstTriPtr, 0, pointer2\vert1)
		dllPokeShort(firsttriptr, 2, pointer2\vert2)
		dllPokeShort(firsttriptr, 4, pointer2\vert3)
		pointer2 = After pointer2
	Next
	RenderWorld
	Flip
Wend
End

Function MatrixIdentity(entity)
For i = 0 To add - 1
	bone = bones(i)
	trans(i, 0) = dllPeekFloat(bone, 176)
	trans(i, 1) = dllPeekFloat(bone, 180)
	trans(i, 2) = dllPeekFloat(bone, 184)
	trans(i, 4) = dllPeekFloat(bone, 188)
	trans(i, 5) = dllPeekFloat(bone, 192)
	trans(i, 6) = dllPeekFloat(bone, 196)
	trans(i, 8) = dllPeekFloat(bone, 200)
	trans(i, 9) = dllPeekFloat(bone, 204)
	trans(i, 10) = dllPeekFloat(bone, 208)
	trans(i, 12) = dllPeekFloat(bone, 212)
	trans(i, 13) = dllPeekFloat(bone, 216)
	trans(i, 14) = dllPeekFloat(bone, 220)
Next
End Function

Global CamXS#, CamZS#, CamRotXS#, CamRotYS#
Function FreeLook(sp# = .1)
If sp# > 0 Then
	CamXS# =(CamXS# +((KeyDown(32) Or KeyDown(205)) -(KeyDown(30) Or KeyDown(203))) * sp#) * .75
	CamZS# =(CamZS# +((KeyDown(17) Or KeyDown(200)) -(KeyDown(31) Or KeyDown(208))) * sp#) * .75
	MoveEntity Cam, CamXS#, 0, CamZS#
EndIf
CamRotXS# =((MouseXSpeed() - CamRotXS#) * .35 + CamRotXS#) * .75
CamRotYS# =((MouseYSpeed() - CamRotYS#) * .35 + CamRotYS#) * .75
If EntityPitch(Cam) + CamRotYS# < -85 pitch# = -85 ElseIf EntityPitch(Cam) + CamRotYS# > 85 pitch# = 85 Else pitch# = EntityPitch(Cam) + CamRotYS#
yaw# = -CamRotXS# + EntityYaw(Cam)
RotateEntity Cam, pitch#, yaw#, 0
MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
End Function