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

Last change on this file since 932 was 931, checked in by garnier, 17 years ago

test pour GL_POINTS au lieu de glBitmap

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