lugre_ColourClipPaneOverlay.cpp

Go to the documentation of this file.
00001 #include "lugre_ColourClipPaneOverlay.h"
00002 #include <OgreOverlayElementFactory.h>
00003 #include <OgreOverlayManager.h>
00004 
00005 using namespace Ogre;
00006 
00007 namespace Lugre {
00008 
00009 String cColourClipPaneOverlay::msTypeName = "ColourClipPane";
00010 
00011 
00012 
00013 // ***** ***** ***** ***** ***** Factory
00014 
00015 
00016 
00018 class ColourClipPaneOverlayElementFactory: public OverlayElementFactory { public:
00020     OverlayElement* createOverlayElement(const String& instanceName) {
00021         return new cColourClipPaneOverlay(instanceName);
00022     }
00024     const String& getTypeName(void) const {
00025         return cColourClipPaneOverlay::msTypeName;
00026     }
00027 };
00028 
00029 void    cColourClipPaneOverlay::RegisterFactory () {
00030     OverlayManager::getSingleton().addOverlayElementFactory(new ColourClipPaneOverlayElementFactory());
00031 }
00032         
00033 
00034 
00035 // ***** ***** ***** ***** ***** Vertex
00036 
00037 
00038 cColourClipPaneOverlay::Vertex::Vertex() {}
00039     
00040 cColourClipPaneOverlay::Vertex::Vertex(const Ogre::Real x,const Ogre::Real y,const Ogre::Real u,const Ogre::Real v,const Ogre::ColourValue& col)
00041     : x(x), y(y), u(u), v(v), col(col) {}
00042         
00043 cColourClipPaneOverlay::Vertex Interpolate  (const cColourClipPaneOverlay::Vertex& a,const cColourClipPaneOverlay::Vertex& b,const float t) {
00044     if (t <= 0.0) return a;
00045     if (t >= 1.0) return b;
00046     return cColourClipPaneOverlay::Vertex(  a.x + t*(b.x-a.x),
00047                                             a.y + t*(b.y-a.y),
00048                                             a.u + t*(b.u-a.u),
00049                                             a.v + t*(b.v-a.v),
00050                                             ColourValue(
00051                                                 a.col.r + t*(b.col.r-a.col.r),
00052                                                 a.col.g + t*(b.col.g-a.col.g),
00053                                                 a.col.b + t*(b.col.b-a.col.b),
00054                                                 a.col.a + t*(b.col.a-a.col.a)
00055                                             ) );
00056 }
00057 
00058 void    cColourClipPaneOverlay::Vertex::Draw    (cRobRenderOp* pRobRenderOp,const Ogre::Real z) {
00059     pRobRenderOp->Vertex(Vector3(x * 2.0 - 1.0,-(y * 2.0 - 1.0),z),u,v,col);
00060 }
00061 
00062 void    cColourClipPaneOverlay::Vertex::Print       () {
00063     printf(" Vertex x=%f y=%f u=%f v=%f\n",x,y,u,v);
00064 }
00065 
00066 
00067 // ***** ***** ***** ***** ***** VertexRect
00068 
00069 
00070 
00071 cColourClipPaneOverlay::VertexRect::VertexRect () {}
00072     
00073 cColourClipPaneOverlay::VertexRect::VertexRect (const cColourClipPaneOverlay::Vertex& lt,
00074                                                 const cColourClipPaneOverlay::Vertex& lb,
00075                                                 const cColourClipPaneOverlay::Vertex& rt,
00076                                                 const cColourClipPaneOverlay::Vertex& rb)
00077     : lt(lt), lb(lb), rt(rt), rb(rb) {}
00078 
00080 void        cColourClipPaneOverlay::VertexRect::SetLTWH     (const Ogre::Real l,const Ogre::Real t,const Ogre::Real w,const Ogre::Real h) {
00081     SetLTRB(l,t,l+w,t+h);
00082 }
00083 
00085 void        cColourClipPaneOverlay::VertexRect::SetLTRB     (const Ogre::Real l,const Ogre::Real t,const Ogre::Real r,const Ogre::Real b) {
00086     lt.x = lb.x = l;
00087     rt.x = rb.x = r;
00088     lt.y = rt.y = t;
00089     lb.y = rb.y = b;
00090 }
00091 
00092 void        cColourClipPaneOverlay::VertexRect::SetCol      (const Ogre::ColourValue& col) {
00093     lt.col = rt.col = lb.col = rb.col = col;
00094 }
00095 
00096 void        cColourClipPaneOverlay::VertexRect::SetUV       (const Ogre::Real u1,const Ogre::Real v1,const Ogre::Real u2,const Ogre::Real v2) {
00097     lt.u = lb.u = u1;
00098     rt.u = rb.u = u2;
00099     lt.v = rt.v = v1;
00100     lb.v = rb.v = v2;
00101 }
00102 
00103 cColourClipPaneOverlay::Vertex      cColourClipPaneOverlay::VertexRect::Pick        (const Ogre::Real x,const Ogre::Real y) {
00104     Real w = rb.x - lt.x;
00105     Real h = rb.y - lt.y;
00106     Real tx = (w>0.0)?((x-lt.x)/w):0.0;
00107     Real ty = (h>0.0)?((y-lt.y)/h):0.0;
00108     if (ty <= 0.0) return       Lugre::Interpolate(lt,rt,tx);
00109     if (ty >= 1.0) return                                       Lugre::Interpolate(lb,rb,tx);
00110     return Lugre::Interpolate(  Lugre::Interpolate(lt,rt,tx) ,  Lugre::Interpolate(lb,rb,tx) , ty );
00111 }
00112 
00113 cColourClipPaneOverlay::VertexRect  cColourClipPaneOverlay::VertexRect::Intersect   (const Ogre::Rectangle& clippingRegion) {
00114     cColourClipPaneOverlay::VertexRect res;
00115     res.lt = Pick(clippingRegion.left,  clippingRegion.top);
00116     res.rt = Pick(clippingRegion.right, clippingRegion.top);
00117     res.lb = Pick(clippingRegion.left,  clippingRegion.bottom);
00118     res.rb = Pick(clippingRegion.right, clippingRegion.bottom);
00119     return res;
00120 }
00121 
00122 void    cColourClipPaneOverlay::VertexRect::DrawStrip       (cRobRenderOp* pRobRenderOp,const Ogre::Real z) {
00123     lt.Draw(pRobRenderOp,z);
00124     lb.Draw(pRobRenderOp,z);
00125     rt.Draw(pRobRenderOp,z);
00126     rb.Draw(pRobRenderOp,z);    
00127 }
00128 
00129 void    cColourClipPaneOverlay::VertexRect::Print       () {
00130     printf(" lt"); lt.Print();
00131     printf(" lb"); lb.Print();
00132     printf(" rt"); rt.Print();
00133     printf(" rb"); rb.Print();  
00134 }
00135 
00136 void    cColourClipPaneOverlay::VertexRect::DrawList        (cRobRenderOp* pRobRenderOp,const Ogre::Real z) {
00137     lt.Draw(pRobRenderOp,z);
00138     lb.Draw(pRobRenderOp,z);
00139     rt.Draw(pRobRenderOp,z);
00140     
00141     rt.Draw(pRobRenderOp,z);
00142     lb.Draw(pRobRenderOp,z);
00143     rb.Draw(pRobRenderOp,z);    
00144 }
00145 
00146 
00147 // ***** ***** ***** ***** ***** cColourClipPaneOverlay
00148 
00149 
00150 
00151 cColourClipPaneOverlay::cColourClipPaneOverlay(const Ogre::String& name) : 
00152     cRobRenderOp(&mRenderOp), OverlayContainer(name), mTransparent(false), mbClipInitialized(false)
00153 {
00154     mForm.SetUV(0,0,1,1);
00155     mForm.SetCol(ColourValue::White);
00156     // default to pixel coords
00157     setMetricsMode(GMM_PIXELS);
00158 }
00159     
00160 cColourClipPaneOverlay::~cColourClipPaneOverlay() {
00161     delete mRenderOp.vertexData; mRenderOp.vertexData = 0;
00162     delete mRenderOp.indexData; mRenderOp.indexData = 0;
00163 }
00164     
00166 void cColourClipPaneOverlay::initialise(void) {
00167     OverlayContainer::initialise();
00168     mInitialised = true;
00169 }
00170 
00172 void    cColourClipPaneOverlay::SetTexCoords    (const Ogre::Real fU1,const Ogre::Real fV1,const Ogre::Real fU2,const Ogre::Real fV2) {
00173     mForm.SetUV(fU1,fV1,fU2,fV2);
00174     mGeomPositionsOutOfDate = true;
00175 }
00176 
00177 void    cColourClipPaneOverlay::setColour       (const Ogre::ColourValue& col) {
00178     SetColours(col,col,col,col);
00179 }
00180 
00181 void    cColourClipPaneOverlay::SetColours      (const Ogre::ColourValue colLT,const Ogre::ColourValue colRT,const Ogre::ColourValue colLB,const Ogre::ColourValue colRB) {
00182     mForm.lt.col = colLT;
00183     mForm.rt.col = colRT;
00184     mForm.lb.col = colLB;
00185     mForm.rb.col = colRB;
00186     mGeomPositionsOutOfDate = true;
00187 }
00188 
00190 void    cColourClipPaneOverlay::SetClip         (const Ogre::Real fCL,const Ogre::Real fCT,const Ogre::Real fCW,const Ogre::Real fCH) {
00191     mbClipInitialized = true;
00192     mClip.left = fCL;
00193     mClip.top = fCT;
00194     mClip.right = fCL+fCW;
00195     mClip.bottom = fCT+fCH;
00196     mGeomPositionsOutOfDate = true;
00197 }
00198 
00199 //---------------------------------------------------------------------
00200 void cColourClipPaneOverlay::setTransparent(bool isTransparent)
00201 {
00202     mTransparent = isTransparent;
00203 }
00204 
00205 //---------------------------------------------------------------------
00206 bool cColourClipPaneOverlay::isTransparent(void) const
00207 {
00208     return mTransparent;
00209 }
00210 
00212 const String& cColourClipPaneOverlay::getTypeName(void) const {
00213     return msTypeName;
00214 }
00215 
00217 void cColourClipPaneOverlay::getRenderOperation(RenderOperation& op) {
00218     op = mRenderOp;
00219 }
00220 
00222 void cColourClipPaneOverlay::setMaterialName(const String& matName) {
00223     OverlayContainer::setMaterialName(matName);
00224 }
00225 
00227 void cColourClipPaneOverlay::_updateRenderQueue(RenderQueue* queue) {
00228     if (mVisible)
00229     {
00230 
00231         if (!mTransparent && !mpMaterial.isNull())
00232         {
00233             OverlayElement::_updateRenderQueue(queue);
00234         }
00235 
00236         // Also add children
00237         ChildIterator it = getChildIterator();
00238         while (it.hasMoreElements())
00239         {
00240             // Give children ZOrder 1 higher than this
00241             it.getNext()->_updateRenderQueue(queue);
00242         }
00243     }
00244 }
00245 
00247 void cColourClipPaneOverlay::updatePositionGeometry(void) {
00248     /*
00249     // init clip to fullscreen
00250     if (!mbClipInitialized) {
00251         mbClipInitialized = true;
00252         mClip.left = 0;
00253         mClip.top = 0;
00254         if (mMetricsMode != GMM_RELATIVE) {
00255             mClip.right = mClip.left+cOgreWrapper::GetSingleton().GetViewportWidth();
00256             mClip.bottom = mClip.top+cOgreWrapper::GetSingleton().GetViewportHeight();
00257         } else {
00258             mClip.right = mClip.left+1.0;
00259             mClip.bottom = mClip.top+1.0;
00260         }
00261     }
00262     */
00263     
00264     static VertexRect clipped;
00265     mForm.SetLTWH(_getDerivedLeft(),_getDerivedTop(),mWidth,mHeight);
00266     
00267     if (mbClipInitialized) {
00268         // calc clip region in screen-relative coords
00269         Ogre::Rectangle clippingRegion = mClip;
00270         if (mMetricsMode != GMM_RELATIVE) {
00271             clippingRegion.left     *= mPixelScaleX;
00272             clippingRegion.right    *= mPixelScaleX;
00273             clippingRegion.top      *= mPixelScaleY;
00274             clippingRegion.bottom   *= mPixelScaleY;
00275         }
00276             
00277         // update form pos and calc clipped form
00278         clipped = mForm.Intersect(clippingRegion);
00279     } else {
00280         // ignore clip
00281         clipped = mForm;
00282     }
00283     
00284         
00285     // clear z buffer under overlay
00286     Real maxz   = GetMaxZ();
00287     
00288     // construct geometry
00289     #if 0
00290         0-----2
00291         |    /|
00292         |  /  |
00293         |/    |
00294         1-----3
00295     #endif
00296     Begin(4,0,false,false,Ogre::RenderOperation::OT_TRIANGLE_STRIP);
00297     clipped.DrawStrip(this,maxz);
00298     End();
00299 }
00300 
00302 void cColourClipPaneOverlay::updateTextureGeometry(void) {}
00303 
00305 void cColourClipPaneOverlay::addBaseParameters(void) { OverlayContainer::addBaseParameters(); }
00306 
00307 };

Generated on Thu Feb 9 06:00:13 2012 for cpp by  doxygen 1.5.6