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