TEveBoxGL.cxx

Go to the documentation of this file.
00001 // @(#)root/eve:$Id: TEveBoxGL.cxx 36816 2010-11-20 22:41:48Z matevz $
00002 // Author: Matevz Tadel, 2010
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TEveBoxGL.h"
00013 #include "TEveBox.h"
00014 
00015 #include "TGLRnrCtx.h"
00016 #include "TGLIncludes.h"
00017 
00018 #include "TMath.h"
00019 
00020 //==============================================================================
00021 // TEveBoxGL
00022 //==============================================================================
00023 
00024 //______________________________________________________________________________
00025 // OpenGL renderer class for TEveBox.
00026 //
00027 
00028 ClassImp(TEveBoxGL);
00029 
00030 //______________________________________________________________________________
00031 TEveBoxGL::TEveBoxGL() :
00032    TGLObject(), fM(0)
00033 {
00034    // Constructor.
00035 
00036    // fDLCache = kFALSE; // Disable display list.
00037 }
00038 
00039 //______________________________________________________________________________
00040 Bool_t TEveBoxGL::SetModel(TObject* obj, const Option_t* /*opt*/)
00041 {
00042    // Set model object.
00043 
00044    fM = SetModelDynCast<TEveBox>(obj);
00045    return kTRUE;
00046 }
00047 
00048 //______________________________________________________________________________
00049 void TEveBoxGL::SetBBox()
00050 {
00051    // Set bounding box.
00052 
00053    // !! This ok if master sub-classed from TAttBBox
00054    SetAxisAlignedBBox(((TEveBox*)fExternalObj)->AssertBBox());
00055 }
00056 
00057 //==============================================================================
00058 
00059 namespace
00060 {
00061    void subtract_and_normalize(const Float_t a[3], const Float_t b[3],
00062                                Float_t o[3])
00063    {
00064       // Calculate a - b and normalize the result.
00065       o[0] = a[0] - b[0];
00066       o[1] = a[1] - b[1];
00067       o[2] = a[2] - b[2];
00068       Float_t d = sqrtf(o[0]*o[0] + o[1]*o[1] + o[2]*o[2]);
00069       if (d != 0)
00070       {
00071          d = 1.0f / d;
00072          o[0] *= d;
00073          o[1] *= d;
00074          o[2] *= d;
00075       }
00076    }
00077 }
00078 
00079 //______________________________________________________________________________
00080 void TEveBoxGL::RenderOutline(const Float_t p[8][3]) const
00081 {
00082    // Render box with without normals.
00083    // To be used with lightning off, for outline.
00084 
00085    glBegin(GL_LINE_STRIP);
00086    glVertex3fv(p[0]); glVertex3fv(p[1]);
00087    glVertex3fv(p[5]); glVertex3fv(p[6]);
00088    glVertex3fv(p[2]); glVertex3fv(p[3]);
00089    glVertex3fv(p[7]); glVertex3fv(p[4]);
00090    glVertex3fv(p[0]); glVertex3fv(p[3]);
00091    glEnd();
00092 
00093    glBegin(GL_LINES);
00094    glVertex3fv(p[1]); glVertex3fv(p[2]);
00095    glVertex3fv(p[4]); glVertex3fv(p[5]);
00096    glVertex3fv(p[6]); glVertex3fv(p[7]);
00097    glEnd();
00098 }
00099 
00100 //______________________________________________________________________________
00101 void TEveBoxGL::RenderBoxStdNorm(const Float_t p[8][3]) const
00102 {
00103    // Render box with standard axis-aligned normals.
00104 
00105    glBegin(GL_QUADS);
00106 
00107    // bottom: 0123
00108    glNormal3f(0, 0, -1);
00109    glVertex3fv(p[0]);  glVertex3fv(p[1]);
00110    glVertex3fv(p[2]);  glVertex3fv(p[3]);
00111    // top:    7654
00112    glNormal3f(0, 0, 1);
00113    glVertex3fv(p[7]); glVertex3fv(p[6]);
00114    glVertex3fv(p[5]); glVertex3fv(p[4]);
00115    // back:  0451
00116    glNormal3f(0, 1, 0);
00117    glVertex3fv(p[0]); glVertex3fv(p[4]);
00118    glVertex3fv(p[5]); glVertex3fv(p[1]);
00119    // front:   3267
00120    glNormal3f(0, -1, 0);
00121    glVertex3fv(p[3]); glVertex3fv(p[2]);
00122    glVertex3fv(p[6]); glVertex3fv(p[7]);
00123    // left:    0374
00124    glNormal3f(-1, 0, 0);
00125    glVertex3fv(p[0]); glVertex3fv(p[3]);
00126    glVertex3fv(p[7]); glVertex3fv(p[4]);
00127    // right:   1562
00128    glNormal3f(1, 0, 0);
00129    glVertex3fv(p[1]); glVertex3fv(p[5]);
00130    glVertex3fv(p[6]); glVertex3fv(p[2]);
00131 
00132    glEnd();
00133 }
00134 
00135 //______________________________________________________________________________
00136 void TEveBoxGL::RenderBoxAutoNorm(const Float_t p[8][3]) const
00137 {
00138    // Render box, calculate normals on the fly from first three points.
00139 
00140    Float_t e[6][3], n[3];
00141    subtract_and_normalize(p[1], p[0], e[0]);
00142    subtract_and_normalize(p[3], p[0], e[1]);
00143    subtract_and_normalize(p[4], p[0], e[2]);
00144    subtract_and_normalize(p[5], p[6], e[3]);
00145    subtract_and_normalize(p[7], p[6], e[4]);
00146    subtract_and_normalize(p[2], p[6], e[5]);
00147 
00148    glBegin(GL_QUADS);
00149 
00150    // bottom: 0123
00151    glNormal3fv(TMath::Cross(e[0], e[1], n));
00152    glVertex3fv(p[0]); glVertex3fv(p[1]);
00153    glVertex3fv(p[2]); glVertex3fv(p[3]);
00154    // top:    7654
00155    glNormal3fv(TMath::Cross(e[3], e[4], n));
00156    glVertex3fv(p[7]); glVertex3fv(p[6]);
00157    glVertex3fv(p[5]); glVertex3fv(p[4]);
00158    // back:  0451
00159    glNormal3fv(TMath::Cross(e[2], e[0], n));
00160    glVertex3fv(p[0]); glVertex3fv(p[4]);
00161    glVertex3fv(p[5]); glVertex3fv(p[1]);
00162    // front:   3267
00163    glNormal3fv(TMath::Cross(e[4], e[5], n));
00164    glVertex3fv(p[3]); glVertex3fv(p[2]);
00165    glVertex3fv(p[6]); glVertex3fv(p[7]);
00166    // left:    0374
00167    glNormal3fv(TMath::Cross(e[1], e[2], n));
00168    glVertex3fv(p[0]); glVertex3fv(p[3]);
00169    glVertex3fv(p[7]); glVertex3fv(p[4]);
00170    // right:   1562
00171    glNormal3fv(TMath::Cross(e[5], e[3], n));
00172    glVertex3fv(p[1]); glVertex3fv(p[5]);
00173    glVertex3fv(p[6]); glVertex3fv(p[2]);
00174 
00175    glEnd();
00176 }
00177 
00178 //______________________________________________________________________________
00179 void TEveBoxGL::Draw(TGLRnrCtx& rnrCtx) const
00180 {
00181    // Render with OpenGL.
00182 
00183    if (rnrCtx.IsDrawPassOutlineLine())
00184    {
00185       RenderOutline(fM->fVertices);
00186       return;
00187    }
00188 
00189    if (fM->fHighlightFrame && rnrCtx.Highlight())
00190    {
00191       if (fM->fDrawFrame)
00192       {
00193          glEnable(GL_BLEND);
00194          TGLUtil::LineWidth(fM->fLineWidth);
00195          TGLUtil::Color(fM->fLineColor);
00196       }
00197       RenderOutline(fM->fVertices);
00198    }
00199    else
00200    {
00201       TGLObject::Draw(rnrCtx);
00202    }
00203 }
00204 
00205 //______________________________________________________________________________
00206 void TEveBoxGL::DirectDraw(TGLRnrCtx&) const
00207 {
00208    // Render with OpenGL, create display-list.
00209 
00210    fMultiColor = (fM->fDrawFrame && fM->fFillColor != fM->fLineColor);
00211 
00212    glPushAttrib(GL_ENABLE_BIT);
00213 
00214    glEnable(GL_POLYGON_OFFSET_FILL);
00215    glPolygonOffset(1.0f, 1.0f);
00216    RenderBoxAutoNorm(fM->fVertices);
00217    glDisable(GL_POLYGON_OFFSET_FILL);
00218 
00219    // Frame
00220    if (fM->fDrawFrame)
00221    {
00222       glEnable(GL_BLEND);
00223       TGLUtil::Color(fM->fLineColor);
00224       TGLUtil::LineWidth(fM->fLineWidth);
00225       RenderOutline(fM->fVertices);
00226    }
00227 
00228    glPopAttrib();
00229 }
00230 
00231 
00232 //==============================================================================
00233 // TEveBoxProjectedGL
00234 //==============================================================================
00235 
00236 //______________________________________________________________________________
00237 // OpenGL renderer class for TEveBoxProjected.
00238 //
00239 
00240 ClassImp(TEveBoxProjectedGL);
00241 
00242 //______________________________________________________________________________
00243 TEveBoxProjectedGL::TEveBoxProjectedGL() :
00244    TGLObject(), fM(0)
00245 {
00246    // Constructor.
00247 
00248    // fDLCache = kFALSE; // Disable display list.
00249 }
00250 
00251 /******************************************************************************/
00252 
00253 //______________________________________________________________________________
00254 Bool_t TEveBoxProjectedGL::SetModel(TObject* obj, const Option_t* /*opt*/)
00255 {
00256    // Set model object.
00257 
00258    fM = SetModelDynCast<TEveBoxProjected>(obj);
00259    return kTRUE;
00260 }
00261 
00262 //______________________________________________________________________________
00263 void TEveBoxProjectedGL::SetBBox()
00264 {
00265    // Set bounding box.
00266 
00267    SetAxisAlignedBBox(((TEveBoxProjected*)fExternalObj)->AssertBBox());
00268 }
00269 
00270 //------------------------------------------------------------------------------
00271 
00272 
00273 //______________________________________________________________________________
00274 void TEveBoxProjectedGL::RenderPoints(Int_t mode) const
00275 {
00276    // Render points with given GL mode.
00277    // This is used for polygon and outline drawing.
00278 
00279    Int_t B = fM->fBreakIdx;
00280    Int_t N = fM->fPoints.size();
00281    if (B != 0)
00282    {
00283       glBegin(mode);
00284       for (Int_t i = 0; i < B; ++i)
00285       {
00286          glVertex2fv(fM->fPoints[i]);
00287       }
00288       glEnd();
00289    }
00290    glBegin(mode);
00291    for (Int_t i = B; i < N; ++i)
00292    {
00293       glVertex2fv(fM->fPoints[i]);
00294    }
00295    glEnd();
00296 }
00297 
00298 //______________________________________________________________________________
00299 void TEveBoxProjectedGL::Draw(TGLRnrCtx& rnrCtx) const
00300 {
00301    // Render with OpenGL.
00302 
00303    if (rnrCtx.IsDrawPassOutlineLine())
00304       return;
00305 
00306    glPushMatrix();
00307    glTranslatef(0.0f, 0.0f, fM->fDepth);
00308 
00309    if (fM->fHighlightFrame && rnrCtx.Highlight())
00310    {
00311       if (fM->fDrawFrame)
00312       {
00313          glEnable(GL_BLEND);
00314          TGLUtil::LineWidth(fM->fLineWidth);
00315          TGLUtil::Color(fM->fLineColor);
00316       }
00317       RenderPoints(GL_LINE_LOOP);
00318    }
00319    else
00320    {
00321       TGLObject::Draw(rnrCtx);
00322    }
00323 
00324    if (TEveBoxProjected::fgDebugCornerPoints && ! fM->fDebugPoints.empty())
00325    {
00326       glColor3f(1,0,0);
00327       Int_t N = fM->fDebugPoints.size();
00328       glPointSize(4);
00329       glBegin(GL_POINTS);
00330       for (Int_t i = 0; i < N; ++i)
00331       {
00332          glVertex2fv(fM->fDebugPoints[i]);
00333       }
00334       glEnd();
00335    }
00336 
00337    glPopMatrix();
00338 }
00339 
00340 //______________________________________________________________________________
00341 void TEveBoxProjectedGL::DirectDraw(TGLRnrCtx&) const
00342 {
00343    // Render with OpenGL, create display-list.
00344 
00345    fMultiColor = (fM->fDrawFrame && fM->fFillColor != fM->fLineColor);
00346 
00347    glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
00348 
00349    glDisable(GL_LIGHTING);
00350 
00351    glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
00352    glEnable(GL_COLOR_MATERIAL);
00353    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00354    glDisable(GL_CULL_FACE);
00355 
00356    glEnable(GL_POLYGON_OFFSET_FILL);
00357    glPolygonOffset(1.0f, 1.0f);
00358    RenderPoints(GL_POLYGON);
00359    glDisable(GL_POLYGON_OFFSET_FILL);
00360 
00361    // Frame
00362    if (fM->fDrawFrame)
00363    {
00364       glEnable(GL_BLEND);
00365       TGLUtil::Color(fM->fLineColor);
00366       TGLUtil::LineWidth(fM->fLineWidth);
00367       RenderPoints(GL_LINE_LOOP);
00368    }
00369 
00370    glPopAttrib();
00371 }

Generated on Tue Jul 5 14:16:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1