32 #ifdef G4VIS_BUILD_OPENGL_DRIVER
53 G4int G4OpenGLStoredSceneHandler::fSceneIdCount = 0;
55 G4int G4OpenGLStoredSceneHandler::fDisplayListId = 0;
56 G4bool G4OpenGLStoredSceneHandler::fMemoryForDisplayLists =
true;
57 G4int G4OpenGLStoredSceneHandler::fDisplayListLimit = 50000;
59 G4OpenGLStoredSceneHandler::PO::PO():
63 fMarkerOrPolyline(
false)
66 G4OpenGLStoredSceneHandler::PO::PO(
const G4OpenGLStoredSceneHandler::PO& po):
67 fDisplayListId(po.fDisplayListId),
68 fTransform(po.fTransform),
69 fPickName(po.fPickName),
71 fpG4TextPlus(po.fpG4TextPlus? new G4TextPlus(*po.fpG4TextPlus): 0),
72 fMarkerOrPolyline(po.fMarkerOrPolyline)
80 fMarkerOrPolyline(
false)
83 G4OpenGLStoredSceneHandler::PO::~PO()
88 G4OpenGLStoredSceneHandler::PO& G4OpenGLStoredSceneHandler::PO::operator=
89 (
const G4OpenGLStoredSceneHandler::PO& rhs)
91 if (&rhs ==
this)
return *
this;
92 fDisplayListId = rhs.fDisplayListId;
93 fTransform = rhs.fTransform;
94 fPickName = rhs.fPickName;
95 fColour = rhs.fColour;
96 fpG4TextPlus = rhs.fpG4TextPlus?
new G4TextPlus(*rhs.fpG4TextPlus): 0;
97 fMarkerOrPolyline = rhs.fMarkerOrPolyline;
101 G4OpenGLStoredSceneHandler::TO::TO():
107 fMarkerOrPolyline(
false)
110 G4OpenGLStoredSceneHandler::TO::TO(
const G4OpenGLStoredSceneHandler::TO& to):
111 fDisplayListId(to.fDisplayListId),
112 fTransform(to.fTransform),
113 fPickName(to.fPickName),
114 fStartTime(to.fStartTime),
115 fEndTime(to.fEndTime),
117 fpG4TextPlus(to.fpG4TextPlus? new G4TextPlus(*to.fpG4TextPlus): 0),
118 fMarkerOrPolyline(to.fMarkerOrPolyline)
128 fMarkerOrPolyline(
false)
131 G4OpenGLStoredSceneHandler::TO::~TO()
136 G4OpenGLStoredSceneHandler::TO& G4OpenGLStoredSceneHandler::TO::operator=
137 (
const G4OpenGLStoredSceneHandler::TO& rhs)
139 if (&rhs ==
this)
return *
this;
140 fDisplayListId = rhs.fDisplayListId;
141 fTransform = rhs.fTransform;
142 fPickName = rhs.fPickName;
143 fStartTime = rhs.fStartTime;
144 fEndTime = rhs.fEndTime;
145 fColour = rhs.fColour;
146 fpG4TextPlus = rhs.fpG4TextPlus?
new G4TextPlus(*rhs.fpG4TextPlus): 0;
147 fMarkerOrPolyline = rhs.fMarkerOrPolyline;
151 G4OpenGLStoredSceneHandler::G4OpenGLStoredSceneHandler
154 G4OpenGLSceneHandler (system, fSceneIdCount++, name),
158 G4OpenGLStoredSceneHandler::~G4OpenGLStoredSceneHandler ()
161 void G4OpenGLStoredSceneHandler::BeginPrimitives
164 G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
165 if (fReadyForTransients) glDrawBuffer (GL_FRONT);
169 void G4OpenGLStoredSceneHandler::EndPrimitives ()
173 glDrawBuffer (GL_BACK);
174 G4OpenGLSceneHandler::EndPrimitives ();
177 void G4OpenGLStoredSceneHandler::BeginPrimitives2D
180 G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
181 if (fReadyForTransients) glDrawBuffer (GL_FRONT);
184 void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
188 glDrawBuffer (GL_BACK);
189 G4OpenGLSceneHandler::EndPrimitives2D ();
194 return AddPrimitivePreambleInternal(visible,
true,
false);
198 return AddPrimitivePreambleInternal(visible,
false,
true);
202 return AddPrimitivePreambleInternal(visible,
false,
false);
205 G4bool G4OpenGLStoredSceneHandler::AddPrimitivePreambleInternal
209 fpVisAttribs = fpViewer->GetApplicableVisAttributes(visible.
GetVisAttributes());
213 G4bool transparency_enabled =
true;
214 G4bool isMarkerNotHidden =
true;
215 G4OpenGLViewer* pOGLViewer =
dynamic_cast<G4OpenGLViewer*
>(fpViewer);
217 transparency_enabled = pOGLViewer->transparency_enabled;
218 isMarkerNotHidden = pOGLViewer->fVP.IsMarkerNotHidden();
221 G4bool isTransparent = opacity < 1.;
222 G4bool isMarkerOrPolyline = isMarker || isPolyline;
223 G4bool treatAsTransparent = transparency_enabled && isTransparent;
224 G4bool treatAsNotHidden = isMarkerNotHidden && isMarkerOrPolyline;
226 if (fProcessing2D) glDisable (GL_DEPTH_TEST);
228 if (isMarkerOrPolyline && isMarkerNotHidden)
229 glDisable (GL_DEPTH_TEST);
230 else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
233 if (fThreePassCapable) {
238 if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) {
240 if (treatAsTransparent) {
241 fSecondPassForTransparencyRequested =
true;
243 if (treatAsNotHidden) {
244 fThirdPassForNonHiddenMarkersRequested =
true;
247 if (treatAsTransparent || treatAsNotHidden) {
253 if (fSecondPassForTransparency) {
254 if (!treatAsTransparent) {
260 if (fThirdPassForNonHiddenMarkers) {
261 if (!treatAsNotHidden) {
269 if (fpViewer->GetViewParameters().IsPicking()) {
271 glLoadName(++fPickName);
273 LoadAtts(visible, holder);
274 fPickMap[fPickName] = holder;
282 goto end_of_display_list_reuse_test;
283 if (fpViewer->GetViewParameters().GetVisAttributesModifiers().size())
285 goto end_of_display_list_reuse_test;
295 goto end_of_display_list_reuse_test;
304 goto end_of_display_list_reuse_test;
308 goto end_of_display_list_reuse_test;
312 if (pCurrentPV -> IsReplicated ()) {
321 if (!(pCurrentPV -> IsParameterised ()) &&
324 !(pCurrentPV -> IsReplicated () && axis ==
kRho) &&
326 (fSolidMap.find (pSolid) != fSolidMap.end ())) {
327 fDisplayListId = fSolidMap [pSolid];
328 PO po(fDisplayListId,fObjectTransformation);
329 if (isPicking) po.fPickName = fPickName;
331 po.fMarkerOrPolyline = isMarkerOrPolyline;
332 fPOList.push_back(po);
342 end_of_display_list_reuse_test:
348 if (fMemoryForDisplayLists) {
349 fDisplayListId = glGenLists (1);
350 if (glGetError() == GL_OUT_OF_MEMORY ||
351 fDisplayListId > fDisplayListLimit) {
353 "********************* WARNING! ********************"
354 "\n* Display list limit reached in OpenGL."
355 "\n* Continuing drawing WITHOUT STORING. Scene only partially refreshable."
356 "\n* Current limit: " << fDisplayListLimit <<
" primitives"
357 ". Change with \"/vis/ogl/set/displayListLimit\"."
358 "\n***************************************************"
360 fMemoryForDisplayLists =
false;
364 if (pSolid) fSolidMap [pSolid] = fDisplayListId;
366 if (fMemoryForDisplayLists) {
367 if (fReadyForTransients) {
368 TO to(fDisplayListId, fObjectTransformation);
369 if (isPicking) to.fPickName = fPickName;
371 to.fStartTime = fpVisAttribs->GetStartTime();
372 to.fEndTime = fpVisAttribs->GetEndTime();
373 to.fMarkerOrPolyline = isMarkerOrPolyline;
374 fTOList.push_back(to);
381 G4OpenGLTransform3D oglt (fObjectTransformation);
382 glMultMatrixd (oglt.GetGLMatrix ());
383 if (transparency_enabled) {
388 (
void) ExtraTOProcessing(visible, fTOList.size() - 1);
392 glNewList (fDisplayListId, GL_COMPILE_AND_EXECUTE);
394 PO po(fDisplayListId, fObjectTransformation);
395 if (isPicking) po.fPickName = fPickName;
397 po.fMarkerOrPolyline = isMarkerOrPolyline;
398 fPOList.push_back(po);
404 if (transparency_enabled) {
409 G4bool usesGLCommands = ExtraPOProcessing(visible, fPOList.size() - 1);
419 if (!usesGLCommands)
return false;
420 glNewList (fDisplayListId, GL_COMPILE);
424 G4OpenGLTransform3D oglt (fObjectTransformation);
425 glMultMatrixd (oglt.GetGLMatrix ());
426 if (transparency_enabled) {
436 glMatrixMode (GL_PROJECTION);
440 pOGLViewer->g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
442 glMatrixMode (GL_MODELVIEW);
445 G4OpenGLTransform3D oglt (fObjectTransformation);
446 glMultMatrixd (oglt.GetGLMatrix ());
447 glDisable (GL_LIGHTING);
450 glDisable (GL_LIGHTING);
452 glEnable (GL_LIGHTING);
459 void G4OpenGLStoredSceneHandler::AddPrimitivePostamble()
463 glMatrixMode (GL_PROJECTION);
465 glMatrixMode (GL_MODELVIEW);
470 if (glGetError() == GL_OUT_OF_MEMORY) {
472 "ERROR: G4OpenGLStoredSceneHandler::AddPrimitivePostamble: Failure"
473 " to allocate display List for fTopPODL - try OpenGL Immediated mode."
476 if (fMemoryForDisplayLists) {
478 if (glGetError() == GL_OUT_OF_MEMORY) {
480 "ERROR: G4OpenGLStoredSceneHandler::AddPrimitivePostamble: Failure"
481 " to allocate display List for fTopPODL - try OpenGL Immediated mode."
485 if (fReadyForTransients || !fMemoryForDisplayLists) {
490 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Polyline& polyline)
492 G4bool furtherprocessing = AddPrimitivePreamble(polyline);
493 if (furtherprocessing) {
494 G4OpenGLSceneHandler::AddPrimitive(polyline);
495 AddPrimitivePostamble();
499 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Polymarker& polymarker)
501 G4bool furtherprocessing = AddPrimitivePreamble(polymarker);
502 if (furtherprocessing) {
503 G4OpenGLSceneHandler::AddPrimitive(polymarker);
504 AddPrimitivePostamble();
508 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Text& text)
513 G4bool furtherprocessing = AddPrimitivePreamble(text);
514 if (furtherprocessing) {
515 G4OpenGLSceneHandler::AddPrimitive(text);
516 AddPrimitivePostamble();
520 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Circle& circle)
522 G4bool furtherprocessing = AddPrimitivePreamble(circle);
523 if (furtherprocessing) {
524 G4OpenGLSceneHandler::AddPrimitive(circle);
525 AddPrimitivePostamble();
529 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Square&
square)
531 G4bool furtherprocessing = AddPrimitivePreamble(square);
532 if (furtherprocessing) {
533 G4OpenGLSceneHandler::AddPrimitive(square);
534 AddPrimitivePostamble();
538 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Scale&
scale)
541 G4OpenGLSceneHandler::AddPrimitive(scale);
544 void G4OpenGLStoredSceneHandler::AddPrimitive (
const G4Polyhedron& polyhedron)
549 G4bool furtherprocessing = AddPrimitivePreamble(polyhedron);
550 if (furtherprocessing) {
551 G4OpenGLSceneHandler::AddPrimitive(polyhedron);
552 AddPrimitivePostamble();
556 void G4OpenGLStoredSceneHandler::BeginModeling () {
564 void G4OpenGLStoredSceneHandler::EndModeling () {
566 fTopPODL = glGenLists (1);
567 if (glGetError() == GL_OUT_OF_MEMORY) {
569 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
570 " display List for fTopPODL - try OpenGL Immediated mode."
574 glNewList (fTopPODL, GL_COMPILE); {
575 for (
size_t i = 0; i < fPOList.size (); i++) {
577 G4OpenGLTransform3D oglt (fPOList[i].fTransform);
578 glMultMatrixd (oglt.GetGLMatrix ());
579 if (fpViewer->GetViewParameters().IsPicking())
580 glLoadName(fPOList[i].fPickName);
581 glCallList (fPOList[i].fDisplayListId);
587 if (glGetError() == GL_OUT_OF_MEMORY) {
589 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
590 " display List for fTopPODL - try OpenGL Immediated mode."
598 void G4OpenGLStoredSceneHandler::ClearStore () {
605 for (
size_t i = 0; i < fPOList.size (); i++)
606 glDeleteLists (fPOList[i].fDisplayListId, 1);
607 if (fTopPODL) glDeleteLists (fTopPODL, 1);
613 ClearAndDestroyAtts();
616 for (
size_t i = 0; i < fTOList.size (); i++)
617 glDeleteLists(fTOList[i].fDisplayListId, 1);
620 fMemoryForDisplayLists =
true;
623 void G4OpenGLStoredSceneHandler::ClearTransientStore ()
628 for (
size_t i = 0; i < fTOList.size (); i++)
629 glDeleteLists(fTOList[i].fDisplayListId, 1);
632 fMemoryForDisplayLists =
true;
636 fpViewer -> SetView ();
637 fpViewer -> ClearView ();
638 fpViewer -> DrawView ();