Ogre笔记02——Camera,light and shadow

1.创建平面

        // Plane
        Ogre::Plane plane(Vector3::NEGATIVE_UNIT_Y, -10);    // 平面的法向量为Y的负半轴,-10这个值比较奇葩,到时候自己琢磨就好了

        Ogre::MeshManager::getSingleton().createPlane("plane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
            plane, 1500, 1500, 20, 20, true, 1, 5, 5, Vector3::UNIT_Z);      //
参数依次为存放平面的法向量、到原点距离信息的plane,width、height,xsegments、ysegments,是否计算法向量,纹理坐标集的个数,uTiles、vTiles,up-Direction(与平面法向量垂直就行了,具体不知道为什么)
        Ogre::Entity* ent2 = mSceneMgr->createEntity("LightPlaneEntity", "plane");
        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent2);
        ent2->setMaterialName("Examples/BeachStones");     //为平面添加纹理

2.添加光源

// Light
        Ogre::Light* light1 = mSceneMgr->createLight("Light1");
        light1->setType(Ogre::Light::LT_POINT);
        light1->setPosition(0,10,0);
        light1->setDiffuseColour(1.0f,1.0f,1.0f);
·三种光源——point light、spotlight、directional light。

·光源可以attach到一个scene node上,也可以独立于任何scene node,此时,光源的位置就有setPosition等函数决定。

3.添加阴影

mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);

·There are multiple ways to generate shadows in a scene, and each has strengths and weaknesses.

  • Stencil-based approaches can be used to draw very long, extreme shadows without loss of precision and the 'additive' version can correctly show the shadowing of complex effects like bump mapping because they physically exclude the light from those areas. However, the edges are very sharp and stencils cannot handle transparency, and they involve a fair amount of CPU work in order to calculate the shadow volumes, especially when animated objects are involved.
  • Texture-based approaches are good for handling transparency (they can, for example, correctly shadow a mesh which uses alpha to represent holes), and they require little CPU overhead, and can happily shadow geometry which is deformed by a vertex program, unlike stencil shadows. However, they have a fixed precision which can introduce 'jaggies' at long range and have fillrate issues of their own.
We support 2 kinds of stencil shadows, and 2 kinds of texture-based shadows, and one simple decal approach. The 2 stencil approaches differ in the amount of multipass work that is required - the modulative approach simply 'darkens' areas in shadow after the main render, which is the least expensive, whilst the additive approach has to perform a render per light and adds the cumulative effect, which is more expensive but more accurate. The texture based shadows both work in roughly the same way, the only difference is that the shadowmap approach is slightly more accurate, but requires a more recent graphics card.

4.创建摄像机

mCamera = mSceneMgr->createCamera("MyCamera1");

mCamera->setPosition(0,100,200);
mCamera->lookAt(0,0,0);
mCamera->setNearClipDistance(5);

5.创建Viewport

·Viewport可视为一个画布,画布上画的是Camera所观察到的图像。

·A viewport is the meeting of a camera and a rendering surface - the camera renders the scene from a viewpoint, and places its results into some subset of a rendering target, which may be the whole surface or just a part of the surface. Each viewport has a single camera as source and a single target as destination. A camera only has 1 viewport, but a render target may have several. A viewport also has a Z-order, i.e. if there is more than one viewport on a single render target and they overlap, one must obscure the other in some predetermined way.(一个windows应用程序的客户区是一个render target,一个render target上可有多个viewport,每个viewport的图像来自camera的观察所得。)

void createViewports() {

      Ogre::Viewport* vp = mWindow->addViewport(mCamera);

      vp->setBackgroundColour(ColourValue(0.0f,0.0f,1.0f));
      mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}

posted @ 2013-02-16 09:55  Wilson Kwok  阅读(290)  评论(0编辑  收藏  举报