1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
19 #include <OpenGl_GlCore11.hxx>
20 #include <OpenGl_tgl_funcs.hxx>
22 #include <Image_AlienPixMap.hxx>
23 #include <Visual3d_Layer.hxx>
25 #include <NCollection_Mat4.hxx>
27 #include <OpenGl_AspectLine.hxx>
28 #include <OpenGl_Context.hxx>
29 #include <OpenGl_Display.hxx>
30 #include <OpenGl_Matrix.hxx>
31 #include <OpenGl_Workspace.hxx>
32 #include <OpenGl_View.hxx>
33 #include <OpenGl_Trihedron.hxx>
34 #include <OpenGl_GraduatedTrihedron.hxx>
35 #include <OpenGl_PrinterContext.hxx>
36 #include <OpenGl_ShaderManager.hxx>
37 #include <OpenGl_ShaderProgram.hxx>
38 #include <OpenGl_Structure.hxx>
45 static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
46 static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f };
47 static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f;
48 static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f;
52 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
54 /*----------------------------------------------------------------------*/
56 struct OPENGL_CLIP_PLANE
63 /*----------------------------------------------------------------------*/
68 /*-----------------------------------------------------------------*/
72 static void bind_light (const OpenGl_Light& theLight,
74 Graphic3d_Vec4& theAmbientColor)
76 // Only 8 lights in OpenGL...
77 if (theLightGlId > GL_LIGHT7)
82 if (theLight.Type == Visual3d_TOLS_AMBIENT)
84 // add RGBA intensity of the ambient light
85 theAmbientColor += theLight.Color;
89 // the light is a headlight?
90 GLint aMatrixModeOld = 0;
91 if (theLight.IsHeadlight)
93 glGetIntegerv (GL_MATRIX_MODE, &aMatrixModeOld);
94 glMatrixMode (GL_MODELVIEW);
100 switch (theLight.Type)
102 case Visual3d_TOLS_DIRECTIONAL:
104 // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one
105 const OpenGl_Vec4 anInfDir = -theLight.Direction;
107 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE.
108 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
109 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
110 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
111 glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData());
112 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
113 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
114 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
117 case Visual3d_TOLS_POSITIONAL:
119 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE
120 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
121 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
122 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
123 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
124 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
125 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
126 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
127 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
128 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
129 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0);
132 case Visual3d_TOLS_SPOT:
134 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
135 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
136 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
137 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
138 glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData());
139 glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f);
140 glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI));
141 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
142 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
143 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f);
148 // restore matrix in case of headlight
149 if (theLight.IsHeadlight)
152 glMatrixMode (aMatrixModeOld);
155 glEnable (theLightGlId++);
158 /*----------------------------------------------------------------------*/
160 void OpenGl_View::DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
162 if ( (theWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
163 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
165 const Standard_Integer aViewWidth = theWorkspace->Width();
166 const Standard_Integer aViewHeight = theWorkspace->Height();
168 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
170 glMatrixMode( GL_PROJECTION );
173 glMatrixMode( GL_MODELVIEW );
177 if ( glIsEnabled( GL_DEPTH_TEST ) )
178 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
180 // drawing bg gradient if:
181 // - gradient fill type is not Aspect_GFM_NONE and
182 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
183 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
184 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
185 myBgTexture.Style == Aspect_FM_NONE ) )
187 Tfloat* corner1 = 0;/* -1,-1*/
188 Tfloat* corner2 = 0;/* 1,-1*/
189 Tfloat* corner3 = 0;/* 1, 1*/
190 Tfloat* corner4 = 0;/* -1, 1*/
194 switch( myBgGradient.type )
197 corner1 = myBgGradient.color1.rgb;
198 corner2 = myBgGradient.color2.rgb;
199 corner3 = myBgGradient.color2.rgb;
200 corner4 = myBgGradient.color1.rgb;
203 corner1 = myBgGradient.color2.rgb;
204 corner2 = myBgGradient.color2.rgb;
205 corner3 = myBgGradient.color1.rgb;
206 corner4 = myBgGradient.color1.rgb;
208 case Aspect_GFM_DIAG1:
209 corner2 = myBgGradient.color2.rgb;
210 corner4 = myBgGradient.color1.rgb;
211 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
212 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
213 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
217 case Aspect_GFM_DIAG2:
218 corner1 = myBgGradient.color2.rgb;
219 corner3 = myBgGradient.color1.rgb;
220 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
221 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
222 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
226 case Aspect_GFM_CORNER1:
227 corner1 = myBgGradient.color2.rgb;
228 corner2 = myBgGradient.color2.rgb;
229 corner3 = myBgGradient.color2.rgb;
230 corner4 = myBgGradient.color1.rgb;
232 case Aspect_GFM_CORNER2:
233 corner1 = myBgGradient.color2.rgb;
234 corner2 = myBgGradient.color2.rgb;
235 corner3 = myBgGradient.color1.rgb;
236 corner4 = myBgGradient.color2.rgb;
238 case Aspect_GFM_CORNER3:
239 corner1 = myBgGradient.color2.rgb;
240 corner2 = myBgGradient.color1.rgb;
241 corner3 = myBgGradient.color2.rgb;
242 corner4 = myBgGradient.color2.rgb;
244 case Aspect_GFM_CORNER4:
245 corner1 = myBgGradient.color1.rgb;
246 corner2 = myBgGradient.color2.rgb;
247 corner3 = myBgGradient.color2.rgb;
248 corner4 = myBgGradient.color2.rgb;
251 //printf("gradient background type not right\n");
255 // Save GL parameters
256 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
259 glGetIntegerv( GL_SHADE_MODEL, &curSM );
260 if ( curSM != GL_SMOOTH )
261 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
263 glBegin(GL_TRIANGLE_FAN);
264 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
266 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
267 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
268 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
269 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
271 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
273 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
274 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
275 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
276 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
280 // Restore GL parameters
281 if ( curSM != GL_SMOOTH )
282 glShadeModel( curSM );
284 // drawing bg image if:
285 // - it is defined and
286 // - fill type is not Aspect_FM_NONE
287 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
289 GLfloat texX_range = 1.F; // texture <s> coordinate
290 GLfloat texY_range = 1.F; // texture <t> coordinate
292 // Set up for stretching or tiling
293 GLfloat x_offset, y_offset;
294 if ( myBgTexture.Style == Aspect_FM_CENTERED )
296 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
297 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
303 if ( myBgTexture.Style == Aspect_FM_TILED )
305 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
306 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
310 // OCCT issue 0023000: Improve the way the gradient and textured
311 // background is managed in 3d viewer (note 0020339)
312 // Setting this coefficient to -1.F allows to tile textures relatively
313 // to the top-left corner of the view (value 1.F corresponds to the
314 // initial behaviour - tiling from the bottom-left corner)
315 GLfloat aCoef = -1.F;
317 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
318 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
320 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
322 glColor3fv (theWorkspace->BackgroundColor().rgb);
323 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
325 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
326 // is simply ignored, and negative multiplier is here for convenience only
327 // and does not result e.g. in texture mirroring
329 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
330 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
331 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
332 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
337 glMatrixMode( GL_PROJECTION );
339 glMatrixMode( GL_MODELVIEW );
341 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
343 if (theWorkspace->UseZBuffer())
344 glEnable (GL_DEPTH_TEST);
346 /* GL_DITHER on/off pour le trace */
347 if (theWorkspace->Dither())
348 glEnable (GL_DITHER);
350 glDisable (GL_DITHER);
354 /*----------------------------------------------------------------------*/
356 //call_func_redraw_all_structs_proc
357 void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
358 const Handle(OpenGl_Workspace) &theWorkspace,
359 const Graphic3d_CView& theCView,
360 const Aspect_CLayer2d& theCUnderLayer,
361 const Aspect_CLayer2d& theCOverLayer)
363 // ==================================
364 // Step 1: Prepare for redraw
365 // ==================================
367 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
369 // Store and disable current clipping planes
370 Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
372 OPENGL_CLIP_PLANE *aOldPlanes = new OPENGL_CLIP_PLANE[aMaxPlanes];
373 OPENGL_CLIP_PLANE *aPtrPlane = aOldPlanes;
375 GLenum aClipPlaneId = GL_CLIP_PLANE0;
376 const GLenum aClipLastId = GL_CLIP_PLANE0 + aMaxPlanes;
377 for (; aClipPlaneId < aClipLastId; aClipPlaneId++, aPtrPlane++)
379 glGetClipPlane (aClipPlaneId, aPtrPlane->Equation);
380 if (aPtrPlane->isEnabled)
382 glDisable (aClipPlaneId);
383 aPtrPlane->isEnabled = GL_TRUE;
387 aPtrPlane->isEnabled = GL_FALSE;
391 // Set OCCT state uniform variables
392 const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
393 if (!aManager->IsEmpty())
395 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
397 aManager->UpdateLightSourceStateTo (&myLights);
398 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
401 if (myProjectionState != myCamera->ProjectionState())
403 myProjectionState = myCamera->ProjectionState();
404 aManager->UpdateProjectionStateTo ((const Tmatrix3*)myCamera->ProjectionMatrix().GetData());
407 if (myModelViewState != myCamera->ModelViewState())
409 myModelViewState = myCamera->ModelViewState();
410 aManager->UpdateWorldViewStateTo ((const Tmatrix3*)myCamera->OrientationMatrix().GetData());
413 if (aManager->ModelWorldState().Index() == 0)
415 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
416 { 0.f, 1.f, 0.f, 0.f },
417 { 0.f, 0.f, 1.f, 0.f },
418 { 0.f, 0.f, 0.f, 1.f } };
420 aContext->ShaderManager()->UpdateModelWorldStateTo (&aModelWorldState);
424 // ====================================
425 // Step 2: Redraw background
426 // ====================================
429 DrawBackground (theWorkspace);
431 // Switch off lighting by default
432 glDisable(GL_LIGHTING);
434 // =================================
435 // Step 3: Draw underlayer
436 // =================================
438 RedrawLayer2d (thePrintContext, theCView, theCUnderLayer);
440 // =================================
441 // Step 4: Redraw main plane
442 // =================================
444 // Setup face culling
445 GLboolean isCullFace = GL_FALSE;
448 isCullFace = glIsEnabled( GL_CULL_FACE );
449 if ( myBackfacing < 0 )
451 glEnable( GL_CULL_FACE );
452 glCullFace( GL_BACK );
455 glDisable( GL_CULL_FACE );
458 // if the view is scaled normal vectors are scaled to unit
459 // length for correct displaying of shaded objects
460 const gp_Pnt anAxialScale = myCamera->AxialScale();
461 if(anAxialScale.X() != 1.F ||
462 anAxialScale.Y() != 1.F ||
463 anAxialScale.Z() != 1.F)
464 glEnable(GL_NORMALIZE);
465 else if(glIsEnabled(GL_NORMALIZE))
466 glDisable(GL_NORMALIZE);
471 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
472 if (myCamera->ZFar() < aFogFrontConverted)
474 aFogFrontConverted = myCamera->ZFar();
475 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
478 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
479 if (myCamera->ZFar() < aFogFrontConverted)
481 aFogBackConverted = myCamera->ZFar();
482 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
485 if (aFogFrontConverted > aFogBackConverted)
487 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
488 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
491 glFogi(GL_FOG_MODE, GL_LINEAR);
492 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
493 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
494 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
500 // Apply InteriorShadingMethod
501 glShadeModel( myIntShadingMethod == TEL_SM_FLAT ? GL_FLAT : GL_SMOOTH );
503 // Apply AntiAliasing
505 theWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
507 theWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
509 if (!aManager->IsEmpty())
511 aManager->UpdateClippingState();
515 if (!myCamera->IsStereo() || !aContext->HasStereoBuffers())
517 // single-pass monographic rendering
518 const OpenGl_Matrix* aProj = (const OpenGl_Matrix*) &myCamera->ProjectionMatrix();
520 const OpenGl_Matrix* aOrient = (const OpenGl_Matrix*) &myCamera->OrientationMatrix();
522 // redraw scene with normal orientation and projection
523 RedrawScene (thePrintContext, theWorkspace, aProj, aOrient);
527 // two stereographic passes
528 const OpenGl_Matrix* aLProj = (const OpenGl_Matrix*) &myCamera->ProjectionStereoLeft();
529 const OpenGl_Matrix* aRProj = (const OpenGl_Matrix*) &myCamera->ProjectionStereoRight();
530 const OpenGl_Matrix* aOrient = (const OpenGl_Matrix*) &myCamera->OrientationMatrix();
532 // safely switch to left Eye buffer
533 aContext->SetDrawBufferLeft();
536 RedrawScene (thePrintContext, theWorkspace, aLProj, aOrient);
538 // reset depth buffer of first rendering pass
539 if (theWorkspace->UseDepthTest())
541 glClear (GL_DEPTH_BUFFER_BIT);
543 // safely switch to right Eye buffer
544 aContext->SetDrawBufferRight();
547 RedrawScene (thePrintContext, theWorkspace, aRProj, aOrient);
549 // switch back to monographic rendering
550 aContext->SetDrawBufferMono();
553 // ===============================
555 // ===============================
557 // Resetting GL parameters according to the default aspects
558 // in order to synchronize GL state with the graphic driver state
559 // before drawing auxiliary stuff (trihedrons, overlayer)
560 // and invoking optional callbacks
561 theWorkspace->ResetAppliedAspect();
563 aContext->ChangeClipping().RemoveAll();
565 if (!aManager->IsEmpty())
567 aManager->ResetMaterialStates();
568 aManager->RevertClippingState();
570 // We need to disable (unbind) all shaders programs to ensure
571 // that all objects without specified aspect will be drawn
572 // correctly (such as background)
573 OpenGl_ShaderProgram::Unbind (aContext);
576 // display global trihedron
577 if (myTrihedron != NULL)
579 myTrihedron->Render (theWorkspace);
581 if (myGraduatedTrihedron != NULL)
583 myGraduatedTrihedron->Render (theWorkspace);
586 // Restore face culling
591 glEnable ( GL_CULL_FACE );
592 glCullFace ( GL_BACK );
595 glDisable ( GL_CULL_FACE );
598 // ===============================
599 // Step 6: Redraw overlay
600 // ===============================
603 theWorkspace->DisplayCallback (theCView, (aMode | OCC_PRE_OVERLAY));
605 RedrawLayer2d (thePrintContext, theCView, theCOverLayer);
607 theWorkspace->DisplayCallback (theCView, aMode);
609 // ===============================
611 // ===============================
613 // Restore clipping planes
614 aClipPlaneId = GL_CLIP_PLANE0;
615 aPtrPlane = aOldPlanes;
617 for (; aClipPlaneId < aClipLastId; aClipPlaneId++, aPtrPlane++)
619 glClipPlane (aClipPlaneId, aPtrPlane->Equation);
620 if (aPtrPlane->isEnabled)
621 glEnable (aClipPlaneId);
623 glDisable (aClipPlaneId);
629 /*----------------------------------------------------------------------*/
632 void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
634 if ( myZLayers.NbStructures() <= 0 )
637 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
639 //TsmPushAttri(); /* save previous graphics context */
641 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
643 const int antiAliasingMode = AWorkspace->GetDisplay()->AntiAliasingMode();
645 if ( !myAntiAliasing )
647 glDisable(GL_POINT_SMOOTH);
648 glDisable(GL_LINE_SMOOTH);
649 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
650 glBlendFunc (GL_ONE, GL_ZERO);
651 glDisable (GL_BLEND);
655 glEnable(GL_POINT_SMOOTH);
656 glEnable(GL_LINE_SMOOTH);
657 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
658 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
663 myZLayers.Render (AWorkspace);
665 //TsmPopAttri(); /* restore previous graphics context; before update lights */
669 /*----------------------------------------------------------------------*/
671 //call_togl_redraw_layer2d
672 void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
673 const Graphic3d_CView& ACView,
674 const Aspect_CLayer2d& ACLayer)
677 || ACLayer.ptrLayer == NULL
678 || ACLayer.ptrLayer->listIndex == 0) return;
680 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
681 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
683 glMatrixMode( GL_MODELVIEW );
687 glMatrixMode (GL_PROJECTION);
691 if (!ACLayer.sizeDependent)
692 glViewport (0, 0, dispWidth, dispHeight);
694 float left = ACLayer.ortho[0];
695 float right = ACLayer.ortho[1];
696 float bottom = ACLayer.ortho[2];
697 float top = ACLayer.ortho[3];
699 int attach = ACLayer.attach;
702 if (!ACLayer.sizeDependent)
703 ratio = (float) dispWidth/dispHeight;
705 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
709 delta = (float )((top - bottom)/2.0);
711 case 0: /* Aspect_TOC_BOTTOM_LEFT */
712 top = bottom + 2*delta/ratio;
714 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
715 top = bottom + 2*delta/ratio;
717 case 2: /* Aspect_TOC_TOP_LEFT */
718 bottom = top - 2*delta/ratio;
720 case 3: /* Aspect_TOC_TOP_RIGHT */
721 bottom = top - 2*delta/ratio;
726 delta = (float )((right - left)/2.0);
728 case 0: /* Aspect_TOC_BOTTOM_LEFT */
729 right = left + 2*delta*ratio;
731 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
732 left = right - 2*delta*ratio;
734 case 2: /* Aspect_TOC_TOP_LEFT */
735 right = left + 2*delta*ratio;
737 case 3: /* Aspect_TOC_TOP_RIGHT */
738 left = right - 2*delta*ratio;
744 // Check printer context that exists only for print operation
745 if (!thePrintContext.IsNull())
747 // additional transformation matrix could be applied to
748 // render only those parts of viewport that will be
749 // passed to a printer as a current "frame" to provide
750 // tiling; scaling of graphics by matrix helps render a
751 // part of a view (frame) in same viewport, but with higher
753 thePrintContext->LoadProjTransformation();
755 // printing operation also assumes other viewport dimension
756 // to comply with transformation matrix or graphics scaling
757 // factors for tiling for layer redraw
758 GLsizei anViewportX = 0;
759 GLsizei anViewportY = 0;
760 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
761 if (anViewportX != 0 && anViewportY != 0)
762 glViewport (0, 0, anViewportX, anViewportY);
766 glOrtho (left, right, bottom, top, -1.0, 1.0);
769 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
770 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
772 glDisable (GL_DEPTH_TEST);
773 glDisable (GL_TEXTURE_1D);
774 glDisable (GL_TEXTURE_2D);
775 glDisable (GL_LIGHTING);
777 // TODO: Obsolete code, the display list is always empty now, to be removed
778 glCallList (ACLayer.ptrLayer->listIndex);
780 //calling dynamic render of LayerItems
781 if ( ACLayer.ptrLayer->layerData )
783 InitLayerProp (ACLayer.ptrLayer->listIndex);
784 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
790 glMatrixMode (GL_PROJECTION);
793 glMatrixMode( GL_MODELVIEW );
796 if (!ACLayer.sizeDependent)
797 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
802 /*----------------------------------------------------------------------*/
804 //call_togl_create_bg_texture
805 void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
806 const Aspect_FillMethod theFillStyle)
808 if (myBgTexture.TexId != 0)
810 // delete existing texture
811 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
812 myBgTexture.TexId = 0;
815 // load image from file
816 Image_AlienPixMap anImageLoaded;
817 if (!anImageLoaded.Load (theFilePath))
822 Image_PixMap anImage;
823 if (anImageLoaded.RowExtraBytes() == 0 &&
824 (anImageLoaded.Format() == Image_PixMap::ImgRGB
825 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
826 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
828 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
829 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
833 // convert image to RGB format
834 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
839 anImage.SetTopDown (false);
840 Image_PixMapData<Image_ColorRGB>& aDataNew = anImage.EditData<Image_ColorRGB>();
841 Quantity_Color aSrcColor;
842 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
844 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
846 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
847 Image_ColorRGB& aColor = aDataNew.ChangeValue (aRow, aCol);
848 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
849 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
850 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
853 anImageLoaded.Clear();
856 // create MipMapped texture
857 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
859 GLuint aTextureId = 0;
860 glGenTextures (1, &aTextureId);
861 glBindTexture (GL_TEXTURE_2D, aTextureId);
863 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
864 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
865 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
866 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
868 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
869 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
870 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
871 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
873 myBgTexture.TexId = aTextureId;
874 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
875 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
876 myBgTexture.Style = theFillStyle;
879 /*----------------------------------------------------------------------*/
881 //call_togl_set_bg_texture_style
882 void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
884 myBgTexture.Style = AFillStyle;
887 /*----------------------------------------------------------------------*/
889 //call_togl_gradient_background
890 void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
891 const Quantity_Color& AColor2,
892 const Aspect_GradientFillMethod AType)
895 AColor1.Values( R, G, B, Quantity_TOC_RGB );
896 myBgGradient.color1.rgb[0] = ( Tfloat )R;
897 myBgGradient.color1.rgb[1] = ( Tfloat )G;
898 myBgGradient.color1.rgb[2] = ( Tfloat )B;
899 myBgGradient.color1.rgb[3] = 0.F;
901 AColor2.Values( R, G, B, Quantity_TOC_RGB );
902 myBgGradient.color2.rgb[0] = ( Tfloat )R;
903 myBgGradient.color2.rgb[1] = ( Tfloat )G;
904 myBgGradient.color2.rgb[2] = ( Tfloat )B;
905 myBgGradient.color2.rgb[3] = 0.F;
907 myBgGradient.type = AType;
910 /*----------------------------------------------------------------------*/
912 //call_togl_set_gradient_type
913 void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
915 myBgGradient.type = AType;
918 //=======================================================================
919 //function : AddZLayer
921 //=======================================================================
923 void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
925 myZLayers.AddLayer (theLayerId);
928 //=======================================================================
929 //function : RemoveZLayer
931 //=======================================================================
933 void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
935 myZLayers.RemoveLayer (theLayerId);
938 //=======================================================================
939 //function : DisplayStructure
941 //=======================================================================
943 void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
944 const Standard_Integer thePriority)
946 Standard_Integer aZLayer = theStructure->GetZLayer ();
947 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
950 //=======================================================================
951 //function : EraseStructure
953 //=======================================================================
955 void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
957 Standard_Integer aZLayer = theStructure->GetZLayer ();
958 myZLayers.RemoveStructure (theStructure, aZLayer);
961 //=======================================================================
962 //function : ChangeZLayer
964 //=======================================================================
966 void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
967 const Standard_Integer theNewLayerId)
969 Standard_Integer anOldLayer = theStructure->GetZLayer ();
970 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);
973 //=======================================================================
974 //function : RedrawScene
976 //=======================================================================
978 void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
979 const Handle(OpenGl_Workspace)& theWorkspace,
980 const OpenGl_Matrix* theProjection,
981 const OpenGl_Matrix* theOrientation)
983 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
985 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
987 Handle(Graphic3d_ClipPlane) aPlaneBack;
988 Handle(Graphic3d_ClipPlane) aPlaneFront;
990 if (myZClip.Back.IsOn)
992 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
993 if (myCamera->ZFar() < aClipBackConverted)
995 aClipBackConverted = myCamera->ZFar();
996 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
998 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
999 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1002 if (myZClip.Front.IsOn)
1004 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1005 if (myCamera->ZNear() > aClipFrontConverted)
1007 aClipFrontConverted = myCamera->ZNear();
1008 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1010 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1011 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1014 // do some "memory allocation"-wise optimization
1015 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1017 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1018 if (!aPlaneBack.IsNull())
1020 aSlicingPlanes.Append (aPlaneBack);
1023 if (!aPlaneFront.IsNull())
1025 aSlicingPlanes.Append (aPlaneFront);
1028 // add planes at loaded view matrix state
1029 aContext->ChangeClipping().AddView (aSlicingPlanes, theWorkspace);
1033 // Apply user clipping planes
1034 if (!myClipPlanes.IsEmpty())
1036 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1037 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1038 for (; aClippingIt.More(); aClippingIt.Next())
1040 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1041 if (aClipPlane->IsOn())
1043 aUserPlanes.Append (aClipPlane);
1047 if (!aUserPlanes.IsEmpty())
1049 // add planes at actual matrix state.
1050 aContext->ChangeClipping().AddWorld (aUserPlanes);
1053 if (!aContext->ShaderManager()->IsEmpty())
1055 aContext->ShaderManager()->UpdateClippingState();
1059 // Setup view projection
1060 glMatrixMode (GL_PROJECTION);
1063 // add printing scale/tiling transformation
1064 if (!thePrintContext.IsNull())
1066 thePrintContext->LoadProjTransformation();
1072 glMultMatrixf ((const GLfloat*)theProjection);
1074 if (!thePrintContext.IsNull())
1076 // update shader uniform projection matrix with new data
1077 Tmatrix3 aResultProjection;
1078 glGetFloatv (GL_PROJECTION_MATRIX, *aResultProjection);
1079 aContext->ShaderManager()->UpdateProjectionStateTo (&aResultProjection);
1081 // force shader uniform restore on next frame
1082 myProjectionState = 0;
1085 // Setup view orientation
1086 theWorkspace->SetViewMatrix (theOrientation);
1091 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1092 THE_DEFAULT_AMBIENT[1],
1093 THE_DEFAULT_AMBIENT[2],
1094 THE_DEFAULT_AMBIENT[3]);
1095 GLenum aLightGlId = GL_LIGHT0;
1096 for (OpenGl_ListOfLight::Iterator aLightIt (myLights);
1097 aLightIt.More(); aLightIt.Next())
1099 bind_light (aLightIt.Value(), aLightGlId, anAmbientColor);
1102 // apply accumulated ambient color
1103 anAmbientColor.a() = 1.0f;
1104 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1106 if (aLightGlId != GL_LIGHT0)
1108 glEnable (GL_LIGHTING);
1110 // switch off unused lights
1111 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1113 glDisable (aLightGlId);
1117 // Clear status bitfields
1118 theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1120 // Added PCT for handling of textures
1121 switch (mySurfaceDetail)
1123 case Visual3d_TOD_NONE:
1124 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1125 theWorkspace->DisableTexture();
1127 RenderStructs (theWorkspace);
1130 case Visual3d_TOD_ENVIRONMENT:
1131 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1132 theWorkspace->EnableTexture (myTextureEnv);
1134 RenderStructs (theWorkspace);
1135 theWorkspace->DisableTexture();
1138 case Visual3d_TOD_ALL:
1140 theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1142 RenderStructs (theWorkspace);
1143 theWorkspace->DisableTexture();
1146 if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1148 theWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1149 theWorkspace->EnableTexture (myTextureEnv);
1151 // Remember OpenGl properties
1152 GLint aSaveBlendDst, aSaveBlendSrc;
1153 GLint aSaveZbuffFunc;
1154 GLboolean aSaveZbuffWrite;
1155 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1156 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
1157 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1158 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
1159 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1160 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1162 // Change the properties for second rendering pass
1163 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1164 glEnable (GL_BLEND);
1166 glDepthFunc (GL_EQUAL);
1167 glDepthMask (GL_FALSE);
1168 glEnable (GL_DEPTH_TEST);
1170 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1173 RenderStructs (theWorkspace);
1174 theWorkspace->DisableTexture();
1176 // Restore properties back
1177 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1178 if (!wasBlendEnabled)
1179 glDisable (GL_BLEND);
1181 glDepthFunc (aSaveZbuffFunc);
1182 glDepthMask (aSaveZbuffWrite);
1183 if (!wasZbuffEnabled)
1184 glDisable (GL_DEPTH_FUNC);