您的位置:首页 > 其它

学习OGRE制作简单人物行走demo(一)

2009-08-13 18:00 429 查看
看了两个星期的ogre框架了,感觉他的文档很全,使用也比较方便,但对于从来没写过3D游戏的我来说,要想作出多伟大的作品来,实在有点遥远,但学习东西最好是去定个目标,然后围绕他去做,这样不会被浩瀚的关联知识所淹没(我也没打算去把c++或者图形学的书看了又看).

所以现在的目标就是做个控制人物行走的DEMO出来。

我用的VC2008,比较方便,下好SDK就可以编译他的例子了,我也用过vc2005,好象还得下个补丁包,麻烦得多。用vc可以去下一个模版,这样创建出来的项目就不用再去设置了。不过release编译选项要再手动设置,看看模版代码,还是挺好改的。

第一天把人物放在场景,设置摄像头和视点。

1.创建ogre工程,我这里命名为ogre6,就两个文件。开始就什么都没有吧。

ogre6.h

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.
-----------------------------------------------------------------------------
*/
#ifndef __ogre6_h_
#define __ogre6_h_
#include "ExampleApplication.h"
class TutorialApplication : public ExampleApplication
{
protected:
public:
TutorialApplication()
{
}
~TutorialApplication()
{
}
protected:
void createScene(void)
{
}
};
#endif // #ifndef __ogre6_h_


ogre6.cpp

/*
-----------------------------------------------------------------------------
Filename:    ogre6.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 <Ogre.h>
#include "ogre6.h"
#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
TutorialApplication 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


很简单,TutorialApplication继承ExampleApplication,什么都不用添加,编译运行,OGRE出生了。

现在,放一个忍者到场景中,这些资源都从ogre直接用,做demo足够了。

TutorialApplication::createScene方法中添加

Entity *ent = mSceneMgr->createEntity( "Ninja", "ninja.mesh" );
SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "NinjaNode" );
node->attachObject(ent);


在视线前方出现了绿色的忍者。场景中产生实体,实体绑定在场景节点上,而场景节点都是root的子节点。

游戏中操作角色的时候,我们的视点都大概在角色的后脑勺上方,所以现在调整摄像头位置。(运行例子的时候按p,可以打印出当前的三维坐标)

添加createCamera方法

void createCamera(void)
{
// create camera, but leave at default position
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setNearClipDistance(5);
mCamera->setPosition(Vector3(0, 400, 400));
mCamera->lookAt(Vector3(0, -100, -300));
}


这个调整我没有好办法,慢慢调的,有个xyz坐标概念就行。

为了找个参照物,创建一个平面吧。

在createScene方法添加代码

Plane plane(Vector3::UNIT_Y, 0);
MeshManager::getSingleton().createPlane("ground",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
ent = mSceneMgr->createEntity("GroundEntity", "ground");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
ent->setMaterialName("Examples/Rockwall");
ent->setCastShadows(false);


接下来应该让忍者移动了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: