]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/QuadSetGL.cxx
Add getter for fValueIsColor.
[u/mrichter/AliRoot.git] / EVE / Reve / QuadSetGL.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 #include <TMath.h>
19
20 #include "QuadSetGL.h"
21 #include <Reve/FrameBoxGL.h>
22
23 #include <TGLDrawFlags.h>
24
25 #include <GL/gl.h>
26 #include <GL/glu.h>
27
28 using namespace Reve;
29
30 //______________________________________________________________________
31 // OldQuadSetGL
32 //
33
34 ClassImp(OldQuadSetGL)
35
36 /**************************************************************************/
37
38 OldQuadSetGL::OldQuadSetGL() : TGLObject()
39 {
40   // fCached = false; // Disable DL.
41 }
42
43 OldQuadSetGL::~OldQuadSetGL()
44 {}
45
46 /**************************************************************************/
47
48 Bool_t OldQuadSetGL::SetModel(TObject* obj)
49 {
50   return SetModelCheckClass(obj, Reve::OldQuadSet::Class());
51 }
52
53 void OldQuadSetGL::SetBBox()
54 {
55   SetAxisAlignedBBox(((OldQuadSet*)fExternalObj)->AssertBBox());
56 }
57
58 /**************************************************************************/
59
60 void OldQuadSetGL::DirectDraw(const TGLDrawFlags & ) const
61 {
62   // printf("OldQuadSetGLRenderer::DirectDraw Style %d, LOD %d\n", flags.Style(), flags.LOD());
63
64   OldQuadSet& Q = * (OldQuadSet*) fExternalObj;
65
66   glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
67
68   glDisable(GL_LIGHTING);
69   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
70   glEnable(GL_COLOR_MATERIAL);
71   glPolygonMode(GL_FRONT, GL_LINE);
72   glPolygonMode(GL_BACK,  GL_LINE);
73   glDisable(GL_CULL_FACE);
74
75   Float_t c[4]; glGetFloatv(GL_CURRENT_COLOR, c);
76   //  UChar_t alpha = (UChar_t)(255*c[3]);
77
78   glBegin(GL_QUADS);
79   for(std::vector<Quad>::iterator q=Q.fQuads.begin(); q!=Q.fQuads.end(); ++q) {
80     UChar_t* c = (UChar_t*) &q->color;
81     //glColor4ub(c[0], c[1], c[2], (c[3]*alpha) >> 8);
82     glColor3ub(c[0], c[1], c[2]);
83     glVertex3fv(q->vertices);
84     glVertex3fv(q->vertices + 3);
85     glVertex3fv(q->vertices + 6);
86     glVertex3fv(q->vertices + 9);
87   }
88   glEnd();
89
90   glPopAttrib();
91 }
92
93
94 /**************************************************************************/
95 /**************************************************************************/
96 /**************************************************************************/
97 /**************************************************************************/
98
99 //______________________________________________________________________
100 // QuadSetGL
101 //
102
103 ClassImp(QuadSetGL)
104
105 /**************************************************************************/
106
107 QuadSetGL::QuadSetGL() : TGLObject(), fM(0)
108 {
109   // fCached = false; // Disable DL.
110 }
111
112 QuadSetGL::~QuadSetGL()
113 {}
114
115 /**************************************************************************/
116
117 Bool_t QuadSetGL::SetModel(TObject* obj)
118 {
119   Bool_t ok = SetModelCheckClass(obj, Reve::QuadSet::Class());
120   fM = ok ? dynamic_cast<Reve::QuadSet*>(obj) : 0;
121   return ok;
122 }
123
124 void QuadSetGL::SetBBox()
125 {
126   SetAxisAlignedBBox(fM->AssertBBox());
127 }
128
129 /**************************************************************************/
130
131 inline Bool_t QuadSetGL::SetupColor(const QuadSet::QuadBase& q) const
132 {
133   if (fM->fValueIsColor)
134   {
135     glColor4ubv((UChar_t*) & q.fValue);
136     return kTRUE;
137   }
138   else
139   {
140     UChar_t c[4];
141     Bool_t visible = fM->fPalette->ColorFromValue(q.fValue, fM->fDefaultValue, c);
142     if (visible)
143       glColor4ubv(c);
144     return visible;
145   }
146 }
147
148 /**************************************************************************/
149
150 void QuadSetGL::DirectDraw(const TGLDrawFlags & flags) const
151 {
152   static const Exc_t eH("QuadSetGL::DirectDraw ");
153
154   // printf("QuadSetGLRenderer::DirectDraw Style %d, LOD %d\n", flags.Style(), flags.LOD());
155
156   QuadSet& mQ = * fM;
157
158   if (mQ.fFrame != 0)
159     FrameBoxGL::Render(mQ.fFrame);
160
161   if (mQ.fPlex.Size() == 0)
162     return;
163   if ( ! mQ.fValueIsColor && mQ.fPalette == 0)
164   {
165     mQ.AssertPalette();
166   }
167
168   glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
169   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
170   glEnable(GL_COLOR_MATERIAL);
171   glDisable(GL_CULL_FACE);
172
173   if (mQ.fRenderMode == QuadSet::RM_Fill)
174     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
175   else if (mQ.fRenderMode == QuadSet::RM_Line)
176     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
177
178   if (mQ.fDisableLigting)  glDisable(GL_LIGHTING);
179
180   if (mQ.fQuadType < QuadSet::QT_Rectangle_End)      RenderQuads(flags);
181   else if (mQ.fQuadType < QuadSet::QT_Line_End)      RenderLines(flags);
182   else if (mQ.fQuadType < QuadSet::QT_Hexagon_End)   RenderHexagons(flags);
183
184   glPopAttrib();
185
186 }
187
188
189 void QuadSetGL::RenderQuads(const TGLDrawFlags &flags) const
190 {
191   static const Exc_t eH("QuadSetGL::RenderQuads ");
192
193   QuadSet& mQ = * fM;
194
195   GLenum primitiveType;
196   if (mQ.fRenderMode != QuadSet::RM_Line)
197   {
198     primitiveType = GL_QUADS;
199     if (mQ.fQuadType == QuadSet::QT_FreeQuad)
200       glEnable(GL_NORMALIZE);
201     else
202       glNormal3f(0, 0, 1);
203   } else {
204     primitiveType = GL_LINE_LOOP;
205   }
206
207   VoidCPlex::iterator qi(mQ.fPlex);
208
209   if (flags.SecSelection()) glPushName(0);
210
211   switch (mQ.fQuadType)
212   {
213
214     case QuadSet::QT_FreeQuad:
215     {
216       Float_t e1[3], e2[3], normal[3];
217       while (qi.next()) {
218         QuadSet::QFreeQuad& q = * (QuadSet::QFreeQuad*) qi();
219         if (SetupColor(q))
220         {
221           Float_t* p = q.fVertices;
222           e1[0] = p[3] - p[0]; e1[1] = p[4] - p[1]; e1[2] = p[5] - p[2];
223           e2[0] = p[6] - p[0]; e2[1] = p[7] - p[1]; e2[2] = p[8] - p[2];
224           TMath::Cross(e1, e2, normal);
225           if (flags.SecSelection()) glLoadName(qi.index());
226           glBegin(primitiveType);
227           glNormal3fv(normal);
228           glVertex3fv(p);
229           glVertex3fv(p + 3);
230           glVertex3fv(p + 6);
231           glVertex3fv(p + 9);
232           glEnd();
233         }
234       }
235       break;
236     }
237
238     case QuadSet::QT_RectangleXY:
239     {
240       while (qi.next()) {
241         QuadSet::QRect& q = * (QuadSet::QRect*) qi();
242         if (SetupColor(q))
243         {
244           if (flags.SecSelection()) glLoadName(qi.index());
245           glBegin(primitiveType);
246           glVertex3f(q.fX,        q.fY,        q.fZ);
247           glVertex3f(q.fX + q.fW, q.fY,        q.fZ);
248           glVertex3f(q.fX + q.fW, q.fY + q.fH, q.fZ);
249           glVertex3f(q.fX,        q.fY + q.fH, q.fZ);
250           glEnd();
251         }
252       }
253       break;
254     }
255
256     case QuadSet::QT_RectangleXYFixedDim:
257     {
258       const Float_t& w = mQ.fDefWidth;
259       const Float_t& h = mQ.fDefHeight;
260       while (qi.next()) {
261         QuadSet::QRectFixDim& q = * (QuadSet::QRectFixDim*) qi();
262         if (SetupColor(q))
263         {
264           if (flags.SecSelection()) glLoadName(qi.index());
265           glBegin(primitiveType);
266           glVertex3f(q.fX,     q.fY,     q.fZ);
267           glVertex3f(q.fX + w, q.fY,     q.fZ);
268           glVertex3f(q.fX + w, q.fY + h, q.fZ);
269           glVertex3f(q.fX,     q.fY + h, q.fZ);
270           glEnd();
271         }
272       }
273       break;
274     }
275
276     case QuadSet::QT_RectangleXYFixedZ:
277     {
278       const Float_t& z = mQ.fDefCoord;
279       while (qi.next()) {
280         QuadSet::QRectFixC& q = * (QuadSet::QRectFixC*) qi();
281         if (SetupColor(q))
282         {
283           if (flags.SecSelection()) glLoadName(qi.index());
284           glBegin(primitiveType);
285           glVertex3f(q.fX,        q.fY,        z);
286           glVertex3f(q.fX + q.fW, q.fY,        z);
287           glVertex3f(q.fX + q.fW, q.fY + q.fH, z);
288           glVertex3f(q.fX,        q.fY + q.fH, z);
289           glEnd();
290         }
291       }
292       break;
293     }
294
295     case QuadSet::QT_RectangleXZFixedY:
296     {
297       const Float_t& z = mQ.fDefCoord;
298       while (qi.next()) {
299         QuadSet::QRectFixC& q = * (QuadSet::QRectFixC*) qi();
300         if (SetupColor(q))
301         {
302           if (flags.SecSelection()) glLoadName(qi.index());
303           glBegin(primitiveType);
304           glVertex3f(q.fX,        z, q.fY);
305           glVertex3f(q.fX + q.fW, z, q.fY);
306           glVertex3f(q.fX + q.fW, z, q.fY + q.fH);
307           glVertex3f(q.fX,        z, q.fY + q.fH);
308           glEnd();
309         }
310       }
311       break;
312     }
313
314     case QuadSet::QT_RectangleXYFixedDimZ:
315     {
316       const Float_t& z = mQ.fDefCoord;
317       const Float_t& w = mQ.fDefWidth;
318       const Float_t& h = mQ.fDefHeight;
319       while (qi.next()) {
320         QuadSet::QRectFixDimC& q = * (QuadSet::QRectFixDimC*) qi();
321         if (SetupColor(q))
322         {
323           if (flags.SecSelection()) glLoadName(qi.index());
324           glBegin(primitiveType);
325           glVertex3f(q.fX,     q.fY,     z);
326           glVertex3f(q.fX + w, q.fY,     z);
327           glVertex3f(q.fX + w, q.fY + h, z);
328           glVertex3f(q.fX,     q.fY + h, z);
329           glEnd();
330         }
331       }
332       break;
333     }
334
335     case QuadSet::QT_RectangleXZFixedDimY:
336     {
337       const Float_t& z = mQ.fDefCoord;
338       const Float_t& w = mQ.fDefWidth;
339       const Float_t& h = mQ.fDefHeight;
340       while (qi.next()) {
341         QuadSet::QRectFixDimC& q = * (QuadSet::QRectFixDimC*) qi();
342         if (SetupColor(q))
343         {
344           if (flags.SecSelection()) glLoadName(qi.index());
345           glBegin(primitiveType);
346           glVertex3f(q.fX,     z, q.fY);
347           glVertex3f(q.fX + w, z, q.fY);
348           glVertex3f(q.fX + w, z, q.fY + h);
349           glVertex3f(q.fX,     z, q.fY + h);
350           glEnd();
351         }
352       }
353       break;
354     }
355
356     default:
357       throw(eH + "unsupported quad-type.");
358
359   } // end switch quad-type
360
361   if (flags.SecSelection()) glPopName();
362 }
363
364
365 void QuadSetGL::RenderLines(const TGLDrawFlags &flags) const
366 {
367   static const Exc_t eH("QuadSetGL::RenderLines ");
368
369   QuadSet& mQ = * fM;
370
371   VoidCPlex::iterator qi(mQ.fPlex);
372
373   if (flags.SecSelection()) glPushName(0);
374
375   switch (mQ.fQuadType)
376   {
377
378     case QuadSet::QT_LineXYFixedZ:
379     {
380       const Float_t& z = mQ.fDefCoord;
381       while (qi.next()) {
382         QuadSet::QLineFixC& q = * (QuadSet::QLineFixC*) qi();
383         if (SetupColor(q))
384         {
385           if (flags.SecSelection()) glLoadName(qi.index());
386           glBegin(GL_LINES);
387           glVertex3f(q.fX,         q.fY,         z);
388           glVertex3f(q.fX + q.fDx, q.fY + q.fDy, z);
389           glEnd();
390         }
391       }
392       break;
393     }
394
395     case QuadSet::QT_LineXZFixedY:
396     {
397       const Float_t& z = mQ.fDefCoord;
398       while (qi.next()) {
399         QuadSet::QLineFixC& q = * (QuadSet::QLineFixC*) qi();
400         if (SetupColor(q))
401         {
402           if (flags.SecSelection()) glLoadName(qi.index());
403           glBegin(GL_LINES);
404           glVertex3f(q.fX,         z, q.fY);
405           glVertex3f(q.fX + q.fDx, z, q.fY + q.fDy);
406           glEnd();
407         }
408       }
409       break;
410     }
411
412     default:
413       throw(eH + "unsupported quad-type.");
414
415   }
416
417   if (flags.SecSelection()) glPopName();
418 }
419
420 void QuadSetGL::RenderHexagons(const TGLDrawFlags &flags) const
421 {
422   static const Exc_t eH("QuadSetGL::RenderHexagons ");
423
424   const Float_t sqr3hf = 0.5*TMath::Sqrt(3);
425
426   QuadSet& mQ = * fM;
427
428   GLenum primitveType = (mQ.fRenderMode != QuadSet::RM_Line) ?
429     GL_POLYGON : GL_LINE_LOOP;
430
431   glNormal3f(0, 0, 1);
432
433   VoidCPlex::iterator qi(mQ.fPlex);
434
435   if (flags.SecSelection()) glPushName(0);
436
437   switch (mQ.fQuadType)
438   {
439
440     case QuadSet::QT_HexagonXY:
441     {
442       while (qi.next()) {
443         QuadSet::QHex& q = * (QuadSet::QHex*) qi();
444         if (SetupColor(q))
445         {
446           const Float_t rh = q.fR * 0.5;
447           const Float_t rs = q.fR * sqr3hf;
448           if (flags.SecSelection()) glLoadName(qi.index());
449           glBegin(primitveType);
450           glVertex3f( q.fR + q.fX,       q.fY, q.fZ);
451           glVertex3f(   rh + q.fX,  rs + q.fY, q.fZ);
452           glVertex3f(  -rh + q.fX,  rs + q.fY, q.fZ);
453           glVertex3f(-q.fR + q.fX,       q.fY, q.fZ);
454           glVertex3f(  -rh + q.fX, -rs + q.fY, q.fZ);
455           glVertex3f(   rh + q.fX, -rs + q.fY, q.fZ);
456           glEnd();
457         }
458       }
459       break;
460     }
461
462     case QuadSet::QT_HexagonYX:
463     {
464       while (qi.next()) {
465         QuadSet::QHex& q = * (QuadSet::QHex*) qi();
466         if (SetupColor(q))
467         {
468           const Float_t rh = q.fR * 0.5;
469           const Float_t rs = q.fR * sqr3hf;
470           if (flags.SecSelection()) glLoadName(qi.index());
471           glBegin(primitveType);
472           glVertex3f( rs + q.fX,    rh + q.fY, q.fZ);
473           glVertex3f(      q.fX,  q.fR + q.fY, q.fZ);
474           glVertex3f(-rs + q.fX,    rh + q.fY, q.fZ);
475           glVertex3f(-rs + q.fX,   -rh + q.fY, q.fZ);
476           glVertex3f(      q.fX, -q.fR + q.fY, q.fZ);
477           glVertex3f( rs + q.fX,   -rh + q.fY, q.fZ);
478           glEnd();
479         }
480       }
481       break;
482     }
483
484     default:
485       throw(eH + "unsupported quad-type.");
486
487   } // end switch quad-type
488
489   if (flags.SecSelection()) glPopName();
490 }
491
492 /**************************************************************************/
493 /**************************************************************************/
494
495 //______________________________________________________________________________
496 void QuadSetGL::ProcessSelection(UInt_t* ptr, TGLViewer*, TGLScene*)
497 {
498    // Processes secondary selection from TGLViewer.
499    // Calls TPointSet3D::PointSelected(Int_t) with index of selected
500    // point as an argument.
501
502    if (ptr[0] < 2) return;
503    fM->QuadSelected(ptr[4]);
504 }