Ogre基础教程二
声明:此文章是根据Ogre官方的wiki翻译的,如有翻译错误,希望高手进行指正,原文在此:
?http://www.ogre3d.org/tikiwiki/Basic+Tutorial+2&structure=Tutorials
?
? ?接下来你需要添加函数的定义来实现BasicTutorial2
?
??/**
BasicTutorial2.cpp**///-------------------------------------------------BasicTutorial2::BasicTutorial2(void){} //-------------------------------------------------BasicTutorial2::~BasicTutorial2(void){} //-------------------------------------------------void BasicTutorial2::createCamera(void){} //-------------------------------------------------void BasicTutorial2::createViewports(void){} //-------------------------------------------------void BasicTutorial2::createScene(void){}?这表示创建了一个Camera,并且它的名字是"PlayerCam"。
?
?请注意,如果你决定不持有一个Camera的时候,你可以基于它的名字"PlayerCam"并使用SceneManager对象的getCamera函数来获得一个Camera,然后使用其他操作,比如将他添加到其他的SceneNode(这个是偶根据实际情况自己加的,O(∩_∩)O。原文如下:Note that you can use the?getCamera?function of SceneManager to get Cameras based on their name if you decide not to hold a pointer to it)。
接下来我们将会设置Camera的位置和朝向
我们将围绕远点放置对象,所以我们在Z轴上选了一个不错的的距离,然后使Camera面朝原点,在上一步的代码后加入以下代码:
?
// set its position, direction mCamera->setPosition(Ogre::Vector3(0,10,500)); mCamera->lookAt(Ogre::Vector3(0,0,0));
?lookAt函数相当优雅,可以是Camera面向任何你想要的位置,而不必使用yaw, rotate, 和pitch等函数。SceneNode也有这个函数,在很多情况下,它将设置实体(SceneNode)的朝向这个操作变得更加简单。
最后,我们将会设置“近距离剪切”为5个单位(原文:Finally we will set a near clipping distance of 5 units)
“近距离剪切”是指当一个Camera离目标多近(或多远)的时候你会看不到它,调整“近距离剪切”可以让你在离目标很近的时候,看到目标背后的东西。也就是说当你离一个东西很近的时候,你只能看到它的一小部分,并且它将填满整个屏幕使你看不到任何东西。你也可以设置“远距离剪切”,这会使引擎停止渲染任何比指定距离远的物体。这项功能的主要用途是:在场景中一个很远的距离需要渲染大量的物体时可以提高你的帧率。
添加以下行来实现"近距离剪切":
?
// set the near clip distance mCamera->setNearClipDistance(5);
?
设置远距离剪切使用类似的?setFarClipDistance方法。在我们的这个教程中,在“Stencil Shadows”效果中,你不应该使用“远距离剪切”。
现在,既然我们覆盖了createCamera方法,我们就要使用最新创建的mCamera对象来构造一个OgreBites::SdkCameraMan对象(Camera控制器):
?
mCameraMan = new OgreBites::SdkCameraMan(mCamera); // create a default camera controller?
?
现在createCamera方法看起来是这样:
?
void BasicTutorial2::createCamera(void){ // create the camera mCamera = mSceneMgr->createCamera("PlayerCam"); // set its position, direction mCamera->setPosition(Ogre::Vector3(0,10,500)); mCamera->lookAt(Ogre::Vector3(0,0,0)); // set the near clip distance mCamera->setNearClipDistance(5); mCameraMan = new OgreBites::SdkCameraMan(mCamera); // create a default camera controller}??
?
现在我们有了View,我们能用它做什么?答案是:不多。我们能用它做的最重要的事情是调用setBackgroundColour函数并用我们选择的任何颜色来设置背景色。
因为在这个教程中我们要处理灯光,所以我们将把背景设置为黑色:
?
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
?Note that ColourValue expects a red, green, and blue color value for its parameters between the values of 0 and 1. The last, and most important thing we need to do is to set the aspect ratio of our Camera. If you are using something other than the standard full-window viewport, then failing to set this can result in a very strange looking scene. We will go ahead and set it even though we are using the default aspect ratio:
注意,ColourValue接受红,绿,蓝作为参数,并且取值的范围是0.0至1.0。最后,还有一件最重要的事是我们必须为Camera指定长宽比。如果你使用的不是标准的窗口,那么不设置这个值会使结果看起来非常奇怪。即使我们使用的是默认的长宽比,但是我们仍然会指定这个值:
?
// Alter the camera aspect ratio to match the viewport mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));?
?
这就是Viewport类的简单用法。
下面是完成的createViewports?函数:
?
void BasicTutorial2::createViewports(void){ // Create one viewport, entire window Ogre::Viewport* vp = mWindow->addViewport(mCamera); vp->setBackgroundColour(Ogre::ColourValue(0,0,0)); // Alter the camera aspect ratio to match the viewport mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight())); }??
到这里,你应该编译并且运行项目,因为是一个空的场景,所以什么都没有(按ESC键退出),继续后面的内容之前,确保你能运行此项目并且不崩溃。
?
?
Ogre目前支持3中类型的Shadows:
?
?