ogre 3d 1.7 beginner’s guide 第三章 第三小节 中文翻译 聚光源
Adding a spotlight
【 添加一个聚光源 】
We have created a point light and now we will create a spotlight—the second light type we
can use.
【 我们已经创建了一个点光源而现在我们将会创建一个聚光源——第二种我们可以使用的灯光类型。】
Time for action – creating a spotlight
【 实践时刻 —— 创建一个聚光源 】
We will use the code we created before and modify it a bit to see how a spotlight works:
【 我们将会使用我们之前已经写好的代码并且简单修改一下,然后观察一个聚光源是如何工作的:】
1. Delete the code where we created the light and insert the following code to create
a new scene node. Be careful not to delete the part of the code we used to create
LigthEnt and then add the following code:
【 1. 删除我们创建灯光的代码并插入以下代码以创建一个新的场景结点。注意不要删除我们使用过的LigthEnt的代码段,然后添加以下代码:】
Ogre::SceneNode* node2 = node->createChildSceneNode("node2");
node2->setPosition(0,100,0);
- Again, create a light, but now set the type to spotlight:
【 2. 同样的,创建一个灯光,但是现在设置灯光的类型为spotlight】
Ogre::Light* light = mSceneMgr->createLight("Light1");
light->setType(Ogre::Light::LT_SPOTLIGHT);
- Now set some parameters; we will discuss their meanings later:
【 3. 现在设置一些参数,我们将会稍后讨论他们的意思。】
light->setDirection(Ogre::Vector3(1,-1,0));
light->setSpotlightInnerAngle(Ogre::Degree(5.0f));
light->setSpotlightOuterAngle(Ogre::Degree(45.0f));
light->setSpotlightFalloff(0.0f);
- Set the light color and add the light to the newly created scene node:
【 4. 设置灯光的颜色然后添加灯光到刚创建的场景结点:】
light->setDiffuseColour(Ogre::ColourValue(0.0f,1.0f,0.0f));
node2->attachObject(light);
- Compile and run the application; it should look like this:
【 5. 编译运行程序。它将会有以下效果:】
What just happened?
【 刚刚发生了什么?】
We created a spotlight in the same manner we created a point light; we just used some
different parameters for the light.
Step 1 created another scene node to be used later. Step 2 created the light as we did
before; we just used a different light type—this time Ogre::Light::LT_SPOTLIGHT—to
get a spotlight. Step 3 is the really interesting one; there we set different parameters for
the spotlight.
【 我们几乎以创建点光源同样的方式创建了一个聚光源。不同的是我们改变了灯光的一些参数。在第一步中,我们创建了在稍后会用到的场景结点。在第二步中,我们如往常一样创建了一个光源,但是我们使用了不同的光源类型——这次我们使用了Ogre::Light::LT_SPOTLIGHT —— 来获取一个聚光源。在第三步是有趣的的步,我们为聚光源设置了不同的参数。】
Spotlights
【 聚光源 】
Spotlights are just like flashlights in their effect. They have a position where they are and a
direction in which they illuminate the scene. This direction was the first thing we set after
creating the light. The direction simply defines in which direction the spotlight is pointed.
The next two parameters we set were the inner and the outer angles of the spotlight. The
inner part of the spotlight illuminates the area with the complete power of the light source's
color. The outer part of the cone uses less power to light the illuminated objects. This is
done to emulate the effects of a real flashlight. A real flashlight also has an inner part and
an outer part that illuminate the area lesser then the center of the spotlight. The inner and
outer angles we set define how big the inner and the outer part should be. After setting the
angles, we set a falloff parameter. This falloff parameter describes how much power
the light loses when illuminating the outer part of the light cone. The farther away a point to
be illuminated is from the inner cone, the more the falloff affects the point. If a point is
outside the outer cone, then it isn't illuminated by the spotlight.
【 聚光源恰如手电筒的效果。它们有发光源的位置和在照亮场景的一个方向。设置光线方向是我们创建聚光源完成后要做的第一件事。光线的方向简单的定义了聚光灯的指向。接下来的我们设置的两个参数为聚光源的内角度和外角度。聚光源的内光部分使用完整的光源颜色来照射区域,而外部的锥体只使用较少的光源能量来照亮物体。这样做是为了模拟手电筒的效果。一个真正的手电筒也是有内光部分和外光部分,其中外光部分没有聚光源的中央光源的亮度强。我们的定义的内角和外角决定了光源照射的内部和外部的范围有多大。在设置完角度之后,我们设置了一个 下降(falloff) 参数.这个下降参数描述了当照射外层锥体光能损失。被照射的点离内部的距离越大,下降效果就越明显。如果一个点是在圆锥体之后,那它将不会被聚光源所照射到。】
We set the falloff to zero. In theory, we should see a perfect light circle on the plane, but
it is rather blurry and deformed. The reason for this is that the lighting that we use at the
moment uses the triangle points of the plane to calculate and apply the illumination. When
creating the plane, we told Ogre 3D that the plane should be created with 20 X 20 segments.
This is a rather low resolution for such a big plane and means the light cannot be calculated
accurately enough, because there are too few points to apply in an area to make a smooth
circle. So to get a better quality render, we have to increase the segments of the plane. Let's
say we increase the segments from 20 to 200. The plane creation code looks like this after
the increase:
【 我们设置下降为0。理论上,我们应该在平面上看到一个完美的光圈,但是实际效果却发生很大的模糊和变形。造成这种效果的原因是我们此刻使用的是平面上的三角形的点去计算并应用于照射。当创建一个平面时,我们告诉Ogre 3D以20*20的切片程度来创建平面。如此大的平面却用如此低的分辨率,这就意味着光线不能被准确的计算,因为在区域内只有很少的点能适用于形成一个边缘光滑的圆形。因此,为了获得一个更好的渲染效果,我们不得不增加平面的切片数。比方说我们增加切片从20到200。那么平面创建代码在切片增加过后就如下面的形式:】
Ogre::MeshManager::getSingleton().createPlane("plane",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,1500,1500,200,200,true,1,5,5,Vector3::UNIT_Z);
Now when recompiling and restarting the application, we get a nice round circle of light from
our spotlight.
【 现在当我们重新编译运行程序,我们将会从我们聚光源得到一个更圆的光圈。】
The circle still isn't perfect; if needed, we could increase the segments of the plane even
further to make it perfect. There are different lighting techniques which give better results
with a low-resolution plane, but they are rather complex and would complicate things now.
But even with the complex lighting techniques, the basics are the same and we can change
our lighting scheme later using the same lights we created here.
【 这个圆任然不是完美的。如果需要,我们可以增加平面的切片程度,也可以把灯光放远一点使它看起来更加完美。也有不同的灯光技术使低分辨率的平面达到更好的效果,但是它们却相当复杂并且使事情之间变得复杂。但是即使是复杂的灯光技术,基本的原理是相同的而且可以通过改变我们的灯光策略来使用这里创建的灯光】
In step 4, we saw another way to describe a color in Ogre 3D. Previously, we used three
values, (r,g,b), to set the diffuse color of our light. Here we used Ogre::ColourValue
(r,g,b), which is basically the same but encapsulated as a class with some additional
functions and thus it makes the intention of the parameter clearer.
【 在第四步中,我们看到了在Ogre3D中描述颜色的另一种方式。在此之前,我们使用三个值(r,g,b), 来设置我们灯光的漫射色。这里我们使用了Ogre::ColourValue (r,g,b) ,两种表述方式基本一样,但是这种方式加以一些额外的函数作为一个类被封装,从而使得参数的意图更为清晰。】
Pop quiz – different light sources
【 简单测试 —— 不同的灯光资源 】
Describe the difference between a point light and a spotlight in a few words.
【 用几句话描述点光源和聚光源之间的不同。】
Have a go hero – mixing light colors
【 让英雄动起来 —— 混合光线颜色 】
Create a second spotlight that is at a different position as compared to the first spotlight.
Give this spotlight a red color and position it in such a way that the circles of both spotlights overlap each other a bit. You should see that the color is mixing in the area where the green and red light overlap.
【 创建与第一个聚光源位置不同的第二个聚光源,给第二个聚光源以红色的光线,以如此方式安置光源,可以使两个聚光源重叠一部分。你就可以看到在绿色和红色的重叠区域会有颜色的混合。】