0025689: IGESCAFControl_Writer crash in constructor.
[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 160
2166f0fa
SK
161/*----------------------------------------------------------------------*/
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,
a1954302 363 const Handle(OpenGl_Workspace)& theWorkspace,
364 const Graphic3d_CView& theCView,
365 const Aspect_CLayer2d& theCUnderLayer,
366 const Aspect_CLayer2d& theCOverLayer,
367 const Standard_Boolean theToDrawImmediate)
e276548b 368{
b5ac8292 369 // ==================================
370 // Step 1: Prepare for redraw
371 // ==================================
372
373 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
374
ca3c13d1 375#if !defined(GL_ES_VERSION_2_0)
8625ef7e 376 // store and disable current clipping planes
377 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
378 NCollection_Array1<OPENGL_CLIP_PLANE> aOldPlanes (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + aMaxPlanes - 1);
379 if (aContext->core11 != NULL)
e276548b 380 {
8625ef7e 381 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
e276548b 382 {
8625ef7e 383 OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
384 aContext->core11->glGetClipPlane (aClipPlaneId, aPlane.Equation);
385 if (aPlane.isEnabled)
386 {
387 aContext->core11fwd->glDisable (aClipPlaneId);
388 aPlane.isEnabled = GL_TRUE;
389 }
390 else
391 {
392 aPlane.isEnabled = GL_FALSE;
393 }
e276548b 394 }
395 }
ca3c13d1 396#endif
e276548b 397
c827ea3a 398 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
b7cd4ba7 399 Standard_Boolean isProjectionMatUpdateNeeded = Standard_False;
400 Standard_Boolean isOrientationMatUpdateNeeded = Standard_False;
401 if (myBVHSelector.ProjectionState() != myCamera->ProjectionState())
402 {
403 isProjectionMatUpdateNeeded = Standard_True;
404 myBVHSelector.ChangeProjectionState() = myCamera->ProjectionState();
405 }
406 if (myBVHSelector.ModelViewState() != myCamera->ModelViewState())
407 {
408 isOrientationMatUpdateNeeded = Standard_True;
409 myBVHSelector.ChangeModelViewState() = myCamera->ModelViewState();
410 }
411
c827ea3a 412 if (isProjectionMatUpdateNeeded
413 || isOrientationMatUpdateNeeded)
414 {
415 myBVHSelector.SetViewVolume (myCamera);
416 }
417
8625ef7e 418 const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
419 const Standard_Boolean isSameView = aManager->IsSameView (this); // force camera state update when needed
256f9ac0 420 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
392ac980 421 {
256f9ac0 422 aManager->UpdateLightSourceStateTo (&myLights);
423 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
424 }
b5ac8292 425
256f9ac0 426 if (myProjectionState != myCamera->ProjectionState()
427 || !isSameView)
428 {
429 myProjectionState = myCamera->ProjectionState();
c827ea3a 430 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
431 aContext->ApplyProjectionMatrix();
256f9ac0 432 }
b5ac8292 433
256f9ac0 434 if (myModelViewState != myCamera->ModelViewState()
435 || !isSameView)
436 {
437 myModelViewState = myCamera->ModelViewState();
c827ea3a 438 aContext->WorldViewState.SetCurrent (myCamera->OrientationMatrixF());
439 aContext->ApplyWorldViewMatrix();
256f9ac0 440 }
b5ac8292 441
256f9ac0 442 if (aManager->ModelWorldState().Index() == 0)
443 {
c827ea3a 444 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
b7cd4ba7 445 }
446
b5ac8292 447 // ====================================
448 // Step 2: Redraw background
449 // ====================================
e276548b 450
451 // Render background
a1954302 452 if (theWorkspace->ToRedrawGL()
453 && !theToDrawImmediate)
a89742cf 454 {
455 DrawBackground (*theWorkspace);
456 }
2166f0fa 457
ca3c13d1 458#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
459 // Switch off lighting by default
460 glDisable(GL_LIGHTING);
ca3c13d1 461#endif
2166f0fa 462
b5ac8292 463 // =================================
464 // Step 3: Draw underlayer
465 // =================================
a1954302 466 if (!theToDrawImmediate)
467 {
468 RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCUnderLayer);
469 }
2166f0fa 470
b5ac8292 471 // =================================
472 // Step 4: Redraw main plane
473 // =================================
2166f0fa
SK
474
475 // Setup face culling
476 GLboolean isCullFace = GL_FALSE;
477 if ( myBackfacing )
478 {
479 isCullFace = glIsEnabled( GL_CULL_FACE );
480 if ( myBackfacing < 0 )
481 {
482 glEnable( GL_CULL_FACE );
483 glCullFace( GL_BACK );
484 }
b5ac8292 485 else
2166f0fa
SK
486 glDisable( GL_CULL_FACE );
487 }
488
ca3c13d1 489#if !defined(GL_ES_VERSION_2_0)
b5ac8292 490 // if the view is scaled normal vectors are scaled to unit
491 // length for correct displaying of shaded objects
492 const gp_Pnt anAxialScale = myCamera->AxialScale();
7d9e854b 493 if (anAxialScale.X() != 1.F ||
494 anAxialScale.Y() != 1.F ||
495 anAxialScale.Z() != 1.F)
496 {
497 aContext->SetGlNormalizeEnabled (Standard_True);
498 }
499 else
500 {
501 aContext->SetGlNormalizeEnabled (Standard_False);
502 }
2166f0fa 503
2166f0fa
SK
504 // Apply Fog
505 if ( myFog.IsOn )
506 {
b5ac8292 507 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
508 if (myCamera->ZFar() < aFogFrontConverted)
2166f0fa 509 {
b5ac8292 510 aFogFrontConverted = myCamera->ZFar();
511 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
12381341 512 }
4fe9ad57 513
b5ac8292 514 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
515 if (myCamera->ZFar() < aFogFrontConverted)
12381341 516 {
b5ac8292 517 aFogBackConverted = myCamera->ZFar();
518 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
12381341 519 }
b5ac8292 520
521 if (aFogFrontConverted > aFogBackConverted)
12381341 522 {
b5ac8292 523 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
524 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
ca3c13d1 525 }
b5ac8292 526
527 glFogi(GL_FOG_MODE, GL_LINEAR);
528 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
529 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
530 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
531 glEnable(GL_FOG);
2166f0fa 532 }
b5ac8292 533 else
534 glDisable(GL_FOG);
2166f0fa
SK
535
536 // Apply InteriorShadingMethod
8625ef7e 537 aContext->core11->glShadeModel (myShadingModel == Visual3d_TOM_FACET
538 || myShadingModel == Visual3d_TOM_NONE ? GL_FLAT : GL_SMOOTH);
ca3c13d1 539#endif
2166f0fa 540
8625ef7e 541 aManager->SetShadingModel (myShadingModel);
542
b5ac8292 543 // Apply AntiAliasing
544 if (myAntiAliasing)
545 theWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
546 else
547 theWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
548
549 if (!aManager->IsEmpty())
2166f0fa 550 {
b5ac8292 551 aManager->UpdateClippingState();
552 }
2166f0fa 553
b5ac8292 554 // Redraw 3d scene
555 if (!myCamera->IsStereo() || !aContext->HasStereoBuffers())
556 {
557 // single-pass monographic rendering
b5ac8292 558 // redraw scene with normal orientation and projection
a1954302 559 RedrawScene (thePrintContext, theWorkspace, theToDrawImmediate);
b5ac8292 560 }
561 else
562 {
563 // two stereographic passes
2166f0fa 564
b5ac8292 565 // safely switch to left Eye buffer
566 aContext->SetDrawBufferLeft();
b859a34d 567
c827ea3a 568 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
569 aContext->ApplyProjectionMatrix();
570
b5ac8292 571 // redraw left Eye
a1954302 572 RedrawScene (thePrintContext, theWorkspace, theToDrawImmediate);
b859a34d 573
b5ac8292 574 // reset depth buffer of first rendering pass
575 if (theWorkspace->UseDepthTest())
4269bd1b 576 {
b5ac8292 577 glClear (GL_DEPTH_BUFFER_BIT);
2166f0fa 578 }
b5ac8292 579 // safely switch to right Eye buffer
580 aContext->SetDrawBufferRight();
ca3c13d1 581
c827ea3a 582 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
583 aContext->ApplyProjectionMatrix();
584
b5ac8292 585 // redraw right Eye
a1954302 586 RedrawScene (thePrintContext, theWorkspace, theToDrawImmediate);
2166f0fa 587
b5ac8292 588 // switch back to monographic rendering
589 aContext->SetDrawBufferMono();
2166f0fa
SK
590 }
591
b5ac8292 592 // ===============================
593 // Step 5: Trihedron
594 // ===============================
2166f0fa 595
26395493 596 // Resetting GL parameters according to the default aspects
597 // in order to synchronize GL state with the graphic driver state
598 // before drawing auxiliary stuff (trihedrons, overlayer)
599 // and invoking optional callbacks
b5ac8292 600 theWorkspace->ResetAppliedAspect();
2166f0fa 601
b859a34d 602 aContext->ChangeClipping().RemoveAll();
2166f0fa 603
392ac980 604 if (!aManager->IsEmpty())
30f0ad28 605 {
392ac980 606 aManager->ResetMaterialStates();
607 aManager->RevertClippingState();
608
609 // We need to disable (unbind) all shaders programs to ensure
610 // that all objects without specified aspect will be drawn
611 // correctly (such as background)
7d3e64ef 612 aContext->BindProgram (NULL);
30f0ad28 613 }
614
a89742cf 615 // Render trihedron
a1954302 616 if (theWorkspace->ToRedrawGL()
617 && !theToDrawImmediate)
a174a3c5 618 {
a89742cf 619 RedrawTrihedron (theWorkspace);
2166f0fa 620
a89742cf 621 // Restore face culling
622 if ( myBackfacing )
2166f0fa 623 {
a89742cf 624 if ( isCullFace )
625 {
626 glEnable ( GL_CULL_FACE );
627 glCullFace ( GL_BACK );
628 }
629 else
630 glDisable ( GL_CULL_FACE );
2166f0fa 631 }
2166f0fa
SK
632 }
633
b5ac8292 634 // ===============================
635 // Step 6: Redraw overlay
636 // ===============================
a1954302 637 if (!theToDrawImmediate)
638 {
639 const int aMode = 0;
640 theWorkspace->DisplayCallback (theCView, (aMode | OCC_PRE_OVERLAY));
b5ac8292 641
a1954302 642 RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCOverLayer);
2166f0fa 643
a1954302 644 theWorkspace->DisplayCallback (theCView, aMode);
645 }
b5ac8292 646
647 // ===============================
648 // Step 7: Finalize
649 // ===============================
2166f0fa 650
ca3c13d1 651#if !defined(GL_ES_VERSION_2_0)
8625ef7e 652 // restore clipping planes
653 if (aContext->core11 != NULL)
2166f0fa 654 {
8625ef7e 655 for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
656 {
657 const OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
658 aContext->core11->glClipPlane (aClipPlaneId, aPlane.Equation);
659 if (aPlane.isEnabled)
660 aContext->core11fwd->glEnable (aClipPlaneId);
661 else
662 aContext->core11fwd->glDisable (aClipPlaneId);
663 }
2166f0fa 664 }
ca3c13d1 665#endif
e6804ff7 666
667 // ==============================================================
668 // Step 8: Keep shader manager informed about last View
669 // ==============================================================
670
671 if (!aManager.IsNull())
672 {
673 aManager->SetLastView (this);
674 }
2166f0fa
SK
675}
676
b7cd4ba7 677// =======================================================================
678// function : InvalidateBVHData
679// purpose :
680// =======================================================================
a1954302 681void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
b7cd4ba7 682{
683 myZLayers.InvalidateBVHData (theLayerId);
684}
685
2166f0fa
SK
686/*----------------------------------------------------------------------*/
687
688//ExecuteViewDisplay
a1954302 689void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& AWorkspace,
690 const Standard_Boolean theToDrawImmediate)
2166f0fa 691{
59f45b7c 692 if ( myZLayers.NbStructures() <= 0 )
693 return;
2166f0fa 694
ca3c13d1 695#if !defined(GL_ES_VERSION_2_0)
2166f0fa 696 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
ca3c13d1 697#endif
2166f0fa 698
2166f0fa
SK
699 //TsmPushAttri(); /* save previous graphics context */
700
701 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
702 {
ca3c13d1 703 #if !defined(GL_ES_VERSION_2_0)
73192b37 704 const int antiAliasingMode = AWorkspace->AntiAliasingMode();
ca3c13d1 705 #endif
2166f0fa
SK
706
707 if ( !myAntiAliasing )
708 {
ca3c13d1 709 #if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
710 glDisable(GL_POINT_SMOOTH);
711 glDisable(GL_LINE_SMOOTH);
712 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
ca3c13d1 713 #endif
2166f0fa
SK
714 glBlendFunc (GL_ONE, GL_ZERO);
715 glDisable (GL_BLEND);
716 }
717 else
718 {
ca3c13d1 719 #if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
720 glEnable(GL_POINT_SMOOTH);
721 glEnable(GL_LINE_SMOOTH);
722 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
ca3c13d1 723 #endif
3c3131a0 724 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2166f0fa
SK
725 glEnable (GL_BLEND);
726 }
727 }
728
a1954302 729 myZLayers.Render (AWorkspace, theToDrawImmediate);
2166f0fa 730
ca3c13d1 731#if !defined(GL_ES_VERSION_2_0)
2166f0fa 732 //TsmPopAttri(); /* restore previous graphics context; before update lights */
de75ed09 733 glPopAttrib();
ca3c13d1 734#endif
2166f0fa
SK
735}
736
737/*----------------------------------------------------------------------*/
738
739//call_togl_redraw_layer2d
a174a3c5 740void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
c827ea3a 741 const Handle(OpenGl_Workspace)& theWorkspace,
93e572ca 742 const Graphic3d_CView& /*ACView*/,
a174a3c5 743 const Aspect_CLayer2d& ACLayer)
2166f0fa 744{
ca3c13d1 745#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
746 if (&ACLayer == NULL
747 || ACLayer.ptrLayer == NULL
748 || ACLayer.ptrLayer->listIndex == 0) return;
749
c827ea3a 750 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
751
529afc1a
K
752 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
753 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
2166f0fa 754
c827ea3a 755 aContext->WorldViewState.Push();
756 aContext->ProjectionState.Push();
2166f0fa 757
c827ea3a 758 aContext->WorldViewState.SetIdentity();
759 aContext->ProjectionState.SetIdentity();
760
761 aContext->ApplyWorldViewMatrix();
762 aContext->ApplyProjectionMatrix();
2166f0fa
SK
763
764 if (!ACLayer.sizeDependent)
e3573bb9 765 aContext->core11fwd->glViewport (0, 0, dispWidth, dispHeight);
2166f0fa
SK
766
767 float left = ACLayer.ortho[0];
768 float right = ACLayer.ortho[1];
769 float bottom = ACLayer.ortho[2];
770 float top = ACLayer.ortho[3];
771
772 int attach = ACLayer.attach;
773
e3573bb9 774 const float ratio = !ACLayer.sizeDependent
775 ? float(dispWidth) / float(dispHeight)
776 : float(theWorkspace->Width()) / float(theWorkspace->Height());
2166f0fa
SK
777
778 float delta;
c9d4eb9d 779 if (ratio >= 1.0) {
2166f0fa
SK
780 delta = (float )((top - bottom)/2.0);
781 switch (attach) {
782 case 0: /* Aspect_TOC_BOTTOM_LEFT */
783 top = bottom + 2*delta/ratio;
784 break;
785 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
786 top = bottom + 2*delta/ratio;
787 break;
788 case 2: /* Aspect_TOC_TOP_LEFT */
789 bottom = top - 2*delta/ratio;
790 break;
791 case 3: /* Aspect_TOC_TOP_RIGHT */
792 bottom = top - 2*delta/ratio;
793 break;
794 }
795 }
c9d4eb9d 796 else {
2166f0fa
SK
797 delta = (float )((right - left)/2.0);
798 switch (attach) {
799 case 0: /* Aspect_TOC_BOTTOM_LEFT */
800 right = left + 2*delta*ratio;
801 break;
802 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
803 left = right - 2*delta*ratio;
804 break;
805 case 2: /* Aspect_TOC_TOP_LEFT */
806 right = left + 2*delta*ratio;
807 break;
808 case 3: /* Aspect_TOC_TOP_RIGHT */
809 left = right - 2*delta*ratio;
810 break;
811 }
812 }
813
a174a3c5 814#ifdef _WIN32
2166f0fa 815 // Check printer context that exists only for print operation
a174a3c5 816 if (!thePrintContext.IsNull())
2166f0fa
SK
817 {
818 // additional transformation matrix could be applied to
819 // render only those parts of viewport that will be
820 // passed to a printer as a current "frame" to provide
821 // tiling; scaling of graphics by matrix helps render a
822 // part of a view (frame) in same viewport, but with higher
823 // resolution
c827ea3a 824
825 // set printing scale/tiling transformation
826 aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation());
827 aContext->ApplyProjectionMatrix();
2166f0fa
SK
828
829 // printing operation also assumes other viewport dimension
830 // to comply with transformation matrix or graphics scaling
831 // factors for tiling for layer redraw
832 GLsizei anViewportX = 0;
833 GLsizei anViewportY = 0;
a174a3c5 834 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
2166f0fa 835 if (anViewportX != 0 && anViewportY != 0)
e3573bb9 836 aContext->core11fwd->glViewport (0, 0, anViewportX, anViewportY);
2166f0fa 837 }
3c3131a0 838#endif
2166f0fa
SK
839
840 glOrtho (left, right, bottom, top, -1.0, 1.0);
841
2166f0fa
SK
842 glPushAttrib (
843 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
844 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
c9d4eb9d 845
2166f0fa 846 glDisable (GL_DEPTH_TEST);
c9d4eb9d 847 glDisable (GL_TEXTURE_1D);
848 glDisable (GL_TEXTURE_2D);
849 glDisable (GL_LIGHTING);
850
851 // TODO: Obsolete code, the display list is always empty now, to be removed
2166f0fa
SK
852 glCallList (ACLayer.ptrLayer->listIndex);
853
854 //calling dynamic render of LayerItems
855 if ( ACLayer.ptrLayer->layerData )
856 {
30f0ad28 857 InitLayerProp (ACLayer.ptrLayer->listIndex);
2166f0fa 858 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
30f0ad28 859 InitLayerProp (0);
2166f0fa
SK
860 }
861
862 glPopAttrib ();
863
c827ea3a 864 aContext->WorldViewState.Pop();
865 aContext->ProjectionState.Pop();
2166f0fa 866
c827ea3a 867 aContext->ApplyProjectionMatrix();
868 aContext->ApplyWorldViewMatrix();
2166f0fa 869
2166f0fa 870 if (!ACLayer.sizeDependent)
e3573bb9 871 aContext->core11fwd->glViewport (0, 0, theWorkspace->Width(), theWorkspace->Height());
2166f0fa
SK
872
873 glFlush ();
ca3c13d1 874#endif
2166f0fa
SK
875}
876
877/*----------------------------------------------------------------------*/
878
a89742cf 879void OpenGl_View::RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
880{
881 // display global trihedron
882 if (myTrihedron != NULL)
883 {
884 myTrihedron->Render (theWorkspace);
885 }
886 if (myGraduatedTrihedron != NULL)
887 {
888 myGraduatedTrihedron->Render (theWorkspace);
889 }
890}
891
892/*----------------------------------------------------------------------*/
893
2166f0fa 894//call_togl_create_bg_texture
3c3131a0 895void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
896 const Aspect_FillMethod theFillStyle)
2166f0fa 897{
3c3131a0 898 if (myBgTexture.TexId != 0)
2166f0fa 899 {
3c3131a0 900 // delete existing texture
901 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
2166f0fa
SK
902 myBgTexture.TexId = 0;
903 }
904
3c3131a0 905 // load image from file
906 Image_AlienPixMap anImageLoaded;
907 if (!anImageLoaded.Load (theFilePath))
908 {
909 return;
910 }
911
912 Image_PixMap anImage;
913 if (anImageLoaded.RowExtraBytes() == 0 &&
914 (anImageLoaded.Format() == Image_PixMap::ImgRGB
915 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
916 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
2166f0fa 917 {
3c3131a0 918 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
919 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
920 }
921 else
922 {
923 // convert image to RGB format
924 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
925 {
926 return;
927 }
928
929 anImage.SetTopDown (false);
3c3131a0 930 Quantity_Color aSrcColor;
931 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
932 {
933 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
2166f0fa 934 {
6a7d83c4 935 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
ca0c0b11 936 Image_ColorRGB& aColor = anImage.ChangeValue<Image_ColorRGB> (aRow, aCol);
8263fcd3 937 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
938 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
939 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
2166f0fa 940 }
3c3131a0 941 }
942 anImageLoaded.Clear();
943 }
2166f0fa 944
3c3131a0 945 // create MipMapped texture
946 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
2166f0fa 947
3c3131a0 948 GLuint aTextureId = 0;
949 glGenTextures (1, &aTextureId);
950 glBindTexture (GL_TEXTURE_2D, aTextureId);
2166f0fa 951
3c3131a0 952 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
953 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
954 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
955 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
2166f0fa 956
3c3131a0 957 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
ca3c13d1 958
959#if !defined(GL_ES_VERSION_2_0)
3c3131a0 960 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
961 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
962 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
ca3c13d1 963#endif
2166f0fa 964
3c3131a0 965 myBgTexture.TexId = aTextureId;
966 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
967 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
968 myBgTexture.Style = theFillStyle;
2166f0fa
SK
969}
970
971/*----------------------------------------------------------------------*/
972
973//call_togl_set_bg_texture_style
974void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
975{
f8b2ed36 976 myBgTexture.Style = AFillStyle;
2166f0fa
SK
977}
978
979/*----------------------------------------------------------------------*/
980
981//call_togl_gradient_background
982void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
983 const Quantity_Color& AColor2,
984 const Aspect_GradientFillMethod AType)
985{
986 Standard_Real R,G,B;
987 AColor1.Values( R, G, B, Quantity_TOC_RGB );
988 myBgGradient.color1.rgb[0] = ( Tfloat )R;
989 myBgGradient.color1.rgb[1] = ( Tfloat )G;
990 myBgGradient.color1.rgb[2] = ( Tfloat )B;
991 myBgGradient.color1.rgb[3] = 0.F;
992
993 AColor2.Values( R, G, B, Quantity_TOC_RGB );
994 myBgGradient.color2.rgb[0] = ( Tfloat )R;
995 myBgGradient.color2.rgb[1] = ( Tfloat )G;
996 myBgGradient.color2.rgb[2] = ( Tfloat )B;
997 myBgGradient.color2.rgb[3] = 0.F;
998
999 myBgGradient.type = AType;
1000}
1001
1002/*----------------------------------------------------------------------*/
1003
1004//call_togl_set_gradient_type
1005void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
1006{
f8b2ed36 1007 myBgGradient.type = AType;
2166f0fa
SK
1008}
1009
59f45b7c 1010//=======================================================================
1011//function : AddZLayer
3c3131a0 1012//purpose :
59f45b7c 1013//=======================================================================
1014
a1954302 1015void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
59f45b7c 1016{
1017 myZLayers.AddLayer (theLayerId);
1018}
1019
1020//=======================================================================
1021//function : RemoveZLayer
3c3131a0 1022//purpose :
59f45b7c 1023//=======================================================================
1024
a1954302 1025void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
59f45b7c 1026{
1027 myZLayers.RemoveLayer (theLayerId);
1028}
1029
1030//=======================================================================
1031//function : DisplayStructure
3c3131a0 1032//purpose :
59f45b7c 1033//=======================================================================
1034
a1954302 1035void OpenGl_View::DisplayStructure (const Handle(Graphic3d_Structure)& theStructure,
1036 const Standard_Integer thePriority)
59f45b7c 1037{
a1954302 1038 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure->CStructure().operator->());
1039 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
1040 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
59f45b7c 1041}
1042
679ecdee 1043//=======================================================================
1044//function : DisplayImmediateStructure
1045//purpose :
1046//=======================================================================
1047
a1954302 1048void OpenGl_View::DisplayImmediateStructure (const Handle(Graphic3d_Structure)& theStructure)
679ecdee 1049{
a1954302 1050 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure->CStructure().operator->());
679ecdee 1051 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1052 anIter.More(); anIter.Next())
1053 {
a1954302 1054 if (anIter.Value() == aStruct)
679ecdee 1055 {
1056 return;
1057 }
1058 }
1059
a1954302 1060 myImmediateList.Append (aStruct);
679ecdee 1061}
1062
59f45b7c 1063//=======================================================================
1064//function : EraseStructure
3c3131a0 1065//purpose :
59f45b7c 1066//=======================================================================
1067
a1954302 1068void OpenGl_View::EraseStructure (const Handle(Graphic3d_Structure)& theStructure)
59f45b7c 1069{
a1954302 1070 myZLayers.RemoveStructure (theStructure);
59f45b7c 1071}
1072
1073//=======================================================================
679ecdee 1074//function : EraseImmediateStructure
1075//purpose :
1076//=======================================================================
1077
1078void OpenGl_View::EraseImmediateStructure (const OpenGl_Structure* theStructure)
1079{
1080 for (OpenGl_SequenceOfStructure::Iterator anIter (myImmediateList);
1081 anIter.More(); anIter.Next())
1082 {
1083 if (anIter.Value() == theStructure)
1084 {
1085 myImmediateList.Remove (anIter);
1086 return;
1087 }
1088 }
1089}
1090
1091//=======================================================================
59f45b7c 1092//function : ChangeZLayer
1093//purpose :
1094//=======================================================================
1095
a1954302 1096void OpenGl_View::ChangeZLayer (const OpenGl_Structure* theStructure,
1097 const Graphic3d_ZLayerId theNewLayerId)
59f45b7c 1098{
a1954302 1099 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
59f45b7c 1100 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);
1101}
b5ac8292 1102
c5751993 1103//=======================================================================
1104//function : SetZLayerSettings
1105//purpose :
1106//=======================================================================
a1954302 1107void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
1108 const Graphic3d_ZLayerSettings& theSettings)
c5751993 1109{
a1954302 1110 myZLayers.SetLayerSettings (theLayerId, theSettings);
c5751993 1111}
1112
b7cd4ba7 1113//=======================================================================
1114//function : ChangePriority
1115//purpose :
1116//=======================================================================
1117void OpenGl_View::ChangePriority (const OpenGl_Structure *theStructure,
1118 const Standard_Integer theNewPriority)
1119{
a1954302 1120 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
b7cd4ba7 1121 myZLayers.ChangePriority (theStructure, aLayerId, theNewPriority);
1122}
c5751993 1123
b5ac8292 1124//=======================================================================
1125//function : RedrawScene
1126//purpose :
1127//=======================================================================
1128
1129void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
a1954302 1130 const Handle(OpenGl_Workspace)& theWorkspace,
1131 const Standard_Boolean theToDrawImmediate)
b5ac8292 1132{
1133 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
1134
1135 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1136 {
1137 Handle(Graphic3d_ClipPlane) aPlaneBack;
1138 Handle(Graphic3d_ClipPlane) aPlaneFront;
1139
1140 if (myZClip.Back.IsOn)
1141 {
1142 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1143 if (myCamera->ZFar() < aClipBackConverted)
1144 {
1145 aClipBackConverted = myCamera->ZFar();
1146 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
1147 }
1148 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
1149 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1150 }
1151
1152 if (myZClip.Front.IsOn)
1153 {
1154 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1155 if (myCamera->ZNear() > aClipFrontConverted)
1156 {
1157 aClipFrontConverted = myCamera->ZNear();
1158 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1159 }
1160 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1161 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1162 }
1163
cddbf6a9 1164 // Specify slicing planes with identity transformation
b5ac8292 1165 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1166 {
1167 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1168 if (!aPlaneBack.IsNull())
1169 {
1170 aSlicingPlanes.Append (aPlaneBack);
1171 }
1172
1173 if (!aPlaneFront.IsNull())
1174 {
1175 aSlicingPlanes.Append (aPlaneFront);
1176 }
1177
1178 // add planes at loaded view matrix state
1179 aContext->ChangeClipping().AddView (aSlicingPlanes, theWorkspace);
1180 }
1181 }
1182
b5ac8292 1183#ifdef _WIN32
c827ea3a 1184 // set printing scale/tiling transformation
b5ac8292 1185 if (!thePrintContext.IsNull())
1186 {
c827ea3a 1187 aContext->ProjectionState.Push();
1188 aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation() * aContext->ProjectionState.Current());
1189 aContext->ApplyProjectionMatrix();
b5ac8292 1190 }
ca3c13d1 1191#endif
b5ac8292 1192
cddbf6a9 1193 // Specify clipping planes in view transformation space
1194 if (!myClipPlanes.IsEmpty())
1195 {
1196 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1197 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1198 for (; aClippingIt.More(); aClippingIt.Next())
1199 {
1200 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1201 if (aClipPlane->IsOn())
1202 {
1203 aUserPlanes.Append (aClipPlane);
1204 }
1205 }
1206
1207 if (!aUserPlanes.IsEmpty())
1208 {
1209 aContext->ChangeClipping().AddWorld (aUserPlanes);
1210 }
1211
1212 if (!aContext->ShaderManager()->IsEmpty())
1213 {
1214 aContext->ShaderManager()->UpdateClippingState();
1215 }
1216 }
1217
ca3c13d1 1218#if !defined(GL_ES_VERSION_2_0)
b5ac8292 1219 // Apply Lights
1220 {
1221 // setup lights
1222 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1223 THE_DEFAULT_AMBIENT[1],
1224 THE_DEFAULT_AMBIENT[2],
1225 THE_DEFAULT_AMBIENT[3]);
1226 GLenum aLightGlId = GL_LIGHT0;
1227 for (OpenGl_ListOfLight::Iterator aLightIt (myLights);
1228 aLightIt.More(); aLightIt.Next())
1229 {
c827ea3a 1230 bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, theWorkspace);
b5ac8292 1231 }
1232
1233 // apply accumulated ambient color
1234 anAmbientColor.a() = 1.0f;
1235 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1236
1237 if (aLightGlId != GL_LIGHT0)
1238 {
1239 glEnable (GL_LIGHTING);
1240 }
1241 // switch off unused lights
1242 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1243 {
1244 glDisable (aLightGlId);
1245 }
1246 }
ca3c13d1 1247#endif
b5ac8292 1248
1249 // Clear status bitfields
1250 theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1251
1252 // Added PCT for handling of textures
1253 switch (mySurfaceDetail)
1254 {
1255 case Visual3d_TOD_NONE:
1256 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1257 theWorkspace->DisableTexture();
1258 // Render the view
a1954302 1259 RenderStructs (theWorkspace, theToDrawImmediate);
b5ac8292 1260 break;
1261
1262 case Visual3d_TOD_ENVIRONMENT:
1263 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1264 theWorkspace->EnableTexture (myTextureEnv);
1265 // Render the view
a1954302 1266 RenderStructs (theWorkspace, theToDrawImmediate);
b5ac8292 1267 theWorkspace->DisableTexture();
1268 break;
1269
1270 case Visual3d_TOD_ALL:
1271 // First pass
1272 theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1273 // Render the view
a1954302 1274 RenderStructs (theWorkspace, theToDrawImmediate);
b5ac8292 1275 theWorkspace->DisableTexture();
1276
1277 // Second pass
1278 if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1279 {
1280 theWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1281 theWorkspace->EnableTexture (myTextureEnv);
1282
1283 // Remember OpenGl properties
ca3c13d1 1284 GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
b5ac8292 1285 GLint aSaveZbuffFunc;
1286 GLboolean aSaveZbuffWrite;
1287 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1288 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
ca3c13d1 1289 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 1290 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1291 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
ca3c13d1 1292 #endif
b5ac8292 1293 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1294 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1295
1296 // Change the properties for second rendering pass
1297 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1298 glEnable (GL_BLEND);
1299
1300 glDepthFunc (GL_EQUAL);
1301 glDepthMask (GL_FALSE);
1302 glEnable (GL_DEPTH_TEST);
1303
1304 theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1305
1306 // Render the view
a1954302 1307 RenderStructs (theWorkspace, theToDrawImmediate);
b5ac8292 1308 theWorkspace->DisableTexture();
1309
1310 // Restore properties back
1311 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1312 if (!wasBlendEnabled)
1313 glDisable (GL_BLEND);
1314
1315 glDepthFunc (aSaveZbuffFunc);
1316 glDepthMask (aSaveZbuffWrite);
1317 if (!wasZbuffEnabled)
1318 glDisable (GL_DEPTH_FUNC);
1319 }
1320 break;
1321 }
c827ea3a 1322
1323 // Apply restored view matrix.
1324 aContext->ApplyWorldViewMatrix();
1325
1326#ifdef _WIN32
1327 // set printing scale/tiling transformation
1328 if (!thePrintContext.IsNull())
1329 {
1330 aContext->ProjectionState.Pop();
1331 aContext->ApplyProjectionMatrix();
1332 }
1333#endif
1334
b5ac8292 1335}