English

ogre 3d 1.7 beginner’s guide 第三章 第二小节 中文翻译 点光源

2011/2/20

Adding a point light

【 添加一个点光源 】

Now that we have created a plane to see the effect that the light has on our scene, we need

to add a light to see something.

【 现在我们已经创建了一个可以扎在我们场景中看见灯光效果的平面,我们需要添加一个灯光去看下效果。】

Time for action – adding a point light

【 实践时刻 ­—— 添加一个点光源 】

We will create a point light and add it to our scene to see the effect it has on our scene:

【 我们将会创建一个点光源并且添加到我们的场景中,然后观察光源在我们场景中的效果: 】

    • Add the following code after setting the material for the plane:

【 1. 在设置完平面的材质之后添加以下代码:】

Ogre::SceneNode* node = mSceneMgr->createSceneNode("Node1");

mSceneMgr->getRootSceneNode()->addChild(node);

    • Create a light with the name Light1 and tell Ogre 3D it's a point light:

【 2. 创建一个名为Light1的灯光并且告诉Ogre3D这是一个点光源:】

Ogre::Light* light1 = mSceneMgr->createLight("Light1");

light1->setType(Ogre::Light::LT_POINT);

    • Set the light color and position:

【 3. 设置灯光的颜色和位置:】

light1->setPosition(0,20,0);

light1->setDiffuseColour(1.0f,1.0f,1.0f);

4.  Create a sphere and set it at the position of the light, so we can see where

the light is:

【 4. 创建一个球并且设置它在点光源的位置,这样我们就可以看到灯光的位置了。】

Ogre::Entity* LightEnt = mSceneMgr->createEntity("MyEntity","sphere.mesh");

Ogre::SceneNode* node3 = node->createChildSceneNode("node3");

node3->setScale(0.1f,0.1f,0.1f);

node3->setPosition(0,20,0);

node3->attachObject(LightEnt);

5.  Compile and run the application; you should see the stone texture lit by a white

light, and see a white sphere a bit above the plane.

【 编译运行程序,你应会看到石头的纹理将会被一个白色的灯光照亮,并且会看到在平面上面有一个白色的圆球。】

What just happened?

【 刚刚发生了什么?】

We added a point light to our scene and used a white sphere to mark the position of

the light. In step 1, we created a scene node and added it to the root scene node. We created the

scene node because we need it later to attach the light sphere to. The first interesting thing

happened in step 2. There we created a new light using the scene manager. Each light will

need a unique name, if we decide to give it a name. If we decide not to use a name, then

Ogre 3D will generate one for us.

【 我们在场景中添加了一个点光源并且使用了一个白色的球体在标明点光源的位置。在第一步中,我们创建了一个场景结点并添加它到我们的场景根结点。我们创建场景结点是因为我们要为稍后关联白色球体做准备。第一个比较有趣的事情是发生在第二步。我们使用场景管理器创建了一个新灯光。如果我们给灯光一名称,每个灯光的名字必须是独一无二的,如果我们不给灯光名称,然后Ogre 3D会为我们生成一个。】

We used Light1 as a name. After creation, we told Ogre 3D that we want to create a point light. There are three different kinds of lights we can create, namely, point lights, spotlights, and directional lights. Here we created a point light; soon we will create the other types of lights. A point light can be thought of as being like a light bulb. It's a point in space which illuminates everything around it. In step 3, we used the created light and set the position of the light and its color. Every light color is described by a tuple (r,g,b). All three parameters have a range from 0.0 to 1.0 and represent the attribution of their assigned color part to the color. 'r' stands for red, 'g' for green, and 'b' for blue. (1.0,1.0,1.0) is white, (1.0,0.0,0.0) is red, and so on. The function we called was setDiffuseColour(r,g,b), which takes exactly these three parameters for the color.

Step 4 added a white sphere at the position of the light, so we could see where the light is

positioned in the scene.

【 我们使用Light1作为灯光的名称。创建之后,我们告诉Ogre 3D 我们想要创建一个点光源。我们可以创建三种不同的光源,即为——点光源,聚光源和方向光。在这我们创建了一个点光源。一会我们将会创建别的类型的光源。一个点光源可以被认为是一个明亮的灯泡。它就好像是空间中可以照亮周围一切的光源。在第三步中,我们使用了刚创建的灯光并且设置了灯光的位置和颜色。每个灯光的颜色是用一个(r,g,b)的数组来描述的。所有三个参数的范围都是从 0.0 到 1.0 而且每个参数表述了它们各自对应颜色属性对最终颜色效果的影响。’r’代表红色,’g’代表绿色,’b’代表蓝色。(1.0,1.0,1.0)是白色,(1.0,0.0,0.0)为红色,其他等等如此类推。我们调用的函数setDiffuseColour(r,g,b)中的三个参数恰恰是对应表述颜色的(r,g,b)三个参数。在第四步在光源的位置添加了一个白色球体,这样我们就可以看到光源在场景中的位置了。】

Have a go hero – adding a second point light

【 让英雄动起来 —— 添加第二个点光源】

Add a second point light at (20,20,20), which illuminates the scene with a red light. Also add

another sphere to show where the point light is. Here's how it should look:

【 在(20,20,20)的位置添加第二个照亮场景的红色点光源。同样的添加另一个球体以显示点光源的位置。以下就是效果图:】