Exemplo n.º 1
0
void MetaballScene::newFrameListener(
	Ogre::RenderWindow* win, Ogre::Camera* cam, DynamicMesh* meshBuilder,
	const GridParams& gridparam)
{
	m_totalTime = 0;

	m_meshBuilder = meshBuilder;

	m_scene = NULL;

	m_nbrScene = 5;
	//m_currentSceneId = 0;//please define CPU_SAMPLER in MarchingCubesImplWithCuda::SampleSpace()
	m_currentSceneId = 4;  //please undefine CPU_SAMPLER in MarchingCubesImplWithCuda::SampleSpace()

	ResetScene(gridparam);

	//Initialize the camera coordinates
//	m_camAzimuth = 0;
//	m_camPolar = 0;

	m_keyboardDelayMax = 2.0f;
	m_keyboardDelay = 0; 
}
Exemplo n.º 2
0
void ProcessKey(unsigned char key)
{
    switch(key)
    {
    case '0':
        currentGravState = GravityState::ZERO;
        break;

    case '1':
        currentGravState = GravityState::NORMAL;
        break;

    case '2':
        currentGravState = GravityState::PLANET_GRAVITY;
        for(int i = 0; i < boxes.size(); i++)
        {
            PxVec3 dir = planets[0]->actor->getGlobalPose().p - boxes[i]->actor->getGlobalPose().p;

            ApplyOrbitVelocity(boxes[i]->actor, 100);
        }
        break;

    case '3':
        currentGravState = GravityState::PULL_SINGLE;
        break;

    case '4':
        currentGravState = GravityState::PULL_DOUBLE;
        break;

    case '5':
        currentGravState = GravityState::PULL_TRIPLE;
        break;

    case '6':
        currentGravState = GravityState::PULL_PUSH;
        break;

    case '7':
        currentGravState = GravityState::PUSH_PULL;
        break;

    case '8':
        currentGravState = GravityState::ORBITS;
        break;

    case 'p':
        isPaused = !isPaused;
        break;

    case 'r':
    case ' ':
        ResetScene();
        break;

    case '\n':
    case '\r':
    {
        for(int i = 0; i < boxes.size(); i++)
        {
            SetVelocity(PxVec3(0,0,0), boxes[i]);
        }
    }
    break;

    case 'v':
    {
        for(int i = 0; i < boxes.size(); i++)
        {
            RandomVelocities(boxes[i], 50, i);
        }
    }
    break;
    }
}
Exemplo n.º 3
0
bool FrankieScene::CreateAvatar ()
{
  printf ("Loading Frankie...\n");

  // Load animesh factory
  csLoadResult rc = avatarTest->loader->Load ("/lib/frankie/frankie.xml");
  if (!rc.success)
    return avatarTest->ReportError ("Can't load Frankie library file!");

  csRef<iMeshFactoryWrapper> meshfact =
    avatarTest->engine->FindMeshFactory ("franky_frankie");
  if (!meshfact)
    return avatarTest->ReportError ("Can't find Frankie's mesh factory!");

  animeshFactory = scfQueryInterface<CS::Mesh::iAnimatedMeshFactory>
    (meshfact->GetMeshObjectFactory ());
  if (!animeshFactory)
    return avatarTest->ReportError ("Can't find Frankie's animesh factory!");

  // Load bodymesh (animesh's physical properties)
  rc = avatarTest->loader->Load ("/lib/frankie/skelfrankie_body");
  if (!rc.success)
    return avatarTest->ReportError ("Can't load Frankie's body mesh file!");

  csRef<CS::Animation::iBodyManager> bodyManager =
    csQueryRegistry<CS::Animation::iBodyManager> (avatarTest->GetObjectRegistry ());
  csRef<CS::Animation::iBodySkeleton> bodySkeleton = bodyManager->FindBodySkeleton ("frankie_body");
  if (!bodySkeleton)
    return avatarTest->ReportError ("Can't find Frankie's body mesh description!");

  // Create a new animation tree. The structure of the tree is:
  //   + Debug animation node (root node)
  //     + Ragdoll animation node (only if physics are enabled)
  //       + 'LookAt' animation node
  //         + 'speed' animation node
  //           + animation nodes for all speeds
  csRef<CS::Animation::iSkeletonAnimPacketFactory> animPacketFactory =
    animeshFactory->GetSkeletonFactory ()->GetAnimationPacket ();

  // Create the 'debug' node
  debugNodeFactory = avatarTest->debugManager->CreateAnimNodeFactory ("debug");
  debugNodeFactory->SetDebugModes (CS::Animation::DEBUG_NONE);
  debugNodeFactory->SetRandomColor (true);
  animPacketFactory->SetAnimationRoot (debugNodeFactory);

  // Create the 'LookAt' animation node
  lookAtNodeFactory = avatarTest->lookAtManager->CreateAnimNodeFactory ("lookat");
  lookAtNodeFactory->SetBodySkeleton (bodySkeleton);
  lookAtNodeFactory->SetBone (animeshFactory->GetSkeletonFactory ()->FindBone ("CTRL_Head"));
  lookAtNodeFactory->SetListenerDelay (0.6f);

  // Create the 'idle' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idleNodeFactory =
    animPacketFactory->CreateAnimationNode ("idle");
  idleNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Idle1"));

  // Create the 'walk_slow' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkSlowNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk_slow");
  walkSlowNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_WalkSlow"));

  // Create the 'walk' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk");
  walkNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Walk"));

  // Create the 'walk_fast' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkFastNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk_fast");
  walkFastNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_WalkFast"));

  // Create the 'footing' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> footingNodeFactory =
    animPacketFactory->CreateAnimationNode ("footing");
  footingNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Runs"));

  // Create the 'run_slow' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runSlowNodeFactory =
    animPacketFactory->CreateAnimationNode ("run_slow");
  runSlowNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_RunSlow"));

  // Create the 'run' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runNodeFactory =
    animPacketFactory->CreateAnimationNode ("run");
  runNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Run"));

  // Create the 'run_fast' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runFastNodeFactory =
    animPacketFactory->CreateAnimationNode ("run_fast");
  runFastNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_RunFaster"));

  // Create the 'speed' animation node (and add all animations of Frankie moving at different speeds)
  // Unfortunately, the Frankie animations from 'walk fast' to 'footing'
  // do not blend well together, but this is just an example...
  csRef<CS::Animation::iSkeletonSpeedNodeFactory> speedNodeFactory =
    avatarTest->speedManager->CreateAnimNodeFactory ("speed");
  speedNodeFactory->AddNode (idleNodeFactory, 0.0f);
  speedNodeFactory->AddNode (walkSlowNodeFactory, 0.4f);
  speedNodeFactory->AddNode (walkNodeFactory, 0.6f);
  speedNodeFactory->AddNode (walkFastNodeFactory, 1.2f);
  speedNodeFactory->AddNode (footingNodeFactory, 1.6f);
  speedNodeFactory->AddNode (runSlowNodeFactory, 2.6f);
  speedNodeFactory->AddNode (runNodeFactory, 3.4f);
  speedNodeFactory->AddNode (runFastNodeFactory, 5.0f);

  lookAtNodeFactory->SetChildNode (speedNodeFactory);

  if (avatarTest->physicsEnabled)
  {
    // Create the ragdoll animation node
    csRef<CS::Animation::iSkeletonRagdollNodeFactory> ragdollNodeFactory =
      avatarTest->ragdollManager->CreateAnimNodeFactory ("ragdoll");
    debugNodeFactory->SetChildNode (ragdollNodeFactory);
    ragdollNodeFactory->SetBodySkeleton (bodySkeleton);
    ragdollNodeFactory->SetChildNode (lookAtNodeFactory);

    // Create a bone chain for the whole body and add it to the ragdoll animation node.
    // The chain will be in kinematic mode when Frankie is alive, and in dynamic state
    // when Frankie has been killed.
    bodyChain = bodySkeleton->CreateBodyChain
      ("body_chain", animeshFactory->GetSkeletonFactory ()->FindBone ("Frankie_Main"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("CTRL_Pelvis"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("CTRL_Head"));
    ragdollNodeFactory->AddBodyChain (bodyChain, CS::Animation::STATE_KINEMATIC);

    // Create a bone chain for the tail of Frankie and add it to the ragdoll animation node.
    // The chain will be in kinematic mode most of the time, and in dynamic mode when the
    // user ask for it with the 'f' key or when Frankie has been killed.
    tailChain = bodySkeleton->CreateBodyChain
      ("tail_chain", animeshFactory->GetSkeletonFactory ()->FindBone ("Tail_1"));
    tailChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("Tail_8"));
    ragdollNodeFactory->AddBodyChain (tailChain, CS::Animation::STATE_KINEMATIC);
  }

  else
    debugNodeFactory->SetChildNode (lookAtNodeFactory);

  // Create the animated mesh
  csRef<iMeshWrapper> avatarMesh =
    avatarTest->engine->CreateMeshWrapper (meshfact, "Frankie",
					   avatarTest->room, csVector3 (0.0f));
  animesh = scfQueryInterface<CS::Mesh::iAnimatedMesh> (avatarMesh->GetMeshObject ());

  // When the animated mesh is created, the animation nodes are created too.
  // We can therefore set them up now.
  CS::Animation::iSkeletonAnimNode* rootNode =
    animesh->GetSkeleton ()->GetAnimationPacket ()->GetAnimationRoot ();

  // Find a reference to the debug node
  debugNode = scfQueryInterface<CS::Animation::iSkeletonDebugNode> (rootNode->FindNode ("debug"));

  // Setup of the LookAt animation node
  lookAtNode = scfQueryInterface<CS::Animation::iSkeletonLookAtNode> (rootNode->FindNode ("lookat"));
  lookAtNode->AddListener (&lookAtListener);

  // Setup of the speed animation node
  speedNode = scfQueryInterface<CS::Animation::iSkeletonSpeedNode> (rootNode->FindNode ("speed"));

  // Setup of the ragdoll animation node
  if (avatarTest->physicsEnabled)
  {
    ragdollNode =
      scfQueryInterface<CS::Animation::iSkeletonRagdollNode> (rootNode->FindNode ("ragdoll"));
    ragdollNode->SetDynamicSystem (avatarTest->dynamicSystem);
  }

  // Reset the scene so as to put the parameters of the animation nodes in a default state
  ResetScene ();

  // Setup the decal
  if (avatarTest->decalManager)
  {
    // Load the decal material
    iMaterialWrapper * material = 
      avatarTest->engine->GetMaterialList()->FindByName("decal");
    if (!material)
    {
      if (!avatarTest->loader->LoadTexture ("decal", "/lib/std/cslogo2.png"))
	avatarTest->ReportWarning ("Could not load the decal texture!");

      material = avatarTest->engine->GetMaterialList()->FindByName("decal");
    }
    
    if (!material)
      avatarTest->ReportWarning ("Error finding decal material");

    // Setup the decal template
    else
    {
      decalTemplate = avatarTest->decalManager->CreateDecalTemplate (material);
      decalTemplate->SetDecalOffset (0.001f);
      decalTemplate->SetClipping (false);
    }
  }

  return true;
}
Exemplo n.º 4
0
bool FrankieScene::OnKeyboard (iEvent &ev)
{
  csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
  if (eventtype == csKeyEventTypeDown)
  {
    // Toggle the target mode of the 'LookAt' animation node
    if (csKeyEventHelper::GetCookedCode (&ev) == 't')
    {
      if (targetMode == LOOKAT_CAMERA)
      {
	lookAtNode->SetTarget (avatarTest->view->GetCamera ()->GetTransform ().GetOrigin ());
	targetMode = LOOKAT_POSITION;
      }

      else if (targetMode == LOOKAT_POSITION)
      {
	lookAtNode->RemoveTarget ();
	targetMode = LOOKAT_NOTHING;
      }

      else
      {
	lookAtNode->SetTarget (avatarTest->view->GetCamera (), csVector3 (0.0f));
	targetMode = LOOKAT_CAMERA;
      }

      return true;
    }

    // Toggle the 'always rotate' option of the 'LookAt' animation node
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'y')
    {
      alwaysRotate = !alwaysRotate;
      lookAtNodeFactory->SetAlwaysRotate (alwaysRotate);
      return true;
    }

    // Toggle the rotation speed of the 'LookAt' animation node
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'u')
    {
      if (rotationSpeed == ROTATION_SLOW)
      {
	rotationSpeed = ROTATION_NORMAL;
	lookAtNodeFactory->SetMaximumSpeed (5.0f);
      }

      else if (rotationSpeed == ROTATION_NORMAL)
      {
	rotationSpeed = ROTATION_IMMEDIATE;
	lookAtNodeFactory->SetMaximumSpeed (0.0f);
      }

      else if (rotationSpeed == ROTATION_IMMEDIATE)
      {
	rotationSpeed = ROTATION_SLOW;
	lookAtNodeFactory->SetMaximumSpeed (0.5f);
      }

      return true;
    }

    // Update the walking speed of the 'speed' animation node
    else if (csKeyEventHelper::GetCookedCode (&ev) == '+')
    {
      if (currentSpeed < 50)
      {
	currentSpeed += 1;
	speedNode->SetSpeed (((float) currentSpeed) / 10.0f);
      }
      return true;
    }

    else if (csKeyEventHelper::GetCookedCode (&ev) == '-')
    {
      if (currentSpeed > 0)
      {
	currentSpeed -= 1;
	speedNode->SetSpeed (((float) currentSpeed) / 10.0f);
      }
      return true;
    }

    // Toggle the physical animation of the tail
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'f'
	     && avatarTest->physicsEnabled
	     && !frankieDead)
    {
      // If the tail is animated by the classical animation then put the tail chain
      // in kinematic state
      if (ragdollNode->GetBodyChainState (tailChain) == CS::Animation::STATE_DYNAMIC)
	ragdollNode->SetBodyChainState (tailChain, CS::Animation::STATE_KINEMATIC);

      // If the tail is animated by the physical simulation then put the tail chain
      // in dynamic state
      else
	ragdollNode->SetBodyChainState (tailChain, CS::Animation::STATE_DYNAMIC);

      // Update the display of the dynamics debugger
      if (avatarTest->dynamicsDebugMode == DYNDEBUG_COLLIDER
	  || avatarTest->dynamicsDebugMode == DYNDEBUG_MIXED)
	avatarTest->dynamicsDebugger->UpdateDisplay ();

      return true;
    }

    // Toggle decals
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'c'
	     && avatarTest->decalManager)
    {
      decalsEnabled = !decalsEnabled;

      if (!decalsEnabled && decal)
      {
	avatarTest->decalManager->DeleteDecal (decal);
	decal = nullptr;
      }

      return true;
    }

    // Switching debug modes
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'a')
    {
      debugBones = !debugBones;
      int debugFlags = debugBones ? 
	(debugNodeFactory->GetDebugModes () | 
	 (CS::Animation::DEBUG_2DLINES | CS::Animation::DEBUG_SQUARES))
	: (debugNodeFactory->GetDebugModes () &
	   ~(CS::Animation::DEBUG_2DLINES | CS::Animation::DEBUG_SQUARES));
      debugNodeFactory->SetDebugModes ((CS::Animation::SkeletonDebugMode) debugFlags);

      return true;
    }

    else if (csKeyEventHelper::GetCookedCode (&ev) == 'b')
    {
      debugBBoxes = !debugBBoxes;
      int debugFlags = debugBBoxes ? 
        (debugNodeFactory->GetDebugModes () | CS::Animation::DEBUG_BBOXES)
	: (debugNodeFactory->GetDebugModes () & ~(CS::Animation::DEBUG_BBOXES));
      debugNodeFactory->SetDebugModes ((CS::Animation::SkeletonDebugMode) debugFlags);

      return true;
    }

    // Reset of the scene
    else if (csKeyEventHelper::GetCookedCode (&ev) == 'r')
    {
      ResetScene ();
      return true;
    }
  }

  return false;
}
Exemplo n.º 5
0
// **************************************************
void menuFunc(int m) {
  switch(m) {
    case MENU_TOGGLE_AMBIENT:
      ToggleAmbient();
      break;
      
    case MENU_TOGGLE_DIFFUSE:
      ToggleDiffuse();
      break;
      
    case MENU_TOGGLE_SPECULAR:
      ToggleSpecular();
      break;
      
    case MENU_TOGGLE_REFLECTIONS:
      ToggleReflections();
      break;
      
    case MENU_TOGGLE_TRANSPARENCY:
      ToggleTransparency();
      break;
      
    case MENU_TOGGLE_TEXTURING:
      ToggleTexturing();
      break;
      
    case MENU_TOGGLE_ANTIALIAS:
      ToggleAntiAliasing();
      break;
      
    case MENU_TOGGLE_ATTENUATION:
      ToggleAttenuation();
      break;
      
    case MENU_ADD_PRIMITIVE:
      AddRandomPrimitive();
      break;
      
    case MENU_ADD_LIGHT:
      AddRandomLight();
      break;
      
    case MENU_PRESET_1:
      LoadPresetScene1();
      break;
      
    case MENU_PRESET_2:
      LoadPresetScene2();
      break;
      
    case MENU_PRESET_3:
      LoadPresetScene3();
      break;
            
    case MENU_RESET_SCENE:
      ResetScene();
      break;
      
    case MENU_EXPORT_SCENE:
      ExportScene();
      break;
      
    default:
      break;
      
  }
  
}
Exemplo n.º 6
0
// ***********************************************************
// custom keyFunc with preset keys
void keyFunc(unsigned char ch, int x, int y) {
  bool bPostRedisplay = false;
  
	switch(ch) {
		case 'q':
    case 'Q':
      bPostRedisplay = false;
			exit(0);
			break;
      
    case '`':
      cindex = 0;
      
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 0.\n");
      else
        printf("Now controlling Primitive 0.\n");        
  
      break;
      
    case '1':
      cindex = 1;
        
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 1.\n");
      else
        printf("Now controlling Primitive 1.\n");  
          
      break;
      
    case '2':
      cindex = 2;
          
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 2.\n");
      else
        printf("Now controlling Primitive 2.\n");  
            
      break;
      
    case '3':
      cindex = 3;
        
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 3.\n");
      else
        printf("Now controlling Primitive 3.\n");  
          
      break;
      
    case '4':
      cindex = 4;
        
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 4.\n");
      else
        printf("Now controlling Primitive 4.\n");  
          
      break;
      
    case '5':
      cindex = 5;
        
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 5.\n");
      else
        printf("Now controlling Primitive 5.\n");  
          
      break;

    case '6':
      cindex = 6;
        
      if(cmode == ControlMode_Lights)
        printf("Now controlling Light 6.\n");
      else
        printf("Now controlling Primitive 6.\n");  
          
      break;
      
    case '7':
      LoadPresetScene1();
      break;

    case '8':
      LoadPresetScene2();
      break;

    case '9':
      LoadPresetScene3();
      break;
      
    case 'A':
    case 'a':
      ToggleAmbient();
      break;
      
    case 'S':
    case 's':
      ToggleSpecular();
      break;
      
    case 'D':
    case 'd':
      ToggleDiffuse();
      break;
            
    case 'R':
    case 'r':
      ToggleReflections();
      break;

    case 'T':
    case 't':
      ToggleTransparency();
      break;
         
    case 'E':
    case 'e':
      ToggleTexturing();
      break;
      
    case 'J':
    case 'j':
      ToggleAntiAliasing();
      break;
      
    case 'U':
    case 'u':
      ToggleAttenuation();
      break;
      
    case 'L':
    case 'l':
      cmode = ControlMode_Lights;
      printf("Control Mode: LIGHTS.\n");
      break;
            
    case 'P':
    case 'p':
      cmode = ControlMode_Prims;
      printf("Control Mode: PRIMITIVES.\n");
      break;
 
    case 'V':
    case 'v':
      TryToggleVisibility();
      
      bPostRedisplay = true;
      break;
      
    case 'M':
    case 'm':
      TryCycleMaterial();
      
      bPostRedisplay = true;
      break;
      
    case 'X':
    case 'x':
      TryCycleTexture();
      
      bPostRedisplay = true;
      break;
        
    // move controlled object left
    case '[':
      TryMove(-2.0f, 0.0f, 0.0f);
      
      bPostRedisplay = true;
      break;
      
    // move controlled object right
    case ']':
      TryMove(2.0f, 0.0f, 0.0f);
      
      bPostRedisplay = true;
      break;
      
    // move controlled object down
    case '-':
      TryMove(0.0f, -2.0f, 0.0f);
      
      bPostRedisplay = true;
      break;
 
    // move controlled object up
    case '=':
      TryMove(0.0f, 2.0f, 0.0f);
      
      bPostRedisplay = true;
      break;
        
    case ';':
      TryMove(0.0f, 0.0f, -2.0f);
      
      bPostRedisplay = true;
      break;
      
    case '\'':
      TryMove(0.0f, 0.0f, 2.0f);
      
      bPostRedisplay = true;
      break;
      
    case '.':
      if( scene->DecreaseRayDepth())
        printf("Ray depth decreased to %d.\n", scene->GetRayDepth());
      else
        printf("Ray depth already at 1.\n");
      
      bPostRedisplay = true;
      break;
      
    case ',':
      if( scene->IncreaseRayDepth())
        printf("Ray depth increased to %d.\n", scene->GetRayDepth());
      else
        printf("Ray depth already at max.\n");
      
      
      bPostRedisplay = true;
      break;
      
    case 'B':
    case 'b':
      ExportScene();
      break;
      
    case 'Z':
    case 'z':
      ResetScene();
      break;
      
    case 'O':
    case 'o':
      if(cmode == ControlMode_Lights) {
        AddRandomLight();

      } else {
        AddRandomPrimitive();
      }
      
      break;

		default:
			/* Unrecognized keypress */
      return;
	}
  
  if(bPostRedisplay)
    glutPostRedisplay();
    
}
Exemplo n.º 7
0
bool FrankieScene::CreateAvatar ()
{
  printf ("Loading Frankie...\n");

  // Load animesh factory
  csLoadResult rc = hairTest->loader->Load ("/lib/frankie/frankie.xml");
  if (!rc.success)
    return hairTest->ReportError ("Can't load Frankie library file!");

  // Load some fur
  rc = hairTest->loader->Load ("/lib/hairtest/frankie_furmesh.xml");
  if (!rc.success)
    return hairTest->ReportError ("Can't load frankie furmesh library!");

  csRef<iMeshWrapper> frankieFurmeshObject = 
    hairTest->engine->FindMeshObject("frankie_furmesh_object");
  if (!frankieFurmeshObject)
    return hairTest->ReportError ("Can't find fur mesh object!");

  csRef<iMeshFactoryWrapper> meshfact =
    hairTest->engine->FindMeshFactory ("franky_frankie");
  if (!meshfact)
    return hairTest->ReportError ("Can't find Frankie's mesh factory!");

  animeshFactory = scfQueryInterface<CS::Mesh::iAnimatedMeshFactory>
    (meshfact->GetMeshObjectFactory ());
  if (!animeshFactory)
    return hairTest->ReportError ("Can't find Frankie's animesh factory!");

  // Load bodymesh (animesh's physical properties)
  rc = hairTest->loader->Load ("/lib/frankie/skelfrankie_body");
  if (!rc.success)
    return hairTest->ReportError ("Can't load Frankie's body mesh file!");

  csRef<CS::Animation::iBodyManager> bodyManager =
    csQueryRegistry<CS::Animation::iBodyManager> (hairTest->GetObjectRegistry ());
  csRef<CS::Animation::iBodySkeleton> bodySkeleton = bodyManager->FindBodySkeleton ("frankie_body");
  if (!bodySkeleton)
    return hairTest->ReportError ("Can't find Frankie's body mesh description!");

  // Create a new animation tree. The structure of the tree is:
  //   + ragdoll controller node (root node - only if physics are enabled)
  //     + 'LookAt' controller node
  //       + 'speed' controller node
  //         + animation nodes for all speeds
  csRef<CS::Animation::iSkeletonAnimPacketFactory> animPacketFactory =
    animeshFactory->GetSkeletonFactory ()->GetAnimationPacket ();

  // Create the 'random' node
  csRef<CS::Animation::iSkeletonRandomNodeFactory> randomNodeFactory =
    animPacketFactory->CreateRandomNode ("random");
  randomNodeFactory->SetAutomaticSwitch (true);

  // Create the 'idle' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idleNodeFactory =
    animPacketFactory->CreateAnimationNode ("idle");
  idleNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Idle1"));

  // Create the 'walk_slow' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkSlowNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk_slow");
  walkSlowNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_WalkSlow"));

  // Create the 'walk' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk");
  walkNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Walk"));

  // Create the 'walk_fast' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> walkFastNodeFactory =
    animPacketFactory->CreateAnimationNode ("walk_fast");
  walkFastNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_WalkFast"));

  // Create the 'footing' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> footingNodeFactory =
    animPacketFactory->CreateAnimationNode ("footing");
  footingNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Runs"));

  // Create the 'run_slow' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runSlowNodeFactory =
    animPacketFactory->CreateAnimationNode ("run_slow");
  runSlowNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_RunSlow"));

  // Create the 'run' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runNodeFactory =
    animPacketFactory->CreateAnimationNode ("run");
  runNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_Run"));

  // Create the 'run_fast' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runFastNodeFactory =
    animPacketFactory->CreateAnimationNode ("run_fast");
  runFastNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_RunFaster"));

  // Create the 'run_jump' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> runJumpNodeFactory =
    animPacketFactory->CreateAnimationNode ("run_jump");
  runJumpNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("Frankie_RunFast2Jump"));

  idleNodeFactory->SetAutomaticReset (true);
  walkSlowNodeFactory->SetAutomaticReset (true);
  walkNodeFactory->SetAutomaticReset (true);
  walkFastNodeFactory->SetAutomaticReset (true);
  footingNodeFactory->SetAutomaticReset (true);
  runSlowNodeFactory->SetAutomaticReset (true);
  runNodeFactory->SetAutomaticReset (true);
  runFastNodeFactory->SetAutomaticReset (true);
  runJumpNodeFactory->SetAutomaticReset (true);

  idleNodeFactory->SetAutomaticStop (false);
  walkSlowNodeFactory->SetAutomaticStop (false);
  walkNodeFactory->SetAutomaticStop (false);
  walkFastNodeFactory->SetAutomaticStop (false);
  footingNodeFactory->SetAutomaticStop (false);
  runSlowNodeFactory->SetAutomaticStop (false);
  runNodeFactory->SetAutomaticStop (false);
  runFastNodeFactory->SetAutomaticStop (false);
  runJumpNodeFactory->SetAutomaticStop (false);

  randomNodeFactory->AddNode (idleNodeFactory, 1.0f);
  randomNodeFactory->AddNode (walkSlowNodeFactory, 1.0f);
  randomNodeFactory->AddNode (walkNodeFactory, 1.0f);
  randomNodeFactory->AddNode (walkFastNodeFactory, 1.0f);
  randomNodeFactory->AddNode (footingNodeFactory, 1.0f);
  randomNodeFactory->AddNode (runSlowNodeFactory, 1.0f);
  randomNodeFactory->AddNode (runNodeFactory, 1.0f);
  randomNodeFactory->AddNode (runFastNodeFactory, 1.0f);
  randomNodeFactory->AddNode (runJumpNodeFactory, 1.0f);

  if (hairTest->physicsEnabled)
  {
    // Create the ragdoll controller
    csRef<CS::Animation::iSkeletonRagdollNodeFactory> ragdollNodeFactory =
      hairTest->ragdollManager->CreateAnimNodeFactory ("ragdoll");
    animPacketFactory->SetAnimationRoot (ragdollNodeFactory);
    ragdollNodeFactory->SetBodySkeleton (bodySkeleton);
    ragdollNodeFactory->SetChildNode (randomNodeFactory);

    // Create a bone chain for the whole body and add it to the ragdoll controller.
    // The chain will be in kinematic mode when Frankie is alive, and in dynamic state
    // when Frankie has been killed.
    bodyChain = bodySkeleton->CreateBodyChain
      ("body_chain", animeshFactory->GetSkeletonFactory ()->FindBone ("Frankie_Main"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("CTRL_Pelvis"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("CTRL_Head"));
    ragdollNodeFactory->AddBodyChain (bodyChain, CS::Animation::STATE_KINEMATIC);

    // Create a bone chain for the tail of Frankie and add it to the ragdoll controller.
    // The chain will be in kinematic mode most of the time, and in dynamic mode when the
    // user ask for it with the 'f' key or when Frankie has been killed.
    tailChain = bodySkeleton->CreateBodyChain
      ("tail_chain", animeshFactory->GetSkeletonFactory ()->FindBone ("Tail_1"));
    tailChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("Tail_8"));
    ragdollNodeFactory->AddBodyChain (tailChain, CS::Animation::STATE_KINEMATIC);
  }

  else
    animPacketFactory->SetAnimationRoot (randomNodeFactory);

  // Create the animated mesh
  csRef<iMeshWrapper> avatarMesh =
    hairTest->engine->CreateMeshWrapper (meshfact, "Frankie",
					   hairTest->room, csVector3 (0.0f));
  animesh = scfQueryInterface<CS::Mesh::iAnimatedMesh> (avatarMesh->GetMeshObject ());

  // When the animated mesh is created, the animation nodes are created too.
  // We can therefore set them up now.
  CS::Animation::iSkeletonAnimNode* rootNode =
    animesh->GetSkeleton ()->GetAnimationPacket ()->GetAnimationRoot ();

  // Setup of the ragdoll controller
  if (hairTest->physicsEnabled)
  {
    ragdollNode =
      scfQueryInterface<CS::Animation::iSkeletonRagdollNode> (rootNode->FindNode ("ragdoll"));

    // Start the ragdoll animation node in order to have the rigid bodies created
    ragdollNode->SetDynamicSystem (hairTest->dynamicSystem);
    ragdollNode->Play ();
  }

  // Load fur material
  rc = hairTest-> loader ->Load ("/lib/hairtest/fur_material_frankie.xml");
  if (!rc.success)
    hairTest->ReportError("Can't load Fur library file!");

  // Load material shader
  csRef<iMaterialWrapper> materialWrapper = 
    hairTest->engine->FindMaterial("kajiya_and_kay_frankie");
  if (!materialWrapper)
    hairTest->ReportError("Can't find fur material!");

  // Surface properties
  animesh->GetSubMesh(0)->SetMaterial(materialWrapper);

  // Get plugin manager
  csRef<iPluginManager> plugmgr = 
    csQueryRegistry<iPluginManager> (hairTest->GetObjectRegistry ());
  if (!plugmgr)
    return hairTest->ReportError("Failed to locate Plugin Manager!");

  // Load furMesh
  csRef<CS::Mesh::iFurMeshType> furMeshType = 
    csQueryRegistry<CS::Mesh::iFurMeshType> (hairTest->GetObjectRegistry ());
  if (!furMeshType)
    return hairTest->ReportError("Failed to locate CS::Mesh::iFurMeshType plugin!");

  csRef<iRigidBody> mainBody = ragdollNode->GetBoneRigidBody
    (animeshFactory->GetSkeletonFactory ()->FindBone ("Frankie_Main"));

  // Load animationPhysicsControl
  animationPhysicsControl = scfQueryInterface<CS::Animation::iFurAnimatedMeshControl>
      (furMeshType->CreateFurAnimatedMeshControl("frankie_fur_animation"));

  animationPhysicsControl->SetAnimatedMesh(animesh);

  furPhysicsControl = scfQueryInterface<CS::Animation::iFurPhysicsControl>
    (furMeshType->CreateFurPhysicsControl("frankie_fur_physics"));

  furPhysicsControl->SetBulletDynamicSystem(hairTest->bulletDynamicSystem);
  furPhysicsControl->SetRigidBody(mainBody);
  furPhysicsControl->SetAnimatedMesh(animesh);

  // Create hairMeshProperties
  csRef<CS::Mesh::iFurMeshMaterialProperties> hairMeshProperties = 
    furMeshType->CreateFurMeshBasicProperties("frankie_kajiya_and_kay");

  hairMeshProperties->SetMaterial(materialWrapper->GetMaterial());

  iSector* sector = hairTest->engine->FindSector("room");

  if (!sector)
    return hairTest->ReportError("Could not find default room!");

  frankieFurmeshObject->GetMovable()->SetSector(sector);
  frankieFurmeshObject->GetMovable()->UpdateMove();

  csRef<iMeshObject> imo = frankieFurmeshObject->GetMeshObject();

  // Get reference to the iFurMesh interface
  furMesh = scfQueryInterface<CS::Mesh::iFurMesh>(imo);

  furMesh->SetFurMeshProperties(hairMeshProperties);

  furMesh->SetAnimatedMesh(animesh);
  furMesh->SetMeshFactory(animeshFactory);
  furMesh->SetMeshFactorySubMesh(animesh -> GetSubMesh(0)->GetFactorySubMesh());
  furMesh->GenerateGeometry(hairTest->view, hairTest->room);

  furMesh->SetAnimationControl(animationPhysicsControl);
  furMesh->StartAnimationControl();

  furMesh->SetGuideLOD(0);
  furMesh->SetStrandLOD(1);

  // Reset the scene so as to put the parameters of the animation nodes in a default state
  ResetScene ();

  return true;
}
Exemplo n.º 8
0
bool KrystalScene::CreateAvatar ()
{
  printf ("Loading Krystal...\n");

  // Load animesh factory
  csLoadResult rc = hairTest->loader->Load ("/lib/krystal/krystal.xml");
  if (!rc.success)
    return hairTest->ReportError ("Can't load Krystal library file!");

  csRef<iMeshFactoryWrapper> meshfact =
    hairTest->engine->FindMeshFactory ("krystal");
  if (!meshfact)
    return hairTest->ReportError ("Can't find Krystal's mesh factory!");

  animeshFactory = scfQueryInterface<CS::Mesh::iAnimatedMeshFactory>
    (meshfact->GetMeshObjectFactory ());
  if (!animeshFactory)
    return hairTest->ReportError ("Can't find Krystal's animesh factory!");

  // Load bodymesh (animesh's physical properties)
  rc = hairTest->loader->Load ("/lib/krystal/skelkrystal_body");
  if (!rc.success)
    return hairTest->ReportError ("Can't load Krystal's body mesh file!");

  csRef<CS::Animation::iBodyManager> bodyManager =
    csQueryRegistry<CS::Animation::iBodyManager> (hairTest->GetObjectRegistry ());
  csRef<CS::Animation::iBodySkeleton> bodySkeleton = 
    bodyManager->FindBodySkeleton ("krystal_body");
  if (!bodySkeleton)
    return hairTest->ReportError ("Can't find Krystal's body mesh description!");

  // Load some fur
  rc = hairTest->loader->Load ("/lib/hairtest/krystal_furmesh.xml");
  if (!rc.success)
    return hairTest->ReportError ("Can't load krystal furmesh library!");

  csRef<iMeshWrapper> krystalFurmeshObject = 
    hairTest->engine->FindMeshObject ("krystal_furmesh_object");
  if (!krystalFurmeshObject)
    return hairTest->ReportError ("Can't find fur mesh object!");

  // Get plugin manager
  csRef<iPluginManager> plugmgr = 
    csQueryRegistry<iPluginManager> (hairTest->GetObjectRegistry ());
  if (!plugmgr)
    return hairTest->ReportError("Failed to locate Plugin Manager!");

  // Find the fur mesh plugin
  csRef<CS::Mesh::iFurMeshType> furMeshType = 
    csQueryRegistry<CS::Mesh::iFurMeshType> (hairTest->GetObjectRegistry ());
  if (!furMeshType)
    return hairTest->ReportError("Failed to locate CS::Mesh::iFurMeshType plugin!");

  // Create a new animation tree. The structure of the tree is:
  //   + ragdoll controller node (root node - only if physics are enabled)
  //     + Random node
  //       + idle animation nodes
  csRef<CS::Animation::iSkeletonAnimPacketFactory> animPacketFactory =
    animeshFactory->GetSkeletonFactory ()->GetAnimationPacket ();

  // Create the 'random' node
  csRef<CS::Animation::iSkeletonRandomNodeFactory> randomNodeFactory =
    animPacketFactory->CreateRandomNode ("random");
  randomNodeFactory->SetAutomaticSwitch (true);

  // Create the 'idle01' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle01NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle01");
  idle01NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle01"));

  // Create the 'idle02' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle02NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle02");
  idle02NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle02"));

  // Create the 'idle03' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle03NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle03");
  idle03NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle03"));

  // Create the 'idle04' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle04NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle04");
  idle04NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle04"));

  // Create the 'idle05' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle05NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle05");
  idle05NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle05"));

  // Create the 'idle06' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> idle06NodeFactory =
    animPacketFactory->CreateAnimationNode ("idle06");
  idle06NodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("idle06"));

  // Create the 'stand' animation node
  csRef<CS::Animation::iSkeletonAnimationNodeFactory> standNodeFactory =
    animPacketFactory->CreateAnimationNode ("stand");
  standNodeFactory->SetAnimation
    (animPacketFactory->FindAnimation ("stand"));

  idle01NodeFactory->SetAutomaticReset (true);
  idle02NodeFactory->SetAutomaticReset (true);
  idle03NodeFactory->SetAutomaticReset (true);
  idle04NodeFactory->SetAutomaticReset (true);
  idle05NodeFactory->SetAutomaticReset (true);
  idle06NodeFactory->SetAutomaticReset (true);
  standNodeFactory->SetAutomaticReset (true);

  idle01NodeFactory->SetAutomaticStop (false);
  idle02NodeFactory->SetAutomaticStop (false);
  idle03NodeFactory->SetAutomaticStop (false);
  idle04NodeFactory->SetAutomaticStop (false);
  idle05NodeFactory->SetAutomaticStop (false);
  idle06NodeFactory->SetAutomaticStop (false);
  standNodeFactory->SetAutomaticStop (false);
  
  randomNodeFactory->AddNode (idle01NodeFactory, 1.0f);
  randomNodeFactory->AddNode (idle02NodeFactory, 1.0f);
//   randomNodeFactory->AddNode (idle03NodeFactory, 1.0f);
  randomNodeFactory->AddNode (idle04NodeFactory, 1.0f);
//   randomNodeFactory->AddNode (idle05NodeFactory, 1.0f);
  randomNodeFactory->AddNode (idle06NodeFactory, 1.0f);
  randomNodeFactory->AddNode (standNodeFactory, 1.0f);
  
  if (hairTest->physicsEnabled)
  {
    // Create the ragdoll controller
    csRef<CS::Animation::iSkeletonRagdollNodeFactory> ragdollNodeFactory =
      hairTest->ragdollManager->CreateAnimNodeFactory ("ragdoll");
    animPacketFactory->SetAnimationRoot (ragdollNodeFactory);
    ragdollNodeFactory->SetBodySkeleton (bodySkeleton);
    ragdollNodeFactory->SetChildNode (randomNodeFactory);

    // Create bone chain for whole body and add it to the ragdoll controller. The chain
    // will be in kinematic mode when Krystal is alive, and in dynamic state when
    // Krystal has been killed.
    bodyChain = bodySkeleton->CreateBodyChain
      ("body_chain", animeshFactory->GetSkeletonFactory ()->FindBone ("Hips"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("Head"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("RightFoot"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("RightHand"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("LeftFoot"));
    bodyChain->AddSubChain (animeshFactory->GetSkeletonFactory ()->FindBone ("LeftHand"));
    ragdollNodeFactory->AddBodyChain (bodyChain, CS::Animation::STATE_KINEMATIC);

  }

  else
    animPacketFactory->SetAnimationRoot (randomNodeFactory);

  // Create the animated mesh
  csRef<iMeshWrapper> avatarMesh =
    hairTest->engine->CreateMeshWrapper (meshfact, "krystal",
    hairTest->room, csVector3 (0.0f));
  animesh = scfQueryInterface<CS::Mesh::iAnimatedMesh> (avatarMesh->GetMeshObject ());

  // When the animated mesh is created, the animation nodes are created too.
  // We can therefore set them up now.
  CS::Animation::iSkeletonAnimNode* rootNode =
    animesh->GetSkeleton ()->GetAnimationPacket ()->GetAnimationRoot ();

  // Setup of the ragdoll controller
  if (hairTest->physicsEnabled)
  {
    ragdollNode =
      scfQueryInterface<CS::Animation::iSkeletonRagdollNode> (rootNode->FindNode ("ragdoll"));

    // Start the ragdoll animation node in order to have the rigid bodies created
    ragdollNode->SetDynamicSystem (hairTest->dynamicSystem);
    ragdollNode->Play ();
  }

  csRef<iRigidBody> headBody = ragdollNode->GetBoneRigidBody
    (animeshFactory->GetSkeletonFactory ()->FindBone ("Head"));

  // Load the fur material
  rc = hairTest-> loader ->Load ("/lib/hairtest/fur_material_krystal.xml");
  if (!rc.success)
    hairTest->ReportError ("Can't load Fur library file!");

  // Load the Marschner shader
  csRef<iMaterialWrapper> materialWrapper = 
    hairTest->engine->FindMaterial ("marschner_material");
  if (!materialWrapper)
    hairTest->ReportError ("Can't find marschner material!");

  // Create the fur properties for the hairs
  csRef<CS::Mesh::iFurMeshMaterialProperties> hairMeshProperties = 
    furMeshType->CreateHairMeshMarschnerProperties ("krystal_marschner");
  hairMeshProperties->SetMaterial(materialWrapper->GetMaterial ());
  animesh->GetSubMesh (1)->SetMaterial (materialWrapper);

  // Create the two possible animation control's. We'll switch between than at the user request.
  hairPhysicsControl = scfQueryInterface<CS::Animation::iFurPhysicsControl>
    (furMeshType->CreateFurPhysicsControl ("krystal_hairs_physics"));
  animationPhysicsControl = scfQueryInterface<CS::Animation::iFurAnimatedMeshControl>
    (furMeshType->CreateFurAnimatedMeshControl ("krystal_hairs_animation"));

  hairPhysicsControl->SetBulletDynamicSystem (hairTest->bulletDynamicSystem);
  hairPhysicsControl->SetRigidBody (headBody);
//   hairPhysicsControl->SetAnimatedMesh(animesh);

  animationPhysicsControl->SetAnimatedMesh (animesh);

  iSector* sector = hairTest->engine->FindSector("room");

  if (!sector)
    return hairTest->ReportError("Could not find default room!");

  krystalFurmeshObject->GetMovable()->SetSector(sector);
  krystalFurmeshObject->GetMovable()->UpdateMove();

  csRef<iMeshObject> imo = krystalFurmeshObject->GetMeshObject();

  // Get reference to the iFurMesh interface
  furMesh = scfQueryInterface<CS::Mesh::iFurMesh> (imo);

  csRef<CS::Mesh::iFurMeshState> ifms = 
    scfQueryInterface<CS::Mesh::iFurMeshState> (furMesh);

  animationPhysicsControl->SetDisplacement (ifms->GetDisplacement ());

  furMesh->SetFurMeshProperties (hairMeshProperties);

  furMesh->SetAnimatedMesh (animesh);
  furMesh->SetMeshFactory (animeshFactory);
  furMesh->SetMeshFactorySubMesh (animesh->GetSubMesh (2)->GetFactorySubMesh ());
  furMesh->GenerateGeometry (hairTest->view, hairTest->room);

  furMesh->SetAnimationControl (hairPhysicsControl);
  furMesh->StartAnimationControl ();

  furMesh->SetGuideLOD (0);
  furMesh->SetStrandLOD (1);
  furMesh->SetControlPointsLOD (0.0f);

  // Reset the scene so as to put the parameters of the animation nodes in a default state
  ResetScene ();

  return true;
}