⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfornetwonogre.cpp

📁 3D引擎Ogre和物理引擎 Netwon 的简单联合应用例子
💻 CPP
字号:
/*
-----------------------------------------------------------------------------
Filename:    TestForNetwonOgre.cpp
-----------------------------------------------------------------------------

This source file is generated by the Ogre AppWizard.

Check out: http://conglomerate.berlios.de/wiki/doku.php?id=ogrewizards

Based on the Example Framework for OGRE
(Object-oriented Graphics Rendering Engine)

Copyright (c) 2000-2007 The OGRE Team
For the latest info, see http://www.ogre3d.org/

You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the OGRE engine.
-----------------------------------------------------------------------------
*/

#include "TestForNetwonOgre.h"
bool TestForNetwonOgreApp::quit(const CEGUI::EventArgs &e)
{
	mContinue = false;
	return true;
}

void TestForNetwonOgreApp::ForceCallBack(OgreNewt::Body* body)
{
	if (body) 
	{
		Real mass;
		Vector3 v,forceVector;
		body->getMassMatrix(mass,v);
		forceVector.y=-9.8*mass;
		forceVector.x=0;
		forceVector.z=0;
		if (mKeyPressed[OIS::KC_SPACE]==TRUE) 
		{
			forceVector.y+=9.8*mass*2;
		}
		else
		{

		}

		if (mKeyPressed[OIS::KC_W]==TRUE) 
		{
			forceVector.z-=9.8*mass;
		}
		else
		{
		}		

		if (mKeyPressed[OIS::KC_S]==TRUE) 
		{
			forceVector.z=9.8*mass;
		}
		else
		{

		}
		
		if (mKeyPressed[OIS::KC_A]==TRUE) 
		{
			forceVector.x-=9.8*mass;
		}
		else
		{

		}		
		
		if (mKeyPressed[OIS::KC_D]==TRUE) 
		{
			forceVector.x=9.8*mass;
		}
		else
		{

		}
		body->setForce(forceVector);
	}
}
//-------------------------------------------------------------------------------------
TestForNetwonOgreApp::TestForNetwonOgreApp(void)
{
	//第一步 创建 OgreNewt world.
	m_World = new OgreNewt::World();
}
//-------------------------------------------------------------------------------------
TestForNetwonOgreApp::~TestForNetwonOgreApp(void)
{
	delete m_World;
	OgreNewt::Debugger::getSingleton().deInit();

}
bool TestForNetwonOgreApp::frameStarted(const FrameEvent& evt)
{
	bool returnValue=true;
	returnValue=BaseApplication::frameStarted(evt);
	if (!mContinue) 
	{
		return false;
	}
	return returnValue;
}
//-------------------------------------------------------------------------------------
void TestForNetwonOgreApp::createScene(void)
{
	//CEGUI init
	mRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
	mSystem = new CEGUI::System(mRenderer);
	//选择SKIN
	CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
	//鼠标指针和字体
	mSystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
	mSystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");

	CEGUI::WindowManager *win = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::Window *sheet = win->createWindow("DefaultGUISheet", "CEGUIDemo/Sheet");

	CEGUI::Window *quit = win->createWindow("TaharezLook/Button", "CEGUIDemo/QuitButton");
	quit->setText("Quit");
	quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.15, 0), CEGUI::UDim(0.05, 0)));

//	CEGUI::WindowManager *wmgr = CEGUI::WindowManager::getSingletonPtr();
//	CEGUI::Window *quit = wmgr->getWindow((CEGUI::utf8*)"CEGUIDemo/QuitButton");
	quit->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&TestForNetwonOgreApp::quit, this));

	sheet->addChildWindow(quit);
	mSystem->setGUISheet(sheet);
	// sky box.创建天空
	mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");

	// shadows on!启动影子
	mSceneMgr->setShadowTechnique( Ogre::SHADOWTYPE_STENCIL_ADDITIVE );
	Entity* floor;
	SceneNode* floornode;
	floor = mSceneMgr->createEntity("Floor", "simple_terrain.mesh" );
	floornode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "FloorNode" );
	floornode->attachObject( floor );
	floor->setMaterialName( "Examples/EnvMappedRustySteel" );
	floor->setCastShadows( false );

	//-------------------------------------------------------------
	// add some other objects.
	Entity* floor2;
	SceneNode* floornode2;
	floor2 = mSceneMgr->createEntity("Floor2", "simple_terrain.mesh" );
	floornode2 = floornode->createChildSceneNode( "FloorNode2" );
	floornode2->attachObject( floor2 );
	floor2->setMaterialName( "Examples/EnvMappedRustySteel" );
	floor2->setCastShadows( false );
	floornode2->setPosition( Ogre::Vector3(80.0f, 0.0f, 0.0f) );

	Entity* floor3;
	SceneNode* floornode3;
	floor3 = mSceneMgr->createEntity("Floor3", "simple_terrain.mesh" );
	floornode3 = floornode->createChildSceneNode( "FloorNode3" );
	floornode3->attachObject( floor3 );
	floor3->setMaterialName( "Examples/Chrome" );
	floor3->setCastShadows( false );
	floornode3->setPosition( Ogre::Vector3(-80.0f, -12.0f, 0.0f) );
	floornode3->setOrientation( Ogre::Quaternion( Ogre::Degree(15.0f), Ogre::Vector3::UNIT_Z ) );
	//-------------------------------------------------------------

	// using the new "SceneParser" TreeCollision primitive.  this will automatically parse an entire tree of
	// SceneNodes (parsing all children), and add collision for all meshes in the tree.
	OgreNewt::CollisionPrimitives::TreeCollisionSceneParser* stat_col = new OgreNewt::CollisionPrimitives::TreeCollisionSceneParser( m_World );
	stat_col->parseScene( floornode, true );
	OgreNewt::Body* bod = new OgreNewt::Body( m_World, stat_col );
	delete stat_col;

	bod->attachToNode( floornode );
	bod->setPositionOrientation( Ogre::Vector3(0.0,-20.0,0.0), Ogre::Quaternion::IDENTITY );

	//make a light
	Ogre::Light* light;

	light = mSceneMgr->createLight( "Light1" );
	light->setType( Ogre::Light::LT_POINT );
	light->setPosition( Ogre::Vector3(0.0, 100.0, 100.0) );

	Entity* ent=mSceneMgr->createEntity("MoveObjectEnt","ogrehead.mesh");
	ent->setNormaliseNormals(true);
	SceneNode* node=mSceneMgr->getRootSceneNode()->createChildSceneNode("MoveObjectNode");
	node->scale(0.2,0.2,0.2);
	node->attachObject(ent);
	OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Box(m_World,Vector3(10,10,10));
	MoveBody=new OgreNewt::Body(m_World,col);
	Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( 10.0, Vector3(10,10,10));
	MoveBody->setMassMatrix( 10.0, inertia );
	MoveBody->attachToNode(node);
//	MoveBody->setCustomForceAndTorqueCallback( &ForceCallBack);
	MoveBody->setCustomForceAndTorqueCallback( boost::bind( &TestForNetwonOgreApp::ForceCallBack, this, _1 ) );  
	MoveBody->setPositionOrientation(Vector3(0,20,0),Quaternion());
	OgreNewt::Joint* joint;
	joint=new OgreNewt::BasicJoints::UpVector(m_World,MoveBody,Vector3(0,1,0));
//	MoveBody->setLinearDamping(0);
	MoveBody->setAutoFreeze(FALSE);

	delete col;
	//渲染到贴图
	//RenderTexture *tex = mRoot->getRenderSystem()->createRenderTexture("RttTex", 512, 512, TEX_TYPE_2D, PF_R8G8B8);

	//Camera *cam = mSceneMgr->createCamera("RttCam");
	//cam->setPosition(100, 100, 100);
	//cam->lookAt(0, 0, -300);

	//Viewport *v = tex->addViewport(cam);
	//v->setOverlaysEnabled(false);
	//v->setClearEveryFrame(true);
	//v->setBackgroundColour(ColourValue::Black);
	//CEGUI::Texture *cTex = mRenderer->createTexture((CEGUI::utf8*)"RttTex");

	//CEGUI::Imageset *imageSet = CEGUI::ImagesetManager::getSingleton().createImageset((CEGUI::utf8*)"RttImageset", cTex);
	//imageSet->defineImage((CEGUI::utf8*)"RttImage", 
	//	CEGUI::Point(0.0f, 0.0f),
	//	CEGUI::Size(cTex->getWidth(), cTex->getHeight()),
	//	CEGUI::Point(0.0f,0.0f));

	//CEGUI::Window *si = win->createWindow((CEGUI::utf8*)"TaharezLook/StaticImage", "RTTWindow");
	//si->setSize(CEGUI::UVector2(CEGUI::UDim(0.5f, 0), CEGUI::UDim(0.4f, 0)));
	//si->setPosition(CEGUI::UVector2(CEGUI::UDim(0.5f, 0), CEGUI::UDim(0.5, 0)));

	//si->setProperty("Image", CEGUI::PropertyHelper::imageToString(&imageSet->getImage((CEGUI::utf8*)"RttImage")));

	//sheet->addChildWindow(si);



}

void TestForNetwonOgreApp::createFrameListener()
{
	// this is our custom frame listener for this app, that lets us shoot cylinders with the space bar, move
	// the camera, etc.
//	mFrameListener = new OgreNewtonFrameListener( mWindow, mCamera, mSceneMgr, m_World, msnCam );
//	mRoot->addFrameListener(mFrameListener);

	// this is a basic frame listener included with OgreNewt that does nothing but update the
	// physics at a set framerate for you.  complex project will want more control, but this
	// works for simple demos like this.  feel free to look at the source to see how it works.
	//	2 创建一个NewtFramListener 用于物理环境的模拟的更新 最后的一个参数用于确定理想的时间
	mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, mSceneMgr, m_World, 120 );
	mRoot->addFrameListener(mNewtonListener);
	BaseApplication::createFrameListener();
	mContinue=TRUE;
}


#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
			TestForNetwonOgreApp 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -