Logo ROOT  
Reference Guide
TGLIsoMesh.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Timur Pocheptsov 28/07/2009
3
4/*************************************************************************
5 * Copyright (C) 1995-2009, 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 "TGLIncludes.h"
13
14#include "TGLPlotPainter.h"
15#include "TGLIsoMesh.h"
16
17namespace Rgl {
18
19//Functions for TGLTF3/TGLIso/TGL5DPainter.
20////////////////////////////////////////////////////////////////////////////////
21///Surface with material and lighting.
22
23template<class V>
24void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<V> &ns,
25 const std::vector<UInt_t> &fTS)
26{
27 glEnableClientState(GL_VERTEX_ARRAY);
28 glEnableClientState(GL_NORMAL_ARRAY);
29 glVertexPointer(3, type, 0, &vs[0]);
30 glNormalPointer(type, 0, &ns[0]);
31 glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
32 glDisableClientState(GL_NORMAL_ARRAY);
33 glDisableClientState(GL_VERTEX_ARRAY);
34}
35
36////////////////////////////////////////////////////////////////////////////////
37///Call function-template.
38
39void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns,
40 const std::vector<UInt_t> &ts)
41{
42 DrawMesh(GL_FLOAT, vs, ns, ts);
43}
44
45////////////////////////////////////////////////////////////////////////////////
46///Call function-template.
47
48void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
49 const std::vector<UInt_t> &ts)
50{
51 DrawMesh(GL_DOUBLE, vs, ns, ts);
52}
53
54////////////////////////////////////////////////////////////////////////////////
55///Only vertices, no normal (no lighting and material).
56
57template<class V>
58void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<UInt_t> &fTS)
59{
60 glEnableClientState(GL_VERTEX_ARRAY);
61 glVertexPointer(3, type, 0, &vs[0]);
62 glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
63 glDisableClientState(GL_VERTEX_ARRAY);
64}
65
66////////////////////////////////////////////////////////////////////////////////
67///Call function-template.
68
69void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts)
70{
71 DrawMesh(GL_FLOAT, vs, ts);
72}
73
74////////////////////////////////////////////////////////////////////////////////
75///Call function-template.
76
77void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts)
78{
79 DrawMesh(GL_DOUBLE, vs, ts);
80}
81
82////////////////////////////////////////////////////////////////////////////////
83///Mesh with cut.
84///Material and lighting are enabled.
85
86template<class V, class GLN, class GLV>
87void DrawMesh(GLN normal3, GLV vertex3, const std::vector<V> &vs,
88 const std::vector<V> &ns, const std::vector<UInt_t> &fTS,
89 const TGLBoxCut &box)
90{
91 glBegin(GL_TRIANGLES);
92
93 for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
94 const UInt_t * t = &fTS[i * 3];
95 if (box.IsInCut(&vs[t[0] * 3]))
96 continue;
97 if (box.IsInCut(&vs[t[1] * 3]))
98 continue;
99 if (box.IsInCut(&vs[t[2] * 3]))
100 continue;
101
102 normal3(&ns[t[0] * 3]);
103 vertex3(&vs[t[0] * 3]);
104
105 normal3(&ns[t[1] * 3]);
106 vertex3(&vs[t[1] * 3]);
107
108 normal3(&ns[t[2] * 3]);
109 vertex3(&vs[t[2] * 3]);
110 }
111
112 glEnd();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116///Call function-template.
117
118void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns,
119 const std::vector<UInt_t> &ts, const TGLBoxCut &box)
120{
121 DrawMesh(&glNormal3fv, &glVertex3fv, vs, ns, ts, box);
122}
123
124////////////////////////////////////////////////////////////////////////////////
125///Call function-template.
126
127void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
128 const std::vector<UInt_t> &ts, const TGLBoxCut &box)
129{
130 DrawMesh(&glNormal3dv, &glVertex3dv, vs, ns, ts, box);
131}
132
133////////////////////////////////////////////////////////////////////////////////
134///Mesh with cut.
135///No material and lighting.
136
137template<class V, class GLV>
138void DrawMesh(GLV vertex3, const std::vector<V> &vs, const std::vector<UInt_t> &fTS,
139 const TGLBoxCut &box)
140{
141 glBegin(GL_TRIANGLES);
142
143 for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
144 const UInt_t * t = &fTS[i * 3];
145 if (box.IsInCut(&vs[t[0] * 3]))
146 continue;
147 if (box.IsInCut(&vs[t[1] * 3]))
148 continue;
149 if (box.IsInCut(&vs[t[2] * 3]))
150 continue;
151
152 vertex3(&vs[t[0] * 3]);
153 vertex3(&vs[t[1] * 3]);
154 vertex3(&vs[t[2] * 3]);
155 }
156
157 glEnd();
158}
159
160////////////////////////////////////////////////////////////////////////////////
161///Call function-template.
162
163void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
164{
165 DrawMesh(&glVertex3fv, vs, ts, box);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169///Call function-template.
170
171void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
172{
173 DrawMesh(&glVertex3dv, vs, ts, box);
174}
175
176////////////////////////////////////////////////////////////////////////////////
177///NormalToColor generates a color from a given normal
178
179void NormalToColor(Double_t *rfColor, const Double_t *n)
180{
181 const Double_t x = n[0];
182 const Double_t y = n[1];
183 const Double_t z = n[2];
184 rfColor[0] = (x > 0. ? x : 0.) + (y < 0. ? -0.5 * y : 0.) + (z < 0. ? -0.5 * z : 0.);
185 rfColor[1] = (y > 0. ? y : 0.) + (z < 0. ? -0.5 * z : 0.) + (x < 0. ? -0.5 * x : 0.);
186 rfColor[2] = (z > 0. ? z : 0.) + (x < 0. ? -0.5 * x : 0.) + (y < 0. ? -0.5 * y : 0.);
187}
188
189////////////////////////////////////////////////////////////////////////////////
190///Colored mesh with lighting disabled.
191
192void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
193 const std::vector<UInt_t> &fTS)
194{
195 Double_t color[] = {0., 0., 0., 0.15};
196
197 glBegin(GL_TRIANGLES);
198
199 for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
200 const UInt_t *t = &fTS[i * 3];
201 const Double_t * n = &ns[t[0] * 3];
202 //
203 NormalToColor(color, n);
204 glColor4dv(color);
205 glVertex3dv(&vs[t[0] * 3]);
206 //
207 n = &ns[t[1] * 3];
208 NormalToColor(color, n);
209 glColor4dv(color);
210 glVertex3dv(&vs[t[1] * 3]);
211 //
212 n = &ns[t[2] * 3];
213 NormalToColor(color, n);
214 glColor4dv(color);
215 glVertex3dv(&vs[t[2] * 3]);
216 }
217
218 glEnd();
219}
220
221////////////////////////////////////////////////////////////////////////////////
222///Colored mesh with cut and disabled lighting.
223
224void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
225 const std::vector<UInt_t> &fTS, const TGLBoxCut & box)
226{
227 Double_t color[] = {0., 0., 0., 0.15};
228
229 glBegin(GL_TRIANGLES);
230
231 for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
232 const UInt_t *t = &fTS[i * 3];
233 if (box.IsInCut(&vs[t[0] * 3]))
234 continue;
235 if (box.IsInCut(&vs[t[1] * 3]))
236 continue;
237 if (box.IsInCut(&vs[t[2] * 3]))
238 continue;
239 const Double_t * n = &ns[t[0] * 3];
240 //
241 NormalToColor(color, n);
242 glColor4dv(color);
243 glVertex3dv(&vs[t[0] * 3]);
244 //
245 n = &ns[t[1] * 3];
246 NormalToColor(color, n);
247 glColor4dv(color);
248 glVertex3dv(&vs[t[1] * 3]);
249 //
250 n = &ns[t[2] * 3];
251 NormalToColor(color, n);
252 glColor4dv(color);
253 glVertex3dv(&vs[t[2] * 3]);
254 }
255
256 glEnd();
257}
258
259}
#define GL_TRIANGLES
Definition: GL_glu.h:287
unsigned int GLenum
Definition: GL_glu.h:266
#define e(i)
Definition: RSha256.hxx:103
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
int type
Definition: TGX11.cxx:121
Used by plot-painters to determine the area of the plot that is cut away.
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
void DrawMapleMesh(const std::vector< Double_t > &vs, const std::vector< Double_t > &ns, const std::vector< UInt_t > &ts)
Colored mesh with lighting disabled.
Definition: TGLIsoMesh.cxx:192
void NormalToColor(Double_t *rfColor, const Double_t *n)
NormalToColor generates a color from a given normal.
Definition: TGLIsoMesh.cxx:179
void DrawMesh(const std::vector< Float_t > &vs, const std::vector< Float_t > &ns, const std::vector< UInt_t > &ts)
Call function-template.
Definition: TGLIsoMesh.cxx:39
static constexpr double ns