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 under
8 // the terms of the GNU Lesser General Public License 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_Matrix.hxx>
30 #include <OpenGl_Workspace.hxx>
31 #include <OpenGl_View.hxx>
32 #include <OpenGl_Trihedron.hxx>
33 #include <OpenGl_GraduatedTrihedron.hxx>
34 #include <OpenGl_PrinterContext.hxx>
35 #include <OpenGl_ShaderManager.hxx>
36 #include <OpenGl_ShaderProgram.hxx>
37 #include <OpenGl_Structure.hxx>
44 static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
45 static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f };
46 static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f;
47 static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f;
51 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
53 /*----------------------------------------------------------------------*/
55 struct OPENGL_CLIP_PLANE
62 /*----------------------------------------------------------------------*/
67 #if !defined(GL_ES_VERSION_2_0)
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++);
159 /*----------------------------------------------------------------------*/
161 void OpenGl_View::DrawBackground (OpenGl_Workspace& theWorkspace)
163 #if !defined(GL_ES_VERSION_2_0)
164 if ( (theWorkspace.NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
165 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
167 const Standard_Integer aViewWidth = theWorkspace.Width();
168 const Standard_Integer aViewHeight = theWorkspace.Height();
170 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
172 glMatrixMode( GL_PROJECTION );
175 glMatrixMode( GL_MODELVIEW );
179 if ( glIsEnabled( GL_DEPTH_TEST ) )
180 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
182 // drawing bg gradient if:
183 // - gradient fill type is not Aspect_GFM_NONE and
184 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
185 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
186 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
187 myBgTexture.Style == Aspect_FM_NONE ) )
189 Tfloat* corner1 = 0;/* -1,-1*/
190 Tfloat* corner2 = 0;/* 1,-1*/
191 Tfloat* corner3 = 0;/* 1, 1*/
192 Tfloat* corner4 = 0;/* -1, 1*/
196 switch( myBgGradient.type )
199 corner1 = myBgGradient.color1.rgb;
200 corner2 = myBgGradient.color2.rgb;
201 corner3 = myBgGradient.color2.rgb;
202 corner4 = myBgGradient.color1.rgb;
205 corner1 = myBgGradient.color2.rgb;
206 corner2 = myBgGradient.color2.rgb;
207 corner3 = myBgGradient.color1.rgb;
208 corner4 = myBgGradient.color1.rgb;
210 case Aspect_GFM_DIAG1:
211 corner2 = myBgGradient.color2.rgb;
212 corner4 = myBgGradient.color1.rgb;
213 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
214 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
215 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
219 case Aspect_GFM_DIAG2:
220 corner1 = myBgGradient.color2.rgb;
221 corner3 = myBgGradient.color1.rgb;
222 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
223 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
224 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
228 case Aspect_GFM_CORNER1:
229 corner1 = myBgGradient.color2.rgb;
230 corner2 = myBgGradient.color2.rgb;
231 corner3 = myBgGradient.color2.rgb;
232 corner4 = myBgGradient.color1.rgb;
234 case Aspect_GFM_CORNER2:
235 corner1 = myBgGradient.color2.rgb;
236 corner2 = myBgGradient.color2.rgb;
237 corner3 = myBgGradient.color1.rgb;
238 corner4 = myBgGradient.color2.rgb;
240 case Aspect_GFM_CORNER3:
241 corner1 = myBgGradient.color2.rgb;
242 corner2 = myBgGradient.color1.rgb;
243 corner3 = myBgGradient.color2.rgb;
244 corner4 = myBgGradient.color2.rgb;
246 case Aspect_GFM_CORNER4:
247 corner1 = myBgGradient.color1.rgb;
248 corner2 = myBgGradient.color2.rgb;
249 corner3 = myBgGradient.color2.rgb;
250 corner4 = myBgGradient.color2.rgb;
253 //printf("gradient background type not right\n");
257 // Save GL parameters
258 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
261 glGetIntegerv( GL_SHADE_MODEL, &curSM );
262 if ( curSM != GL_SMOOTH )
263 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
265 glBegin(GL_TRIANGLE_FAN);
266 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
268 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
269 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
270 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
271 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
273 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
275 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
276 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
277 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
278 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
282 // Restore GL parameters
283 if ( curSM != GL_SMOOTH )
284 glShadeModel( curSM );
286 // drawing bg image if:
287 // - it is defined and
288 // - fill type is not Aspect_FM_NONE
289 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
291 GLfloat texX_range = 1.F; // texture <s> coordinate
292 GLfloat texY_range = 1.F; // texture <t> coordinate
294 // Set up for stretching or tiling
295 GLfloat x_offset, y_offset;
296 if ( myBgTexture.Style == Aspect_FM_CENTERED )
298 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
299 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
305 if ( myBgTexture.Style == Aspect_FM_TILED )
307 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
308 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
312 // OCCT issue 0023000: Improve the way the gradient and textured
313 // background is managed in 3d viewer (note 0020339)
314 // Setting this coefficient to -1.F allows to tile textures relatively
315 // to the top-left corner of the view (value 1.F corresponds to the
316 // initial behaviour - tiling from the bottom-left corner)
317 GLfloat aCoef = -1.F;
319 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
320 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
322 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
324 glColor3fv (theWorkspace.BackgroundColor().rgb);
325 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
327 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
328 // is simply ignored, and negative multiplier is here for convenience only
329 // and does not result e.g. in texture mirroring
331 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
332 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
333 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
334 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
339 glMatrixMode( GL_PROJECTION );
341 glMatrixMode( GL_MODELVIEW );
343 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
345 if (theWorkspace.UseZBuffer())
347 glEnable (GL_DEPTH_TEST);
353 /*----------------------------------------------------------------------*/
355 //call_func_redraw_all_structs_proc
356 void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
357 const Handle(OpenGl_Workspace) &theWorkspace,
358 const Graphic3d_CView& theCView,
359 const Aspect_CLayer2d& theCUnderLayer,
360 const Aspect_CLayer2d& theCOverLayer)
362 // ==================================
363 // Step 1: Prepare for redraw
364 // ==================================
366 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
368 #if !defined(GL_ES_VERSION_2_0)
369 // store and disable current clipping planes
370 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
371 NCollection_Array1<OPENGL_CLIP_PLANE> aOldPlanes (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + aMaxPlanes - 1);
372 if (aContext->core11 != NULL)
374 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
376 OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
377 aContext->core11->glGetClipPlane (aClipPlaneId, aPlane.Equation);
378 if (aPlane.isEnabled)
380 aContext->core11fwd->glDisable (aClipPlaneId);
381 aPlane.isEnabled = GL_TRUE;
385 aPlane.isEnabled = GL_FALSE;
391 Standard_Boolean isProjectionMatUpdateNeeded = Standard_False;
392 Standard_Boolean isOrientationMatUpdateNeeded = Standard_False;
393 if (myBVHSelector.ProjectionState() != myCamera->ProjectionState())
395 isProjectionMatUpdateNeeded = Standard_True;
396 myBVHSelector.ChangeProjectionState() = myCamera->ProjectionState();
398 if (myBVHSelector.ModelViewState() != myCamera->ModelViewState())
400 isOrientationMatUpdateNeeded = Standard_True;
401 myBVHSelector.ChangeModelViewState() = myCamera->ModelViewState();
404 // Set OCCT state uniform variables
405 const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
406 const Standard_Boolean isSameView = aManager->IsSameView (this); // force camera state update when needed
407 if (!aManager->IsEmpty())
409 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
411 aManager->UpdateLightSourceStateTo (&myLights);
412 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
415 if (myProjectionState != myCamera->ProjectionState()
418 myProjectionState = myCamera->ProjectionState();
419 aManager->UpdateProjectionStateTo ((const Tmatrix3*)myCamera->ProjectionMatrixF().GetData());
422 if (myModelViewState != myCamera->ModelViewState()
425 myModelViewState = myCamera->ModelViewState();
426 aManager->UpdateWorldViewStateTo ((const Tmatrix3*)myCamera->OrientationMatrixF().GetData());
429 if (aManager->ModelWorldState().Index() == 0)
431 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
432 { 0.f, 1.f, 0.f, 0.f },
433 { 0.f, 0.f, 1.f, 0.f },
434 { 0.f, 0.f, 0.f, 1.f } };
436 aContext->ShaderManager()->UpdateModelWorldStateTo (&aModelWorldState);
440 if (isProjectionMatUpdateNeeded
441 || isOrientationMatUpdateNeeded)
443 myBVHSelector.SetViewVolume (myCamera);
446 // ====================================
447 // Step 2: Redraw background
448 // ====================================
451 if (theWorkspace->ToRedrawGL())
453 DrawBackground (*theWorkspace);
456 #if !defined(GL_ES_VERSION_2_0)
457 // Switch off lighting by default
458 glDisable(GL_LIGHTING);
461 // =================================
462 // Step 3: Draw underlayer
463 // =================================
465 RedrawLayer2d (thePrintContext, theCView, theCUnderLayer);
467 // =================================
468 // Step 4: Redraw main plane
469 // =================================
471 // Setup face culling
472 GLboolean isCullFace = GL_FALSE;
475 isCullFace = glIsEnabled( GL_CULL_FACE );
476 if ( myBackfacing < 0 )
478 glEnable( GL_CULL_FACE );
479 glCullFace( GL_BACK );
482 glDisable( GL_CULL_FACE );
485 #if !defined(GL_ES_VERSION_2_0)
486 // if the view is scaled normal vectors are scaled to unit
487 // length for correct displaying of shaded objects
488 const gp_Pnt anAxialScale = myCamera->AxialScale();
489 if(anAxialScale.X() != 1.F ||
490 anAxialScale.Y() != 1.F ||
491 anAxialScale.Z() != 1.F)
492 glEnable(GL_NORMALIZE);
493 else if(glIsEnabled(GL_NORMALIZE))
494 glDisable(GL_NORMALIZE);
499 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
500 if (myCamera->ZFar() < aFogFrontConverted)
502 aFogFrontConverted = myCamera->ZFar();
503 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
506 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
507 if (myCamera->ZFar() < aFogFrontConverted)
509 aFogBackConverted = myCamera->ZFar();
510 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
513 if (aFogFrontConverted > aFogBackConverted)
515 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
516 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
519 glFogi(GL_FOG_MODE, GL_LINEAR);
520 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
521 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
522 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
528 // Apply InteriorShadingMethod
529 aContext->core11->glShadeModel (myShadingModel == Visual3d_TOM_FACET
530 || myShadingModel == Visual3d_TOM_NONE ? GL_FLAT : GL_SMOOTH);
533 aManager->SetShadingModel (myShadingModel);
535 // Apply AntiAliasing
537 theWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
539 theWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
541 if (!aManager->IsEmpty())
543 aManager->UpdateClippingState();
547 if (!myCamera->IsStereo() || !aContext->HasStereoBuffers())
549 // single-pass monographic rendering
550 const OpenGl_Matrix* aProj = (const OpenGl_Matrix*) &myCamera->ProjectionMatrixF();
552 const OpenGl_Matrix* aOrient = (const OpenGl_Matrix*) &myCamera->OrientationMatrixF();
554 // redraw scene with normal orientation and projection
555 RedrawScene (thePrintContext, theWorkspace, aProj, aOrient);
559 // two stereographic passes
560 const OpenGl_Matrix* aLProj = (const OpenGl_Matrix*) &myCamera->ProjectionStereoLeftF();
561 const OpenGl_Matrix* aRProj = (const OpenGl_Matrix*) &myCamera->ProjectionStereoRightF();
562 const OpenGl_Matrix* aOrient = (const OpenGl_Matrix*) &myCamera->OrientationMatrixF();
564 // safely switch to left Eye buffer
565 aContext->SetDrawBufferLeft();
568 RedrawScene (thePrintContext, theWorkspace, aLProj, aOrient);
570 // reset depth buffer of first rendering pass
571 if (theWorkspace->UseDepthTest())
573 glClear (GL_DEPTH_BUFFER_BIT);
575 // safely switch to right Eye buffer
576 aContext->SetDrawBufferRight();
579 RedrawScene (thePrintContext, theWorkspace, aRProj, aOrient);
581 // switch back to monographic rendering
582 aContext->SetDrawBufferMono();
585 // ===============================
587 // ===============================
589 // Resetting GL parameters according to the default aspects
590 // in order to synchronize GL state with the graphic driver state
591 // before drawing auxiliary stuff (trihedrons, overlayer)
592 // and invoking optional callbacks
593 theWorkspace->ResetAppliedAspect();
595 aContext->ChangeClipping().RemoveAll();
597 if (!aManager->IsEmpty())
599 aManager->ResetMaterialStates();
600 aManager->RevertClippingState();
602 // We need to disable (unbind) all shaders programs to ensure
603 // that all objects without specified aspect will be drawn
604 // correctly (such as background)
605 aContext->BindProgram (NULL);
609 if (theWorkspace->ToRedrawGL())
611 RedrawTrihedron (theWorkspace);
613 // Restore face culling
618 glEnable ( GL_CULL_FACE );
619 glCullFace ( GL_BACK );
622 glDisable ( GL_CULL_FACE );
626 // ===============================
627 // Step 6: Redraw overlay
628 // ===============================
631 theWorkspace->DisplayCallback (theCView, (aMode | OCC_PRE_OVERLAY));
633 RedrawLayer2d (thePrintContext, theCView, theCOverLayer);
635 theWorkspace->DisplayCallback (theCView, aMode);
637 // ===============================
639 // ===============================
641 #if !defined(GL_ES_VERSION_2_0)
642 // restore clipping planes
643 if (aContext->core11 != NULL)
645 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
647 const OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
648 aContext->core11->glClipPlane (aClipPlaneId, aPlane.Equation);
649 if (aPlane.isEnabled)
650 aContext->core11fwd->glEnable (aClipPlaneId);
652 aContext->core11fwd->glDisable (aClipPlaneId);
657 // ==============================================================
658 // Step 8: Keep shader manager informed about last View
659 // ==============================================================
661 if (!aManager.IsNull())
663 aManager->SetLastView (this);
667 // =======================================================================
668 // function : InvalidateBVHData
670 // =======================================================================
671 void OpenGl_View::InvalidateBVHData (const Standard_Integer theLayerId)
673 myZLayers.InvalidateBVHData (theLayerId);
676 /*----------------------------------------------------------------------*/
679 void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
681 if ( myZLayers.NbStructures() <= 0 )
684 #if !defined(GL_ES_VERSION_2_0)
685 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
688 //TsmPushAttri(); /* save previous graphics context */
690 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
692 #if !defined(GL_ES_VERSION_2_0)
693 const int antiAliasingMode = AWorkspace->AntiAliasingMode();
696 if ( !myAntiAliasing )
698 #if !defined(GL_ES_VERSION_2_0)
699 glDisable(GL_POINT_SMOOTH);
700 glDisable(GL_LINE_SMOOTH);
701 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
703 glBlendFunc (GL_ONE, GL_ZERO);
704 glDisable (GL_BLEND);
708 #if !defined(GL_ES_VERSION_2_0)
709 glEnable(GL_POINT_SMOOTH);
710 glEnable(GL_LINE_SMOOTH);
711 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
713 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
718 myZLayers.Render (AWorkspace);
720 #if !defined(GL_ES_VERSION_2_0)
721 //TsmPopAttri(); /* restore previous graphics context; before update lights */
726 /*----------------------------------------------------------------------*/
728 //call_togl_redraw_layer2d
729 void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
730 const Graphic3d_CView& ACView,
731 const Aspect_CLayer2d& ACLayer)
733 #if !defined(GL_ES_VERSION_2_0)
735 || ACLayer.ptrLayer == NULL
736 || ACLayer.ptrLayer->listIndex == 0) return;
738 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
739 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
741 glMatrixMode( GL_MODELVIEW );
745 glMatrixMode (GL_PROJECTION);
749 if (!ACLayer.sizeDependent)
750 glViewport (0, 0, dispWidth, dispHeight);
752 float left = ACLayer.ortho[0];
753 float right = ACLayer.ortho[1];
754 float bottom = ACLayer.ortho[2];
755 float top = ACLayer.ortho[3];
757 int attach = ACLayer.attach;
760 if (!ACLayer.sizeDependent)
761 ratio = (float) dispWidth/dispHeight;
763 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
767 delta = (float )((top - bottom)/2.0);
769 case 0: /* Aspect_TOC_BOTTOM_LEFT */
770 top = bottom + 2*delta/ratio;
772 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
773 top = bottom + 2*delta/ratio;
775 case 2: /* Aspect_TOC_TOP_LEFT */
776 bottom = top - 2*delta/ratio;
778 case 3: /* Aspect_TOC_TOP_RIGHT */
779 bottom = top - 2*delta/ratio;
784 delta = (float )((right - left)/2.0);
786 case 0: /* Aspect_TOC_BOTTOM_LEFT */
787 right = left + 2*delta*ratio;
789 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
790 left = right - 2*delta*ratio;
792 case 2: /* Aspect_TOC_TOP_LEFT */
793 right = left + 2*delta*ratio;
795 case 3: /* Aspect_TOC_TOP_RIGHT */
796 left = right - 2*delta*ratio;
802 // Check printer context that exists only for print operation
803 if (!thePrintContext.IsNull())
805 // additional transformation matrix could be applied to
806 // render only those parts of viewport that will be
807 // passed to a printer as a current "frame" to provide
808 // tiling; scaling of graphics by matrix helps render a
809 // part of a view (frame) in same viewport, but with higher
811 thePrintContext->LoadProjTransformation();
813 // printing operation also assumes other viewport dimension
814 // to comply with transformation matrix or graphics scaling
815 // factors for tiling for layer redraw
816 GLsizei anViewportX = 0;
817 GLsizei anViewportY = 0;
818 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
819 if (anViewportX != 0 && anViewportY != 0)
820 glViewport (0, 0, anViewportX, anViewportY);
824 glOrtho (left, right, bottom, top, -1.0, 1.0);
827 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
828 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
830 glDisable (GL_DEPTH_TEST);
831 glDisable (GL_TEXTURE_1D);
832 glDisable (GL_TEXTURE_2D);
833 glDisable (GL_LIGHTING);
835 // TODO: Obsolete code, the display list is always empty now, to be removed
836 glCallList (ACLayer.ptrLayer->listIndex);
838 //calling dynamic render of LayerItems
839 if ( ACLayer.ptrLayer->layerData )
841 InitLayerProp (ACLayer.ptrLayer->listIndex);
842 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
848 glMatrixMode (GL_PROJECTION);
851 glMatrixMode( GL_MODELVIEW );
854 if (!ACLayer.sizeDependent)
855 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
861 /*----------------------------------------------------------------------*/
863 void OpenGl_View::RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
865 // display global trihedron
866 if (myTrihedron != NULL)
868 myTrihedron->Render (theWorkspace);
870 if (myGraduatedTrihedron != NULL)
872 myGraduatedTrihedron->Render (theWorkspace);
876 /*----------------------------------------------------------------------*/
878 //call_togl_create_bg_texture
879 void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
880 const Aspect_FillMethod theFillStyle)
882 if (myBgTexture.TexId != 0)
884 // delete existing texture
885 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
886 myBgTexture.TexId = 0;
889 // load image from file
890 Image_AlienPixMap anImageLoaded;
891 if (!anImageLoaded.Load (theFilePath))
896 Image_PixMap anImage;
897 if (anImageLoaded.RowExtraBytes() == 0 &&
898 (anImageLoaded.Format() == Image_PixMap::ImgRGB
899 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
900 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
902 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
903 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
907 // convert image to RGB format
908 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
913 anImage.SetTopDown (false);
914 Quantity_Color aSrcColor;
915 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
917 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
919 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
920 Image_ColorRGB& aColor = anImage.ChangeValue<Image_ColorRGB> (aRow, aCol);
921 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
922 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
923 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
926 anImageLoaded.Clear();
929 // create MipMapped texture
930 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
932 GLuint aTextureId = 0;
933 glGenTextures (1, &aTextureId);
934 glBindTexture (GL_TEXTURE_2D, aTextureId);
936 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
937 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
938 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
939 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
941 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
943 #if !defined(GL_ES_VERSION_2_0)
944 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
945 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
946 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
949 myBgTexture.TexId = aTextureId;
950 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
951 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
952 myBgTexture.Style = theFillStyle;
955 /*----------------------------------------------------------------------*/
957 //call_togl_set_bg_texture_style
958 void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
960 myBgTexture.Style = AFillStyle;
963 /*----------------------------------------------------------------------*/
965 //call_togl_gradient_background
966 void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
967 const Quantity_Color& AColor2,
968 const Aspect_GradientFillMethod AType)
971 AColor1.Values( R, G, B, Quantity_TOC_RGB );
972 myBgGradient.color1.rgb[0] = ( Tfloat )R;
973 myBgGradient.color1.rgb[1] = ( Tfloat )G;
974 myBgGradient.color1.rgb[2] = ( Tfloat )B;
975 myBgGradient.color1.rgb[3] = 0.F;
977 AColor2.Values( R, G, B, Quantity_TOC_RGB );
978 myBgGradient.color2.rgb[0] = ( Tfloat )R;
979 myBgGradient.color2.rgb[1] = ( Tfloat )G;
980 myBgGradient.color2.rgb[2] = ( Tfloat )B;
981 myBgGradient.color2.rgb[3] = 0.F;
983 myBgGradient.type = AType;
986 /*----------------------------------------------------------------------*/
988 //call_togl_set_gradient_type
989 void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
991 myBgGradient.type = AType;
994 //=======================================================================
995 //function : AddZLayer
997 //=======================================================================
999 void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
1001 myZLayers.AddLayer (theLayerId);
1004 //=======================================================================
1005 //function : RemoveZLayer
1007 //=======================================================================
1009 void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
1011 myZLayers.RemoveLayer (theLayerId);
1014 //=======================================================================
1015 //function : DisplayStructure
1017 //=======================================================================
1019 void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
1020 const Standard_Integer thePriority)
1022 Standard_Integer aZLayer = theStructure->GetZLayer ();
1023 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
1026 //=======================================================================
1027 //function : DisplayImmediateStructure
1029 //=======================================================================
1031 void OpenGl_View::DisplayImmediateStructure (const OpenGl_Structure* theStructure)
1033 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1034 anIter.More(); anIter.Next())
1036 if (anIter.Value() == theStructure)
1042 myImmediateList.Append (theStructure);
1045 //=======================================================================
1046 //function : EraseStructure
1048 //=======================================================================
1050 void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
1052 Standard_Integer aZLayer = theStructure->GetZLayer ();
1053 myZLayers.RemoveStructure (theStructure, aZLayer);
1056 //=======================================================================
1057 //function : EraseImmediateStructure
1059 //=======================================================================
1061 void OpenGl_View::EraseImmediateStructure (const OpenGl_Structure* theStructure)
1063 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1064 anIter.More(); anIter.Next())
1066 if (anIter.Value() == theStructure)
1068 myImmediateList.Remove (anIter);
1074 //=======================================================================
1075 //function : ChangeZLayer
1077 //=======================================================================
1079 void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
1080 const Standard_Integer theNewLayerId)
1082 Standard_Integer anOldLayer = theStructure->GetZLayer ();
1083 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);
1086 //=======================================================================
1087 //function : SetZLayerSettings
1089 //=======================================================================
1090 void OpenGl_View::SetZLayerSettings (const Standard_Integer theLayerId,
1091 const Graphic3d_ZLayerSettings theSettings)
1093 myZLayers.Layer (theLayerId).SetLayerSettings (theSettings);
1096 //=======================================================================
1097 //function : ChangePriority
1099 //=======================================================================
1100 void OpenGl_View::ChangePriority (const OpenGl_Structure *theStructure,
1101 const Standard_Integer theNewPriority)
1103 Standard_Integer aLayerId = theStructure->GetZLayer();
1104 myZLayers.ChangePriority (theStructure, aLayerId, theNewPriority);
1107 //=======================================================================
1108 //function : RedrawScene
1110 //=======================================================================
1112 void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
1113 const Handle(OpenGl_Workspace)& theWorkspace,
1114 const OpenGl_Matrix* theProjection,
1115 const OpenGl_Matrix* theOrientation)
1117 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
1119 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1121 Handle(Graphic3d_ClipPlane) aPlaneBack;
1122 Handle(Graphic3d_ClipPlane) aPlaneFront;
1124 if (myZClip.Back.IsOn)
1126 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1127 if (myCamera->ZFar() < aClipBackConverted)
1129 aClipBackConverted = myCamera->ZFar();
1130 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
1132 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
1133 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1136 if (myZClip.Front.IsOn)
1138 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1139 if (myCamera->ZNear() > aClipFrontConverted)
1141 aClipFrontConverted = myCamera->ZNear();
1142 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1144 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1145 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1148 // Specify slicing planes with identity transformation
1149 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1151 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1152 if (!aPlaneBack.IsNull())
1154 aSlicingPlanes.Append (aPlaneBack);
1157 if (!aPlaneFront.IsNull())
1159 aSlicingPlanes.Append (aPlaneFront);
1162 // add planes at loaded view matrix state
1163 aContext->ChangeClipping().AddView (aSlicingPlanes, theWorkspace);
1167 #if !defined(GL_ES_VERSION_2_0)
1168 // Setup view projection
1169 glMatrixMode (GL_PROJECTION);
1172 // add printing scale/tiling transformation
1173 if (!thePrintContext.IsNull())
1175 thePrintContext->LoadProjTransformation();
1181 glMultMatrixf ((const GLfloat*)theProjection);
1183 if (!thePrintContext.IsNull())
1185 // update shader uniform projection matrix with new data
1186 Tmatrix3 aResultProjection;
1187 glGetFloatv (GL_PROJECTION_MATRIX, *aResultProjection);
1188 aContext->ShaderManager()->UpdateProjectionStateTo (&aResultProjection);
1190 // force shader uniform restore on next frame
1191 myProjectionState = 0;
1195 // Setup view orientation
1196 theWorkspace->SetViewMatrix (theOrientation);
1198 // Specify clipping planes in view transformation space
1199 if (!myClipPlanes.IsEmpty())
1201 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1202 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1203 for (; aClippingIt.More(); aClippingIt.Next())
1205 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1206 if (aClipPlane->IsOn())
1208 aUserPlanes.Append (aClipPlane);
1212 if (!aUserPlanes.IsEmpty())
1214 aContext->ChangeClipping().AddWorld (aUserPlanes);
1217 if (!aContext->ShaderManager()->IsEmpty())
1219 aContext->ShaderManager()->UpdateClippingState();
1223 #if !defined(GL_ES_VERSION_2_0)
1227 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1228 THE_DEFAULT_AMBIENT[1],
1229 THE_DEFAULT_AMBIENT[2],
1230 THE_DEFAULT_AMBIENT[3]);
1231 GLenum aLightGlId = GL_LIGHT0;
1232 for (OpenGl_ListOfLight::Iterator aLightIt (myLights);
1233 aLightIt.More(); aLightIt.Next())
1235 bind_light (aLightIt.Value(), aLightGlId, anAmbientColor);
1238 // apply accumulated ambient color
1239 anAmbientColor.a() = 1.0f;
1240 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1242 if (aLightGlId != GL_LIGHT0)
1244 glEnable (GL_LIGHTING);
1246 // switch off unused lights
1247 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1249 glDisable (aLightGlId);
1254 // Clear status bitfields
1255 theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1257 // Added PCT for handling of textures
1258 switch (mySurfaceDetail)
1260 case Visual3d_TOD_NONE:
1261 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1262 theWorkspace->DisableTexture();
1264 RenderStructs (theWorkspace);
1267 case Visual3d_TOD_ENVIRONMENT:
1268 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1269 theWorkspace->EnableTexture (myTextureEnv);
1271 RenderStructs (theWorkspace);
1272 theWorkspace->DisableTexture();
1275 case Visual3d_TOD_ALL:
1277 theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1279 RenderStructs (theWorkspace);
1280 theWorkspace->DisableTexture();
1283 if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1285 theWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1286 theWorkspace->EnableTexture (myTextureEnv);
1288 // Remember OpenGl properties
1289 GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
1290 GLint aSaveZbuffFunc;
1291 GLboolean aSaveZbuffWrite;
1292 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1293 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
1294 #if !defined(GL_ES_VERSION_2_0)
1295 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1296 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
1298 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1299 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1301 // Change the properties for second rendering pass
1302 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1303 glEnable (GL_BLEND);
1305 glDepthFunc (GL_EQUAL);
1306 glDepthMask (GL_FALSE);
1307 glEnable (GL_DEPTH_TEST);
1309 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1312 RenderStructs (theWorkspace);
1313 theWorkspace->DisableTexture();
1315 // Restore properties back
1316 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1317 if (!wasBlendEnabled)
1318 glDisable (GL_BLEND);
1320 glDepthFunc (aSaveZbuffFunc);
1321 glDepthMask (aSaveZbuffWrite);
1322 if (!wasZbuffEnabled)
1323 glDisable (GL_DEPTH_FUNC);