ogre 种草
ogre中来创建一个草地。
class TutorialApplication : public BaseApplication
{
public:
TutorialApplication(void);
virtual ~TutorialApplication(void);
CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID);
protected:
virtual void createScene(void);
void setupCEGui();
CEGUI::OgreRenderer* mRenderer;
bool quit(const CEGUI::EventArgs &e);
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);
virtual void destroyScene();
void createGrassMesh();
private:
};
//---------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void):mRenderer(0)
{
}
//---------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
CEGUI::OgreRenderer::destroySystem();
}
//---------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
// Create your scene here :)
setupCEGui();
createGrassMesh();
mSceneMgr->setAmbientLight(Ogre::ColourValue(1.0, 1.0, 1.0));
mCamera->setPosition(150,50,150);
mCamera->lookAt(0,0,0);
Ogre::Entity* robot = mSceneMgr->createEntity("robot","robot.mesh");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(robot);
Ogre::Plane plane;
plane.normal = Ogre::Vector3::UNIT_Y;
plane.d = 0;
Ogre::MeshManager::getSingleton().createPlane(
"floor",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
plane,
450.0, 450.0,
10, 10, true, 1,
50.0, 50.0,
Ogre::Vector3::UNIT_Z);
Ogre::Entity* planeEntity = mSceneMgr->createEntity("floor");
planeEntity->setMaterialName("Examples/GrassFloor");
planeEntity->setCastShadows(false);
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(planeEntity);
Ogre::Entity* grass = mSceneMgr->createEntity("GrassBladesMesh");
Ogre::StaticGeometry* sg = mSceneMgr->createStaticGeometry("GrassArea");
const int size = 375;
const int amount = 20;
sg->setRegionDimensions(Ogre::Vector3(size, size, size));
sg->setOrigin(Ogre::Vector3(-size/2, 0, -size/2));
for (int x = -size/2; x < size/2; x += (size / amount))
{
for (int z = -size/2; z < size/2; z += (size / amount))
{
Ogre::Real r = size / (float)amount / 2;
Ogre::Vector3 pos(
x + Ogre::Math::RangeRandom(-r, r),
0,
z + Ogre::Math::RangeRandom(-r, r));
Ogre::Vector3 scale(1, Ogre::Math::RangeRandom(0.9, 1.1), 1);
Ogre::Quaternion orientation;
orientation.FromAngleAxis(
Ogre::Degree(Ogre::Math::RangeRandom(0, 359)),
Ogre::Vector3::UNIT_Y);
sg->addEntity(grass, pos, orientation, scale);
}
}
sg->build();
}
bool TutorialApplication::quit(const CEGUI::EventArgs &e)
{
mShutDown = true;
return true;
}
//---------------------------------------------------------------------------
bool TutorialApplication::mouseMoved(const OIS::MouseEvent &arg)
{
//关联鼠标
if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseMove(arg.state.X.rel, arg.state.Y.rel))
{
return true;
}
return BaseApplication::mouseMoved(arg);
}
//---------------------------------------------------------------------------
bool TutorialApplication::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
if(context.injectMouseButtonDown(convertButton(id)))
{
if (id == OIS::MB_Left)
{
}
else if (id == OIS::MB_Right)
{
}
return true;
}
//关联鼠标
return BaseApplication::mousePressed(arg,id);
}
//---------------------------------------------------------------------------
bool TutorialApplication::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
if (id == OIS::MB_Left)
{
}
//关联鼠标
if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(convertButton(id))) return true;
return BaseApplication::mouseReleased(arg,id);
}
CEGUI::MouseButton TutorialApplication::convertButton(OIS::MouseButtonID buttonID)
{
//关联鼠标按键
switch (buttonID)
{
case OIS::MB_Left:
return CEGUI::LeftButton;
break;
case OIS::MB_Right:
return CEGUI::RightButton;
break;
case OIS::MB_Middle:
return CEGUI::MiddleButton;
break;
default:
return CEGUI::LeftButton;
break;
}
}
void TutorialApplication::setupCEGui()
{
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing CEGUI ***");
mRenderer = &CEGUI::OgreRenderer::bootstrapSystem();
//设置资源路径
CEGUI::ImageManager::setImagesetDefaultResourceGroup("Imagesets");
CEGUI::Font::setDefaultResourceGroup("Fonts");
CEGUI::Scheme::setDefaultResourceGroup("Schemes");
CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
CEGUI::WindowManager::setDefaultResourceGroup("Layouts");
//设置鼠标图片
CEGUI::SchemeManager::getSingleton().createFromFile("TaharezLook.scheme");
CEGUI::FontManager::getSingleton().createFromFile("DejaVuSans-10.font");
CEGUI::System::getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");
CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
context.setDefaultFont("DejaVuSans-10");
context.getMouseCursor().setDefaultImage("TaharezLook/MouseArrow");
Ogre::LogManager::getSingletonPtr()->logMessage("Finished");
//创建窗口
CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::Window* rootWin = wmgr.createWindow("DefaultWindow", "CEGUIDemo/Sheet");
CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(rootWin);
}
void TutorialApplication::destroyScene()
{
}
void TutorialApplication::createGrassMesh()
{
const float width = 25;
const float height = 30;
Ogre::Vector3 vec(width/2,0,0);
Ogre::ManualObject obj("GrassObject");
Ogre::Quaternion quat;
quat.FromAngleAxis(Ogre::Degree(60),Ogre::Vector3::UNIT_Y);
obj.begin("Examples/GrassBlades",Ogre::RenderOperation::OT_TRIANGLE_LIST);
for (int i = 0;i < 3;++i)
{
obj.position(-vec.x, height, -vec.z);
obj.textureCoord(0, 0);
obj.position(vec.x, height, vec.z);
obj.textureCoord(1, 0);
obj.position(-vec.x, 0, -vec.z);
obj.textureCoord(0, 1);
obj.position(vec.x, 0, vec.z);
obj.textureCoord(1, 1);
int offset = 4 * i;
obj.triangle(offset, offset + 3, offset + 1);
obj.triangle(offset, offset + 2, offset + 3);
vec = quat * vec;
}
obj.end();
obj.convertToMesh("GrassBladesMesh");
}
createGrassMesh 我们建立了一个草的mesh,sg->addEntity我们把它铺到了地面上。