瀏覽代碼

Fixed assimp loader hierarchy/bone bug + cleaned up tests.

Mark Sibly 7 年之前
父節點
當前提交
f6cff57bd3

+ 40 - 41
modules/mojo3d-loaders/loaders/assimp.monkey2

@@ -28,7 +28,9 @@ Struct aiMatrix4x4 Extension
 	End
 	End
 	
 	
 	Operator To:AffineMat4f()
 	Operator To:AffineMat4f()
-		If d1<>0 Or d2<>0 Or d3<>0 Or d4<>1 Print "WARNING! Assimp node matrix is not affine!"
+	
+'		If d1<>0 Or d2<>0 Or d3<>0 Or d4<>1 Print "WARNING! Assimp node matrix is not affine! d1="+d1+", d2="+d2+", d3="+d3+", d4="+d4
+			
 		Return New AffineMat4f(
 		Return New AffineMat4f(
 			New Vec3f( a1,b1,c1 ),
 			New Vec3f( a1,b1,c1 ),
 			New Vec3f( a2,b2,c2 ),
 			New Vec3f( a2,b2,c2 ),
@@ -96,17 +98,11 @@ Class AssimpLoader
 	
 	
 	Method LoadBonedModel:Model()
 	Method LoadBonedModel:Model()
 		
 		
-'		#rem
 		Local model:=New Model
 		Local model:=New Model
 
 
-'		_nodes[""]=model
-'		_entityIds[""]=_entities.Length
-'		_entities.Add( model )
-		
-		LoadNode( _scene.mRootNode,model )
-'		#end
+		CreateNodes( _scene.mRootNode,model )
 		
 		
-'		Local model:=LoadNode( _scene.mRootNode,Null )
+		LoadNodes( _scene.mRootNode )
 		
 		
 		LoadAnimator( model )
 		LoadAnimator( model )
 		
 		
@@ -165,18 +161,16 @@ Class AssimpLoader
 					bp[k]=i0+i
 					bp[k]=i0+i
 					Exit
 					Exit
 				Next
 				Next
+				
 				If k=4 Print "Too many vertex weights"
 				If k=4 Print "Too many vertex weights"
-
 			Next
 			Next
-		
+			
 			bones[i0+i].entity=_entities[ _entityIds[ aibone.mName.data ] ]
 			bones[i0+i].entity=_entities[ _entityIds[ aibone.mName.data ] ]
 			
 			
 			bones[i0+i].offset=Cast<AffineMat4f>( aibone.mOffsetMatrix )
 			bones[i0+i].offset=Cast<AffineMat4f>( aibone.mOffsetMatrix )
 		Next
 		Next
 		
 		
 		model.Bones=bones
 		model.Bones=bones
-		
-		'Print "bones.Length="+bones.Length
 	End
 	End
 	
 	
 	Method LoadMesh( aimesh:aiMesh,mesh:Mesh,model:Model,boned:bool )
 	Method LoadMesh( aimesh:aiMesh,mesh:Mesh,model:Model,boned:bool )
@@ -198,11 +192,6 @@ Class AssimpLoader
 			
 			
 			If cp
 			If cp
 				Local color:=cp[i]
 				Local color:=cp[i]
-				Print "r="+color.r+", g="+color.g+", b="+color.b+", a="+color.a
-				color.r=1
-				color.g=1
-				color.b=0
-				color.a=1
 				Local a:=color.a * 255.0
 				Local a:=color.a * 255.0
 				vertices[i].color=UInt(a) Shl 24 | UInt(color.b*a) Shl 16 | UInt(color.g*a) Shl 8 | UInt(color.r*a)
 				vertices[i].color=UInt(a) Shl 24 | UInt(color.b*a) Shl 16 | UInt(color.g*a) Shl 8 | UInt(color.r*a)
 			Endif
 			Endif
@@ -283,30 +272,25 @@ Class AssimpLoader
 		Next
 		Next
 	End
 	End
 	
 	
-	Method LoadNode:Model( node:aiNode,parent:Model )
-		
+	Method CreateNodes( node:aiNode,parent:Model )
+
 		Local model:=New Model( parent )
 		Local model:=New Model( parent )
-		
 		model.Name=node.mName.data
 		model.Name=node.mName.data
+		model.LocalMatrix=Cast<AffineMat4f>( node.mTransformation )
 		
 		
-		Local matrix:=Cast<AffineMat4f>( node.mTransformation )
-
-		Local scl:=matrix.m.GetScaling()
-		Local rot:=matrix.m.Scale( 1/scl.x,1/scl.y,1/scl.z )
-		Local pos:=matrix.t
-
-		model.LocalPosition=pos
-		model.LocalBasis=rot
-		model.LocalScale=scl
-		
-		_nodes[ node.mName.data ]=model
-		_entityIds[ node.mName.data ]=_entities.Length
+		_nodes[ model.Name ]=model
+		_entityIds[ model.Name ]=_entities.Length
 		_entities.Push( model )
 		_entities.Push( model )
 		
 		
 		For Local i:=0 Until node.mNumChildren
 		For Local i:=0 Until node.mNumChildren
 			
 			
-			LoadNode( node.mChildren[i],model )
+			CreateNodes( node.mChildren[i],model )
 		Next
 		Next
+	End
+	
+	Method LoadNodes( node:aiNode )
+		
+		Local model:=Cast<Model>( _nodes[ node.mName.data ] )
 		
 		
 		Local mesh:=New Mesh
 		Local mesh:=New Mesh
 		
 		
@@ -318,9 +302,11 @@ Class AssimpLoader
 			
 			
 			mesh.AddMaterials( 1 )
 			mesh.AddMaterials( 1 )
 			
 			
-			LoadMesh( aimesh,mesh,model,aimesh.mNumBones>0 )
+			Local boned:=aimesh.mNumBones>0
+			
+			LoadMesh( aimesh,mesh,model,boned )
 			
 			
-			materials.Push( LoadMaterial( aimesh,aimesh.mNumBones>0 ) )
+			materials.Push( LoadMaterial( aimesh,boned ) )
 		Next
 		Next
 		
 		
 		If materials.Length
 		If materials.Length
@@ -328,8 +314,12 @@ Class AssimpLoader
 			model.Mesh=mesh
 			model.Mesh=mesh
 			model.Materials=materials.ToArray()
 			model.Materials=materials.ToArray()
 		Endif
 		Endif
+		
+		For Local i:=0 Until node.mNumChildren
 			
 			
-		Return model
+			LoadNodes( node.mChildren[i] )
+		Next
+		
 	End
 	End
 	
 	
 	Method LoadAnimationChannel:AnimationChannel( aichan:aiNodeAnim )
 	Method LoadAnimationChannel:AnimationChannel( aichan:aiNodeAnim )
@@ -383,9 +373,10 @@ Class AssimpLoader
 			channels[id]=channel
 			channels[id]=channel
 			
 			
 '			Print "channel "+id+", numposkeys="+channel.PositionKeys.Length+", numrotkeys="+channel.RotationKeys.Length+", numsclkeys="+channel.ScaleKeys.Length
 '			Print "channel "+id+", numposkeys="+channel.PositionKeys.Length+", numrotkeys="+channel.RotationKeys.Length+", numsclkeys="+channel.ScaleKeys.Length
-		
 		Next
 		Next
 		
 		
+'		Print "duration="+aianim.mDuration+", mTicksPerSecond="+aianim.mTicksPerSecond
+		
 		Local animation:=New Animation( aianim.mName.data,channels,aianim.mDuration,aianim.mTicksPerSecond,AnimationMode.Looping )
 		Local animation:=New Animation( aianim.mName.data,channels,aianim.mDuration,aianim.mTicksPerSecond,AnimationMode.Looping )
 		
 		
 		Return animation
 		Return animation
@@ -393,6 +384,8 @@ Class AssimpLoader
 	
 	
 	Method LoadAnimator:Animator( entity:Entity )
 	Method LoadAnimator:Animator( entity:Entity )
 		
 		
+'		Print "mNumAnimations="+_scene.mNumAnimations
+		
 		If Not _scene.mNumAnimations Return Null
 		If Not _scene.mNumAnimations Return Null
 		
 		
 		Local animations:=New Animation[_scene.mNumAnimations]
 		Local animations:=New Animation[_scene.mNumAnimations]
@@ -476,9 +469,11 @@ Class AssimpMojo3dLoader Extends Mojo3dLoader
 '		flags|=aiProcess_GenSmoothNormals | aiProcess_FixInfacingNormals | aiProcess_Triangulate
 '		flags|=aiProcess_GenSmoothNormals | aiProcess_FixInfacingNormals | aiProcess_Triangulate
 		flags|=aiProcess_GenSmoothNormals |aiProcess_Triangulate
 		flags|=aiProcess_GenSmoothNormals |aiProcess_Triangulate
 '		flags|=aiProcess_SplitByBoneCount
 '		flags|=aiProcess_SplitByBoneCount
-		flags|=aiProcess_LimitBoneWeights
-		flags|=aiProcess_FindInvalidData
-		flags|=aiProcess_OptimizeMeshes
+
+'		flags|=aiProcess_LimitBoneWeights
+'		flags|=aiProcess_FindInvalidData
+'		flags|=aiProcess_OptimizeMeshes
+
 '		flags|=aiProcess_OptimizeGraph	'fails quite spectacularly!
 '		flags|=aiProcess_OptimizeGraph	'fails quite spectacularly!
 		
 		
 		Local scene:=LoadScene( path,flags )
 		Local scene:=LoadScene( path,flags )
@@ -498,9 +493,13 @@ Class AssimpMojo3dLoader Extends Mojo3dLoader
 		flags|=aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_FlipUVs
 		flags|=aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_FlipUVs
 		'flags|=aiProcess_JoinIdenticalVertices | aiProcess_RemoveRedundantMaterials | aiProcess_FindDegenerates | aiProcess_SortByPType
 		'flags|=aiProcess_JoinIdenticalVertices | aiProcess_RemoveRedundantMaterials | aiProcess_FindDegenerates | aiProcess_SortByPType
 		flags|=aiProcess_JoinIdenticalVertices | aiProcess_RemoveRedundantMaterials | aiProcess_SortByPType
 		flags|=aiProcess_JoinIdenticalVertices | aiProcess_RemoveRedundantMaterials | aiProcess_SortByPType
+		
 '		flags|=aiProcess_GenSmoothNormals | aiProcess_FixInfacingNormals | aiProcess_Triangulate
 '		flags|=aiProcess_GenSmoothNormals | aiProcess_FixInfacingNormals | aiProcess_Triangulate
+
 		flags|=aiProcess_GenSmoothNormals |aiProcess_Triangulate
 		flags|=aiProcess_GenSmoothNormals |aiProcess_Triangulate
+
 '		flags|=aiProcess_SplitByBoneCount
 '		flags|=aiProcess_SplitByBoneCount
+
 		flags|=aiProcess_LimitBoneWeights
 		flags|=aiProcess_LimitBoneWeights
 		flags|=aiProcess_FindInvalidData
 		flags|=aiProcess_FindInvalidData
 		flags|=aiProcess_OptimizeMeshes
 		flags|=aiProcess_OptimizeMeshes

+ 3 - 5
modules/mojo3d-loaders/tests/castle.monkey2

@@ -38,7 +38,7 @@ Class MyWindow Extends Window
 		
 		
 		'create camera
 		'create camera
 		'
 		'
-		_camera=New Camera
+		_camera=New Camera( Self )
 		_camera.Near=.1
 		_camera.Near=.1
 		_camera.Far=100
 		_camera.Far=100
 		_camera.Move( 0,10,-10 )
 		_camera.Move( 0,10,-10 )
@@ -71,11 +71,9 @@ Class MyWindow Extends Window
 		
 		
 		_scene.Update()
 		_scene.Update()
 		
 		
-		_scene.Render( canvas,_camera )
+		_camera.Render( canvas )
 
 
-		canvas.Scale( Width/640.0,Height/480.0 )
-		
-		canvas.DrawText( "Width="+Width+", Height="+Height+", FPS="+App.FPS,0,0 )
+		canvas.DrawText( "FPS="+App.FPS,1,0,Width,0 )
 	End
 	End
 	
 	
 End
 End

+ 2 - 2
modules/mojo3d-loaders/tests/ninja.monkey2

@@ -37,7 +37,7 @@ Class MyWindow Extends Window
 		
 		
 		'create camera
 		'create camera
 		'
 		'
-		_camera=New Camera
+		_camera=New Camera( Self )
 		_camera.AddComponent<FlyBehaviour>()
 		_camera.AddComponent<FlyBehaviour>()
 		_camera.Near=.1
 		_camera.Near=.1
 		_camera.Far=100
 		_camera.Far=100
@@ -97,7 +97,7 @@ Class MyWindow Extends Window
 	
 	
 		_scene.Update()
 		_scene.Update()
 		
 		
-		_scene.Render( canvas,_camera )
+		_camera.Render( canvas )
 		
 		
 		canvas.Scale( Width/640.0,Height/480.0 )
 		canvas.Scale( Width/640.0,Height/480.0 )
 		
 		

+ 3 - 9
modules/mojo3d-loaders/tests/turtle.monkey2

@@ -31,19 +31,13 @@ Class MyWindow Extends Window
 
 
 		Super.New( title,width,height,flags )
 		Super.New( title,width,height,flags )
 		
 		
-		Print opengl.glGetString( opengl.GL_VERSION )
-		
-		SwapInterval=0
-		
 		'create scene
 		'create scene
 		'		
 		'		
 		_scene=Scene.GetCurrent()
 		_scene=Scene.GetCurrent()
 		
 		
-		_scene.SkyTexture=Texture.Load( "asset::miramar-skybox.jpg",TextureFlags.FilterMipmap|TextureFlags.Cubemap )
-		
 		'create camera
 		'create camera
 		'
 		'
-		_camera=New Camera
+		_camera=New Camera( Self )
 		_camera.Near=.1
 		_camera.Near=.1
 		_camera.Far=100
 		_camera.Far=100
 		_camera.Move( 0,10,-20 )
 		_camera.Move( 0,10,-20 )
@@ -84,7 +78,7 @@ Class MyWindow Extends Window
 			
 			
 		Next
 		Next
 		
 		
-		_turtle.Animator.Animate( "Walk",1 )
+		_turtle.Animator.Animate( "Walk" )
 	End
 	End
 		
 		
 	Method OnRender( canvas:Canvas ) Override
 	Method OnRender( canvas:Canvas ) Override
@@ -93,7 +87,7 @@ Class MyWindow Extends Window
 
 
 		_scene.Update()
 		_scene.Update()
 		
 		
-		_scene.Render( canvas,_camera )
+		_camera.Render( canvas )
 		
 		
 		canvas.Scale( Width/640.0,Height/480.0 )
 		canvas.Scale( Width/640.0,Height/480.0 )