现在的位置: 首页 > 综合 > 正文

Ogre-渐变背景色(gradient background)的实现

2013年09月17日 ⁄ 综合 ⁄ 共 3706字 ⁄ 字号 评论关闭

背景色在ogre里面是通过ViewPort类中的setBackgroundColour()这个成员函数实现的,不过此类只提供给单一颜色的设置!!

 

不过,我们可以通过间接的方法去创建渐变的背景色,下面详细介绍:

 

1. 重构Ogre::Rectangle2D类:(OgreColourRectangle2D.h)


 

#ifndef OGRECOLOUREDRECTANGLE2D_H
#define OGRECOLOUREDRECTANGLE2D_H

#include "OgreRectangle2D.h"

class ColouredRectangle2D : public Ogre::Rectangle2D
{
public:
   ColouredRectangle2D(bool includeTextureCoordinates = false);
   ~ColouredRectangle2D();

   void setColours(const Ogre::ColourValue &topLeft, const Ogre::ColourValue &bottomLeft, const Ogre::ColourValue &topRight, const Ogre::ColourValue &bottomRight);

};

#endif // OGRECOLOUREDRECTANGLE2D_H


2. 重构类的实现: (OgreColourRectangle2D.cpp)


#include "OgreColouredRectangle2D.h"

#include "OgreHardwareBufferManager.h"

//#define POSITION_BINDING 0
//#define NORMAL_BINDING 1
//#define TEXCOORD_BINDING 2
#define COLOUR_BINDING 3

ColouredRectangle2D::ColouredRectangle2D(bool includeTextureCoordinates /*= false*/):Ogre::Rectangle2D(includeTextureCoordinates)
{
   Ogre::VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration;

   decl->addElement(COLOUR_BINDING, 0, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
   Ogre::VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding;

    Ogre::HardwareVertexBufferSharedPtr vbuf =
        Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
        decl->getVertexSize(COLOUR_BINDING),
        mRenderOp.vertexData->vertexCount,
        Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);

    // Bind buffer
    bind->setBinding(COLOUR_BINDING, vbuf);
}

ColouredRectangle2D::~ColouredRectangle2D()
{
}

void ColouredRectangle2D::setColours(const Ogre::ColourValue &topLeft, const Ogre::ColourValue &bottomLeft, const Ogre::ColourValue &topRight, const Ogre::ColourValue &bottomRight)
{
   Ogre::HardwareVertexBufferSharedPtr vbuf =
      mRenderOp.vertexData->vertexBufferBinding->getBuffer(COLOUR_BINDING);
   unsigned int* pUint32 = static_cast<unsigned int*>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));

   const Ogre::VertexElementType srcType = Ogre::VertexElement::getBestColourVertexElementType();

   *pUint32++ = Ogre::VertexElement::convertColourValue( topLeft, srcType );

   *pUint32++ = Ogre::VertexElement::convertColourValue( bottomLeft, srcType );

   *pUint32++ = Ogre::VertexElement::convertColourValue( topRight, srcType );

   *pUint32++ = Ogre::VertexElement::convertColourValue( bottomRight, srcType );

   vbuf->unlock();
}


3. 重构类应用

// Create background material
      Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("Background", "General");
      material->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
      material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
      material->getTechnique(0)->getPass(0)->setLightingEnabled(false);
      
      // Create background rectangle covering the whole screen
      ColouredRectangle2D* rect = new ColouredRectangle2D();
      rect->setCorners(-1.0, 1.0, 1.0, -1.0);
      rect->setMaterial("Background");
     
      // Set the colours
      rect->setColours( Ogre::ColourValue::Red, Ogre::ColourValue::Green, Ogre::ColourValue::Blue, Ogre::ColourValue::Black );

      // Render the background before everything else
      rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
      
      // Use infinite AAB to always stay visible
      Ogre::AxisAlignedBox aabInf;
      aabInf.setInfinite();
      rect->setBoundingBox(aabInf);
      
      // Attach background to the scene
      SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Background");
      node->attachObject(rect);

完成以上三步,就可以实现一个viewport的渐变背景色。基本原理是:创建s一个拥有渐变颜色的2D Rectangle,并使其作为背景渲染。有几点值得注意的时候:

 

1. #define COLOUR_BINDING 3,以及后续用到COLOUR_BINDING的各个地方。由于Ogre的各个版本的差别(本人使用的是1.7版本的),一些低版本的Ogre没有实现像cpp中注释的前三种,即POSITION、Normal、TEXCoord,此时如果冒然使用Colour,就会出现意想不到的错误。(当前的Mogre版本就没有实现3个,具体哪几个不慎清楚)

2. rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);这句很重要。

3. 如果要使用某个特定的图片作为背景,则简单的多:有兴趣的可以自己试试

抱歉!评论已关闭.