第四章 Frame Listeners and Unbuffered Input

本文介绍了一个使用Ogre游戏引擎创建基本场景的例子。通过设置环境光、加载模型、控制光照及角色移动,演示了如何利用Ogre进行实时渲染。文章详细展示了如何在Ogre中实现动态光照效果以及通过键盘输入来控制角色移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BasicTutorial4.h

#include "BaseApplication.h"
class BasicTutorial4:public BaseApplication
{
public:
	BasicTutorial4(void);
	virtual ~BasicTutorial4(void);
protected:
	virtual void createScene(void);
	virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
private:
	bool processUnbufferInput(const Ogre::FrameEvent& evt);

};

BasicTutorial4.cpp

#include "BasicTutorial4.h"

BasicTutorial4::BasicTutorial4(void)
{

}
BasicTutorial4::~BasicTutorial4(void)
{
	
}
void BasicTutorial4::createScene(void)
{
	mSceneMgr->setAmbientLight(Ogre::ColourValue(0.25,0.25,0.25));
	Ogre::Entity* ninjaEntity=mSceneMgr->createEntity("Ninja","ninja.mesh");
	Ogre::SceneNode* node=mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
	node->attachObject(ninjaEntity);
	Ogre::Light* pointLight=mSceneMgr->createLight("pointLight");
	pointLight->setType(Ogre::Light::LT_POINT);
	pointLight->setPosition(Ogre::Vector3(250,150,250));
	pointLight->setDiffuseColour(Ogre::ColourValue::White);
	pointLight->setSpecularColour(Ogre::ColourValue::White);

}
bool BasicTutorial4::processUnbufferInput(const Ogre::FrameEvent& evt)
{
	static bool mMouseDown = false;
	static Ogre::Real mToggle=0.0;
	static Ogre::Real mRotate=0.13;
	static Ogre::Real mMove=20;

	bool currMouse=mMouse->getMouseState().buttonDown(OIS::MB_Left);

	if(currMouse && ! mMouseDown)
	{
		Ogre::Light* light=mSceneMgr->getLight("pointLight");
		light->setVisible(!light->isVisible());
	}
	mMouseDown = currMouse;
	mToggle-=evt.timeSinceLastFrame;
	if((mToggle<0.0f) && mKeyboard->isKeyDown(OIS::KC_1))
	{
		mToggle=0.5f;
		Ogre::Light* light=mSceneMgr->getLight("pointLight");
		light->setVisible(!light->isVisible());

	}
	Ogre::Vector3 transVector= Ogre::Vector3::ZERO;
	if(mKeyboard->isKeyDown(OIS::KC_I))
	{
		transVector.z -= mMove;
	}
	if(mKeyboard->isKeyDown(OIS::KC_K))
	{
		transVector.z+=mMove;
	}
	if(mKeyboard->isKeyDown(OIS::KC_J))
	{
		if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
		{
			mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(mRotate*5));
		}
		else
		{
			transVector.x-=mMove;
		}
	}
	if(mKeyboard->isKeyDown(OIS::KC_L))
	{
		if(mKeyboard->isKeyDown(OIS::KC_LSHIFT))
		{
			mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(-mRotate*5));
		}
		else
		{
			transVector.x+=mMove;
		}
	}
	if(mKeyboard->isKeyDown(OIS::KC_U))
	{
		transVector.y+=mMove;
	}
	if(mKeyboard->isKeyDown(OIS::KC_O))
	{
		transVector.y-=mMove;
	}
	mSceneMgr->getSceneNode("NinjaNode")->translate(transVector*evt.timeSinceLastFrame,Ogre::Node::TS_LOCAL);
	//since we are trying to make the ninja go forward when we press I, we need it to go in the direction that the node is actually facing, so we use the local transformation space.
	/*
	There is another way we can do this (though it is less direct). We could have gotten the orientation of the node, a quaternion, and multiplied this by the direction vector to
	get the same result. This would be perfectly valid:
	mSceneMgr->getSceneNode("NinjaNode")->translate(mSceneMgr->getSceneNode("NinjaNode")->getOrientation() * transVector * evt.timeSinceLastFrame, Ogre::Node::TS_WORLD);
	*/
	return true;
}
bool BasicTutorial4::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
	bool ret=BaseApplication::frameRenderingQueued(evt);
	if(!processUnbufferInput(evt))return false;
	return ret;
}



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
 
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        BasicTutorial4 app;
 
        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }
 
        return 0;
    }
 
#ifdef __cplusplus
}
#endif


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值