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

Last change on this file since 1346 was 1346, checked in by garnier, 15 years ago

before tag

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