#ifndef __basetutorial_h_
#define __basetutorial_h_
#include "BaseApplication.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif
#include <Terrain/OgreTerrain.h>
#include <Terrain/OgreTerrainGroup.h>
#include "BaseApplication.h"
#include <iostream>
#include <list>
using namespace Ogre;
using namespace std;
class basetutorial : public BaseApplication
{
public:
basetutorial(void);
virtual ~basetutorial(void);
private:
Ogre::TerrainGlobalOptions* mTerrainGlobals;
Ogre::TerrainGroup* mTerrainGroup;
bool mTerrainsImported;
OgreBites::Label* mInfoLabel;
void defineTerrain(long x, long y);
void initBlendMaps(Ogre::Terrain* terrain);
void configureTerrainDefaults(Ogre::Light* light);
////////////////////////////////////////////////////
////////////////////////////////////////////////////
void create_robot();
protected:
virtual void createScene(void);
virtual void createFrameListener(void);
virtual void destroyScene(void);
virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
///////////////////////////////////////////////////////////////////////////////////////////
virtual bool keyPressed(const OIS::KeyEvent& arg);
virtual bool keyReleased(const OIS::KeyEvent& arg);
///////////////////////////////////////////////////////////////////////////////////////////
virtual bool mouseMoved(const OIS::MouseEvent& arg);
virtual bool mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id);
virtual bool mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id);
Real mRotate;
Real mMove;
SceneNode * mCamNode;
Vector3 mDirection;
struct Robot
{
Entity *rEntity;
SceneNode *rSceneNode;
};
std::list<Robot *> Robot_list;
AnimationState *robot_AnimationState;
};
void getTerrainImage(bool flipX, bool flipY, Ogre::Image& img)
{
img.load("terrain.png", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
if (flipX)
img.flipAroundY();
if (flipY)
img.flipAroundX();
}
//-------------------------------------------------------------------------------------
void basetutorial::defineTerrain(long x, long y)
{
Ogre::String filename = mTerrainGroup->generateFilename(x, y);
if (Ogre::ResourceGroupManager::getSingleton().resourceExists(mTerrainGroup->getResourceGroup(), filename))
{
mTerrainGroup->defineTerrain(x, y);
}
else
{
Ogre::Image img;
getTerrainImage(x % 2 != 0, y % 2 != 0, img);
mTerrainGroup->defineTerrain(x, y, &img);
mTerrainsImported = true;
}
}
//-------------------------------------------------------------------------------------
void basetutorial::initBlendMaps(Ogre::Terrain* terrain)
{
Ogre::TerrainLayerBlendMap* blendMap0 = terrain->getLayerBlendMap(1);
Ogre::TerrainLayerBlendMap* blendMap1 = terrain->getLayerBlendMap(2);
Ogre::Real minHeight0 = 70;
Ogre::Real fadeDist0 = 40;
Ogre::Real minHeight1 = 70;
Ogre::Real fadeDist1 = 15;
float* pBlend0 = blendMap0->getBlendPointer();
float* pBlend1 = blendMap1->getBlendPointer();
for (Ogre::uint16 y = 0; y < terrain->getLayerBlendMapSize(); ++y)
{
for (Ogre::uint16 x = 0; x < terrain->getLayerBlendMapSize(); ++x)
{
Ogre::Real tx, ty;
blendMap0->convertImageToTerrainSpace(x, y, &tx, &ty);
Ogre::Real height = terrain->getHeightAtTerrainPosition(tx, ty);
Ogre::Real val = (height - minHeight0) / fadeDist0;
val = Ogre::Math::Clamp(val, (Ogre::Real)0, (Ogre::Real)1);
*pBlend0++ = val;
val = (height - minHeight1) / fadeDist1;
val = Ogre::Math::Clamp(val, (Ogre::Real)0, (Ogre::Real)1);
*pBlend1++ = val;
}
}
blendMap0->dirty();
blendMap1->dirty();
blendMap0->update();
blendMap1->update();
}
//-------------------------------------------------------------------------------------
void basetutorial::configureTerrainDefaults(Ogre::Light* light)
{
// Configure global
mTerrainGlobals->setMaxPixelError(8);
// testing composite map
mTerrainGlobals->setCompositeMapDistance(3000);
// Important to set these so that the terrain knows what to use for derived (non-realtime) data
mTerrainGlobals->setLightMapDirection(light->getDerivedDirection());
mTerrainGlobals->setCompositeMapAmbient(mSceneMgr->getAmbientLight());
mTerrainGlobals->setCompositeMapDiffuse(light->getDiffuseColour());
// Configure default import settings for if we use imported image
Ogre::Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
defaultimp.terrainSize = 513;
defaultimp.worldSize = 12000.0f;
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");
}
bool basetutorial::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
bool ret = BaseApplication::frameRenderingQueued(evt);
if (mTerrainGroup->isDerivedDataUpdateInProgress())
{
mTrayMgr->moveWidgetToTray(mInfoLabel, OgreBites::TL_TOP, 0);
mInfoLabel->show();
if (mTerrainsImported)
{
mInfoLabel->setCaption("Building terrain, please wait...");
}
else
{
mInfoLabel->setCaption("Updating textures, patience...");
}
}
else
{
mTrayMgr->removeWidgetFromTray(mInfoLabel);
mInfoLabel->hide();
if (mTerrainsImported)
{
mTerrainGroup->saveAllTerrains(true);
mTerrainsImported = false;
}
}
if(mWindow->isClosed()) return false;
if(mShutDown)return false;
mKeyboard->capture();
mMouse->capture();
mTrayMgr->frameRenderingQueued(evt);
mCamNode->translate(mDirection *evt.timeSinceLastFrame, Node::TS_LOCAL);
robot_AnimationState->addTime(evt.timeSinceLastFrame);
//return ret;
return true;
}
void basetutorial::createFrameListener(void)
{
BaseApplication::createFrameListener();
mCamNode = mCamera->getParentSceneNode();
mRotate = 0.13;
mMove = 250;
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
mDirection = Vector3::ZERO;
mInfoLabel = mTrayMgr->createLabel(OgreBites::TL_TOP, "TInfo", "", 350);
}
void basetutorial::destroyScene(void)
{
OGRE_DELETE mTerrainGroup;
OGRE_DELETE mTerrainGlobals;
}
////////////////////////////////////////////////////////////////////////////////////////
bool basetutorial::keyPressed(const OIS::KeyEvent& arg)
{
switch (arg.key)
{
case OIS::KC_1:
mCamera->getParentSceneNode()->detachObject(mCamera);
mCamNode = mSceneMgr->getSceneNode("CamNode1");
mCamNode->attachObject(mCamera);
break;
case OIS::KC_2:
mCamera->getParentSceneNode()->detachObject(mCamera);
mCamNode = mSceneMgr->getSceneNode("CamNode2");
mCamNode->attachObject(mCamera);
break;
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = -mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.z = -10*mMove;}
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.z = 10*mMove;}
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = -mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.x = -10*mMove;}
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.x = 10*mMove;}
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y = -mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.y = -10*mMove;}
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y = mMove;
if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
{mDirection.y = 10*mMove;}
break;
case OIS::KC_ESCAPE:
mShutDown = true;
break;
default:
break;
}
return true;
}
bool basetutorial::keyReleased(const OIS::KeyEvent& arg)
{
switch (arg.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = 0;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = 0;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = 0;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = 0;
break;
case OIS::KC_PGDOWN:
case OIS::KC_E:
mDirection.y = 0;
break;
case OIS::KC_PGUP:
case OIS::KC_Q:
mDirection.y = 0;
break;
default:
break;
}
return true;
}
bool basetutorial::mouseMoved(const OIS::MouseEvent& arg)
{
if (arg.state.buttonDown(OIS::MB_Right))
{
mCamNode->yaw(Ogre::Degree(-mRotate * arg.state.X.rel), Ogre::Node::TS_WORLD);
mCamNode->pitch(Ogre::Degree(-mRotate * arg.state.Y.rel), Ogre::Node::TS_LOCAL);
}
return true;
}
bool basetutorial::mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
{
Light *light = mSceneMgr->getLight("Light1");
switch (id)
{
case OIS::MB_Left:
light->setVisible(! light->isVisible());
create_robot();
break;
default:
break;
}
return true;
}
bool basetutorial::mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
{
return true;
}
void basetutorial::create_robot()
{
Robot *NewRobot = new Robot;
memset(NewRobot, 0, sizeof(Robot));
char size_ID[32] = "";
sprintf_s(size_ID,32,"ID%d",(int)NewRobot);
NewRobot->rEntity = mSceneMgr->createEntity(size_ID,"robot.mesh");
NewRobot->rSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
NewRobot->rSceneNode->attachObject(NewRobot->rEntity);
NewRobot->rSceneNode->setPosition(mCamera->getPositionForViewUpdate().x+100,mCamera->getPositionForViewUpdate().y+100,mCamera->getPositionForViewUpdate().z+100);
robot_AnimationState = Robot.rEntity->getAnimationState("Idle"); // 이부분입니다 이런식으로 접근하면 정의되지 않은 엔티티라는 에러가 나오더군여 혹시나 싶어서 protect에서 구조체를 선언해도 접근이 안되네여 ㅠㅠ
robot_AnimationState->setLoop(true);
robot_AnimationState->setEnabled(true);
Robot_list.push_back(NewRobot);
}
#endif // #ifndef __basetutorial_h_
헤더의 소스는 위와 같습니다 로봇이 리스트쪽에서 생성되면 거기에 애니메이션을 주고 싶은데 방법이 도저히 생각이 안나네요