ogre里面的灯光

来源:互联网 发布:球球大作战改圣衣软件 编辑:程序博客网 时间:2024/04/29 04:00

实现的效果是一闪一闪的灯光,像警报器一样。

代码:

 

#include "ExampleApplication.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

 

Light* mLight;
SceneNode* mLightNode = 0;
ColourValue mMinLightColour(0.8, 0.1, 0.0);
ColourValue mMaxLightColour(1.0, 0.1, 0.0);
Real mMinFlareSize = 30;
Real mMaxFlareSize = 60;


/** This class 'wibbles' the light and billboard */
class LightWibbler : public ControllerValue<Real>
{
protected:
 Light* mLight;
 Billboard* mBillboard;
 ColourValue mColourRange;
 ColourValue mHalfColour;
 Real mMinSize;
 Real mSizeRange;
 Real intensity;
public:
 LightWibbler(Light* light, Billboard* billboard, const ColourValue& minColour,
  const ColourValue& maxColour, Real minSize, Real maxSize)
 {
  mLight = light;
  mBillboard = billboard;
  mColourRange.r = (maxColour.r - minColour.r) * 0.5;
  mColourRange.g = (maxColour.g - minColour.g) * 0.5;
  mColourRange.b = (maxColour.b - minColour.b) * 0.5;
  mHalfColour = minColour + mColourRange;
  mMinSize = minSize;
  mSizeRange = maxSize - minSize;
 }

 virtual Real  getValue (void) const
 {
  return intensity;
 }

 virtual void  setValue (Real value)
 {
  intensity = value;

  ColourValue newColour;

  // Attenuate the brightness of the light
  newColour.r = mHalfColour.r + (mColourRange.r * intensity);
  newColour.g = mHalfColour.g + (mColourRange.g * intensity);
  newColour.b = mHalfColour.b + (mColourRange.b * intensity);

  mLight->setDiffuseColour(newColour);
  mBillboard->setColour(newColour);
  // set billboard size
  Real newSize = mMinSize + (intensity * mSizeRange);
  mBillboard->setDimensions(newSize, newSize);

 }
};

class GrassListener : public ExampleFrameListener
{
protected:
 SceneManager* mSceneManager;

public:
 GrassListener(RenderWindow* win, Camera* cam, SceneManager* sceneManager)
  : ExampleFrameListener(win, cam),
  mSceneManager(sceneManager)
 {
 }


 bool frameRenderingQueued(const FrameEvent& evt)
 {
  if( ExampleFrameListener::frameRenderingQueued(evt) == false )
   return false;

  static Real timeDelay = 0;
  timeDelay -= evt.timeSinceLastFrame;

  return true;
 }
};

 

class Grass_Application : public ExampleApplication
{
public:
    Grass_Application() {}
 
protected:
 SceneNode *mpObjsNode; // the node wich will hold our entities

 void setupLighting()
 {
  mSceneMgr->setAmbientLight(ColourValue::Black);
  // Point light, movable, reddish
  mLight = mSceneMgr->createLight("Light2");
  mLight->setDiffuseColour(mMinLightColour);
  mLight->setSpecularColour(1, 1, 1);
  mLight->setAttenuation(8000,1,0.0005,0);

  //创建灯光节点
  mLightNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(
   "MovingLightNode");
  mLightNode->attachObject(mLight);


  // create billboard set
  BillboardSet* bbs = mSceneMgr->createBillboardSet("lightbbs", 1);
  bbs->setMaterialName("Examples/Flare");
  Billboard* bb = bbs->createBillboard(0,0,0,mMinLightColour);
  // attach
  mLightNode->attachObject(bbs);

  // create controller, after this is will get updated on its own
  ControllerFunctionRealPtr func = ControllerFunctionRealPtr(
   new WaveformControllerFunction(Ogre::WFT_SINE, 0.0, 0.5));
  ControllerManager& contMgr = ControllerManager::getSingleton();
  ControllerValueRealPtr val = ControllerValueRealPtr(
   new LightWibbler(mLight, bb, mMinLightColour, mMaxLightColour,
   mMinFlareSize, mMaxFlareSize));
  Controller<Real>* controller = contMgr.createController(
   contMgr.getFrameTimeSource(), val, func);

  //mLight->setPosition(Vector3(300,250,-300));
  mLightNode->setPosition(Vector3(300,250,-300));

 }

 void createScene(void)
    {
  mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2));

  mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");

  setupLighting();


  /*Entity* e = mSceneMgr->createEntity("head", "ogrehead.mesh");
  e->setMaterialName("Examples/OffsetMapping/Specular");
  SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  headNode->attachObject(e);
  headNode->setScale(7,7,7);
  headNode->setPosition(0,200,0);
  headNode->yaw(Degree(15));
  mCamera->move(Vector3(0,350,0));*/
 }

    // Create new frame listener
    void createFrameListener(void)
    {
        mFrameListener= new GrassListener(mWindow, mCamera, mSceneMgr);
        mRoot->addFrameListener(mFrameListener);
    }
};

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
    // Create application object
    Grass_Application app;

    try {
        app.go();
    } catch( Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        std::cerr << "An exception has occured: " << e.getFullDescription();
#endif
    }

    return 0;
}

#ifdef __cplusplus
}
#endif

 

 

 

有问题可以留言,呵呵。

我觉得能用最为重要,相关知识不了解也行。

原创粉丝点击