Explorar el Código

Config settings and physics fixes.

Mark Sibly hace 7 años
padre
commit
673bb8ada4

+ 23 - 5
modules/bullet/bullet.monkey2

@@ -384,15 +384,15 @@ Class btTriangleIndexVertexArray Extends btStridingMeshInterface
 
 End
 
-Class btMotionState Extends btObject
-
-	Method setWorldTransform( worldTrans:btTransform )
+Class btMotionState Extends btObject="bbBullet::MotionState"
+	
+	Method setWorldTransform( worldTrans:btTransform Ptr ) Virtual
 		
-	Method getWorldTransform:btTransform() Extension="bbBullet::getWorldTransform"
+	Method getWorldTransform( worldTrans:btTransform Ptr ) Virtual
 	
 End
 
-Class btDefaultMotionState Extends btMotionState
+Class btDefaultMotionState Extends btMotionState="bbBullet::DefaultMotionState"
 
 	Field m_graphicsWorldTrans:btTransform
 	Field m_centerOfMassOffset:btTransform
@@ -420,6 +420,18 @@ Class btCollisionObject Extends btObject
 	Const CF_DISABLE_VISUALIZE_OBJECT:Int="btCollisionObject::CF_DISABLE_VISUALIZE_OBJECT"
 	Const CF_DISABLE_SPU_COLLISION_PROCESSING:Int="btCollisionObject::CF_DISABLE_SPU_COLLISION_PROCESSING"
 	
+	Method isStaticObject:Bool()
+		
+	Method isKinematicObject:Bool()
+		
+	Method isStaticOrKinematicObject:Bool()
+		
+	Method hasContactResponse:Bool()
+		
+	Method setCollisionShape( collisionShape:btCollisionShape )
+		
+	Method getCollisionShape:btCollisionShape()
+	
 	Method setWorldTransform( transform:btTransform )
 	
 	Method getWorldTransform:btTransform()
@@ -449,6 +461,8 @@ Class btCollisionObject Extends btObject
 	Method getCollisionFlags:Int()
 		
 	Method setActivationState( newState:Int )
+		
+	Method forceActivationState( newState:Int )
 	
 	Method getActivationState:Int()
 		
@@ -490,6 +504,10 @@ Class btRigidBody Extends btCollisionObject
 	
 	Method clearForces()
 		
+	Method setMassProps( mass:btScalar,inertia:btVector3 )
+		
+	Method getInvMass:btScalar()
+		
 	Method setGravity( acceleration:btVector3 )
 		
 	Method getGravity:btVector3()

+ 38 - 0
modules/bullet/bullet_glue.h

@@ -10,6 +10,44 @@ namespace bbBullet{
 	
 	btTransform getWorldTransform( btMotionState *self );
 	
+	struct MotionState : public btMotionState{
+		
+		virtual void setWorldTransform( btTransform *worldTrans ){
+		}
+
+		virtual void getWorldTransform( btTransform *worldTrans ){
+		}
+
+		virtual void setWorldTransform( const btTransform &worldTrans ){
+			
+			this->setWorldTransform( const_cast<btTransform*>( &worldTrans ) );
+		}
+				
+		virtual void getWorldTransform( btTransform &worldTrans )const{
+			
+			const_cast<MotionState*>( this )->getWorldTransform( &worldTrans );
+		}
+	};
+	
+	struct DefaultMotionState : public btDefaultMotionState{
+
+		DefaultMotionState(){
+		}
+		
+ 		DefaultMotionState( const btTransform &startTrans,const btTransform &centerOfMassOffset ):btDefaultMotionState( startTrans,centerOfMassOffset ){
+ 		}
+
+		void setWorldTransform( btTransform *worldTrans ){
+		
+			btDefaultMotionState::setWorldTransform( *worldTrans );
+		}
+
+		void getWorldTransform( btTransform *worldTrans ){
+
+			btDefaultMotionState::getWorldTransform( *worldTrans );
+		}
+	};
+	
 	void rayTest( btCollisionWorld *self,
 		const btVector3 &rayFromWorld,
 		const btVector3 &rayToWorld,

+ 12 - 1
modules/mojo3d-physics/physics/collider.monkey2

@@ -10,12 +10,21 @@ Function CreateInternalEdgeInfo( mesh:btBvhTriangleMeshShape )="bbBullet::create
 	
 Public
 
+Class Entity Extension
+	
+	Property Collider:Collider()
+		
+		Return GetComponent<Collider>()
+	End
+	
+End
 
 Class Collider Extends Component
 	
 	Const Type:=New ComponentType( "Collider",-1,ComponentTypeFlags.Singleton )
 	
 	Method New( entity:Entity )
+		
 		Super.New( entity,Type )
 	End
 	
@@ -49,8 +58,10 @@ Protected
 		If Not _btshape Return
 		
 		_btshape.destroy()
-		
+
 		_btshape=Null
+
+		Modified()
 	End
 
 	function SetOrigin:btCollisionShape( shape:btCollisionShape,origin:Vec3f )

+ 195 - 86
modules/mojo3d-physics/physics/rigidbody.monkey2

@@ -1,37 +1,59 @@
 
-Namespace mojo3d.physics
+#rem
 
-#Import "native/kinematicmotionstate.h"
+Notes:
 
-Extern Private
+* Have to remove/add bodies from world if collision shape changes. http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=5194
 
-Class bbKinematicMotionState Extends btMotionState
-	
-	Method GetWorldTransform:btTransform() Abstract="getWorldTransform"
-	
-End
+* default btCollsionObject activationState=ACTIVE_TAG (1)?
+
+#end
+
+Namespace mojo3d.physics
 
 Private
 
-Class KinematicMotionState Extends bbKinematicMotionState
+Class MotionState Extends btMotionState
 	
 	Method New( entity:Entity )
 		
 		_entity=entity
 	End
 	
-	Method GetWorldTransform:btTransform() Override
+	Method getWorldTransform( tform:btTransform Ptr ) Override
 		
-		Return _entity.Matrix
+		If Not _entity.RigidBody.Kinematic Print "Dynamic getWorldTransform! Position="+_entity.Position
+
+		tform->setOrigin( _entity.Position )
+		
+		tform->setBasis( _entity.Basis )
 	End
 	
-Private
+	Method setWorldTransform( tform:btTransform Ptr ) Override
+		
+		If _entity.RigidBody.Kinematic Print "Kinematic setWorldTransform!"
+		
+		_entity.Position=tform->getOrigin()
+		
+		_entity.Basis=tform->getBasis()
+	End
+	
+	Private
 	
 	Field _entity:Entity
 End
 
 Public
 
+Class Entity Extension
+	
+	Property RigidBody:RigidBody()
+		
+		Return GetComponent<RigidBody>()
+	End
+	
+End
+
 Class RigidBody Extends Component
 	
 	Const Type:=New ComponentType( "RigidBody",1,ComponentTypeFlags.Singleton )
@@ -39,8 +61,36 @@ Class RigidBody Extends Component
 	Method New( entity:Entity )
 		
 		Super.New( entity,Type )
+		
+		Local collider:=entity.Collider
+		Local inertia:btVector3=collider?.CalculateLocalInertia( _mass )
+		
+		_btmotion=New MotionState( entity )
+		
+		_btbody=New btRigidBody( _mass,_btmotion,collider.btShape,inertia )
+		
+		Kinematic=False
+		Mass=1
+		Friction=1
+		RollingFriction=1
+		Restitution=0
+		CollisionGroup=1
+		CollisionMask=1
 	End
 	
+	Method New( entity:Entity,body:RigidBody )
+		
+		Self.New( entity )
+		
+		Kinematic=body.Kinematic
+		Mass=body.Mass
+		Friction=body.Friction
+		RollingFriction=body.RollingFriction
+		Restitution=body.Restitution
+		CollisionGroup=body.CollisionGroup
+		CollisionMask=body.CollisionMask
+	End
+
 	Property World:World()
 		
 		Return World.GetWorld( Entity.Scene )
@@ -52,7 +102,19 @@ Class RigidBody Extends Component
 	
 	Setter( kinematic:Bool )
 		
+		If kinematic=_kinematic Return
+		
 		_kinematic=kinematic
+		
+		If _kinematic
+			_btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_KINEMATIC_OBJECT )
+			_btbody.setActivationState( DISABLE_DEACTIVATION )
+		Else
+			_btbody.setCollisionFlags( _btbody.getCollisionFlags() & ~btCollisionObject.CF_KINEMATIC_OBJECT )
+			_btbody.forceActivationState( ACTIVE_TAG )
+		Endif
+			
+'		_dirty|=Dirty.Kinematic
 	End
 	
 	Property Mass:Float()
@@ -61,34 +123,60 @@ Class RigidBody Extends Component
 		
 	Setter( mass:Float )
 		
+		If mass=_mass Return
+		
 		_mass=mass
+		
+		Local collider:=Entity.Collider
+		Local inertia:=collider?.CalculateLocalInertia( _mass )
+		_btbody.setMassProps( _mass,inertia )
+		
+'		_dirty|=Dirty.Mass
 	End
 
 	Property Friction:Float()
 		
-		Return _friction
+		Return _btbody.getFriction()
 	
 	Setter( friction:Float )
 		
-		_friction=friction
+		_btbody.setFriction( friction )
 	End
 	
 	Property RollingFriction:Float()
 		
-		Return _rfriction
+		Return _btbody.getRollingFriction()
 	
 	Setter( friction:Float )
 		
-		_rfriction=friction
+		_btbody.setRollingFriction( friction )
 	End
 	
 	Property Restitution:Float()
-	
-		Return _restitution
+		
+		Return _btbody.getRestitution()
 		
 	Setter( restitution:Float )
 		
-		_restitution=restitution
+		_btbody.setRestitution( restitution )
+	End
+	
+	Property LinearVelocity:Vec3f()
+		
+		Return _btbody.getLinearVelocity()
+		
+	Setter( velocity:Vec3f )
+		
+		_btbody.setLinearVelocity( velocity )
+	End
+	
+	Property AngularVelocity:Vec3f()
+		
+		Return _btbody.getAngularVelocity()
+	
+	Setter( avelocity:Vec3f )
+		
+		_btbody.setAngularVelocity( avelocity )
 	End
 
 	Property CollisionGroup:Short()
@@ -114,105 +202,126 @@ Class RigidBody Extends Component
 		Return _btbody
 	End
 	
+	Method ClearForces()
+
+		_btbody.clearForces()
+	End
+
+	Protected
+	
 	Method OnCopy:RigidBody( entity:Entity ) Override
 		
-		Local body:=New RigidBody( entity )
-		
-		body.Kinematic=Kinematic
-		body.Mass=Mass
-		body.Friction=Friction
-		body.RollingFriction=RollingFriction
-		body.Restitution=Restitution
-		body.CollisionGroup=CollisionGroup
-		body.CollisionMask=CollisionMask
-		
-		Return body
+		Return New RigidBody( entity,Self )
 	End
 
 	Method OnBeginUpdate() Override
 		
-		If Not _btbody
-			Validate()
-			Return
-		End
-		
-		If _kinematic Return
-	
-		If _seq=Entity.Seq Return
+'		Validate()
+		
+		Local collider:=Entity.Collider
+		
+		Local seq:=collider?.GuidSeq
+		
+		If seq<>_colliderseq
+			
+			If _rvisible
+				If Entity.ReallyVisible 
+					World.btWorld.removeRigidBody( _btbody )
+				Else
+					_rvisible=False
+					World.Remove( Self )
+				Endif
+			Endif
+			
+			_btbody.setCollisionShape( collider?.btShape )
+			Local inertia:btVector3=collider?.CalculateLocalInertia( _mass )
+			_btbody.setMassProps( _mass,inertia )
+			
+			If _rvisible World.btWorld.addRigidBody( _btbody )
+			
+			_colliderseq=seq
+			
+		Endif
+			
+		If Entity.ReallyVisible<>_rvisible
+			
+			_rvisible=Entity.ReallyVisible
 
-		_btbody.clearForces()
-		_btbody.setLinearVelocity( New Vec3f( 0 ) )
-		_btbody.setAngularVelocity( New Vec3f( 0 ) )
-		_btbody.setWorldTransform( Entity.Matrix )
-		_btmotion.setWorldTransform( Entity.Matrix )
+			If _rvisible World.Add( Self ) Else World.Remove( Self )
+				
+		Endif
+
+		If Not _kinematic And Entity.Seq<>_seq 
+			
+			_btbody.setWorldTransform( Entity.Matrix )
+		Endif
+		
 	End
 	
 	Method OnUpdate( elapsed:Float ) Override
 		
-		If _kinematic Return
+'		If _kinematic Return
 		
-		Local tform:=_btmotion.getWorldTransform()
+'		Local tform:=_btbody.getWorldTransform()
 		
-		Entity.Position=tform.getOrigin()
+'		Entity.Position=tform.getOrigin()
 		
-		Entity.Basis=tform.getBasis()
+'		Entity.Basis=tform.getBasis()
 		
 		_seq=Entity.Seq
 	End
 	
 	Method OnDestroy() Override
 		
-		If Not _btbody Return
+		If Not _rvisible Return
 		
-		World.GetCurrent().Remove( Self )
+		_rvisible=False
 		
-		_btbody=Null
+		World.Remove( Self )
 	End
 	
-	Internal
+	Private
 	
-	Method Validate:btRigidBody()
-		
-		If _btbody Return _btbody
-
-		If _kinematic		
-			_btmotion=New KinematicMotionState( Entity )
-		Else
-			_btmotion=New btDefaultMotionState( Entity.Matrix )
-		Endif
-		
-		Local collider:=Entity.GetComponent<Collider>()
-		
-		Local inertia:btVector3=collider ? collider.CalculateLocalInertia( _mass ) Else Null
-		
-		_btbody=New btRigidBody( _mass,_btmotion,collider.btShape,inertia )
-
-		If _kinematic
-			_btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_KINEMATIC_OBJECT )
-			_btbody.setActivationState( DISABLE_DEACTIVATION )
-		Endif
-		
-		_btbody.setFriction( _friction )
-		_btbody.setRollingFriction( _rfriction )
-		_btbody.setRestitution( _restitution )
-		
-		World.Add( Self )
-		
-		Return _btbody
+	Enum Dirty
+		Mass=1
+		Kinematic=2
 	End
 	
-	Private
-
 	Field _kinematic:Bool=False
 	Field _mass:Float=1
-	Field _friction:Float=1
-	Field _rfriction:Float=1
-	Field _restitution:Float=0
 	Field _collGroup:Short=1
 	Field _collMask:Short=1
-	
-	Field _btmotion:btMotionState
+
+	Field _btmotion:MotionState
 	Field _btbody:btRigidBody
+	Field _dirty:Dirty=Null
+	
+	Field _colliderseq:Int
+	Field _rvisible:Bool
 	Field _seq:Int
+	
+	Method Validate()
+		
+		If Not _dirty Return
+		
+		If _dirty & Dirty.Mass
 
+			Local collider:=Entity.Collider
+			Local inertia:btVector3=collider?.CalculateLocalInertia( _mass )
+			_btbody.setMassProps( _mass,inertia )
+		Endif
+		
+		If _dirty & Dirty.Kinematic
+			
+			If _kinematic
+				_btbody.setCollisionFlags( _btbody.getCollisionFlags() | btCollisionObject.CF_KINEMATIC_OBJECT )
+				_btbody.setActivationState( DISABLE_DEACTIVATION )
+			Else
+				_btbody.setCollisionFlags( _btbody.getCollisionFlags() & ~btCollisionObject.CF_KINEMATIC_OBJECT )
+				_btbody.forceActivationState( ACTIVE_TAG )
+			Endif
+		Endif
+		
+		_dirty=Null
+	End
 End

+ 11 - 2
modules/mojo3d-physics/physics/world.monkey2

@@ -102,6 +102,11 @@ Class World
 		Return GetWorld( mojo3d.Scene.GetCurrent() )
 	End
 	
+	Property btWorld:btDynamicsWorld()
+		
+		Return _btworld
+	End
+	
 	Internal
 	
 	Function GetWorld:World( scene:Scene )
@@ -115,9 +120,11 @@ Class World
 	
 	Method Add( body:RigidBody )
 		
+		Print "World.Add( RigidBody )"
+		
 		_bodies.Add( body )
 		
-		Local btbody:=body.Validate()
+		Local btbody:=body.btBody
 		
 		btbody.setUserPointer( Cast<Void Ptr>( body ) )
 		
@@ -126,7 +133,9 @@ Class World
 	
 	Method Remove( body:RigidBody )
 		
-		Local btbody:=body.Validate()
+		Print "World.Remove( RigidBody )"
+		
+		Local btbody:=body.btBody
 		
 		_btworld.removeRigidBody( btbody )
 		

+ 33 - 37
modules/mojo3d-physics/tests/shapes.monkey2

@@ -24,40 +24,30 @@ Class MyWindow Extends Window
 	
 	Field _ground:Model
 	
-	Field _collider:SphereCollider
-	
 	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
 
 		Super.New( title,width,height,flags )
 		
 		_scene=Scene.GetCurrent()
 		
-		_scene.ClearColor=Color.Sky
-		
 		'create camera
 		'
 		_camera=New Camera
 		_camera.Near=.1
 		_camera.Far=60
 		_camera.Move( 0,10,-10 )
+		New FlyBehaviour( _camera )
 		
+		Local camCollider:=New SphereCollider( _camera )
+		camCollider.Radius=1
 		Local camBody:=New RigidBody( _camera )
 		camBody.Kinematic=True
-		camBody.Mass=1
-		
-		Local camCollider:=New SphereCollider( _camera )
-		
-		'create fog
-		'		
-		Local fog:=New FogEffect
-		fog.Color=Color.Sky
-		fog.Near=50
-		fog.Far=60
+		camBody.Mass=0
 		
 		'create light
 		'
 		_light=New Light
-		_light.RotateX( 90 )	'aim directional light 'down'.
+		_light.RotateX( 75,15 )
 		
 		'create ground
 		'
@@ -65,45 +55,45 @@ Class MyWindow Extends Window
 		
 		_ground=Model.CreateBox( groundBox,16,16,16,New PbrMaterial( Color.Green ) )
 		
-		Local collider:=New BoxCollider( _ground )
-		collider.Box=groundBox
-		
-		Local body:=New RigidBody( _ground )
-		body.Mass=0
+		Local groundCollider:=New BoxCollider( _ground )
+		groundCollider.Box=groundBox
 		
-		'create some meshes/colliders
+		Local groundBody:=New RigidBody( _ground )
+		groundBody.Mass=0
 		
 		Local material:=New PbrMaterial( Color.White )
-		
-		Local model0:=Model.CreateBox( New Boxf( -1,-5,-1,1,5,1 ),1,1,1,material )
-		model0.AddComponent<RigidBody>()
-		Local collider0:=model0.AddComponent<BoxCollider>()
-		collider0.Box=New Boxf( -1,-5,-1,1,5,1 )
 
+		Local box0:=New Boxf( -1,-5,-1,1,5,1 )		
+		Local model0:=Model.CreateBox( box0,1,1,1,material )
+		Local collider0:=New BoxCollider( model0 )
+		collider0.Box=box0
+		Local body0:=New RigidBody( model0 )
+		
 		Local model1:=Model.CreateSphere( 1,32,16,material )
-		model1.AddComponent<RigidBody>()
-		Local collider1:=model1.AddComponent<SphereCollider>()
+		Local collider1:=New SphereCollider( model1 )
+		Local body1:=New RigidBody( model1 )
 
 		Local model2:=Model.CreateCylinder( 1,8,Axis.Y,32,material )
-		model2.AddComponent<RigidBody>()
-		Local collider2:=model2.AddComponent<CylinderCollider>()
+		Local collider2:=New CylinderCollider( model2 )
 		collider2.Radius=1
 		collider2.Length=8
+		Local body2:=New RigidBody( model2 )
 
 		Local model3:=Model.CreateCapsule( 1,10,Axis.Y,32,material )
-		model3.AddComponent<RigidBody>()
-		Local collider3:=model3.AddComponent<CapsuleCollider>()
+		Local collider3:=New CapsuleCollider( model3 )
 		collider3.Radius=1
 		collider3.Length=10
+		Local body3:=New RigidBody( model3 )
 
 		Local model4:=Model.CreateCone( 2.5,5,Axis.Y,32,material )
-		model4.AddComponent<RigidBody>()
-		Local collider4:=model4.AddComponent<ConeCollider>()
+		Local collider4:=New ConeCollider( model4 )
 		collider4.Radius=2.5
 		collider4.Length=5
+		Local body4:=New RigidBody( model4 )
 		
 		Local models:=New Model[]( model0,model1,model2,model3,model4 )
 		
+		#rem
 		For Local x:=-40 To 40 Step 8
 			
 			For Local z:=-40 To 40 Step 8
@@ -118,9 +108,17 @@ Class MyWindow Extends Window
 			Next
 		
 		Next
+		#end
+		
+		Local x:=0.0
 		
 		For Local model:=Eachin models
-			model.Destroy()
+			
+			model.Move( x,10,0 )
+			
+			x+=5.0
+			
+			'model.Destroy()
 		Next
 	End
 	
@@ -128,8 +126,6 @@ Class MyWindow Extends Window
 		
 		RequestRender()
 		
-		util.Fly( _camera,Self )
-			
 		_scene.Update()
 		
 		_scene.Render( canvas,_camera )

+ 10 - 4
modules/mojo3d/entities/camera.monkey2

@@ -41,7 +41,7 @@ Class Camera Extends Entity
 
 		_aspect=Float( _viewport.Width )/Float( _viewport.Height )
 		
-		_dirty|=Dirty.ProjMatrix
+		'_dirty|=Dirty.ProjMatrix
 	End
 	
 	#rem monkeydoc Aspect ratio.
@@ -67,7 +67,7 @@ Class Camera Extends Entity
 	
 		_fovy=fovy
 		
-		_dirty|=Dirty.ProjMatrix
+		'_dirty|=Dirty.ProjMatrix
 	End
 	
 	#rem monkeydoc Near clip plane distance.
@@ -83,7 +83,7 @@ Class Camera Extends Entity
 	
 		_near=nearz
 		
-		_dirty|=Dirty.ProjMatrix
+		'_dirty|=Dirty.ProjMatrix
 	End
 	
 	#rem monkeydoc Far clip plane distance.
@@ -99,7 +99,7 @@ Class Camera Extends Entity
 	
 		_farz=farz
 		
-		_dirty|=Dirty.ProjMatrix
+		'_dirty|=Dirty.ProjMatrix
 	End
 	
 	#rem monkeydoc @hidden
@@ -114,6 +114,12 @@ Class Camera Extends Entity
 		Endif
 		
 		Return _projMatrix
+		
+	Setter( matrix:Mat4f )
+		
+		_projMatrix=matrix
+		
+		_dirty&=~Dirty.ProjMatrix
 	End
 	
 	#rem monkeydoc Converts a point from world coordinates to viewport coordinates.

+ 20 - 2
modules/mojo3d/mojo3d.monkey2

@@ -3,6 +3,7 @@ Namespace mojo3d
 
 #Import "<std>"
 #Import "<mojo>"
+#Import "<opengl>"
 
 #Import "assets/"
 
@@ -50,6 +51,23 @@ Namespace mojo3d
 #Import "geometry/util3d"
 
 Using std..
-Using gles20..
 Using mojo..
-Using mojo3d..
+Using opengl..
+
+Function Main()
+	
+#If __DESKTOP_TARGET__ Or __WEB_TARGET__
+
+	SetEnv( "MOJO3D_DEFAULT_RENDERER","deferred" )
+	
+#Else if __MOBILE_TARGET__
+	
+	SetEnv( "MOJO3D_DEFAULT_RENDERER","forward" )
+	
+	SetEnv( "MOJO3D_FORWARD_RENDERER_DIRECT",1 )
+	
+	SetEnv( "MOJO_DEPTH_BUFFER_BITS",16 )
+	
+#endif
+	
+End

+ 6 - 5
modules/mojo3d/render/deferredrenderer.monkey2

@@ -15,16 +15,17 @@ Material render passes:
 #end
 Class DeferredRenderer Extends Renderer
 
-	#rem monkeydoc @hidden
+	#rem monkeydoc Creates a deferred renderer.
 	#end
 	Method New()
-		Super.New( True )
-		
+
 		Print "Creating DeferredRenderer"
+		
+		Super.Init( True )
 	
 		_lightShader=Shader.Open( "lighting-deferred",ShaderDefs )
-		_copyShader=Shader.Open( "copy" )
 		
+		_copyShader=Shader.Open( "copy" )
 	End
 
 	Protected
@@ -207,8 +208,8 @@ Class DeferredRenderer Extends Renderer
 		_device.CullMode=CullMode.None
 		_device.Shader=_lightShader
 		_device.RenderPass=pass
+		
 		RenderQuad()
-
 	End
 
 	Method RenderEffects( scene:Scene )

+ 13 - 7
modules/mojo3d/render/forwardrenderer.monkey2

@@ -19,14 +19,20 @@ Render passes:
 #end
 Class ForwardRenderer Extends Renderer
 
-	#rem monkeydoc @hidden
+	#rem monkeydoc Creates a forward renderer.
+	
+	If the config setting "MOJO3D_FORWARD_RENDERER_DIRECT" is set to "1", the renderer will render directly to the render target when [[Render]] is invoked.
+
+	Config settings may be set using the [[std::std.filesystem.SetConfig|SetConfig]] function.
+
 	#end
-	Method New( direct:Bool )
-		Super.New( Not direct )
+	Method New()
 		
-		_direct=direct
-		
-		Print "Creating ForwardRenderer, direct="+Int( _direct )
+		_direct=Int( GetConfig( "MOJO3D_FORWARD_RENDERER_DIRECT" ) )
+
+		Print "Creating ForwardRenderer, direct="+_direct
+			
+		Super.Init( Not _direct )
 		
 		If Not _direct _copyShader=Shader.Open( "copy" )
 	End
@@ -53,7 +59,7 @@ Class ForwardRenderer Extends Renderer
 			Const depth_format:=PixelFormat.Depth32
 			#Endif
 				
-			_colorBuffer=New Texture( size.x,size.y,color_format,TextureFlags.Filter|TextureFlags.Dynamic )
+			_colorBuffer=New Texture( size.x,size.y,color_format,TextureFlags.Dynamic|TextureFlags.Filter )
 			_depthBuffer=New Texture( size.x,size.y,depth_format,TextureFlags.Dynamic )
 			_colorTarget0=New RenderTarget( New Texture[]( _colorBuffer ),_depthBuffer )
 			_colorTarget1=New RenderTarget( New Texture[]( _colorBuffer ),Null )

+ 36 - 51
modules/mojo3d/render/renderer.monkey2

@@ -54,30 +54,36 @@ Class Renderer
 		_psmSize=size
 	End
 	
+	Function SetCurrent( renderer:Renderer )
+		
+		DebugAssert( Not _current,"Renderer.Current already set" )
+		
+		_current=renderer
+	End
+	
 	#rem monkeydoc Gets the current renderer.
+	
+	The config setting "MOJO3D_DEFAULT_RENDERER" can be used to override the default renderer created. It should be set to "deferred" or "forward".
+
+	Config settings may be changed using the [[std::std.filesystem.SetConfig|SetConfig]] function.
+	
 	#end
 	Function GetCurrent:Renderer()
 		
-		Global _current:Renderer
+		If _current Return _current
 		
-		If Not _current
-			
-			Local hasDepth:=Int( App.GetConfig( "GL_depth_buffer_enabled","0" ) )<>0
-			
-			Select App.GetConfig( "mojo3d_renderer","" )
-			Case "deferred"
-				_current=New DeferredRenderer
-			Case "forward-direct"
-				_current=New ForwardRenderer( True )
-			Case "forward"
-				_current=New ForwardRenderer( False )
-			Default
+		Select( GetConfig( "MOJO3D_DEFAULT_RENDERER" ) )
+		Case "deferred"
+			_current=New DeferredRenderer
+		Case "forward"
+			_current=New ForwardRenderer
+		Default
 #If Not __MOBILE_TARGET__					
-				If glexts.GL_draw_buffers _current=New DeferredRenderer
+			_current=New DeferredRenderer
+#else
+			_current=New ForwardRenderer
 #Endif
-			End
-			If Not _current _current=New ForwardRenderer( hasDepth )
-		Endif
+		End
 		
 		Return _current
 	End
@@ -185,6 +191,11 @@ Class Renderer
 		_renderScene=Null
 	End
 	
+	Property ShaderDefs:String()
+		
+		Return _linearOutput ? "MX2_LINEAROUTPUT~n" Else "MX2_SRGBOUTPUT~n"
+	End
+	
 	Protected
 	
 	Method OnValidateSize( size:Vec2i ) Virtual
@@ -192,19 +203,13 @@ Class Renderer
 	
 	Method OnRender( scene:Scene,camera:Camera,device:GraphicsDevice ) Virtual
 	End
-
-	Method New( linearOutput:Bool )
+	
+	Method Init( linearOutput:Bool )
 		
 		_linearOutput=linearOutput
 
 		_rgbaDepthTextures=False
 		
-		_shaderDefs+=_linearOutput ? "MX2_LINEAROUTPUT~n" Else "MX2_SRGBOUTPUT~n"
-		
-		If _rgbaDepthTextures _shaderDefs+="MX2_RGBADEPTHTEXTURES~n"
-		
-		Print "Renderer.ShaderDefs="+ShaderDefs
-		
 		_device=New GraphicsDevice( 0,0 )
 		
 		_runiforms=New UniformBlock( 1,True )
@@ -305,7 +310,7 @@ Class Renderer
 	End
 	
 	Method RenderCSMShadows( light:Light )
-	
+		
 		'Perhaps use a different device for CSM...?
 		'
 		Local t_rtarget:=_device.RenderTarget
@@ -420,21 +425,10 @@ Class Renderer
 		_device.Scissor=t_scissor
 	End
 
-	Internal
-	
-	Property ShaderDefs:String()
-		
-		Return _shaderDefs
-	End
-	
-	Property LinearOutput:Bool()
-	
-		Return _linearOutput
-	
-	End
-
 	Private
 	
+	Global _current:Renderer
+	
 	Field _rgbaDepthTextures:=False
 	
 	Field _shaderDefs:String
@@ -478,18 +472,11 @@ Class Renderer
 			_csmTexture?.Discard()
 			_csmDepth?.Discard()
 
-			const color_format:=PixelFormat.RGBA8
 			const depth_format:=PixelFormat.Depth32
 			
-			If _rgbaDepthTextures
-				_csmTexture=New Texture( _csmSize,_csmSize,color_format,TextureFlags.Dynamic )
-				_csmDepth=New Texture( _csmSize,_csmSize,depth_format,TextureFlags.Dynamic )
-				_csmTarget=New RenderTarget( New Texture[]( _csmTexture ),_csmDepth )
-			Else
-				_csmTexture=New Texture( _csmSize,_csmSize,depth_format,TextureFlags.Dynamic )
-				_csmTarget=New RenderTarget( Null,_csmTexture )
-				_csmDepth=Null
-			Endif
+			_csmTexture=New Texture( _csmSize,_csmSize,depth_format,TextureFlags.Dynamic )
+			_csmTarget=New RenderTarget( Null,_csmTexture )
+			_csmDepth=Null
 			
 		Endif
 		
@@ -512,8 +499,6 @@ Class Renderer
 				Local face:=_psmTexture.GetCubeFace( Cast<CubeFace>( i ) )
 				_psmTargets[i]=New RenderTarget( New Texture[]( face ),_psmDepth )
 			Next
-			
-			glCheck()
 		
 		Endif
 		

+ 24 - 3
modules/mojo3d/scene/component.monkey2

@@ -46,10 +46,9 @@ Class Component
 	Method New( entity:Entity,type:ComponentType )
 		
 		_entity=entity
-		
 		_type=type
-		
 		_entity.AddComponent( Self )
+		Modified()
 	End
 	
 	Property Entity:Entity()
@@ -61,6 +60,25 @@ Class Component
 		
 		Return _type
 	End
+	
+	Property GuidSeq:Int()
+		
+		Return _guidseq
+	End
+	
+	Property Seq:Int()
+		
+		Return _seq
+	End
+	
+	Protected
+	
+	Method Modified()
+		
+		_nextguidseq+=1
+		_guidseq=_nextguidseq
+		_seq+=1
+	End
 		
 	Internal
 
@@ -88,9 +106,12 @@ Class Component
 	
 	Private
 	
-	Field _entity:Entity
+	Global _nextguidseq:=0
 	
+	Field _entity:Entity
 	Field _type:ComponentType
+	Field _guidseq:=0
+	Field _seq:=0
 End
 
 

+ 3 - 4
modules/mojo3d/scene/entity.monkey2

@@ -303,10 +303,9 @@ Class Entity Extends DynamicObject
 			_children.Top.Destroy()
 		Wend
 		
-		If _visible
-			_visible=False
-			 OnHide()
-		Endif
+		_visible=False
+		
+		ValidateVisibility()
 		
 		For Local c:=Eachin _components
 			c.OnDestroy()

+ 20 - 1
modules/mojo3d/scene/scene.monkey2

@@ -95,6 +95,17 @@ Class Scene Extends DynamicObject
 		_ambientDiffuse=color
 	End
 	
+	#rem monkeydoc Renderer for the scene.
+	#end
+	Property Renderer:Renderer()
+		
+		Return _renderer
+	
+	Setter( renderer:Renderer )
+		
+		_renderer=renderer
+	End
+	
 	#rem monkeydoc Adds a post effect to the scene.
 	#end
 	Method AddPostEffect( postEffect:PostEffect )
@@ -165,7 +176,9 @@ Class Scene Extends DynamicObject
 			
 		canvas.Flush()
 		
-		Renderer.GetCurrent().Render( Self,camera,canvas.GraphicsDevice )
+		Local renderer:=Renderer.GetCurrent()
+		
+		renderer.Render( Self,camera,canvas.GraphicsDevice )
 	End
 
 	#rem monkeydoc Enumerates all entities in the scene with null parents.
@@ -179,6 +192,8 @@ Class Scene Extends DynamicObject
 	#end
 	Function SetCurrent( scene:Scene )
 		
+		DebugAssert( Not _current,"Scene.Current already set" )
+		
 		_current=scene
 	End
 	
@@ -231,8 +246,12 @@ Class Scene Extends DynamicObject
 	
 	Field _clearColor:Color
 	Field _ambientDiffuse:Color
+	
+	Field _renderer:Renderer
+	
 	Field _postEffects:=New Stack<PostEffect>
 	
+	
 	Field _rootEntities:=New Stack<Entity>
 	
 	Field _cameras:=New Stack<Camera>

+ 4 - 8
modules/mojo3d/tests/anisotropic.monkey2

@@ -13,8 +13,8 @@ Using std..
 Using mojo..
 Using mojo3d..
 
-Const MaxAnisotropy:=0			'set to 0 to use use HW max (usually 16) 1 for min/off.
-
+Const MaxAnisotropy:=16
+			
 Class MyWindow Extends Window
 	
 	Field _scene:Scene
@@ -29,7 +29,7 @@ Class MyWindow Extends Window
 
 		Super.New( title,width,height,flags )
 		
-		Print gles20.glGetString( gles20.GL_EXTENSIONS ).Replace( " ","~n" )
+		SetEnv( "MX2_MOJO_TEXTURE_MAX_ANISOTROPY",MaxAnisotropy )
 		
 		'create scene
 		'		
@@ -87,11 +87,7 @@ End
 
 Function Main()
 	
-	Local config:=New StringMap<String>
-	
-	If MaxAnisotropy config["GL_texture_max_anisotropy"]=MaxAnisotropy
-
-	New AppInstance( config )
+	New AppInstance
 	
 	New MyWindow
 	

+ 13 - 22
modules/mojo3d/tests/ducks.monkey2

@@ -13,6 +13,15 @@ Using std..
 Using mojo..
 Using mojo3d..
 
+Function Main()
+
+	New AppInstance
+	
+	New MyWindow
+	
+	App.Run()
+End
+
 Class MyWindow Extends Window
 	
 	Field _scene:Scene
@@ -33,7 +42,7 @@ Class MyWindow Extends Window
 
 		Super.New( title,width,height,flags )
 		
-		Print gles20.glGetString( gles20.GL_EXTENSIONS ).Replace( " ","~n" )
+		Print "GL_VERSION="+opengl.glGetString( opengl.GL_VERSION )
 		
 		'create scene
 		'		
@@ -57,11 +66,12 @@ Class MyWindow Extends Window
 		_camera.FOV=90
 		_camera.Move( 0,15,-20 )
 		
-		_camera.AddComponent<FlyBehaviour>()
+		New FlyBehaviour( _camera )
 		
 		'create light
 		_light=New Light
-		_light.Type=LightType.Point
+'		_light.Type=LightType.Point
+		_light.Rotate( 75,15,0 )
 		_light.Move( 0,20,0 )
 		_light.Range=40
 		_light.CastsShadow=True
@@ -120,8 +130,6 @@ Class MyWindow Extends Window
 
 		RequestRender()
 		
-		'_ducks[0].RotateY( 1 )
-
 		_scene.Update()
 		
 		_scene.Render( canvas,_camera )
@@ -133,20 +141,3 @@ Class MyWindow Extends Window
 	
 End
 
-Function Main()
-	
-	Local config:=New StringMap<String>
-
-'	config["mojo3d_renderer"]="deferred"		'defeault on non-mobile targets.
-
-'	config["mojo3d_renderer"]="forward-direct"	'default on mobile targets. depth buffer must be enabled too.
-'	config["GL_depth_buffer_enabled"]=1
-
-'	config["mojo3d_renderer"]="forward"
-		
-	New AppInstance( config )
-	
-	New MyWindow
-	
-	App.Run()
-End

+ 1 - 12
modules/mojo3d/tests/morpher.monkey2

@@ -94,8 +94,6 @@ Class MyWindow Extends Window
 
 		Super.New( title,width,height,flags )
 		
-		Print gles20.glGetString( gles20.GL_EXTENSIONS ).Replace( " ","~n" )
-		
 		'create scene
 		'		
 		_scene=Scene.GetCurrent()
@@ -149,16 +147,7 @@ End
 
 Function Main()
 	
-	Local config:=New StringMap<String>
-
-'	config["mojo3d_renderer"]="deferred"		'defeault on non-mobile targets.
-
-'	config["mojo3d_renderer"]="forward-direct"	'default on mobile targets. depth buffer must be enabled too.
-'	config["GL_depth_buffer_enabled"]=1
-
-'	config["mojo3d_renderer"]="forward"
-		
-	New AppInstance( config )
+	New AppInstance
 	
 	New MyWindow
 	

+ 1 - 10
modules/mojo3d/tests/particles.monkey2

@@ -98,16 +98,7 @@ End
 
 Function Main()
 	
-	Local config:=New StringMap<String>
-
-'	config["mojo3d_renderer"]="deferred"		'defeault on non-mobile targets.
-
-'	config["mojo3d_renderer"]="forward-direct"	'default on mobile targets. depth buffer must be enabled too.
-'	config["GL_depth_buffer_enabled"]=1
-
-'	config["mojo3d_renderer"]="forward"
-		
-	New AppInstance( config )
+	New AppInstance
 	
 	New MyWindow
 	

+ 1 - 10
modules/mojo3d/tests/sprites.monkey2

@@ -104,16 +104,7 @@ End
 
 Function Main()
 	
-	Local config:=New StringMap<String>
-
-'	config["mojo3d_renderer"]="deferred"		'defeault on non-mobile targets.
-
-'	config["mojo3d_renderer"]="forward-direct"	'default on mobile targets. depth buffer must be enabled too.
-'	config["GL_depth_buffer_enabled"]=1
-
-'	config["mojo3d_renderer"]="forward"
-		
-	New AppInstance( config )
+	New AppInstance
 	
 	New MyWindow
 	

+ 1 - 5
modules/mojo3d/tests/water.monkey2

@@ -79,11 +79,7 @@ End
 
 Function Main()
 
-	Local config:=New StringMap<String>
-
-	config["mojo3d_renderer"]="deferred"
-
-	New AppInstance( config )
+	New AppInstance
 	
 	New MyWindow