0024381: Visualization, TKOpenGl - revise matrices stack and usage of temporary matrices
[occt.git] / src / OpenGl / OpenGl_View_2.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa
SK
16#include <stdio.h>
17#include <stdlib.h>
18
5f8b738e 19#include <OpenGl_GlCore11.hxx>
2166f0fa 20#include <OpenGl_tgl_funcs.hxx>
2166f0fa 21
3c3131a0 22#include <Image_AlienPixMap.hxx>
2166f0fa
SK
23#include <Visual3d_Layer.hxx>
24
30f0ad28 25#include <NCollection_Mat4.hxx>
26
2166f0fa 27#include <OpenGl_AspectLine.hxx>
30f0ad28 28#include <OpenGl_Context.hxx>
30f0ad28 29#include <OpenGl_Matrix.hxx>
2166f0fa
SK
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>
30f0ad28 35#include <OpenGl_ShaderManager.hxx>
36#include <OpenGl_ShaderProgram.hxx>
59f45b7c 37#include <OpenGl_Structure.hxx>
2166f0fa 38
2166f0fa
SK
39#define EPSI 0.0001
40
12381341 41namespace
42{
43
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;
48
49};
2166f0fa 50
30f0ad28 51extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
2166f0fa
SK
52
53/*----------------------------------------------------------------------*/
54
55struct OPENGL_CLIP_PLANE
56{
57 GLboolean isEnabled;
58 GLdouble Equation[4];
1c35b92f 59 DEFINE_STANDARD_ALLOC
2166f0fa
SK
60};
61
62/*----------------------------------------------------------------------*/
63/*
64* Fonctions privees
65*/
66
ca3c13d1 67#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
68/*-----------------------------------------------------------------*/
69/*
70* Set des lumieres
71*/
c827ea3a 72static void bindLight (const OpenGl_Light& theLight,
73 GLenum& theLightGlId,
74 Graphic3d_Vec4& theAmbientColor,
75 const Handle(OpenGl_Workspace)& theWorkspace)
2166f0fa
SK
76{
77 // Only 8 lights in OpenGL...
12381341 78 if (theLightGlId > GL_LIGHT7)
2166f0fa 79 {
12381341 80 return;
2166f0fa
SK
81 }
82
12381341 83 if (theLight.Type == Visual3d_TOLS_AMBIENT)
2166f0fa 84 {
4fe9ad57 85 // add RGBA intensity of the ambient light
86 theAmbientColor += theLight.Color;
12381341 87 return;
88 }
2166f0fa 89
c827ea3a 90 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
91
12381341 92 // the light is a headlight?
12381341 93 if (theLight.IsHeadlight)
94 {
c827ea3a 95
96 aContext->WorldViewState.Push();
97 aContext->WorldViewState.SetIdentity();
98
99 aContext->ApplyWorldViewMatrix();
2166f0fa
SK
100 }
101
12381341 102 // setup light type
103 switch (theLight.Type)
3c3131a0 104 {
12381341 105 case Visual3d_TOLS_DIRECTIONAL:
106 {
107 // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one
108 const OpenGl_Vec4 anInfDir = -theLight.Direction;
109
110 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE.
111 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
112 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
113 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
114 glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData());
115 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
116 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
117 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
118 break;
119 }
120 case Visual3d_TOLS_POSITIONAL:
121 {
122 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE
123 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
124 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
125 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
126 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
127 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
128 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
129 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
130 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
131 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
132 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0);
133 break;
134 }
135 case Visual3d_TOLS_SPOT:
136 {
137 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
138 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
139 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
140 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
141 glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData());
142 glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f);
143 glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI));
144 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
145 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
146 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f);
147 break;
148 }
2166f0fa
SK
149 }
150
12381341 151 // restore matrix in case of headlight
152 if (theLight.IsHeadlight)
2166f0fa 153 {
c827ea3a 154 aContext->WorldViewState.Pop();
2166f0fa 155 }
12381341 156
157 glEnable (theLightGlId++);
2166f0fa 158}
ca3c13d1 159#endif
2166f0fa
SK
160
161/*----------------------------------------------------------------------*/
2166f0fa 162
fc73a202 163void OpenGl_View::DrawBackground (OpenGl_Workspace& theWorkspace)
2166f0fa 164{
ca3c13d1 165#if !defined(GL_ES_VERSION_2_0)
fc73a202 166 if ( (theWorkspace.NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
b5ac8292 167 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
2166f0fa 168 {
fc73a202 169 const Standard_Integer aViewWidth = theWorkspace.Width();
170 const Standard_Integer aViewHeight = theWorkspace.Height();
2166f0fa
SK
171
172 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
173
c827ea3a 174 const Handle(OpenGl_Context)& aContext = theWorkspace.GetGlContext();
175
176 aContext->WorldViewState.Push();
177 aContext->ProjectionState.Push();
178
179 aContext->WorldViewState.SetIdentity();
180 aContext->ProjectionState.SetIdentity();
181
182 aContext->ApplyProjectionMatrix();
183 aContext->ApplyWorldViewMatrix();
2166f0fa
SK
184
185 if ( glIsEnabled( GL_DEPTH_TEST ) )
186 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
187
f8b2ed36 188 // drawing bg gradient if:
189 // - gradient fill type is not Aspect_GFM_NONE and
190 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
191 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
e276548b 192 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
193 myBgTexture.Style == Aspect_FM_NONE ) )
2166f0fa
SK
194 {
195 Tfloat* corner1 = 0;/* -1,-1*/
196 Tfloat* corner2 = 0;/* 1,-1*/
197 Tfloat* corner3 = 0;/* 1, 1*/
198 Tfloat* corner4 = 0;/* -1, 1*/
199 Tfloat dcorner1[3];
200 Tfloat dcorner2[3];
201
202 switch( myBgGradient.type )
203 {
e276548b 204 case Aspect_GFM_HOR:
205 corner1 = myBgGradient.color1.rgb;
206 corner2 = myBgGradient.color2.rgb;
207 corner3 = myBgGradient.color2.rgb;
208 corner4 = myBgGradient.color1.rgb;
209 break;
210 case Aspect_GFM_VER:
211 corner1 = myBgGradient.color2.rgb;
212 corner2 = myBgGradient.color2.rgb;
213 corner3 = myBgGradient.color1.rgb;
214 corner4 = myBgGradient.color1.rgb;
215 break;
216 case Aspect_GFM_DIAG1:
217 corner2 = myBgGradient.color2.rgb;
218 corner4 = myBgGradient.color1.rgb;
219 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
220 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
221 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
222 corner1 = dcorner1;
223 corner3 = dcorner2;
224 break;
225 case Aspect_GFM_DIAG2:
226 corner1 = myBgGradient.color2.rgb;
227 corner3 = myBgGradient.color1.rgb;
228 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
229 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
230 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
231 corner2 = dcorner1;
232 corner4 = dcorner2;
233 break;
234 case Aspect_GFM_CORNER1:
235 corner1 = myBgGradient.color2.rgb;
236 corner2 = myBgGradient.color2.rgb;
237 corner3 = myBgGradient.color2.rgb;
238 corner4 = myBgGradient.color1.rgb;
239 break;
240 case Aspect_GFM_CORNER2:
241 corner1 = myBgGradient.color2.rgb;
242 corner2 = myBgGradient.color2.rgb;
243 corner3 = myBgGradient.color1.rgb;
244 corner4 = myBgGradient.color2.rgb;
245 break;
246 case Aspect_GFM_CORNER3:
247 corner1 = myBgGradient.color2.rgb;
248 corner2 = myBgGradient.color1.rgb;
249 corner3 = myBgGradient.color2.rgb;
250 corner4 = myBgGradient.color2.rgb;
251 break;
252 case Aspect_GFM_CORNER4:
253 corner1 = myBgGradient.color1.rgb;
254 corner2 = myBgGradient.color2.rgb;
255 corner3 = myBgGradient.color2.rgb;
256 corner4 = myBgGradient.color2.rgb;
257 break;
258 default:
259 //printf("gradient background type not right\n");
260 break;
2166f0fa
SK
261 }
262
263 // Save GL parameters
264 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
265
266 GLint curSM;
267 glGetIntegerv( GL_SHADE_MODEL, &curSM );
268 if ( curSM != GL_SMOOTH )
269 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
270
271 glBegin(GL_TRIANGLE_FAN);
f8b2ed36 272 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
2166f0fa
SK
273 {
274 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
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.);
3c3131a0 278 }
f8b2ed36 279 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
2166f0fa
SK
280 {
281 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
282 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
283 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
284 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
285 }
286 glEnd();
287
288 // Restore GL parameters
289 if ( curSM != GL_SMOOTH )
290 glShadeModel( curSM );
291 }
f8b2ed36 292 // drawing bg image if:
293 // - it is defined and
294 // - fill type is not Aspect_FM_NONE
295 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
296 {
297 GLfloat texX_range = 1.F; // texture <s> coordinate
298 GLfloat texY_range = 1.F; // texture <t> coordinate
299
e276548b 300 // Set up for stretching or tiling
f8b2ed36 301 GLfloat x_offset, y_offset;
302 if ( myBgTexture.Style == Aspect_FM_CENTERED )
303 {
304 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
305 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
306 }
307 else
308 {
309 x_offset = 1.F;
310 y_offset = 1.F;
311 if ( myBgTexture.Style == Aspect_FM_TILED )
312 {
313 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
314 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
315 }
316 }
317
1e743e91 318 // OCCT issue 0023000: Improve the way the gradient and textured
319 // background is managed in 3d viewer (note 0020339)
320 // Setting this coefficient to -1.F allows to tile textures relatively
321 // to the top-left corner of the view (value 1.F corresponds to the
322 // initial behaviour - tiling from the bottom-left corner)
323 GLfloat aCoef = -1.F;
324
f8b2ed36 325 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
326 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
327
328 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
329
fc73a202 330 glColor3fv (theWorkspace.BackgroundColor().rgb);
b5ac8292 331 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
f8b2ed36 332
1e743e91 333 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
334 // is simply ignored, and negative multiplier is here for convenience only
335 // and does not result e.g. in texture mirroring
f8b2ed36 336 glBegin( GL_QUADS );
1e743e91 337 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
338 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
339 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
340 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
f8b2ed36 341 glEnd();
342 }
2166f0fa 343
c827ea3a 344 aContext->WorldViewState.Pop();
345 aContext->ProjectionState.Pop();
346
347 aContext->ApplyProjectionMatrix();
2166f0fa
SK
348
349 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
350
fc73a202 351 if (theWorkspace.UseZBuffer())
73192b37 352 {
b5ac8292 353 glEnable (GL_DEPTH_TEST);
73192b37 354 }
2166f0fa 355 }
ca3c13d1 356#endif
e276548b 357}
358
359/*----------------------------------------------------------------------*/
360
361//call_func_redraw_all_structs_proc
362void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
b5ac8292 363 const Handle(OpenGl_Workspace) &theWorkspace,
364 const Graphic3d_CView& theCView,
365 const Aspect_CLayer2d& theCUnderLayer,
366 const Aspect_CLayer2d& theCOverLayer)
e276548b 367{
b5ac8292 368 // ==================================
369 // Step 1: Prepare for redraw
370 // ==================================
371
372 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
373
ca3c13d1 374#if !defined(GL_ES_VERSION_2_0)
8625ef7e 375 // store and disable current clipping planes
376 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
377 NCollection_Array1<OPENGL_CLIP_PLANE> aOldPlanes (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + aMaxPlanes - 1);
378 if (aContext->core11 != NULL)
e276548b 379 {
8625ef7e 380 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
e276548b 381 {
8625ef7e 382 OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
383 aContext->core11->glGetClipPlane (aClipPlaneId, aPlane.Equation);
384 if (aPlane.isEnabled)
385 {
386 aContext->core11fwd->glDisable (aClipPlaneId);
387 aPlane.isEnabled = GL_TRUE;
388 }
389 else
390 {
391 aPlane.isEnabled = GL_FALSE;
392 }
e276548b 393 }
394 }
ca3c13d1 395#endif
e276548b 396
c827ea3a 397 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
b7cd4ba7 398 Standard_Boolean isProjectionMatUpdateNeeded = Standard_False;
399 Standard_Boolean isOrientationMatUpdateNeeded = Standard_False;
400 if (myBVHSelector.ProjectionState() != myCamera->ProjectionState())
401 {
402 isProjectionMatUpdateNeeded = Standard_True;
403 myBVHSelector.ChangeProjectionState() = myCamera->ProjectionState();
404 }
405 if (myBVHSelector.ModelViewState() != myCamera->ModelViewState())
406 {
407 isOrientationMatUpdateNeeded = Standard_True;
408 myBVHSelector.ChangeModelViewState() = myCamera->ModelViewState();
409 }
410
c827ea3a 411 if (isProjectionMatUpdateNeeded
412 || isOrientationMatUpdateNeeded)
413 {
414 myBVHSelector.SetViewVolume (myCamera);
415 }
416
8625ef7e 417 const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
418 const Standard_Boolean isSameView = aManager->IsSameView (this); // force camera state update when needed
256f9ac0 419 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
392ac980 420 {
256f9ac0 421 aManager->UpdateLightSourceStateTo (&myLights);
422 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
423 }
b5ac8292 424
256f9ac0 425 if (myProjectionState != myCamera->ProjectionState()
426 || !isSameView)
427 {
428 myProjectionState = myCamera->ProjectionState();
c827ea3a 429 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
430 aContext->ApplyProjectionMatrix();
256f9ac0 431 }
b5ac8292 432
256f9ac0 433 if (myModelViewState != myCamera->ModelViewState()
434 || !isSameView)
435 {
436 myModelViewState = myCamera->ModelViewState();
c827ea3a 437 aContext->WorldViewState.SetCurrent (myCamera->OrientationMatrixF());
438 aContext->ApplyWorldViewMatrix();
256f9ac0 439 }
b5ac8292 440
256f9ac0 441 if (aManager->ModelWorldState().Index() == 0)
442 {
c827ea3a 443 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
b7cd4ba7 444 }
445
b5ac8292 446 // ====================================
447 // Step 2: Redraw background
448 // ====================================
e276548b 449
450 // Render background
a89742cf 451 if (theWorkspace->ToRedrawGL())
452 {
453 DrawBackground (*theWorkspace);
454 }
2166f0fa 455
ca3c13d1 456#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
457 // Switch off lighting by default
458 glDisable(GL_LIGHTING);
ca3c13d1 459#endif
2166f0fa 460
b5ac8292 461 // =================================
462 // Step 3: Draw underlayer
463 // =================================
464
c827ea3a 465 RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCUnderLayer);
2166f0fa 466
b5ac8292 467 // =================================
468 // Step 4: Redraw main plane
469 // =================================
2166f0fa
SK
470
471 // Setup face culling
472 GLboolean isCullFace = GL_FALSE;
473 if ( myBackfacing )
474 {
475 isCullFace = glIsEnabled( GL_CULL_FACE );
476 if ( myBackfacing < 0 )
477 {
478 glEnable( GL_CULL_FACE );
479 glCullFace( GL_BACK );
480 }
b5ac8292 481 else
2166f0fa
SK
482 glDisable( GL_CULL_FACE );
483 }
484
ca3c13d1 485#if !defined(GL_ES_VERSION_2_0)
b5ac8292 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();
7d9e854b 489 if (anAxialScale.X() != 1.F ||
490 anAxialScale.Y() != 1.F ||
491 anAxialScale.Z() != 1.F)
492 {
493 aContext->SetGlNormalizeEnabled (Standard_True);
494 }
495 else
496 {
497 aContext->SetGlNormalizeEnabled (Standard_False);
498 }
2166f0fa 499
2166f0fa
SK
500 // Apply Fog
501 if ( myFog.IsOn )
502 {
b5ac8292 503 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
504 if (myCamera->ZFar() < aFogFrontConverted)
2166f0fa 505 {
b5ac8292 506 aFogFrontConverted = myCamera->ZFar();
507 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
12381341 508 }
4fe9ad57 509
b5ac8292 510 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
511 if (myCamera->ZFar() < aFogFrontConverted)
12381341 512 {
b5ac8292 513 aFogBackConverted = myCamera->ZFar();
514 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
12381341 515 }
b5ac8292 516
517 if (aFogFrontConverted > aFogBackConverted)
12381341 518 {
b5ac8292 519 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
520 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
ca3c13d1 521 }
b5ac8292 522
523 glFogi(GL_FOG_MODE, GL_LINEAR);
524 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
525 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
526 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
527 glEnable(GL_FOG);
2166f0fa 528 }
b5ac8292 529 else
530 glDisable(GL_FOG);
2166f0fa
SK
531
532 // Apply InteriorShadingMethod
8625ef7e 533 aContext->core11->glShadeModel (myShadingModel == Visual3d_TOM_FACET
534 || myShadingModel == Visual3d_TOM_NONE ? GL_FLAT : GL_SMOOTH);
ca3c13d1 535#endif
2166f0fa 536
8625ef7e 537 aManager->SetShadingModel (myShadingModel);
538
b5ac8292 539 // Apply AntiAliasing
540 if (myAntiAliasing)
541 theWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
542 else
543 theWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
544
545 if (!aManager->IsEmpty())
2166f0fa 546 {
b5ac8292 547 aManager->UpdateClippingState();
548 }
2166f0fa 549
b5ac8292 550 // Redraw 3d scene
551 if (!myCamera->IsStereo() || !aContext->HasStereoBuffers())
552 {
553 // single-pass monographic rendering
b5ac8292 554 // redraw scene with normal orientation and projection
c827ea3a 555 RedrawScene (thePrintContext, theWorkspace);
b5ac8292 556 }
557 else
558 {
559 // two stereographic passes
2166f0fa 560
b5ac8292 561 // safely switch to left Eye buffer
562 aContext->SetDrawBufferLeft();
b859a34d 563
c827ea3a 564 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
565 aContext->ApplyProjectionMatrix();
566
b5ac8292 567 // redraw left Eye
c827ea3a 568 RedrawScene (thePrintContext, theWorkspace);
b859a34d 569
b5ac8292 570 // reset depth buffer of first rendering pass
571 if (theWorkspace->UseDepthTest())
4269bd1b 572 {
b5ac8292 573 glClear (GL_DEPTH_BUFFER_BIT);
2166f0fa 574 }
b5ac8292 575 // safely switch to right Eye buffer
576 aContext->SetDrawBufferRight();
ca3c13d1 577
c827ea3a 578 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
579 aContext->ApplyProjectionMatrix();
580
b5ac8292 581 // redraw right Eye
c827ea3a 582 RedrawScene (thePrintContext, theWorkspace);
2166f0fa 583
b5ac8292 584 // switch back to monographic rendering
585 aContext->SetDrawBufferMono();
2166f0fa
SK
586 }
587
b5ac8292 588 // ===============================
589 // Step 5: Trihedron
590 // ===============================
2166f0fa 591
26395493 592 // Resetting GL parameters according to the default aspects
593 // in order to synchronize GL state with the graphic driver state
594 // before drawing auxiliary stuff (trihedrons, overlayer)
595 // and invoking optional callbacks
b5ac8292 596 theWorkspace->ResetAppliedAspect();
2166f0fa 597
b859a34d 598 aContext->ChangeClipping().RemoveAll();
2166f0fa 599
392ac980 600 if (!aManager->IsEmpty())
30f0ad28 601 {
392ac980 602 aManager->ResetMaterialStates();
603 aManager->RevertClippingState();
604
605 // We need to disable (unbind) all shaders programs to ensure
606 // that all objects without specified aspect will be drawn
607 // correctly (such as background)
7d3e64ef 608 aContext->BindProgram (NULL);
30f0ad28 609 }
610
a89742cf 611 // Render trihedron
612 if (theWorkspace->ToRedrawGL())
a174a3c5 613 {
a89742cf 614 RedrawTrihedron (theWorkspace);
2166f0fa 615
a89742cf 616 // Restore face culling
617 if ( myBackfacing )
2166f0fa 618 {
a89742cf 619 if ( isCullFace )
620 {
621 glEnable ( GL_CULL_FACE );
622 glCullFace ( GL_BACK );
623 }
624 else
625 glDisable ( GL_CULL_FACE );
2166f0fa 626 }
2166f0fa
SK
627 }
628
b5ac8292 629 // ===============================
630 // Step 6: Redraw overlay
631 // ===============================
632
529afc1a 633 const int aMode = 0;
b5ac8292 634 theWorkspace->DisplayCallback (theCView, (aMode | OCC_PRE_OVERLAY));
2166f0fa 635
c827ea3a 636 RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCOverLayer);
2166f0fa 637
b5ac8292 638 theWorkspace->DisplayCallback (theCView, aMode);
639
640 // ===============================
641 // Step 7: Finalize
642 // ===============================
2166f0fa 643
ca3c13d1 644#if !defined(GL_ES_VERSION_2_0)
8625ef7e 645 // restore clipping planes
646 if (aContext->core11 != NULL)
2166f0fa 647 {
8625ef7e 648 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
649 {
650 const OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
651 aContext->core11->glClipPlane (aClipPlaneId, aPlane.Equation);
652 if (aPlane.isEnabled)
653 aContext->core11fwd->glEnable (aClipPlaneId);
654 else
655 aContext->core11fwd->glDisable (aClipPlaneId);
656 }
2166f0fa 657 }
ca3c13d1 658#endif
e6804ff7 659
660 // ==============================================================
661 // Step 8: Keep shader manager informed about last View
662 // ==============================================================
663
664 if (!aManager.IsNull())
665 {
666 aManager->SetLastView (this);
667 }
2166f0fa
SK
668}
669
b7cd4ba7 670// =======================================================================
671// function : InvalidateBVHData
672// purpose :
673// =======================================================================
674void OpenGl_View::InvalidateBVHData (const Standard_Integer theLayerId)
675{
676 myZLayers.InvalidateBVHData (theLayerId);
677}
678
2166f0fa
SK
679/*----------------------------------------------------------------------*/
680
681//ExecuteViewDisplay
682void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
683{
59f45b7c 684 if ( myZLayers.NbStructures() <= 0 )
685 return;
2166f0fa 686
ca3c13d1 687#if !defined(GL_ES_VERSION_2_0)
2166f0fa 688 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
ca3c13d1 689#endif
2166f0fa 690
2166f0fa
SK
691 //TsmPushAttri(); /* save previous graphics context */
692
693 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
694 {
ca3c13d1 695 #if !defined(GL_ES_VERSION_2_0)
73192b37 696 const int antiAliasingMode = AWorkspace->AntiAliasingMode();
ca3c13d1 697 #endif
2166f0fa
SK
698
699 if ( !myAntiAliasing )
700 {
ca3c13d1 701 #if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
702 glDisable(GL_POINT_SMOOTH);
703 glDisable(GL_LINE_SMOOTH);
704 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
ca3c13d1 705 #endif
2166f0fa
SK
706 glBlendFunc (GL_ONE, GL_ZERO);
707 glDisable (GL_BLEND);
708 }
709 else
710 {
ca3c13d1 711 #if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
712 glEnable(GL_POINT_SMOOTH);
713 glEnable(GL_LINE_SMOOTH);
714 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
ca3c13d1 715 #endif
3c3131a0 716 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2166f0fa
SK
717 glEnable (GL_BLEND);
718 }
719 }
720
59f45b7c 721 myZLayers.Render (AWorkspace);
2166f0fa 722
ca3c13d1 723#if !defined(GL_ES_VERSION_2_0)
2166f0fa 724 //TsmPopAttri(); /* restore previous graphics context; before update lights */
de75ed09 725 glPopAttrib();
ca3c13d1 726#endif
2166f0fa
SK
727}
728
729/*----------------------------------------------------------------------*/
730
731//call_togl_redraw_layer2d
a174a3c5 732void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
c827ea3a 733 const Handle(OpenGl_Workspace)& theWorkspace,
a174a3c5 734 const Graphic3d_CView& ACView,
735 const Aspect_CLayer2d& ACLayer)
2166f0fa 736{
ca3c13d1 737#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
738 if (&ACLayer == NULL
739 || ACLayer.ptrLayer == NULL
740 || ACLayer.ptrLayer->listIndex == 0) return;
741
c827ea3a 742 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
743
529afc1a
K
744 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
745 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
2166f0fa 746
c827ea3a 747 aContext->WorldViewState.Push();
748 aContext->ProjectionState.Push();
2166f0fa 749
c827ea3a 750 aContext->WorldViewState.SetIdentity();
751 aContext->ProjectionState.SetIdentity();
752
753 aContext->ApplyWorldViewMatrix();
754 aContext->ApplyProjectionMatrix();
2166f0fa
SK
755
756 if (!ACLayer.sizeDependent)
757 glViewport (0, 0, dispWidth, dispHeight);
758
759 float left = ACLayer.ortho[0];
760 float right = ACLayer.ortho[1];
761 float bottom = ACLayer.ortho[2];
762 float top = ACLayer.ortho[3];
763
764 int attach = ACLayer.attach;
765
766 float ratio;
767 if (!ACLayer.sizeDependent)
768 ratio = (float) dispWidth/dispHeight;
769 else
770 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
771
772 float delta;
c9d4eb9d 773 if (ratio >= 1.0) {
2166f0fa
SK
774 delta = (float )((top - bottom)/2.0);
775 switch (attach) {
776 case 0: /* Aspect_TOC_BOTTOM_LEFT */
777 top = bottom + 2*delta/ratio;
778 break;
779 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
780 top = bottom + 2*delta/ratio;
781 break;
782 case 2: /* Aspect_TOC_TOP_LEFT */
783 bottom = top - 2*delta/ratio;
784 break;
785 case 3: /* Aspect_TOC_TOP_RIGHT */
786 bottom = top - 2*delta/ratio;
787 break;
788 }
789 }
c9d4eb9d 790 else {
2166f0fa
SK
791 delta = (float )((right - left)/2.0);
792 switch (attach) {
793 case 0: /* Aspect_TOC_BOTTOM_LEFT */
794 right = left + 2*delta*ratio;
795 break;
796 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
797 left = right - 2*delta*ratio;
798 break;
799 case 2: /* Aspect_TOC_TOP_LEFT */
800 right = left + 2*delta*ratio;
801 break;
802 case 3: /* Aspect_TOC_TOP_RIGHT */
803 left = right - 2*delta*ratio;
804 break;
805 }
806 }
807
a174a3c5 808#ifdef _WIN32
2166f0fa 809 // Check printer context that exists only for print operation
a174a3c5 810 if (!thePrintContext.IsNull())
2166f0fa
SK
811 {
812 // additional transformation matrix could be applied to
813 // render only those parts of viewport that will be
814 // passed to a printer as a current "frame" to provide
815 // tiling; scaling of graphics by matrix helps render a
816 // part of a view (frame) in same viewport, but with higher
817 // resolution
c827ea3a 818
819 // set printing scale/tiling transformation
820 aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation());
821 aContext->ApplyProjectionMatrix();
2166f0fa
SK
822
823 // printing operation also assumes other viewport dimension
824 // to comply with transformation matrix or graphics scaling
825 // factors for tiling for layer redraw
826 GLsizei anViewportX = 0;
827 GLsizei anViewportY = 0;
a174a3c5 828 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
2166f0fa
SK
829 if (anViewportX != 0 && anViewportY != 0)
830 glViewport (0, 0, anViewportX, anViewportY);
831 }
3c3131a0 832#endif
2166f0fa
SK
833
834 glOrtho (left, right, bottom, top, -1.0, 1.0);
835
2166f0fa
SK
836 glPushAttrib (
837 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
838 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
c9d4eb9d 839
2166f0fa 840 glDisable (GL_DEPTH_TEST);
c9d4eb9d 841 glDisable (GL_TEXTURE_1D);
842 glDisable (GL_TEXTURE_2D);
843 glDisable (GL_LIGHTING);
844
845 // TODO: Obsolete code, the display list is always empty now, to be removed
2166f0fa
SK
846 glCallList (ACLayer.ptrLayer->listIndex);
847
848 //calling dynamic render of LayerItems
849 if ( ACLayer.ptrLayer->layerData )
850 {
30f0ad28 851 InitLayerProp (ACLayer.ptrLayer->listIndex);
2166f0fa 852 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
30f0ad28 853 InitLayerProp (0);
2166f0fa
SK
854 }
855
856 glPopAttrib ();
857
c827ea3a 858 aContext->WorldViewState.Pop();
859 aContext->ProjectionState.Pop();
2166f0fa 860
c827ea3a 861 aContext->ApplyProjectionMatrix();
862 aContext->ApplyWorldViewMatrix();
2166f0fa 863
2166f0fa
SK
864 if (!ACLayer.sizeDependent)
865 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
866
867 glFlush ();
ca3c13d1 868#endif
2166f0fa
SK
869}
870
871/*----------------------------------------------------------------------*/
872
a89742cf 873void OpenGl_View::RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
874{
875 // display global trihedron
876 if (myTrihedron != NULL)
877 {
878 myTrihedron->Render (theWorkspace);
879 }
880 if (myGraduatedTrihedron != NULL)
881 {
882 myGraduatedTrihedron->Render (theWorkspace);
883 }
884}
885
886/*----------------------------------------------------------------------*/
887
2166f0fa 888//call_togl_create_bg_texture
3c3131a0 889void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
890 const Aspect_FillMethod theFillStyle)
2166f0fa 891{
3c3131a0 892 if (myBgTexture.TexId != 0)
2166f0fa 893 {
3c3131a0 894 // delete existing texture
895 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
2166f0fa
SK
896 myBgTexture.TexId = 0;
897 }
898
3c3131a0 899 // load image from file
900 Image_AlienPixMap anImageLoaded;
901 if (!anImageLoaded.Load (theFilePath))
902 {
903 return;
904 }
905
906 Image_PixMap anImage;
907 if (anImageLoaded.RowExtraBytes() == 0 &&
908 (anImageLoaded.Format() == Image_PixMap::ImgRGB
909 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
910 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
2166f0fa 911 {
3c3131a0 912 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
913 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
914 }
915 else
916 {
917 // convert image to RGB format
918 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
919 {
920 return;
921 }
922
923 anImage.SetTopDown (false);
3c3131a0 924 Quantity_Color aSrcColor;
925 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
926 {
927 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
2166f0fa 928 {
6a7d83c4 929 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
ca0c0b11 930 Image_ColorRGB& aColor = anImage.ChangeValue<Image_ColorRGB> (aRow, aCol);
8263fcd3 931 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
932 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
933 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
2166f0fa 934 }
3c3131a0 935 }
936 anImageLoaded.Clear();
937 }
2166f0fa 938
3c3131a0 939 // create MipMapped texture
940 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
2166f0fa 941
3c3131a0 942 GLuint aTextureId = 0;
943 glGenTextures (1, &aTextureId);
944 glBindTexture (GL_TEXTURE_2D, aTextureId);
2166f0fa 945
3c3131a0 946 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
947 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
948 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
949 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
2166f0fa 950
3c3131a0 951 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
ca3c13d1 952
953#if !defined(GL_ES_VERSION_2_0)
3c3131a0 954 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
955 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
956 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
ca3c13d1 957#endif
2166f0fa 958
3c3131a0 959 myBgTexture.TexId = aTextureId;
960 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
961 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
962 myBgTexture.Style = theFillStyle;
2166f0fa
SK
963}
964
965/*----------------------------------------------------------------------*/
966
967//call_togl_set_bg_texture_style
968void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
969{
f8b2ed36 970 myBgTexture.Style = AFillStyle;
2166f0fa
SK
971}
972
973/*----------------------------------------------------------------------*/
974
975//call_togl_gradient_background
976void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
977 const Quantity_Color& AColor2,
978 const Aspect_GradientFillMethod AType)
979{
980 Standard_Real R,G,B;
981 AColor1.Values( R, G, B, Quantity_TOC_RGB );
982 myBgGradient.color1.rgb[0] = ( Tfloat )R;
983 myBgGradient.color1.rgb[1] = ( Tfloat )G;
984 myBgGradient.color1.rgb[2] = ( Tfloat )B;
985 myBgGradient.color1.rgb[3] = 0.F;
986
987 AColor2.Values( R, G, B, Quantity_TOC_RGB );
988 myBgGradient.color2.rgb[0] = ( Tfloat )R;
989 myBgGradient.color2.rgb[1] = ( Tfloat )G;
990 myBgGradient.color2.rgb[2] = ( Tfloat )B;
991 myBgGradient.color2.rgb[3] = 0.F;
992
993 myBgGradient.type = AType;
994}
995
996/*----------------------------------------------------------------------*/
997
998//call_togl_set_gradient_type
999void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
1000{
f8b2ed36 1001 myBgGradient.type = AType;
2166f0fa
SK
1002}
1003
59f45b7c 1004//=======================================================================
1005//function : AddZLayer
3c3131a0 1006//purpose :
59f45b7c 1007//=======================================================================
1008
1009void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
1010{
1011 myZLayers.AddLayer (theLayerId);
1012}
1013
1014//=======================================================================
1015//function : RemoveZLayer
3c3131a0 1016//purpose :
59f45b7c 1017//=======================================================================
1018
1019void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
1020{
1021 myZLayers.RemoveLayer (theLayerId);
1022}
1023
1024//=======================================================================
1025//function : DisplayStructure
3c3131a0 1026//purpose :
59f45b7c 1027//=======================================================================
1028
1029void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
1030 const Standard_Integer thePriority)
1031{
1032 Standard_Integer aZLayer = theStructure->GetZLayer ();
1033 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
1034}
1035
1036//=======================================================================
679ecdee 1037//function : DisplayImmediateStructure
1038//purpose :
1039//=======================================================================
1040
1041void OpenGl_View::DisplayImmediateStructure (const OpenGl_Structure* theStructure)
1042{
1043 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1044 anIter.More(); anIter.Next())
1045 {
1046 if (anIter.Value() == theStructure)
1047 {
1048 return;
1049 }
1050 }
1051
1052 myImmediateList.Append (theStructure);
1053}
1054
1055//=======================================================================
59f45b7c 1056//function : EraseStructure
3c3131a0 1057//purpose :
59f45b7c 1058//=======================================================================
1059
1060void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
1061{
1062 Standard_Integer aZLayer = theStructure->GetZLayer ();
1063 myZLayers.RemoveStructure (theStructure, aZLayer);
1064}
1065
1066//=======================================================================
679ecdee 1067//function : EraseImmediateStructure
1068//purpose :
1069//=======================================================================
1070
1071void OpenGl_View::EraseImmediateStructure (const OpenGl_Structure* theStructure)
1072{
1073 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1074 anIter.More(); anIter.Next())
1075 {
1076 if (anIter.Value() == theStructure)
1077 {
1078 myImmediateList.Remove (anIter);
1079 return;
1080 }
1081 }
1082}
1083
1084//=======================================================================
59f45b7c 1085//function : ChangeZLayer
1086//purpose :
1087//=======================================================================
1088
1089void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
1090 const Standard_Integer theNewLayerId)
1091{
1092 Standard_Integer anOldLayer = theStructure->GetZLayer ();
1093 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);
1094}
b5ac8292 1095
1096//=======================================================================
c5751993 1097//function : SetZLayerSettings
1098//purpose :
1099//=======================================================================
1100void OpenGl_View::SetZLayerSettings (const Standard_Integer theLayerId,
1101 const Graphic3d_ZLayerSettings theSettings)
1102{
1103 myZLayers.Layer (theLayerId).SetLayerSettings (theSettings);
1104}
1105
b7cd4ba7 1106//=======================================================================
1107//function : ChangePriority
1108//purpose :
1109//=======================================================================
1110void OpenGl_View::ChangePriority (const OpenGl_Structure *theStructure,
1111 const Standard_Integer theNewPriority)
1112{
1113 Standard_Integer aLayerId = theStructure->GetZLayer();
1114 myZLayers.ChangePriority (theStructure, aLayerId, theNewPriority);
1115}
c5751993 1116
1117//=======================================================================
b5ac8292 1118//function : RedrawScene
1119//purpose :
1120//=======================================================================
1121
1122void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
c827ea3a 1123 const Handle(OpenGl_Workspace)& theWorkspace)
b5ac8292 1124{
1125 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
1126
1127 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1128 {
1129 Handle(Graphic3d_ClipPlane) aPlaneBack;
1130 Handle(Graphic3d_ClipPlane) aPlaneFront;
1131
1132 if (myZClip.Back.IsOn)
1133 {
1134 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1135 if (myCamera->ZFar() < aClipBackConverted)
1136 {
1137 aClipBackConverted = myCamera->ZFar();
1138 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
1139 }
1140 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
1141 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1142 }
1143
1144 if (myZClip.Front.IsOn)
1145 {
1146 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1147 if (myCamera->ZNear() > aClipFrontConverted)
1148 {
1149 aClipFrontConverted = myCamera->ZNear();
1150 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1151 }
1152 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1153 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1154 }
1155
cddbf6a9 1156 // Specify slicing planes with identity transformation
b5ac8292 1157 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1158 {
1159 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1160 if (!aPlaneBack.IsNull())
1161 {
1162 aSlicingPlanes.Append (aPlaneBack);
1163 }
1164
1165 if (!aPlaneFront.IsNull())
1166 {
1167 aSlicingPlanes.Append (aPlaneFront);
1168 }
1169
1170 // add planes at loaded view matrix state
1171 aContext->ChangeClipping().AddView (aSlicingPlanes, theWorkspace);
1172 }
1173 }
1174
b5ac8292 1175#ifdef _WIN32
c827ea3a 1176 // set printing scale/tiling transformation
b5ac8292 1177 if (!thePrintContext.IsNull())
1178 {
c827ea3a 1179 aContext->ProjectionState.Push();
1180 aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation() * aContext->ProjectionState.Current());
1181 aContext->ApplyProjectionMatrix();
b5ac8292 1182 }
ca3c13d1 1183#endif
b5ac8292 1184
cddbf6a9 1185 // Specify clipping planes in view transformation space
1186 if (!myClipPlanes.IsEmpty())
1187 {
1188 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1189 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1190 for (; aClippingIt.More(); aClippingIt.Next())
1191 {
1192 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1193 if (aClipPlane->IsOn())
1194 {
1195 aUserPlanes.Append (aClipPlane);
1196 }
1197 }
1198
1199 if (!aUserPlanes.IsEmpty())
1200 {
1201 aContext->ChangeClipping().AddWorld (aUserPlanes);
1202 }
1203
1204 if (!aContext->ShaderManager()->IsEmpty())
1205 {
1206 aContext->ShaderManager()->UpdateClippingState();
1207 }
1208 }
1209
ca3c13d1 1210#if !defined(GL_ES_VERSION_2_0)
b5ac8292 1211 // Apply Lights
1212 {
1213 // setup lights
1214 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1215 THE_DEFAULT_AMBIENT[1],
1216 THE_DEFAULT_AMBIENT[2],
1217 THE_DEFAULT_AMBIENT[3]);
1218 GLenum aLightGlId = GL_LIGHT0;
1219 for (OpenGl_ListOfLight::Iterator aLightIt (myLights);
1220 aLightIt.More(); aLightIt.Next())
1221 {
c827ea3a 1222 bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, theWorkspace);
b5ac8292 1223 }
1224
1225 // apply accumulated ambient color
1226 anAmbientColor.a() = 1.0f;
1227 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1228
1229 if (aLightGlId != GL_LIGHT0)
1230 {
1231 glEnable (GL_LIGHTING);
1232 }
1233 // switch off unused lights
1234 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1235 {
1236 glDisable (aLightGlId);
1237 }
1238 }
ca3c13d1 1239#endif
b5ac8292 1240
1241 // Clear status bitfields
1242 theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1243
1244 // Added PCT for handling of textures
1245 switch (mySurfaceDetail)
1246 {
1247 case Visual3d_TOD_NONE:
1248 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1249 theWorkspace->DisableTexture();
1250 // Render the view
1251 RenderStructs (theWorkspace);
1252 break;
1253
1254 case Visual3d_TOD_ENVIRONMENT:
1255 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1256 theWorkspace->EnableTexture (myTextureEnv);
1257 // Render the view
1258 RenderStructs (theWorkspace);
1259 theWorkspace->DisableTexture();
1260 break;
1261
1262 case Visual3d_TOD_ALL:
1263 // First pass
1264 theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1265 // Render the view
1266 RenderStructs (theWorkspace);
1267 theWorkspace->DisableTexture();
1268
1269 // Second pass
1270 if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1271 {
1272 theWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1273 theWorkspace->EnableTexture (myTextureEnv);
1274
1275 // Remember OpenGl properties
ca3c13d1 1276 GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
b5ac8292 1277 GLint aSaveZbuffFunc;
1278 GLboolean aSaveZbuffWrite;
1279 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1280 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
ca3c13d1 1281 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 1282 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1283 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
ca3c13d1 1284 #endif
b5ac8292 1285 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1286 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1287
1288 // Change the properties for second rendering pass
1289 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1290 glEnable (GL_BLEND);
1291
1292 glDepthFunc (GL_EQUAL);
1293 glDepthMask (GL_FALSE);
1294 glEnable (GL_DEPTH_TEST);
1295
1296 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1297
1298 // Render the view
1299 RenderStructs (theWorkspace);
1300 theWorkspace->DisableTexture();
1301
1302 // Restore properties back
1303 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1304 if (!wasBlendEnabled)
1305 glDisable (GL_BLEND);
1306
1307 glDepthFunc (aSaveZbuffFunc);
1308 glDepthMask (aSaveZbuffWrite);
1309 if (!wasZbuffEnabled)
1310 glDisable (GL_DEPTH_FUNC);
1311 }
1312 break;
1313 }
c827ea3a 1314
1315 // Apply restored view matrix.
1316 aContext->ApplyWorldViewMatrix();
1317
1318#ifdef _WIN32
1319 // set printing scale/tiling transformation
1320 if (!thePrintContext.IsNull())
1321 {
1322 aContext->ProjectionState.Pop();
1323 aContext->ApplyProjectionMatrix();
1324 }
1325#endif
1326
b5ac8292 1327}