forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEveBoxGL.cxx
358 lines (299 loc) · 9.07 KB
/
TEveBoxGL.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// @(#)root/eve:$Id$
// Author: Matevz Tadel, 2010
/*************************************************************************
* Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "TEveBoxGL.h"
#include "TEveBox.h"
#include "TGLRnrCtx.h"
#include "TGLIncludes.h"
#include "TMath.h"
/** \class TEveBoxGL
\ingroup TEve
OpenGL renderer class for TEveBox.
*/
ClassImp(TEveBoxGL);
////////////////////////////////////////////////////////////////////////////////
/// Constructor.
TEveBoxGL::TEveBoxGL() :
TGLObject(), fM(0)
{
// fDLCache = kFALSE; // Disable display list.
}
////////////////////////////////////////////////////////////////////////////////
/// Set model object.
Bool_t TEveBoxGL::SetModel(TObject* obj, const Option_t* /*opt*/)
{
fM = SetModelDynCast<TEveBox>(obj);
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Set bounding box.
void TEveBoxGL::SetBBox()
{
// !! This ok if master sub-classed from TAttBBox
SetAxisAlignedBBox(((TEveBox*)fExternalObj)->AssertBBox());
}
namespace
{
void subtract_and_normalize(const Float_t a[3], const Float_t b[3],
Float_t o[3])
{
// Calculate a - b and normalize the result.
o[0] = a[0] - b[0];
o[1] = a[1] - b[1];
o[2] = a[2] - b[2];
Float_t d = sqrtf(o[0]*o[0] + o[1]*o[1] + o[2]*o[2]);
if (d != 0)
{
d = 1.0f / d;
o[0] *= d;
o[1] *= d;
o[2] *= d;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Render box with without normals.
/// To be used with lightning off, for outline.
void TEveBoxGL::RenderOutline(const Float_t p[8][3]) const
{
glBegin(GL_LINE_STRIP);
glVertex3fv(p[0]); glVertex3fv(p[1]);
glVertex3fv(p[5]); glVertex3fv(p[6]);
glVertex3fv(p[2]); glVertex3fv(p[3]);
glVertex3fv(p[7]); glVertex3fv(p[4]);
glVertex3fv(p[0]); glVertex3fv(p[3]);
glEnd();
glBegin(GL_LINES);
glVertex3fv(p[1]); glVertex3fv(p[2]);
glVertex3fv(p[4]); glVertex3fv(p[5]);
glVertex3fv(p[6]); glVertex3fv(p[7]);
glEnd();
}
////////////////////////////////////////////////////////////////////////////////
/// Render box with standard axis-aligned normals.
void TEveBoxGL::RenderBoxStdNorm(const Float_t p[8][3]) const
{
glBegin(GL_QUADS);
// bottom: 0123
glNormal3f(0, 0, -1);
glVertex3fv(p[0]); glVertex3fv(p[1]);
glVertex3fv(p[2]); glVertex3fv(p[3]);
// top: 7654
glNormal3f(0, 0, 1);
glVertex3fv(p[7]); glVertex3fv(p[6]);
glVertex3fv(p[5]); glVertex3fv(p[4]);
// back: 0451
glNormal3f(0, 1, 0);
glVertex3fv(p[0]); glVertex3fv(p[4]);
glVertex3fv(p[5]); glVertex3fv(p[1]);
// front: 3267
glNormal3f(0, -1, 0);
glVertex3fv(p[3]); glVertex3fv(p[2]);
glVertex3fv(p[6]); glVertex3fv(p[7]);
// left: 0374
glNormal3f(-1, 0, 0);
glVertex3fv(p[0]); glVertex3fv(p[3]);
glVertex3fv(p[7]); glVertex3fv(p[4]);
// right: 1562
glNormal3f(1, 0, 0);
glVertex3fv(p[1]); glVertex3fv(p[5]);
glVertex3fv(p[6]); glVertex3fv(p[2]);
glEnd();
}
////////////////////////////////////////////////////////////////////////////////
/// Render box, calculate normals on the fly from first three points.
void TEveBoxGL::RenderBoxAutoNorm(const Float_t p[8][3]) const
{
Float_t e[6][3], n[3];
subtract_and_normalize(p[1], p[0], e[0]);
subtract_and_normalize(p[3], p[0], e[1]);
subtract_and_normalize(p[4], p[0], e[2]);
subtract_and_normalize(p[5], p[6], e[3]);
subtract_and_normalize(p[7], p[6], e[4]);
subtract_and_normalize(p[2], p[6], e[5]);
glBegin(GL_QUADS);
// bottom: 0123
glNormal3fv(TMath::Cross(e[0], e[1], n));
glVertex3fv(p[0]); glVertex3fv(p[1]);
glVertex3fv(p[2]); glVertex3fv(p[3]);
// top: 7654
glNormal3fv(TMath::Cross(e[3], e[4], n));
glVertex3fv(p[7]); glVertex3fv(p[6]);
glVertex3fv(p[5]); glVertex3fv(p[4]);
// back: 0451
glNormal3fv(TMath::Cross(e[2], e[0], n));
glVertex3fv(p[0]); glVertex3fv(p[4]);
glVertex3fv(p[5]); glVertex3fv(p[1]);
// front: 3267
glNormal3fv(TMath::Cross(e[4], e[5], n));
glVertex3fv(p[3]); glVertex3fv(p[2]);
glVertex3fv(p[6]); glVertex3fv(p[7]);
// left: 0374
glNormal3fv(TMath::Cross(e[1], e[2], n));
glVertex3fv(p[0]); glVertex3fv(p[3]);
glVertex3fv(p[7]); glVertex3fv(p[4]);
// right: 1562
glNormal3fv(TMath::Cross(e[5], e[3], n));
glVertex3fv(p[1]); glVertex3fv(p[5]);
glVertex3fv(p[6]); glVertex3fv(p[2]);
glEnd();
}
////////////////////////////////////////////////////////////////////////////////
/// Render with OpenGL.
void TEveBoxGL::Draw(TGLRnrCtx& rnrCtx) const
{
if (rnrCtx.IsDrawPassOutlineLine())
{
RenderOutline(fM->fVertices);
return;
}
if (fM->fHighlightFrame && rnrCtx.Highlight())
{
if (fM->fDrawFrame)
{
glEnable(GL_BLEND);
TGLUtil::LineWidth(fM->fLineWidth);
TGLUtil::Color(fM->fLineColor);
}
RenderOutline(fM->fVertices);
}
else
{
TGLObject::Draw(rnrCtx);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Render with OpenGL, create display-list.
void TEveBoxGL::DirectDraw(TGLRnrCtx&) const
{
fMultiColor = (fM->fDrawFrame && fM->fFillColor != fM->fLineColor);
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
RenderBoxAutoNorm(fM->fVertices);
glDisable(GL_POLYGON_OFFSET_FILL);
// Frame
if (fM->fDrawFrame)
{
glEnable(GL_BLEND);
TGLUtil::Color(fM->fLineColor);
TGLUtil::LineWidth(fM->fLineWidth);
RenderOutline(fM->fVertices);
}
glPopAttrib();
}
/** \class TEveBoxProjectedGL
\ingroup TEve
OpenGL renderer class for TEveBoxProjected.
*/
ClassImp(TEveBoxProjectedGL);
////////////////////////////////////////////////////////////////////////////////
/// Constructor.
TEveBoxProjectedGL::TEveBoxProjectedGL() :
TGLObject(), fM(0)
{
// fDLCache = kFALSE; // Disable display list.
}
////////////////////////////////////////////////////////////////////////////////
/// Set model object.
Bool_t TEveBoxProjectedGL::SetModel(TObject* obj, const Option_t* /*opt*/)
{
fM = SetModelDynCast<TEveBoxProjected>(obj);
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Set bounding box.
void TEveBoxProjectedGL::SetBBox()
{
SetAxisAlignedBBox(((TEveBoxProjected*)fExternalObj)->AssertBBox());
}
////////////////////////////////////////////////////////////////////////////////
/// Render points with given GL mode.
/// This is used for polygon and outline drawing.
void TEveBoxProjectedGL::RenderPoints(Int_t mode) const
{
Int_t B = fM->fBreakIdx;
Int_t N = fM->fPoints.size();
if (B != 0)
{
glBegin(mode);
for (Int_t i = 0; i < B; ++i)
{
glVertex2fv(fM->fPoints[i]);
}
glEnd();
}
glBegin(mode);
for (Int_t i = B; i < N; ++i)
{
glVertex2fv(fM->fPoints[i]);
}
glEnd();
}
////////////////////////////////////////////////////////////////////////////////
/// Render with OpenGL.
void TEveBoxProjectedGL::Draw(TGLRnrCtx& rnrCtx) const
{
if (rnrCtx.IsDrawPassOutlineLine())
return;
glPushMatrix();
glTranslatef(0.0f, 0.0f, fM->fDepth);
if (fM->fHighlightFrame && rnrCtx.Highlight())
{
if (fM->fDrawFrame)
{
glEnable(GL_BLEND);
TGLUtil::LineWidth(fM->fLineWidth);
TGLUtil::Color(fM->fLineColor);
}
RenderPoints(GL_LINE_LOOP);
}
else
{
TGLObject::Draw(rnrCtx);
}
if (TEveBoxProjected::fgDebugCornerPoints && ! fM->fDebugPoints.empty())
{
glColor3f(1,0,0);
Int_t N = fM->fDebugPoints.size();
glPointSize(4);
glBegin(GL_POINTS);
for (Int_t i = 0; i < N; ++i)
{
glVertex2fv(fM->fDebugPoints[i]);
}
glEnd();
}
glPopMatrix();
}
////////////////////////////////////////////////////////////////////////////////
/// Render with OpenGL, create display-list.
void TEveBoxProjectedGL::DirectDraw(TGLRnrCtx&) const
{
fMultiColor = (fM->fDrawFrame && fM->fFillColor != fM->fLineColor);
glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
glDisable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_CULL_FACE);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
RenderPoints(GL_POLYGON);
glDisable(GL_POLYGON_OFFSET_FILL);
// Frame
if (fM->fDrawFrame)
{
glEnable(GL_BLEND);
TGLUtil::Color(fM->fLineColor);
TGLUtil::LineWidth(fM->fLineWidth);
RenderPoints(GL_LINE_LOOP);
}
glPopAttrib();
}