ogre一些代码(人物 动作 地形 粒子系统)ogre例子的改写

/*
 -----------------------------------------------------------------------------
 This source file is part of OGRE
 (Object-oriented Graphics Rendering Engine)
 For the latest info, see http://www.ogre3d.org/
 
 Copyright (c) 2000-2012 Torus Knot Software Ltd
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 -----------------------------------------------------------------------------
 */
#include "OgrePlatform.h"

#include "SampleBrowser.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include "OgreString.h"
#elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include "SampleBrowser_OSX.h"
#elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
#include "SampleBrowser_iOS.h"
#elif OGRE_PLATFORM == OGRE_PLATFORM_NACL
#include "SampleBrowser_NaCl.h"
#endif
#include "OgreTerrain.h"
#include "OgreTerrainGroup.h"
#if OGRE_PLATFORM != OGRE_PLATFORM_NACL

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR cmdLine, INT)
#else
int main(int argc, char *argv[])
#endif
{
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 int retVal = UIApplicationMain(argc, argv, @"UIApplication", @"AppDelegate");
 [pool release];
 return retVal;
#elif (OGRE_PLATFORM == OGRE_PLATFORM_APPLE) && __LP64__
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    mAppDelegate = [[AppDelegate alloc] init];
    [[NSApplication sharedApplication] setDelegate:mAppDelegate];
 int retVal = NSApplicationMain(argc, (const char **) argv);

 [pool release];

 return retVal;
#else

 try
 {
        bool nograb = false;
#if OGRE_PLATFORM != OGRE_PLATFORM_WIN32
        if (argc >= 2 && Ogre::String(argv[1]) == "nograb")
            nograb = true;
#else
        // somewhat hacky, but much simpler than other solutions
        if (Ogre::String(cmdLine).find("nograb") != Ogre::String::npos)
            nograb = true;
#endif
  //OgreBites::SampleBrowser brows (nograb);
  //brows.go();
  //if(0)
   //return 0;
  //create root
  Ogre::Root* rt=new Ogre::Root("C:\\workspace\\ogre\\ogre4vs2010\\MyOgre\\Debug\\plugins.cfg",
   "C:\\workspace\\ogre\\ogre4vs2010\\MyOgre\\Debug\\ogre.cfg",
   "C:\\workspace\\ogre\\ogre4vs2010\\MyOgre\\Debug\\ogre.log");
  if(!rt->restoreConfig())
   rt->showConfigDialog();

  //create window
  Ogre::RenderWindow* rw=rt->initialise(true);
  size_t winHandler=0;
  rw->getCustomAttribute("WINDOW",&winHandler);
  rw->setAutoUpdated(true);

  //create input system
  std::ostringstream winHandlerStr;
  winHandlerStr<<winHandler;
  OIS::ParamList pl;
  pl.insert(std::make_pair("WINDOW",winHandlerStr.str()));
  OIS::InputManager* im=OIS::InputManager::createInputSystem(pl);
  
  /**Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
   "C:\\workspace\\ogre\\ogre4vs2010\\OgreSDK_vc10_v1-8-1\\media\\models"
   ,"FileSystem"
   ,"models");
  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
   "C:\\workspace\\ogre\\ogre4vs2010\\OgreSDK_vc10_v1-8-1\\media\\materials\\scripts"
   ,"FileSystem"
   ,"materials");
   **/

  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
   "C:\\workspace\\ogre\\ogre4vs2010\\OgreSDK_vc10_v1-8-1\\media\\materials\\textures"
   ,"FileSystem"
   ,Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  
  Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
   "C:\\workspace\\ogre\\ogre4vs2010\\OgreSDK_vc10_v1-8-1\\media\\materials\\textures\\nvidia"
   ,"FileSystem"
   ,Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  
   Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
   "C:\\ogremaxexport"
   ,"FileSystem"
   ,"dc");
   Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
 Ogre::ResourceGroupManager::getSingleton().loadResourceGroup("dc");
 //Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
 Ogre::StringVectorPtr dc=Ogre::ResourceGroupManager::getSingleton().listResourceNames("dc");
 Ogre::StringVectorPtr ds=Ogre::ResourceGroupManager::getSingleton().listResourceLocations("dc");
 //Ogre::MaterialManager.
  /**
  Ogre::OverlayManager& om=Ogre::OverlayManager::getSingleton();
  Ogre::Overlay* ol=om.create("test");

  Ogre::OverlayContainer* testPanel=(Ogre::OverlayContainer*)om.createOverlayElementFromTemplate("SdkTrays/Cursor","","");
  testPanel->setMaterialName("BaseWhite");
  ol->add2D(testPanel);
  ol->setZOrder(10);
  
  ol->show();
  **/
  /**
  Ogre::Overlay* overlay=om.create("overlay");
  overlay->setZOrder(100);
  Ogre::OverlayContainer* ole=(Ogre::OverlayContainer*)om.createOverlayElement("Panel","Backdrop");
  overlay->add2D(ole);
  overlay->show();
  **/
  rw->removeAllViewports();
  
 
  //create scenemanager and camera
  Ogre::SceneManager* sm=rt->createSceneManager(Ogre::ST_GENERIC,"testScence");
  Ogre::Camera* oc=sm->createCamera("testCamera");
  /**
  oc->setNearClipDistance(0.1);
  oc->setPosition(-3,0,0);
  oc->lookAt(1,0,0);
  **/
  
  Ogre::Viewport* ovp=rw->addViewport(oc);
  ovp->setBackgroundColour(Ogre::ColourValue(0.8f, 0.8f, 0.8f));
  //sm->setFog(Ogre::FOG_LINEAR, Ogre::ColourValue(0.2f, 0.2f, 0.2f), 0, 15, 100);
 

  /**
  //save camera status
  Ogre::NameValuePairList state;
  state["CameraPosition"] = Ogre::StringConverter::toString(oc->getPosition());
  state["CameraOrientation"] = Ogre::StringConverter::toString(oc->getOrientation());
  oc->setPosition(Ogre::StringConverter::parseVector3(state["CameraPosition"]));
  oc->setOrientation(Ogre::StringConverter::parseQuaternion(state["CameraOrientation"]));

  
  //create entity and attach to scence node
  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  /**
  Ogre::Entity* et=sm->createEntity("robot.mesh");
  //Ogre::ResourceGroupManager::getSingleton().create
  //et->setMaterial();
  Ogre::SceneNode* node=sm->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(0,0,0));
  node->attachObject(et);
  node->roll(Ogre::Radian(0.9));
  node->scale(1,1,3);

  Ogre::Entity* et2=sm->createEntity("ogrehead.mesh");
  Ogre::SceneNode* node2=sm->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(0,0,100));
  node2->attachObject(et2);
  **/
  using namespace Ogre;
  /**
  Light* light = sm->createLight();
  light->setType(Light::LT_POINT);
  light->setPosition(-10, 0, 0);
  light->setSpecularColour(ColourValue::White);
  **/
  Ogre::Entity* et=sm->createEntity("__.mesh");
  et->getSubEntity(0)->setMaterialName("dcy","dc");
  et->getSubEntity(1)->setMaterialName("dcyzs","dc");
  et->getSubEntity(2)->setMaterialName("dct","dc");
  et->getSubEntity(3)->setMaterialName("dcts","dc");
  Ogre::SceneNode* node=sm->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(10,-3,0));
  node->attachObject(et);
  const Ogre::Radian or(0.9);
  //node->roll(Ogre::Radian(0.9));
  node->rotate(Ogre::Vector3(0,1,0),Ogre::Radian(0.9));
 
     Ogre::AnimationState* ao= et->getAnimationState("releax");
  ao->setLoop(true);
  ao->setEnabled(true);
  node->scale(3,3,3);
  //set light
  sm->setAmbientLight(Ogre::ColourValue(1,1,1));
  
  /**
  Ogre::ParticleSystem* ps= sm->createParticleSystem("Nimbus", "PEExamples/blast"); 
  node->attachObject(ps);
  ps->setVisible("true");
  **/
  //start rendering
  //rt->startRendering();
    

  //sm->setAmbientLight(ColourValue(0.2, 0.2, 0.2));
  //terrain
  Vector3 mTerrainPos(1000,0,5000);
  oc->setPosition(mTerrainPos + Vector3(1683, 50, 2116));
  oc->lookAt(Vector3(1963, 50, 1660));
  oc->setNearClipDistance(0.1);
  oc->setFarClipDistance(50000);

  TerrainGlobalOptions* mTerrainGlobals = OGRE_NEW Ogre::TerrainGlobalOptions();

  Light* l = sm->createLight("tstLight");
  l->setType(Light::LT_DIRECTIONAL);
  Vector3 lightdir(0.55, -0.3, 0.75);
  l->setDirection(lightdir);
  l->setDiffuseColour(ColourValue::White);
  l->setSpecularColour(ColourValue(0.4, 0.4, 0.4));
  #define TERRAIN_WORLD_SIZE 12000.0f
  #define TERRAIN_SIZE 513
  Ogre::TerrainGroup* mTerrainGroup = OGRE_NEW Ogre::TerrainGroup(sm, Terrain::ALIGN_X_Z, TERRAIN_SIZE, TERRAIN_WORLD_SIZE);
  mTerrainGroup->setFilenameConvention("test", "sad");
 // Vector3 mTerrainPos(1000,0,5000);
  mTerrainGroup->setOrigin(mTerrainPos);

   mTerrainGlobals->setMaxPixelError(8);
  // testing composite map
  mTerrainGlobals->setCompositeMapDistance(3000);
  //mTerrainGlobals->setUseRayBoxDistanceCalculation(true);
  //mTerrainGlobals->getDefaultMaterialGenerator()->setDebugLevel(1);
  //mTerrainGlobals->setLightMapSize(256);

  //matProfile->setLightmapEnabled(false);
  // Important to set these so that the terrain knows what to use for derived (non-realtime) data
  mTerrainGlobals->setLightMapDirection(l->getDerivedDirection());
  mTerrainGlobals->setCompositeMapAmbient(sm->getAmbientLight());
  //mTerrainGlobals->setCompositeMapAmbient(ColourValue::Red);
  mTerrainGlobals->setCompositeMapDiffuse(l->getDiffuseColour());

  // Configure default import settings for if we use imported image
  Ogre::Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
  defaultimp.terrainSize = TERRAIN_SIZE;
  defaultimp.worldSize = TERRAIN_WORLD_SIZE;
  defaultimp.inputScale = 600;
  defaultimp.minBatchSize = 33;
  defaultimp.maxBatchSize = 65;
  // textures
  defaultimp.layerList.resize(3);
  defaultimp.layerList[0].worldSize = 100;
  defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_diffusespecular.dds");
  defaultimp.layerList[0].textureNames.push_back("dirt_grayrocky_normalheight.dds");
  defaultimp.layerList[1].worldSize = 30;
  defaultimp.layerList[1].textureNames.push_back("grass_green-01_diffusespecular.dds");
  defaultimp.layerList[1].textureNames.push_back("grass_green-01_normalheight.dds");
  defaultimp.layerList[2].worldSize = 200;
  defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_diffusespecular.dds");
  defaultimp.layerList[2].textureNames.push_back("growth_weirdfungus-03_normalheight.dds");

  int x=0;int y=0;
  //String filename = mTerrainGroup->generateFilename(x, y);
   //if (ResourceGroupManager::getSingleton().resourceExists(mTerrainGroup->getResourceGroup(), filename))
   //{
    //mTerrainGroup->defineTerrain(x, y);
   //}
   //else
   {
    Image img;

    //getTerrainImage(x % 2 != 0, y % 2 != 0, img);
    {
      img.load("terrain.png", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
      if (x % 2 != 0)
       img.flipAroundY();
      if (y % 2 != 0)
       img.flipAroundX();
    }
    mTerrainGroup->defineTerrain(x, y, &img);
    //mTerrainsImported = true;
   }

  mTerrainGroup->loadAllTerrains(true);

  TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
   while(ti.hasMoreElements())
   {
    Terrain* terrain = ti.getNext()->instance;
    //initBlendMaps(terrain);
    {
      TerrainLayerBlendMap* blendMap0 = terrain->getLayerBlendMap(1);
      TerrainLayerBlendMap* blendMap1 = terrain->getLayerBlendMap(2);
      Real minHeight0 = 70;
      Real fadeDist0 = 40;
      Real minHeight1 = 70;
      Real fadeDist1 = 15;
      float* pBlend1 = blendMap1->getBlendPointer();
      for (Ogre::uint16 y = 0; y < terrain->getLayerBlendMapSize(); ++y)
      {
       for (Ogre::uint16 x = 0; x < terrain->getLayerBlendMapSize(); ++x)
       {
        Real tx, ty;

        blendMap0->convertImageToTerrainSpace(x, y, &tx, &ty);
        Real height = terrain->getHeightAtTerrainPosition(tx, ty);
        Real val = (height - minHeight0) / fadeDist0;
        val = Math::Clamp(val, (Real)0, (Real)1);
        //*pBlend0++ = val;

        val = (height - minHeight1) / fadeDist1;
        val = Math::Clamp(val, (Real)0, (Real)1);
        *pBlend1++ = val;

       }
      }
      blendMap0->dirty();
      blendMap1->dirty();
      //blendMap0->loadImage("blendmap1.png", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
      blendMap0->update();
      blendMap1->update();
    }
   }
   mTerrainGroup->freeTemporaryResources();

  while(1){
   ao->addTime(0.01);
   rt->renderOneFrame();
  }
  //clear scene and destroy it
  sm->clearScene();
  rt->destroySceneManager(sm);

  //unload resource
  Ogre::ResourceGroupManager::ResourceManagerIterator resMgrs =
   Ogre::ResourceGroupManager::getSingleton().getResourceManagerIterator();

   while (resMgrs.hasMoreElements())
   {
    resMgrs.getNext()->unloadUnreferencedResources();
   }

 }
 catch (Ogre::Exception& e)
 {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
#else
  std::cerr << "An exception has occurred: " << e.getFullDescription().c_str() << std::endl;
#endif
 }

#endif
 return 0;
}

#endif   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值