source: trunk/source/visualization/OpenGL/src/G4OpenGLSceneHandler.cc@ 1128

Last change on this file since 1128 was 1128, checked in by garnier, 16 years ago

pb quand on ne met pas la vis QT corrige

  • Property svn:mime-type set to text/cpp
File size: 27.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4OpenGLSceneHandler.cc,v 1.55 2009/03/03 14:51:29 lgarnier Exp $
28// GEANT4 tag $Name: $
29//
30//
31// Andrew Walkden 27th March 1996
32// OpenGL stored scene - creates OpenGL display lists.
33// OpenGL immediate scene - draws immediately to buffer
34// (saving space on server).
35
36#ifdef G4VIS_BUILD_OPENGL_DRIVER
37
38// Included here - problems with HP compiler if not before other includes?
39#include "G4NURBS.hh"
40
41// Here follows a special for Mesa, the OpenGL emulator. Does not affect
42// other OpenGL's, as far as I'm aware. John Allison 18/9/96.
43#define CENTERLINE_CLPP /* CenterLine C++ workaround: */
44// Also seems to be required for HP's CC and AIX xlC, at least.
45
46
47#include "G4OpenGLQtViewer.hh"
48#include "G4OpenGLSceneHandler.hh"
49#include "G4OpenGLViewer.hh"
50#include "G4OpenGLFontBaseStore.hh"
51#include "G4OpenGLTransform3D.hh"
52#include "G4Point3D.hh"
53#include "G4Normal3D.hh"
54#include "G4Transform3D.hh"
55#include "G4Polyline.hh"
56#include "G4Polymarker.hh"
57#include "G4Text.hh"
58#include "G4Circle.hh"
59#include "G4Square.hh"
60#include "G4VMarker.hh"
61#include "G4Polyhedron.hh"
62#include "G4VisAttributes.hh"
63#include "G4PhysicalVolumeModel.hh"
64#include "G4VPhysicalVolume.hh"
65#include "G4LogicalVolume.hh"
66#include "G4VSolid.hh"
67#include "G4Scene.hh"
68#include "G4VisExtent.hh"
69#include "G4AttHolder.hh"
70
71G4OpenGLSceneHandler::G4OpenGLSceneHandler (G4VGraphicsSystem& system,
72 G4int id,
73 const G4String& name):
74 G4VSceneHandler (system, id, name),
75 fPickName(0),
76 fProcessing2D (false),
77 fProcessingPolymarker(false)
78{}
79
80G4OpenGLSceneHandler::~G4OpenGLSceneHandler ()
81{
82 ClearStore ();
83}
84
85const GLubyte G4OpenGLSceneHandler::fStippleMaskHashed [128] = {
86 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
87 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
88 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
89 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
90 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
91 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
92 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
93 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
94 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
95 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
96 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
97 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
98 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
99 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
100 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
101 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
102};
103
104void G4OpenGLSceneHandler::ClearAndDestroyAtts()
105{
106 std::map<GLuint, G4AttHolder*>::iterator i;
107 for (i = fPickMap.begin(); i != fPickMap.end(); ++i) delete i->second;
108 fPickMap.clear();
109}
110
111void G4OpenGLSceneHandler::PreAddSolid
112(const G4Transform3D& objectTransformation,
113 const G4VisAttributes& visAttribs)
114{
115 G4VSceneHandler::PreAddSolid (objectTransformation, visAttribs);
116}
117
118void G4OpenGLSceneHandler::BeginPrimitives
119(const G4Transform3D& objectTransformation)
120{
121 G4VSceneHandler::BeginPrimitives (objectTransformation);
122}
123
124void G4OpenGLSceneHandler::EndPrimitives ()
125{
126 G4VSceneHandler::EndPrimitives ();
127}
128
129void G4OpenGLSceneHandler::BeginPrimitives2D
130(const G4Transform3D& objectTransformation)
131{
132 G4VSceneHandler::BeginPrimitives2D (objectTransformation);
133 fProcessing2D = true;
134}
135
136void G4OpenGLSceneHandler::EndPrimitives2D ()
137{
138 fProcessing2D = false;
139 G4VSceneHandler::EndPrimitives2D ();
140}
141
142const G4Polyhedron* G4OpenGLSceneHandler::CreateSectionPolyhedron ()
143{
144 // Clipping done in G4OpenGLViewer::SetView.
145 return 0;
146
147 // But...OpenGL no longer seems to reconstruct clipped edges, so,
148 // when the BooleanProcessor is up to it, abandon this and use
149 // generic clipping in G4VSceneHandler::CreateSectionPolyhedron...
150 // return G4VSceneHandler::CreateSectionPolyhedron();
151}
152
153const G4Polyhedron* G4OpenGLSceneHandler::CreateCutawayPolyhedron ()
154{
155 // Cutaway done in G4OpenGLViewer::SetView.
156 return 0;
157
158 // But...if not, when the BooleanProcessor is up to it...
159 // return G4VSceneHandler::CreateCutawayPolyhedron();
160}
161
162void G4OpenGLSceneHandler::AddPrimitive (const G4Polyline& line)
163{
164 G4int nPoints = line.size ();
165 if (nPoints <= 0) return;
166
167 // Loads G4Atts for picking...
168 if (fpViewer->GetViewParameters().IsPicking()) {
169 G4AttHolder* holder = new G4AttHolder;
170 LoadAtts(line, holder);
171 fPickMap[fPickName] = holder;
172 }
173
174 // Note: colour treated in sub-class.
175
176 if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
177 glDisable (GL_DEPTH_TEST);
178 else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
179
180 glDisable (GL_LIGHTING);
181
182 // Get vis attributes - pick up defaults if none.
183 const G4VisAttributes* pVA =
184 fpViewer -> GetApplicableVisAttributes (line.GetVisAttributes ());
185
186 G4double lineWidth = GetLineWidth(pVA);
187 glLineWidth(lineWidth);
188
189 glBegin (GL_LINE_STRIP);
190 for (G4int iPoint = 0; iPoint < nPoints; iPoint++) {
191 G4double x, y, z;
192 x = line[iPoint].x();
193 y = line[iPoint].y();
194 z = line[iPoint].z();
195 glVertex3d (x, y, z);
196 }
197 glEnd ();
198}
199
200void G4OpenGLSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
201{
202 G4int nPoints = polymarker.size ();
203 if (nPoints <= 0) return;
204
205 fProcessingPolymarker = true;
206
207 // Loads G4Atts for picking...
208 if (fpViewer->GetViewParameters().IsPicking()) {
209 G4AttHolder* holder = new G4AttHolder;
210 LoadAtts(polymarker, holder);
211 fPickMap[fPickName] = holder;
212 }
213
214 switch (polymarker.GetMarkerType()) {
215 default:
216 case G4Polymarker::dots:
217 {
218 for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
219 G4Circle dot (polymarker);
220 dot.SetPosition (polymarker[iPoint]);
221 dot.SetWorldSize (0.);
222 dot.SetScreenSize (0.1); // Very small circle.
223 G4OpenGLSceneHandler::AddPrimitive (dot);
224 }
225 }
226 break;
227 case G4Polymarker::circles:
228 {
229 for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
230 G4Circle circle (polymarker);
231 circle.SetPosition (polymarker[iPoint]);
232 G4OpenGLSceneHandler::AddPrimitive (circle);
233 }
234 }
235 break;
236 case G4Polymarker::squares:
237 {
238 for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
239 G4Square square (polymarker);
240 square.SetPosition (polymarker[iPoint]);
241 G4OpenGLSceneHandler::AddPrimitive (square);
242 }
243 }
244 break;
245 }
246
247 fProcessingPolymarker = false;
248}
249
250void G4OpenGLSceneHandler::AddPrimitive (const G4Text& text) {
251
252 // Loads G4Atts for picking...
253 if (fpViewer->GetViewParameters().IsPicking()) {
254 G4AttHolder* holder = new G4AttHolder;
255 LoadAtts(text, holder);
256 fPickMap[fPickName] = holder;
257 }
258
259 const G4Colour& c = GetTextColour (text); // Picks up default if none.
260 MarkerSizeType sizeType;
261 G4double size = GetMarkerSize (text, sizeType);
262 G4ThreeVector position (text.GetPosition ());
263 G4String textString = text.GetText();
264
265 G4int font_base = G4OpenGLFontBaseStore::GetFontBase(fpViewer,size);
266 if (font_base < 0) {
267
268#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
269 G4OpenGLQtViewer* oGLSQtViewer = dynamic_cast<G4OpenGLQtViewer*>(fpViewer);
270 if (oGLSQtViewer) {
271 // FIXME : No font for the moment
272 const char* textCString = textString.c_str();
273 oGLSQtViewer->drawText(textCString,position.x(),position.y(),position.z(),size);
274 }
275#endif
276 static G4int callCount = 0;
277 ++callCount;
278 if (callCount <= 10 || callCount%100 == 0) {
279 G4cout <<
280 "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
281 << callCount <<
282 "\n No fonts available."
283 "\n Called with text \""
284 << text.GetText ()
285 << "\"\n at " << position
286 << ", size " << size
287 << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
288 << ", type " << G4int(sizeType)
289 << ", colour " << c
290 << G4endl;
291 }
292 return;
293 }
294 const char* textCString = textString.c_str();
295 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
296 G4OpenGLQtViewer* oGLSQtViewer = dynamic_cast<G4OpenGLQtViewer*>(fpViewer);
297
298 if (oGLSQtViewer) {
299 // FIXME : No font for the moment
300 oGLSQtViewer->drawText(textCString,position.x(),position.y(),position.z(),size);
301 } else {
302 glDisable (GL_DEPTH_TEST);
303 glDisable (GL_LIGHTING);
304
305 glRasterPos3d(position.x(),position.y(),position.z());
306 // No action on offset or layout at present.
307 glPushAttrib(GL_LIST_BIT);
308 glListBase(font_base);
309 glCallLists(strlen(textCString), GL_UNSIGNED_BYTE, (GLubyte *)textCString);
310 glPopAttrib();
311 }
312 // //////////////
313 // makeCurrent();
314 // glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT);
315 // glRasterPos3d(x, y, z);
316 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
317 // glEnable(GL_BLEND);
318 // glListBase(fontDisplayListBase(fnt, listBase));
319 // glCallLists(str.length(), GL_UNSIGNED_BYTE, str.local8Bit());
320 // glPopAttrib();
321 // //////////////
322
323#ifdef G4DEBUG_VIS_OGL
324 printf ("G4OpenGLSceneHandler::AddPrimitives TEXT\n");
325#endif
326}
327
328void G4OpenGLSceneHandler::AddPrimitive (const G4Circle& circle) {
329 glEnable (GL_POINT_SMOOTH);
330 AddCircleSquare (circle, G4OpenGLBitMapStore::circle);
331}
332
333void G4OpenGLSceneHandler::AddPrimitive (const G4Square& square) {
334 glDisable (GL_POINT_SMOOTH);
335 AddCircleSquare (square, G4OpenGLBitMapStore::square);
336}
337
338void G4OpenGLSceneHandler::AddCircleSquare
339(const G4VMarker& marker,
340 G4OpenGLBitMapStore::Shape shape) {
341
342 if (!fProcessingPolymarker) { // Polymarker has already loaded atts.
343 // Loads G4Atts for picking...
344 if (fpViewer->GetViewParameters().IsPicking()) {
345 G4AttHolder* holder = new G4AttHolder;
346 LoadAtts(marker, holder);
347 fPickMap[fPickName] = holder;
348 }
349 }
350
351 // Note: colour treated in sub-class.
352
353 if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
354 glDisable (GL_DEPTH_TEST);
355 else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
356
357 glDisable (GL_LIGHTING);
358
359 // Get vis attributes - pick up defaults if none.
360 const G4VisAttributes* pVA =
361 fpViewer -> GetApplicableVisAttributes (marker.GetVisAttributes ());
362
363 G4double lineWidth = GetLineWidth(pVA);
364 glLineWidth(lineWidth);
365
366 G4VMarker::FillStyle style = marker.GetFillStyle();
367
368 G4bool filled = false;
369 static G4bool hashedWarned = false;
370
371 switch (style) {
372 case G4VMarker::noFill:
373 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
374 filled = false;
375 break;
376
377 case G4VMarker::hashed:
378 if (!hashedWarned) {
379 G4cout << "Hashed fill style in G4OpenGLSceneHandler."
380 << "\n Not implemented. Using G4VMarker::filled."
381 << G4endl;
382 hashedWarned = true;
383 }
384 // Maybe use
385 //glPolygonStipple (fStippleMaskHashed);
386 // Drop through to filled...
387
388 case G4VMarker::filled:
389 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
390 filled = true;
391 break;
392
393 }
394
395 // A few useful quantities...
396 G4Point3D centre = marker.GetPosition();
397 MarkerSizeType sizeType;
398 G4double size = GetMarkerSize(marker, sizeType);
399
400 // Draw...
401 if (sizeType == world) { // Size specified in world coordinates.
402
403 DrawXYPolygon (shape, size, centre, pVA);
404
405 } else { // Size specified in screen (window) coordinates.
406 glPointSize (size);
407 glBegin (GL_POINTS);
408 glVertex3f(centre.x(),centre.y(),centre.z());
409 glEnd();
410 //Antialiasing
411 glEnable (GL_POINT_SMOOTH);
412 //Transparency
413 glEnable(GL_BLEND);
414 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
415
416 // L. GARNIER 1 March 2009
417 // Old method, we draw a bitmap instead of a GL_POINT.
418 // I remove it because it cost in term of computing performances
419 // and gl2ps can't draw bitmaps
420
421 // glRasterPos3d(centre.x(),centre.y(),centre.z());
422 // const GLubyte* marker =
423 // G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
424 // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
425 // glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
426 }
427}
428
429void G4OpenGLSceneHandler::DrawXYPolygon
430(G4OpenGLBitMapStore::Shape shape,
431 G4double size,
432 const G4Point3D& centre,
433 const G4VisAttributes* pApplicableVisAtts)
434{
435 G4int nSides;
436 G4double startPhi;
437 if (shape == G4OpenGLBitMapStore::circle) {
438 nSides = GetNoOfSides(pApplicableVisAtts);
439 startPhi = 0.;
440 } else {
441 nSides = 4;
442 startPhi = -pi / 4.;
443 }
444
445 const G4Vector3D& viewpointDirection =
446 fpViewer -> GetViewParameters().GetViewpointDirection();
447 const G4Vector3D& up = fpViewer->GetViewParameters().GetUpVector();
448 const G4double dPhi = twopi / nSides;
449 const G4double radius = size / 2.;
450 G4Vector3D start = radius * (up.cross(viewpointDirection)).unit();
451 G4double phi;
452 G4int i;
453
454 glBegin (GL_POLYGON);
455 for (i = 0, phi = startPhi; i < nSides; i++, phi += dPhi) {
456 G4Vector3D r = start; r.rotate(phi, viewpointDirection);
457 G4Vector3D p = centre + r;
458 glVertex3d (p.x(), p.y(), p.z());
459 }
460 glEnd ();
461}
462
463void G4OpenGLSceneHandler::AddPrimitive (const G4Scale& scale)
464{
465 G4VSceneHandler::AddPrimitive(scale);
466}
467
468//Method for handling G4Polyhedron objects for drawing solids.
469void G4OpenGLSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) {
470
471 // Assume all facets are planar convex quadrilaterals.
472 // Draw each facet individually
473
474 if (polyhedron.GetNoFacets() == 0) return;
475
476 // Loads G4Atts for picking...
477 if (fpViewer->GetViewParameters().IsPicking()) {
478 G4AttHolder* holder = new G4AttHolder;
479 LoadAtts(polyhedron, holder);
480 fPickMap[fPickName] = holder;
481 }
482
483 // Get vis attributes - pick up defaults if none.
484 const G4VisAttributes* pVA =
485 fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
486
487 // Get view parameters that the user can force through the vis
488 // attributes, thereby over-riding the current view parameter.
489 G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
490
491 //Get colour, etc...
492 G4bool transparency_enabled = true;
493 G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
494 if (pViewer) transparency_enabled = pViewer->transparency_enabled;
495 const G4Colour& c = pVA->GetColour();
496 GLfloat materialColour [4];
497 materialColour [0] = c.GetRed ();
498 materialColour [1] = c.GetGreen ();
499 materialColour [2] = c.GetBlue ();
500 if (transparency_enabled) {
501 materialColour [3] = c.GetAlpha ();
502 } else {
503 materialColour [3] = 1.;
504 }
505
506 G4double lineWidth = GetLineWidth(pVA);
507 glLineWidth(lineWidth);
508
509 GLfloat clear_colour[4];
510 glGetFloatv (GL_COLOR_CLEAR_VALUE, clear_colour);
511
512 G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
513
514 G4bool clipping = pViewer->fVP.IsSection() || pViewer->fVP.IsCutaway();
515
516 // Lighting disabled unless otherwise requested
517 glDisable (GL_LIGHTING);
518
519 switch (drawing_style) {
520 case (G4ViewParameters::hlhsr):
521 // Set up as for hidden line removal but paint polygon faces later...
522 case (G4ViewParameters::hlr):
523 glEnable (GL_STENCIL_TEST);
524 // The stencil buffer is cleared in G4OpenGLViewer::ClearView.
525 // The procedure below leaves it clear.
526 glStencilFunc (GL_ALWAYS, 0, 1);
527 glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
528 glEnable (GL_DEPTH_TEST);
529 glDepthFunc (GL_LEQUAL);
530 if (materialColour[3] < 1.) {
531 // Transparent...
532 glDisable (GL_CULL_FACE);
533 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
534 } else {
535 // Opaque...
536 if (clipping) {
537 glDisable (GL_CULL_FACE);
538 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
539 } else {
540 glEnable (GL_CULL_FACE);
541 glCullFace (GL_BACK);
542 glPolygonMode (GL_FRONT, GL_LINE);
543 }
544 }
545 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
546 break;
547 case (G4ViewParameters::hsr):
548 glEnable (GL_DEPTH_TEST);
549 glDepthFunc (GL_LEQUAL);
550 if (materialColour[3] < 1.) {
551 // Transparent...
552 glDepthMask (0); // Make depth buffer read-only.
553 glDisable (GL_CULL_FACE);
554 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
555 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialColour);
556 } else {
557 // Opaque...
558 glDepthMask (1); // Make depth buffer writable (default).
559 if (clipping) {
560 glDisable (GL_CULL_FACE);
561 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
562 } else {
563 glEnable (GL_CULL_FACE);
564 glCullFace (GL_BACK);
565 glPolygonMode (GL_FRONT, GL_FILL);
566 }
567 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
568 }
569 if (!fProcessing2D) glEnable (GL_LIGHTING);
570 break;
571 case (G4ViewParameters::wireframe):
572 default:
573 glEnable (GL_DEPTH_TEST);
574 glDepthFunc (GL_LEQUAL); //??? was GL_ALWAYS
575 glDisable (GL_CULL_FACE);
576 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
577 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
578 break;
579 }
580
581 //Loop through all the facets...
582 glBegin (GL_QUADS);
583 G4bool notLastFace;
584 do {
585
586 //First, find vertices, edgeflags and normals and note "not last facet"...
587 G4Point3D vertex[4];
588 G4int edgeFlag[4];
589 G4Normal3D normals[4];
590 G4int n;
591 notLastFace = polyhedron.GetNextFacet(n, vertex, edgeFlag, normals);
592
593 //Loop through the four edges of each G4Facet...
594 G4int edgeCount = 0;
595 for(edgeCount = 0; edgeCount < n; ++edgeCount) {
596 // Check to see if edge is visible or not...
597 if (isAuxEdgeVisible) {
598 edgeFlag[edgeCount] = 1;
599 }
600 if (edgeFlag[edgeCount] > 0) {
601 glEdgeFlag (GL_TRUE);
602 } else {
603 glEdgeFlag (GL_FALSE);
604 }
605 glNormal3d (normals[edgeCount].x(),
606 normals[edgeCount].y(),
607 normals[edgeCount].z());
608 glVertex3d (vertex[edgeCount].x(),
609 vertex[edgeCount].y(),
610 vertex[edgeCount].z());
611 }
612 // HepPolyhedron produces triangles too; in that case add an extra
613 // vertex identical to first...
614 if (n == 3) {
615 edgeCount = 3;
616 normals[edgeCount] = normals[0];
617 vertex[edgeCount] = vertex[0];
618 edgeFlag[edgeCount] = -1;
619 glEdgeFlag (GL_FALSE);
620 glNormal3d (normals[edgeCount].x(),
621 normals[edgeCount].y(),
622 normals[edgeCount].z());
623 glVertex3d (vertex[edgeCount].x(),
624 vertex[edgeCount].y(),
625 vertex[edgeCount].z());
626 }
627 // Trap situation where number of edges is > 4...
628 if (n > 4) {
629 G4cerr <<
630 "G4OpenGLSceneHandler::AddPrimitive(G4Polyhedron): WARNING";
631 G4PhysicalVolumeModel* pPVModel =
632 dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
633 if (pPVModel) {
634 G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
635 G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
636 G4cerr <<
637 "\n Volume " << pCurrentPV->GetName() <<
638 ", Solid " << pCurrentLV->GetSolid()->GetName() <<
639 " (" << pCurrentLV->GetSolid()->GetEntityType();
640 }
641 G4cerr<<
642 "\n G4Polyhedron facet with " << n << " edges" << G4endl;
643 }
644
645 // Do it all over again (twice) for hlr...
646 if (drawing_style == G4ViewParameters::hlr ||
647 drawing_style == G4ViewParameters::hlhsr) {
648
649 glEnd (); // Placed here to balance glBegin above, allowing GL
650 // state changes below, then glBegin again. Avoids
651 // having glBegin/End pairs *inside* loop in the more
652 // usual case of no hidden line removal.
653
654 // Lighting disabled unless otherwise requested
655 glDisable (GL_LIGHTING);
656
657 // Draw through stencil...
658 glStencilFunc (GL_EQUAL, 0, 1);
659 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
660 if (drawing_style == G4ViewParameters::hlhsr) {
661 if (!fProcessing2D) glEnable (GL_LIGHTING);
662 }
663 glEnable (GL_DEPTH_TEST);
664 glDepthFunc (GL_LEQUAL);
665 if (materialColour[3] < 1.) {
666 // Transparent...
667 glDepthMask (0); // Make depth buffer read-only.
668 glDisable (GL_CULL_FACE);
669 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
670 } else {
671 // Opaque...
672 glDepthMask (1); // Make depth buffer writable (default).
673 if (clipping) {
674 glDisable (GL_CULL_FACE);
675 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
676 } else {
677 glEnable (GL_CULL_FACE);
678 glCullFace (GL_BACK);
679 glPolygonMode (GL_FRONT, GL_FILL);
680 }
681 }
682 GLfloat* painting_colour;
683 if (drawing_style == G4ViewParameters::hlr) {
684 if (materialColour[3] < 1.) {
685 // Transparent - don't paint...
686 goto end_of_drawing_through_stencil;
687 }
688 painting_colour = clear_colour;
689 } else { // drawing_style == G4ViewParameters::hlhsr
690 painting_colour = materialColour;
691 }
692 if (materialColour[3] < 1.) {
693 // Transparent...
694 glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, painting_colour);
695 } else {
696 // Opaque...
697 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, painting_colour);
698 }
699 glColor4fv (painting_colour);
700 glBegin (GL_QUADS);
701 for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
702 if (edgeFlag[edgeCount] > 0) {
703 glEdgeFlag (GL_TRUE);
704 } else {
705 glEdgeFlag (GL_FALSE);
706 }
707 glNormal3d (normals[edgeCount].x(),
708 normals[edgeCount].y(),
709 normals[edgeCount].z());
710 glVertex3d (vertex[edgeCount].x(),
711 vertex[edgeCount].y(),
712 vertex[edgeCount].z());
713 }
714 glEnd ();
715 end_of_drawing_through_stencil:
716
717 // and once more to reset the stencil bits...
718 glStencilFunc (GL_ALWAYS, 0, 1);
719 glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
720 glDepthFunc (GL_LEQUAL); // to make sure line gets drawn.
721 if (materialColour[3] < 1.) {
722 // Transparent...
723 glDisable (GL_CULL_FACE);
724 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
725 } else {
726 // Opaque...
727 if (clipping) {
728 glDisable (GL_CULL_FACE);
729 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
730 } else {
731 glEnable (GL_CULL_FACE);
732 glCullFace (GL_BACK);
733 glPolygonMode (GL_FRONT, GL_LINE);
734 }
735 }
736 glDisable (GL_LIGHTING);
737 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
738 glBegin (GL_QUADS);
739 for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
740 if (edgeFlag[edgeCount] > 0) {
741 glEdgeFlag (GL_TRUE);
742 } else {
743 glEdgeFlag (GL_FALSE);
744 }
745 glNormal3d (normals[edgeCount].x(),
746 normals[edgeCount].y(),
747 normals[edgeCount].z());
748 glVertex3d (vertex[edgeCount].x(),
749 vertex[edgeCount].y(),
750 vertex[edgeCount].z());
751 }
752 glEnd ();
753 glDepthFunc (GL_LEQUAL); // Revert for next facet.
754 glBegin (GL_QUADS); // Ready for next facet. GL
755 // says it ignores incomplete
756 // quadrilaterals, so final empty
757 // glBegin/End sequence should be OK.
758 }
759 } while (notLastFace);
760
761 glEnd ();
762 glDisable (GL_STENCIL_TEST); // Revert to default for next primitive.
763 glDepthMask (1); // Revert to default for next primitive.
764 glDisable (GL_LIGHTING); // Revert to default for next primitive.
765}
766
767//Method for handling G4NURBS objects for drawing solids.
768//Knots and Ctrl Pnts MUST be arrays of GLfloats.
769void G4OpenGLSceneHandler::AddPrimitive (const G4NURBS& nurb) {
770
771 // Loads G4Atts for picking...
772 if (fpViewer->GetViewParameters().IsPicking()) {
773 G4AttHolder* holder = new G4AttHolder;
774 LoadAtts(nurb, holder);
775 fPickMap[fPickName] = holder;
776 }
777
778 GLUnurbsObj *gl_nurb;
779 gl_nurb = gluNewNurbsRenderer ();
780
781 GLfloat *u_knot_array, *u_knot_array_ptr;
782 u_knot_array = u_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::U)];
783 G4NURBS::KnotsIterator u_iterator (nurb, G4NURBS::U);
784 while (u_iterator.pick (u_knot_array_ptr++)){}
785
786 GLfloat *v_knot_array, *v_knot_array_ptr;
787 v_knot_array = v_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::V)];
788 G4NURBS::KnotsIterator v_iterator (nurb, G4NURBS::V);
789 while (v_iterator.pick (v_knot_array_ptr++)){}
790
791 GLfloat *ctrl_pnt_array, *ctrl_pnt_array_ptr;
792 ctrl_pnt_array = ctrl_pnt_array_ptr =
793 new GLfloat [nurb.GettotalnbrCtrlPts () * G4NURBS::NofC];
794 G4NURBS::CtrlPtsCoordsIterator c_p_iterator (nurb);
795 while (c_p_iterator.pick (ctrl_pnt_array_ptr++)){}
796
797 // Get vis attributes - pick up defaults if none.
798 const G4VisAttributes* pVA =
799 fpViewer -> GetApplicableVisAttributes (nurb.GetVisAttributes ());
800
801 // Get view parameters that the user can force through the vis
802 // attributes, thereby over-riding the current view parameter.
803 G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
804 //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
805
806 //Get colour, etc..
807 const G4Colour& c = pVA -> GetColour ();
808
809 switch (drawing_style) {
810
811 case (G4ViewParameters::hlhsr):
812 // G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
813 // << "Using hidden surface removal." << G4endl;
814 case (G4ViewParameters::hsr):
815 {
816 if (!fProcessing2D) glEnable (GL_LIGHTING);
817 glEnable (GL_DEPTH_TEST);
818 glEnable (GL_AUTO_NORMAL);
819 glEnable (GL_NORMALIZE);
820 gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_FILL);
821 gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
822 GLfloat materialColour [4];
823 materialColour [0] = c.GetRed ();
824 materialColour [1] = c.GetGreen ();
825 materialColour [2] = c.GetBlue ();
826 materialColour [3] = 1.0; // = c.GetAlpha () for transparency -
827 // but see complication in
828 // AddPrimitive(const G4Polyhedron&).
829 glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
830 break;
831 }
832 case (G4ViewParameters::hlr):
833 // G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
834 // << "Using wireframe." << G4endl;
835 case (G4ViewParameters::wireframe):
836 default:
837 glDisable (GL_LIGHTING);
838// glDisable (GL_DEPTH_TEST);
839 glEnable (GL_DEPTH_TEST);
840 glDisable (GL_AUTO_NORMAL);
841 glDisable (GL_NORMALIZE);
842 gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
843 gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
844 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
845 break;
846 }
847
848 gluBeginSurface (gl_nurb);
849 G4int u_stride = 4;
850 G4int v_stride = nurb.GetnbrCtrlPts(G4NURBS::U) * 4;
851
852 gluNurbsSurface (gl_nurb,
853 nurb.GetnbrKnots (G4NURBS::U), (GLfloat*)u_knot_array,
854 nurb.GetnbrKnots (G4NURBS::V), (GLfloat*)v_knot_array,
855 u_stride,
856 v_stride,
857 ctrl_pnt_array,
858 nurb.GetUorder (),
859 nurb.GetVorder (),
860 GL_MAP2_VERTEX_4);
861
862 gluEndSurface (gl_nurb);
863
864 delete [] u_knot_array; // These should be allocated with smart allocators
865 delete [] v_knot_array; // to avoid memory explosion.
866 delete [] ctrl_pnt_array;
867
868 gluDeleteNurbsRenderer (gl_nurb);
869}
870
871void G4OpenGLSceneHandler::AddCompound(const G4VTrajectory& traj) {
872 G4VSceneHandler::AddCompound(traj); // For now.
873}
874
875void G4OpenGLSceneHandler::AddCompound(const G4VHit& hit) {
876 G4VSceneHandler::AddCompound(hit); // For now.
877}
878
879#endif
Note: See TracBrowser for help on using the repository browser.