Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TEveTriangleSetGL.cxx
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TEveTriangleSetGL.h"
13 #include "TEveTriangleSet.h"
14 #include "TVector3.h"
15 
16 #include "TGLIncludes.h"
17 
18 /** \class TEveTriangleSetGL
19 \ingroup TEve
20 GL-renderer for TEveTriangleSet class.
21 
22 See also: TGLObject, TGLLogicalShape.
23 */
24 
25 ClassImp(TEveTriangleSetGL);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Constructor.
29 
30 TEveTriangleSetGL::TEveTriangleSetGL() : TGLObject(), fM(0)
31 {
32  // fDLCache = false; // Disable display list.
33  fMultiColor = kTRUE;
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Destructor.
38 
39 TEveTriangleSetGL::~TEveTriangleSetGL()
40 {
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Set model object.
45 
46 Bool_t TEveTriangleSetGL::SetModel(TObject* obj, const Option_t* /*opt*/)
47 {
48  fM = SetModelDynCast<TEveTriangleSet>(obj);
49  return kTRUE;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Set bounding-box from the model.
54 
55 void TEveTriangleSetGL::SetBBox()
56 {
57  // !! This ok if master sub-classed from TAttBBox
58  SetAxisAlignedBBox(((TEveTriangleSet*)fExternalObj)->AssertBBox());
59 }
60 
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Low-level GL rendering.
64 
65 void TEveTriangleSetGL::DirectDraw(TGLRnrCtx & /*rnrCtx*/) const
66 {
67  TEveTriangleSet& refTS = *fM;
68  Bool_t isScaled = refTS.RefMainTrans().IsScale();
69 
70  GLint ex_shade_model;
71  glGetIntegerv(GL_SHADE_MODEL, &ex_shade_model);
72  glShadeModel(GL_FLAT);
73 
74  glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
75 
76  glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
77  glEnable(GL_COLOR_MATERIAL);
78  glDisable(GL_CULL_FACE);
79  if (isScaled) glEnable(GL_NORMALIZE);
80  glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
81  glVertexPointer(3, GL_FLOAT, 0, refTS.fVerts);
82  glEnableClientState(GL_VERTEX_ARRAY);
83 
84  Int_t* tng = refTS.fTrings;
85  Float_t* nrm = refTS.fTringNorms;
86  UChar_t* col = refTS.fTringCols;
87 
88  TVector3 e1, e2, n;
89 
90  glBegin(GL_TRIANGLES);
91  for(Int_t t=0; t<refTS.fNTrings; ++t) {
92  if (nrm) {
93  glNormal3fv(nrm); nrm += 3;
94  } else {
95  Float_t* v0 = refTS.Vertex(tng[0]);
96  Float_t* v1 = refTS.Vertex(tng[1]);
97  Float_t* v2 = refTS.Vertex(tng[2]);
98  e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
99  e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
100  n = e1.Cross(e2);
101  if (!isScaled) n.SetMag(1);
102  glNormal3d(n.x(), n.y(), n.z());
103  }
104  if (col) {
105  TGLUtil::Color3ubv(col); col += 3;
106  }
107  glArrayElement(tng[0]);
108  glArrayElement(tng[1]);
109  glArrayElement(tng[2]);
110  tng += 3;
111  }
112  glEnd();
113 
114  glPopClientAttrib();
115  glPopAttrib();
116  glShadeModel(ex_shade_model);
117 }