ogre 3d 1.7 beginner’s guide 第四章 第九小节 中文翻译 添加线框模式和点模式
Adding wireframe and point render mode
【 添加线框模式和点模式 】
In a previous chapter, that is, Chapter 3, Camera, Light, and Shadow, we used the R key to
change the render mode to wireframe or point rendering. We want to add this feature to our own framelistener now.
【 在之前的第三章(摄像机,光源和阴影),我们使用R键来改变渲染模式为线框模式或点模式。现在我们添加这个特性到我们的帧监听。】
Time for action – adding wireframe and point render mode
【 实践时刻 —— 添加线框模和点渲染模式 】
We use the code we just created, and as always, simply add the code we need for this
new feature:
【 我们使用刚创建的代码,并一如既往简单的添加的代码来实现我们想要的新特性:】
1. We need a new member variable in the framelistener to save the current
render mode:
【 1. 我们需要在帧监听中添加一个新的成员变量在保存现在的渲染模式:】
Ogre::PolygonMode _PolyMode;
2. Init the value in the constructor with PM_SOLID:
【 2. 在构造函数中用PM_SOLID初始化创建的变量。】
_PolyMode = Ogre::PolygonMode::PM_SOLID;
3. We then add a new if condition in the frameStarted() function, which tests
if the R key is pressed. If this is the case, we can change the render mode. If the
current mode is solid, and we want it to be wireframe:
【 3. 然后我们在frameStarted()中添加一个新的函数,来测试R键是否按下。如果情况是这样,我们可以改变渲染模式。如果目前的模式为实体模式,我们想要它成为线框渲染模式。】
if(_key->isKeyDown(OIS::KC_R))
{
if(_PolyMode == PM_SOLID)
{
_PolyMode = Ogre::PolygonMode::PM_WIREFRAME;
}
3. If it is wireframe, and we want it to change to point mode:
【 如果它是线框型的,我们想要改变为点模式:】
else if(_PolyMode == PM_WIREFRAME)
{
_PolyMode = Ogre::PolygonMode::PM_POINTS;
}
4. And from point mode, back again to solid:
【 4. 如果它是点模式,最后让它变回实体模式:】
else if(_PolyMode == PM_POINTS)
{
_PolyMode = Ogre::PolygonMode::PM_SOLID;
}
6. Now that we have calculated the new render mode, we can apply it and close the
if condition:
【 6. 现在我们已经设置了新的渲染模式,我们可以应用它并关闭if 语句了:】
_Cam->setPolygonMode(_PolyMode);
}
7. Compile and run the application; you should be able to change the render mode by pressing R.
【 7. 编译运行程序;你应会看到按过R键后,渲染模式改变:】
What just happened?
【 刚刚发生了什么?】
We used the function setPolygonMode() to change the render mode from solid to
wireframe to point. We always saved the last value, so when changing again we know which is the current mode and what the next one will be. We also made sure that we have a circle for the different modes. We changed from solid to wireframe to point and then back to solid. One thing we noticed is that the modes change rather quickly when pressing the R key. This is because at each frame we check if the R key is pressed and as we humans are slow, chances are high that we press the R key longer than one frame. The result is that our application thinks we have pressed the R key several times in a short period of time and toggles the wireframe each frame. This isn't optimal and there is a better way to do it, which we will see now.
【 我们使用了函数setPolygonMode() 来改变实体模式为线框模式,然后是点模式。我们总是保存最后一个值。这样当改变模式时,我们知道当下的模式时什么,我们将会改变为什么模式。我们从实体模式改变为线框模式到点模式,然后又变为实体模式。我们注意到,当按下R键时,渲染模式改变的相当迅速。这个因为我们每一帧都检查R键是否犯下并且相对于计算机的每帧的速度,人类按下R键的速度是很慢的。这导致,我们的程序会认为我们在短时间内按下多次R键,这就意味着不定的哪帧将会切换到线框模式。这不是最理想的情况,但存在一种办法让我们做的的更好,我们下面将会看到。】