1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
23 #include <OpenGl_GlCore11.hxx>
24 #include <OpenGl_tgl_funcs.hxx>
26 #include <Image_AlienPixMap.hxx>
27 #include <Visual3d_Layer.hxx>
29 #include <NCollection_Mat4.hxx>
31 #include <OpenGl_AspectLine.hxx>
32 #include <OpenGl_Context.hxx>
33 #include <OpenGl_Display.hxx>
34 #include <OpenGl_Matrix.hxx>
35 #include <OpenGl_Workspace.hxx>
36 #include <OpenGl_View.hxx>
37 #include <OpenGl_Trihedron.hxx>
38 #include <OpenGl_GraduatedTrihedron.hxx>
39 #include <OpenGl_PrinterContext.hxx>
40 #include <OpenGl_ShaderManager.hxx>
41 #include <OpenGl_ShaderProgram.hxx>
42 #include <OpenGl_Structure.hxx>
49 static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
50 static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f };
51 static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f;
52 static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f;
56 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
58 /*----------------------------------------------------------------------*/
60 struct OPENGL_CLIP_PLANE
67 /*----------------------------------------------------------------------*/
72 /*-----------------------------------------------------------------*/
76 static void bind_light (const OpenGl_Light& theLight,
78 Graphic3d_Vec4& theAmbientColor)
80 // Only 8 lights in OpenGL...
81 if (theLightGlId > GL_LIGHT7)
86 if (theLight.Type == Visual3d_TOLS_AMBIENT)
88 // add RGBA intensity of the ambient light
89 theAmbientColor += theLight.Color;
93 // the light is a headlight?
94 GLint aMatrixModeOld = 0;
95 if (theLight.IsHeadlight)
97 glGetIntegerv (GL_MATRIX_MODE, &aMatrixModeOld);
98 glMatrixMode (GL_MODELVIEW);
104 switch (theLight.Type)
106 case Visual3d_TOLS_DIRECTIONAL:
108 // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one
109 const OpenGl_Vec4 anInfDir = -theLight.Direction;
111 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE.
112 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
113 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
114 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
115 glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData());
116 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
117 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
118 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
121 case Visual3d_TOLS_POSITIONAL:
123 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE
124 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
125 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
126 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
127 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
128 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
129 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
130 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
131 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
132 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
133 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0);
136 case Visual3d_TOLS_SPOT:
138 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
139 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
140 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
141 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
142 glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData());
143 glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f);
144 glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI));
145 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
146 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
147 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f);
152 // restore matrix in case of headlight
153 if (theLight.IsHeadlight)
156 glMatrixMode (aMatrixModeOld);
159 glEnable (theLightGlId++);
162 /*----------------------------------------------------------------------*/
167 static void call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
168 float *ox, float *oy, float *oz );
169 static void call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c);
171 /*----------------------------------------------------------------------*/
177 * Evaluates orientation matrix.
179 /* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
180 void call_func_eval_ori_matrix3 (const point3* vrp, // view reference point
181 const vec3* vpn, // view plane normal
182 const vec3* vup, // view up vector
184 float mout[4][4]) // OUT view orientation matrix
187 /* Translate to VRP then change the basis.
188 * The old basis is: e1 = < 1, 0, 0>, e2 = < 0, 1, 0>, e3 = < 0, 0, 1>.
189 * The new basis is: ("x" means cross product)
191 * e1' = VUP x VPN / |VUP x VPN|
193 * Therefore the transform from old to new is x' = TAx, where:
195 * | e1'x e2'x e3'x 0 | | 1 0 0 0 |
196 * A = | e1'y e2'y e3'y 0 |, T = | 0 1 0 0 |
197 * | e1'z e2'z e3'z 0 | | 0 0 1 0 |
198 * | 0 0 0 1 | | -vrp.x -vrp.y -vrp.z 1 |
203 * These ei's are really ei primes.
205 register float (*m)[4][4];
206 point3 e1, e2, e3, e4;
210 * e1' = VUP x VPN / |VUP x VPN|, but do the division later.
212 e1.x = vup->delta_y * vpn->delta_z - vup->delta_z * vpn->delta_y;
213 e1.y = vup->delta_z * vpn->delta_x - vup->delta_x * vpn->delta_z;
214 e1.z = vup->delta_x * vpn->delta_y - vup->delta_y * vpn->delta_x;
215 s = sqrt( e1.x * e1.x + e1.y * e1.y + e1.z * e1.z);
219 v = sqrt( e3.x * e3.x + e3.y * e3.y + e3.z * e3.z);
221 * Check for vup and vpn colinear (zero dot product).
223 if ((s > -EPSI) && (s < EPSI))
227 * Check for a normal vector not null.
229 if ((v > -EPSI) && (v < EPSI))
247 e2.x = e3.y * e1.z - e3.z * e1.y;
248 e2.y = e3.z * e1.x - e3.x * e1.z;
249 e2.z = e3.x * e1.y - e3.y * e1.x;
251 * Add the translation
253 e4.x = -( e1.x * vrp->x + e1.y * vrp->y + e1.z * vrp->z);
254 e4.y = -( e2.x * vrp->x + e2.y * vrp->y + e2.z * vrp->z);
255 e4.z = -( e3.x * vrp->x + e3.y * vrp->y + e3.z * vrp->z);
257 * Homogeneous entries
259 * | e1.x e2.x e3.x 0.0 | | 1 0 0 0 |
260 * | e1.y e2.y e3.y 0.0 | * | 0 1 0 0 |
261 * | e1.z e2.z e3.z 0.0 | | a b 1 c |
262 * | e4.x e4.y e4.z 1.0 | | 0 0 0 1 |
265 m = (float (*)[4][4])mout;
270 (*m)[0][3] = ( float )0.0;
275 (*m)[1][3] = ( float )0.0;
280 (*m)[2][3] = ( float )0.0;
285 (*m)[3][3] = ( float )1.0;
291 /*----------------------------------------------------------------------*/
293 * Evaluates mapping matrix.
295 /* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
296 void call_func_eval_map_matrix3(
307 matrix3 aux_mat1, aux_mat2, aux_mat3;
313 Spar[i][j] = Sper[i][j] = aux_mat1[i][j] = aux_mat2[i][j] =
314 aux_mat3[i][j] = Tper[i][j] = Tpar[i][j] = Tprp[i][j] =
315 Shear[i][j] = Scale[i][j] = ( float )(i == j);
317 Prp.x = Map->proj_ref_point.x;
318 Prp.y = Map->proj_ref_point.y;
319 Prp.z = Map->proj_ref_point.z;
324 if (Map->proj_type == TYPE_PARAL)
330 float cx, cy, gx, gy, xsf, ysf, zsf;
332 float dopx, dopy, dopz;
333 matrix3 tmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
334 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
335 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
336 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
337 matrix3 smat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
338 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
339 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
340 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
341 matrix3 shmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
342 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
343 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
344 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
345 matrix3 tshmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
346 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
347 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
348 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
351 cx = Map->win.x_min + Map->win.x_max, cx /= ( float )2.0;
352 cy = Map->win.y_min + Map->win.y_max, cy /= ( float )2.0;
354 gx = 2.0/ (Map->win.x_max - Map->win.x_min);
355 gy = 2.0/ (Map->win.y_max - Map->win.y_min);
359 tmat[2][3] = (Map->front_plane + Map->back_plane)/(Map->front_plane - Map->back_plane);
363 smat[2][2] = -2./(Map->front_plane - Map->back_plane);
371 shmat[0][2] = -(dopx/dopz);
372 shmat[1][2] = -(dopy/dopz);
374 /* multiply to obtain mapping matrix */
375 call_util_mat_mul( tmat, shmat, tshmat );
376 call_util_mat_mul( smat, tshmat, mat );
382 Map->proj_vp.z_min = ( float )0.0;
383 Map->proj_vp.z_max = ( float )1.0;
386 /* Shear matrix calculation */
387 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0;
388 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0;
389 if(Prp.z == Map->view_plane){
390 /* Projection reference point is on the view plane */
394 Shear[2][0] = ( float )(-1.0) * ((Prp.x-umid)/(Prp.z-Map->view_plane));
395 Shear[2][1] = ( float )(-1.0) * ((Prp.y-vmid)/(Prp.z-Map->view_plane));
398 * Calculate the lower left coordinate of the view plane
399 * after the Shearing Transformation.
401 call_util_apply_trans2(Map->win.x_min, Map->win.y_min,
402 Map->view_plane, Shear, &(temp.x), &(temp.y), &(temp.z));
404 /* Translate the back plane to the origin */
405 Tpar[3][0] = ( float )(-1.0) * temp.x;
406 Tpar[3][1] = ( float )(-1.0) * temp.y;
407 Tpar[3][2] = ( float )(-1.0) * Map->back_plane;
409 call_util_mat_mul(Shear, Tpar, aux_mat1);
411 /* Calculation of Scaling transformation */
412 Spar[0][0] = ( float )1.0 / (Map->win.x_max - Map->win.x_min);
413 Spar[1][1] = ( float )1.0 / (Map->win.y_max - Map->win.y_min);
414 Spar[2][2] = ( float )1.0 / (Map->front_plane - Map->back_plane );
415 call_util_mat_mul (aux_mat1, Spar, aux_mat2);
416 /* Atlast we transformed view volume to NPC */
418 /* Translate and scale the view plane to projection view port */
419 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
420 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
421 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
422 Map->proj_vp.x_min > Map->proj_vp.x_max ||
423 Map->proj_vp.y_min > Map->proj_vp.y_max ||
424 Map->proj_vp.z_min > Map->proj_vp.z_max){
430 aux_mat1[i][j] = (float)(i==j);
431 aux_mat1[0][0] = Map->proj_vp.x_max-Map->proj_vp.x_min;
432 aux_mat1[1][1] = Map->proj_vp.y_max-Map->proj_vp.y_min;
433 aux_mat1[2][2] = Map->proj_vp.z_max-Map->proj_vp.z_min;
434 aux_mat1[3][0] = Map->proj_vp.x_min;
435 aux_mat1[3][1] = Map->proj_vp.y_min;
436 aux_mat1[3][2] = Map->proj_vp.z_min;
437 call_util_mat_mul (aux_mat2, aux_mat1, mat);
445 else if (Map->proj_type == TYPE_PERSPECT)
452 Map->proj_vp.z_min = ( float )0.0;
453 Map->proj_vp.z_max = ( float )1.0;
457 F = Map->front_plane;
460 if(Prp.z == Map->view_plane){
461 /* Centre of Projection is on the view plane */
465 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
466 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
467 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
468 Map->proj_vp.x_min > Map->proj_vp.x_max ||
469 Map->proj_vp.y_min > Map->proj_vp.y_max ||
470 Map->proj_vp.z_min > Map->proj_vp.z_max ||
476 /* This is the transformation to move VRC to Center Of Projection */
477 Tprp[3][0] = ( float )(-1.0)*Prp.x;
478 Tprp[3][1] = ( float )(-1.0)*Prp.y;
479 Tprp[3][2] = ( float )(-1.0)*Prp.z;
481 /* Calculation of Shear matrix */
482 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0-Prp.x;
483 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0-Prp.y;
484 Shear[2][0] = ( float )(-1.0)*umid/(Map->view_plane-Prp.z);
485 Shear[2][1] = ( float )(-1.0)*vmid/(Map->view_plane-Prp.z);
486 call_util_mat_mul(Tprp, Shear, aux_mat3);
488 /* Scale the view volume to canonical view volume
489 * Centre of projection at origin.
490 * 0 <= N <= -1, -0.5 <= U <= 0.5, -0.5 <= V <= 0.5
492 Scale[0][0] = (( float )(-1.0)*Prp.z+V)/
493 ((Map->win.x_max-Map->win.x_min)*(( float )(-1.0)*Prp.z+B));
494 Scale[1][1] = (( float )(-1.0)*Prp.z+V)/
495 ((Map->win.y_max-Map->win.y_min)*(( float )(-1.0)*Prp.z+B));
496 Scale[2][2] = ( float )(-1.0) / (( float )(-1.0)*Prp.z+B);
498 call_util_mat_mul(aux_mat3, Scale, aux_mat1);
501 * Transform the Perspective view volume into
502 * Parallel view volume.
503 * Lower left coordinate: (-0.5,-0.5, -1)
504 * Upper right coordinate: (0.5, 0.5, 1.0)
506 Zvmin = ( float )(-1.0*(-1.0*Prp.z+F)/(-1.0*Prp.z+B));
507 aux_mat2[2][2] = ( float )1.0/(( float )1.0+Zvmin);
508 aux_mat2[2][3] = ( float )(-1.0);
509 aux_mat2[3][2] = ( float )(-1.0)*Zvmin*aux_mat2[2][2];
510 aux_mat2[3][3] = ( float )0.0;
511 call_util_mat_mul(aux_mat1, aux_mat2, Shear);
515 aux_mat1[i][j] = aux_mat2[i][j] = (float)(i==j);
517 /* Translate and scale the view plane to projection view port */
518 aux_mat2[0][0] = (Map->proj_vp.x_max-Map->proj_vp.x_min);
519 aux_mat2[1][1] = (Map->proj_vp.y_max-Map->proj_vp.y_min);
520 aux_mat2[2][2] = (Map->proj_vp.z_max-Map->proj_vp.z_min);
521 aux_mat2[3][0] = aux_mat2[0][0]/( float )2.0+Map->proj_vp.x_min;
522 aux_mat2[3][1] = aux_mat2[1][1]/( float )2.0+Map->proj_vp.y_min;
523 aux_mat2[3][2] = aux_mat2[2][2]+Map->proj_vp.z_min;
524 call_util_mat_mul (Shear, aux_mat2, mat);
532 /*----------------------------------------------------------------------*/
535 call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
536 float *ox, float *oy, float *oz )
539 *ox = ix*mat[0][0]+iy*mat[1][0]+iz*mat[2][0]+mat[3][0];
540 *oy = ix*mat[0][1]+iy*mat[1][1]+iz*mat[2][1]+mat[3][1];
541 *oz = ix*mat[0][2]+iy*mat[1][2]+iz*mat[2][2]+mat[3][2];
542 temp = ix * mat[0][3]+iy * mat[1][3]+iz * mat[2][3]+mat[3][3];
548 /*----------------------------------------------------------------------*/
551 call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c)
557 for (mat_c[i][j] = ( float )0.0,k=0; k<4; k++)
558 mat_c[i][j] += mat_a[i][k] * mat_b[k][j];
561 /*----------------------------------------------------------------------*/
563 void OpenGl_View::DrawBackground (const Handle(OpenGl_Workspace) &AWorkspace)
565 /////////////////////////////////////////////////////////////////////////////
566 // Step 1: Prepare for redraw
569 if ( (AWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
570 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
572 const Standard_Integer aViewWidth = AWorkspace->Width();
573 const Standard_Integer aViewHeight = AWorkspace->Height();
575 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
577 glMatrixMode( GL_PROJECTION );
580 glMatrixMode( GL_MODELVIEW );
584 if ( glIsEnabled( GL_DEPTH_TEST ) )
585 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
587 // drawing bg gradient if:
588 // - gradient fill type is not Aspect_GFM_NONE and
589 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
590 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
591 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
592 myBgTexture.Style == Aspect_FM_NONE ) )
594 Tfloat* corner1 = 0;/* -1,-1*/
595 Tfloat* corner2 = 0;/* 1,-1*/
596 Tfloat* corner3 = 0;/* 1, 1*/
597 Tfloat* corner4 = 0;/* -1, 1*/
601 switch( myBgGradient.type )
604 corner1 = myBgGradient.color1.rgb;
605 corner2 = myBgGradient.color2.rgb;
606 corner3 = myBgGradient.color2.rgb;
607 corner4 = myBgGradient.color1.rgb;
610 corner1 = myBgGradient.color2.rgb;
611 corner2 = myBgGradient.color2.rgb;
612 corner3 = myBgGradient.color1.rgb;
613 corner4 = myBgGradient.color1.rgb;
615 case Aspect_GFM_DIAG1:
616 corner2 = myBgGradient.color2.rgb;
617 corner4 = myBgGradient.color1.rgb;
618 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
619 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
620 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
624 case Aspect_GFM_DIAG2:
625 corner1 = myBgGradient.color2.rgb;
626 corner3 = myBgGradient.color1.rgb;
627 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
628 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
629 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
633 case Aspect_GFM_CORNER1:
634 corner1 = myBgGradient.color2.rgb;
635 corner2 = myBgGradient.color2.rgb;
636 corner3 = myBgGradient.color2.rgb;
637 corner4 = myBgGradient.color1.rgb;
639 case Aspect_GFM_CORNER2:
640 corner1 = myBgGradient.color2.rgb;
641 corner2 = myBgGradient.color2.rgb;
642 corner3 = myBgGradient.color1.rgb;
643 corner4 = myBgGradient.color2.rgb;
645 case Aspect_GFM_CORNER3:
646 corner1 = myBgGradient.color2.rgb;
647 corner2 = myBgGradient.color1.rgb;
648 corner3 = myBgGradient.color2.rgb;
649 corner4 = myBgGradient.color2.rgb;
651 case Aspect_GFM_CORNER4:
652 corner1 = myBgGradient.color1.rgb;
653 corner2 = myBgGradient.color2.rgb;
654 corner3 = myBgGradient.color2.rgb;
655 corner4 = myBgGradient.color2.rgb;
658 //printf("gradient background type not right\n");
662 // Save GL parameters
663 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
666 glGetIntegerv( GL_SHADE_MODEL, &curSM );
667 if ( curSM != GL_SMOOTH )
668 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
670 glBegin(GL_TRIANGLE_FAN);
671 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
673 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
674 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
675 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
676 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
678 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
680 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
681 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
682 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
683 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
687 // Restore GL parameters
688 if ( curSM != GL_SMOOTH )
689 glShadeModel( curSM );
691 // drawing bg image if:
692 // - it is defined and
693 // - fill type is not Aspect_FM_NONE
694 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
696 GLfloat texX_range = 1.F; // texture <s> coordinate
697 GLfloat texY_range = 1.F; // texture <t> coordinate
699 // Set up for stretching or tiling
700 GLfloat x_offset, y_offset;
701 if ( myBgTexture.Style == Aspect_FM_CENTERED )
703 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
704 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
710 if ( myBgTexture.Style == Aspect_FM_TILED )
712 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
713 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
717 // OCCT issue 0023000: Improve the way the gradient and textured
718 // background is managed in 3d viewer (note 0020339)
719 // Setting this coefficient to -1.F allows to tile textures relatively
720 // to the top-left corner of the view (value 1.F corresponds to the
721 // initial behaviour - tiling from the bottom-left corner)
722 GLfloat aCoef = -1.F;
724 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
725 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
727 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
729 glColor3fv( AWorkspace->BackgroundColor().rgb );
730 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
732 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
733 // is simply ignored, and negative multiplier is here for convenience only
734 // and does not result e.g. in texture mirroring
736 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
737 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
738 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
739 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
744 glMatrixMode( GL_PROJECTION );
746 glMatrixMode( GL_MODELVIEW );
748 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
750 if ( AWorkspace->UseZBuffer() )
751 glEnable( GL_DEPTH_TEST );
753 /* GL_DITHER on/off pour le trace */
754 if (AWorkspace->Dither())
755 glEnable (GL_DITHER);
757 glDisable (GL_DITHER);
761 /*----------------------------------------------------------------------*/
763 //call_func_redraw_all_structs_proc
764 void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
765 const Handle(OpenGl_Workspace) &AWorkspace,
766 const Graphic3d_CView& ACView,
767 const Aspect_CLayer2d& ACUnderLayer,
768 const Aspect_CLayer2d& ACOverLayer)
770 // Store and disable current clipping planes
771 const Handle(OpenGl_Context)& aContext = AWorkspace->GetGlContext();
772 const Standard_Integer aMaxClipPlanes = aContext->MaxClipPlanes();
773 const GLenum lastid = GL_CLIP_PLANE0 + aMaxClipPlanes;
774 OPENGL_CLIP_PLANE *oldPlanes = new OPENGL_CLIP_PLANE[aMaxClipPlanes];
775 OPENGL_CLIP_PLANE *ptrPlane = oldPlanes;
776 GLenum planeid = GL_CLIP_PLANE0;
777 for ( ; planeid < lastid; planeid++, ptrPlane++ )
779 glGetClipPlane( planeid, ptrPlane->Equation );
780 if ( ptrPlane->isEnabled )
782 glDisable( planeid );
783 ptrPlane->isEnabled = GL_TRUE;
787 ptrPlane->isEnabled = GL_FALSE;
791 // Set OCCT state uniform variables
792 const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
793 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
795 aManager->UpdateLightSourceStateTo (&myLights);
796 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
798 if (StateInfo (myCurrViewMappingState, aManager->ProjectionState().Index()) != myLastViewMappingState)
800 aManager->UpdateProjectionStateTo (myMappingMatrix);
801 myLastViewMappingState = StateInfo (myCurrViewMappingState, aManager->ProjectionState().Index());
803 if (StateInfo (myCurrOrientationState, aManager->WorldViewState().Index()) != myLastOrientationState)
805 aManager->UpdateWorldViewStateTo (myOrientationMatrix);
806 myLastOrientationState = StateInfo (myCurrOrientationState, aManager->WorldViewState().Index());
808 if (aManager->ModelWorldState().Index() == 0)
810 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
811 { 0.f, 1.f, 0.f, 0.f },
812 { 0.f, 0.f, 1.f, 0.f },
813 { 0.f, 0.f, 0.f, 1.f } };
815 aManager->UpdateModelWorldStateTo (aModelWorldState);
818 /////////////////////////////////////////////////////////////////////////////
819 // Step 1: Prepare for redraw
822 DrawBackground (AWorkspace);
824 // Switch off lighting by default
825 glDisable(GL_LIGHTING);
827 /////////////////////////////////////////////////////////////////////////////
828 // Step 2: Draw underlayer
829 RedrawLayer2d (thePrintContext, ACView, ACUnderLayer);
831 /////////////////////////////////////////////////////////////////////////////
832 // Step 3: Redraw main plane
834 // Setup face culling
835 GLboolean isCullFace = GL_FALSE;
838 isCullFace = glIsEnabled( GL_CULL_FACE );
839 if ( myBackfacing < 0 )
841 glEnable( GL_CULL_FACE );
842 glCullFace( GL_BACK );
845 glDisable( GL_CULL_FACE );
848 //TsmPushAttri(); /* save previous graphics context */
850 // if the view is scaled normal vectors are scaled to unit length for correct displaying of shaded objects
851 if(myExtra.scaleFactors[0] != 1.F ||
852 myExtra.scaleFactors[1] != 1.F ||
853 myExtra.scaleFactors[2] != 1.F)
854 glEnable(GL_NORMALIZE);
855 else if(glIsEnabled(GL_NORMALIZE))
856 glDisable(GL_NORMALIZE);
858 // Apply View Projection
859 // This routine activates the Projection matrix for a view.
861 glMatrixMode( GL_PROJECTION );
864 // add printing scale/tiling transformation
865 if (!thePrintContext.IsNull())
867 thePrintContext->LoadProjTransformation();
873 glMultMatrixf( (const GLfloat *) myMappingMatrix );
875 // Add translation necessary for the environnement mapping
876 if (mySurfaceDetail != Visual3d_TOD_NONE)
878 // OCC280: FitAll work incorrect for perspective view if the SurfaceDetail mode is V3d_TEX_ENVIRONMENT or V3d_TEX_ALL
879 // const GLfloat dep = vptr->vrep.extra.map.fpd * 0.5F;
880 const GLfloat dep = (myExtra.map.fpd + myExtra.map.bpd) * 0.5F;
881 glTranslatef(-dep*myExtra.vpn[0],-dep*myExtra.vpn[1],-dep*myExtra.vpn[2]);
885 AWorkspace->SetViewMatrix((const OpenGl_Matrix *)myOrientationMatrix);
888 While drawing after a clipplane has been defined and enabled, each vertex
889 is transformed to eye-coordinates, where it is dotted with the transformed
890 clipping plane equation. Eye-coordinate vertexes whose dot product with
891 the transformed clipping plane equation is positive or zero are in, and
892 require no clipping. Those eye-coordinate vertexes whose dot product is
893 negative are clipped. Because clipplane clipping is done in eye-
894 coordinates, changes to the projection matrix have no effect on its
897 A point and a normal are converted to a plane equation in the following manner:
912 D = -[Px,Py,Pz] dot |Nx|
921 const GLfloat ramp = myExtra.map.fpd - myExtra.map.bpd;
922 const GLfloat fog_start = myFog.Front * ramp - myExtra.map.fpd;
923 const GLfloat fog_end = myFog.Back * ramp - myExtra.map.fpd;
925 glFogi(GL_FOG_MODE, GL_LINEAR);
926 glFogf(GL_FOG_START, fog_start);
927 glFogf(GL_FOG_END, fog_end);
928 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
937 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
938 THE_DEFAULT_AMBIENT[1],
939 THE_DEFAULT_AMBIENT[2],
940 THE_DEFAULT_AMBIENT[3]);
941 GLenum aLightGlId = GL_LIGHT0;
942 for (OpenGl_ListOfLight::Iterator aLightIt (myLights);
943 aLightIt.More(); aLightIt.Next())
945 bind_light (aLightIt.Value(), aLightGlId, anAmbientColor);
948 // apply accumulated ambient color
949 anAmbientColor.a() = 1.0f;
950 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
952 if (aLightGlId != GL_LIGHT0)
954 glEnable (GL_LIGHTING);
956 // switch off unused lights
957 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
959 glDisable (aLightGlId);
963 // Apply InteriorShadingMethod
964 glShadeModel( myIntShadingMethod == TEL_SM_FLAT ? GL_FLAT : GL_SMOOTH );
966 // Apply clipping planes
968 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
970 const GLdouble ramp = myExtra.map.fpd - myExtra.map.bpd;
972 Handle(Graphic3d_ClipPlane) aPlaneBack;
973 Handle(Graphic3d_ClipPlane) aPlaneFront;
975 if (myZClip.Back.IsOn)
977 const GLdouble back = ramp * myZClip.Back.Limit + myExtra.map.bpd;
978 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, -back);
979 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
982 if (myZClip.Front.IsOn)
984 const GLdouble front = ramp * myZClip.Front.Limit + myExtra.map.bpd;
985 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, front);
986 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
989 // do some "memory allocation"-wise optimization
990 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
992 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
993 if (!aPlaneBack.IsNull())
995 aSlicingPlanes.Append (aPlaneBack);
998 if (!aPlaneFront.IsNull())
1000 aSlicingPlanes.Append (aPlaneFront);
1003 // add planes at loaded view matrix state
1004 aContext->ChangeClipping().AddView (aSlicingPlanes, AWorkspace);
1008 // Apply user clipping planes
1009 if (!myClipPlanes.IsEmpty())
1011 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1012 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1013 for (; aClippingIt.More(); aClippingIt.Next())
1015 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1016 if (aClipPlane->IsOn())
1018 aUserPlanes.Append (aClipPlane);
1022 if (!aUserPlanes.IsEmpty())
1024 // add planes at actual matrix state.
1025 aContext->ChangeClipping().AddWorld (aUserPlanes);
1029 if (!aManager->IsEmpty())
1031 aManager->UpdateClippingState();
1035 // Apply AntiAliasing
1038 AWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
1040 AWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
1043 // Clear status bitfields
1044 AWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1046 // Added PCT for handling of textures
1047 switch (mySurfaceDetail)
1049 case Visual3d_TOD_NONE:
1050 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1051 AWorkspace->DisableTexture();
1053 RenderStructs(AWorkspace);
1056 case Visual3d_TOD_ENVIRONMENT:
1057 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1058 AWorkspace->EnableTexture (myTextureEnv);
1060 RenderStructs(AWorkspace);
1061 AWorkspace->DisableTexture();
1064 case Visual3d_TOD_ALL:
1066 AWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1068 RenderStructs(AWorkspace);
1069 AWorkspace->DisableTexture();
1072 if (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1074 AWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1075 AWorkspace->EnableTexture (myTextureEnv);
1077 /* sauvegarde de quelques parametres OpenGL */
1078 GLint blend_dst, blend_src;
1081 glGetBooleanv(GL_DEPTH_WRITEMASK, &zbuff_w);
1082 glGetIntegerv(GL_DEPTH_FUNC, &zbuff_f);
1083 glGetIntegerv(GL_BLEND_DST, &blend_dst);
1084 glGetIntegerv(GL_BLEND_SRC, &blend_src);
1085 GLboolean zbuff_state = glIsEnabled(GL_DEPTH_TEST);
1086 GLboolean blend_state = glIsEnabled(GL_BLEND);
1088 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1091 glDepthFunc(GL_EQUAL);
1092 glDepthMask(GL_FALSE);
1093 glEnable(GL_DEPTH_TEST);
1095 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1098 RenderStructs(AWorkspace);
1099 AWorkspace->DisableTexture();
1101 /* restauration des parametres OpenGL */
1102 glBlendFunc(blend_src, blend_dst);
1103 if (!blend_state) glDisable(GL_BLEND);
1105 glDepthFunc(zbuff_f);
1106 glDepthMask(zbuff_w);
1107 if (!zbuff_state) glDisable(GL_DEPTH_FUNC);
1112 // Resetting GL parameters according to the default aspects
1113 // in order to synchronize GL state with the graphic driver state
1114 // before drawing auxiliary stuff (trihedrons, overlayer)
1115 // and invoking optional callbacks
1116 AWorkspace->ResetAppliedAspect();
1118 aContext->ChangeClipping().RemoveAll();
1120 if (!aManager->IsEmpty())
1122 aManager->ResetMaterialStates();
1123 aManager->RevertClippingState();
1125 // We need to disable (unbind) all shaders programs to ensure
1126 // that all objects without specified aspect will be drawn
1127 // correctly (such as background)
1128 OpenGl_ShaderProgram::Unbind (aContext);
1131 // display global trihedron
1132 if (myTrihedron != NULL)
1134 myTrihedron->Render (AWorkspace);
1136 if (myGraduatedTrihedron != NULL)
1138 myGraduatedTrihedron->Render (AWorkspace);
1141 // Restore face culling
1146 glEnable ( GL_CULL_FACE );
1147 glCullFace ( GL_BACK );
1150 glDisable ( GL_CULL_FACE );
1153 /////////////////////////////////////////////////////////////////////////////
1154 // Step 6: Draw overlayer
1155 const int aMode = 0;
1156 AWorkspace->DisplayCallback (ACView, (aMode | OCC_PRE_OVERLAY));
1158 RedrawLayer2d (thePrintContext, ACView, ACOverLayer);
1160 AWorkspace->DisplayCallback (ACView, aMode);
1162 // Restore clipping planes
1163 for ( ptrPlane = oldPlanes, planeid = GL_CLIP_PLANE0; planeid < lastid; planeid++, ptrPlane++ )
1165 glClipPlane( planeid, ptrPlane->Equation );
1166 if ( ptrPlane->isEnabled )
1167 glEnable( planeid );
1169 glDisable( planeid );
1174 /*----------------------------------------------------------------------*/
1176 //ExecuteViewDisplay
1177 void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
1179 if ( myZLayers.NbStructures() <= 0 )
1182 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
1184 //TsmPushAttri(); /* save previous graphics context */
1186 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
1188 const int antiAliasingMode = AWorkspace->GetDisplay()->AntiAliasingMode();
1190 if ( !myAntiAliasing )
1192 glDisable(GL_POINT_SMOOTH);
1193 glDisable(GL_LINE_SMOOTH);
1194 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
1195 glBlendFunc (GL_ONE, GL_ZERO);
1196 glDisable (GL_BLEND);
1200 glEnable(GL_POINT_SMOOTH);
1201 glEnable(GL_LINE_SMOOTH);
1202 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
1203 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1204 glEnable (GL_BLEND);
1208 myZLayers.Render (AWorkspace);
1210 //TsmPopAttri(); /* restore previous graphics context; before update lights */
1214 /*----------------------------------------------------------------------*/
1216 //call_togl_redraw_layer2d
1217 void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
1218 const Graphic3d_CView& ACView,
1219 const Aspect_CLayer2d& ACLayer)
1221 if (&ACLayer == NULL
1222 || ACLayer.ptrLayer == NULL
1223 || ACLayer.ptrLayer->listIndex == 0) return;
1225 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
1226 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
1228 glMatrixMode( GL_MODELVIEW );
1232 glMatrixMode (GL_PROJECTION);
1236 if (!ACLayer.sizeDependent)
1237 glViewport (0, 0, dispWidth, dispHeight);
1239 float left = ACLayer.ortho[0];
1240 float right = ACLayer.ortho[1];
1241 float bottom = ACLayer.ortho[2];
1242 float top = ACLayer.ortho[3];
1244 int attach = ACLayer.attach;
1247 if (!ACLayer.sizeDependent)
1248 ratio = (float) dispWidth/dispHeight;
1250 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
1254 delta = (float )((top - bottom)/2.0);
1256 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1257 top = bottom + 2*delta/ratio;
1259 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1260 top = bottom + 2*delta/ratio;
1262 case 2: /* Aspect_TOC_TOP_LEFT */
1263 bottom = top - 2*delta/ratio;
1265 case 3: /* Aspect_TOC_TOP_RIGHT */
1266 bottom = top - 2*delta/ratio;
1271 delta = (float )((right - left)/2.0);
1273 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1274 right = left + 2*delta*ratio;
1276 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1277 left = right - 2*delta*ratio;
1279 case 2: /* Aspect_TOC_TOP_LEFT */
1280 right = left + 2*delta*ratio;
1282 case 3: /* Aspect_TOC_TOP_RIGHT */
1283 left = right - 2*delta*ratio;
1289 // Check printer context that exists only for print operation
1290 if (!thePrintContext.IsNull())
1292 // additional transformation matrix could be applied to
1293 // render only those parts of viewport that will be
1294 // passed to a printer as a current "frame" to provide
1295 // tiling; scaling of graphics by matrix helps render a
1296 // part of a view (frame) in same viewport, but with higher
1298 thePrintContext->LoadProjTransformation();
1300 // printing operation also assumes other viewport dimension
1301 // to comply with transformation matrix or graphics scaling
1302 // factors for tiling for layer redraw
1303 GLsizei anViewportX = 0;
1304 GLsizei anViewportY = 0;
1305 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
1306 if (anViewportX != 0 && anViewportY != 0)
1307 glViewport (0, 0, anViewportX, anViewportY);
1311 glOrtho (left, right, bottom, top, -1.0, 1.0);
1314 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
1315 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
1317 glDisable (GL_DEPTH_TEST);
1318 glDisable (GL_TEXTURE_1D);
1319 glDisable (GL_TEXTURE_2D);
1320 glDisable (GL_LIGHTING);
1322 // TODO: Obsolete code, the display list is always empty now, to be removed
1323 glCallList (ACLayer.ptrLayer->listIndex);
1325 //calling dynamic render of LayerItems
1326 if ( ACLayer.ptrLayer->layerData )
1328 InitLayerProp (ACLayer.ptrLayer->listIndex);
1329 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
1335 glMatrixMode (GL_PROJECTION);
1338 glMatrixMode( GL_MODELVIEW );
1341 if (!ACLayer.sizeDependent)
1342 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
1347 /*----------------------------------------------------------------------*/
1349 //call_togl_create_bg_texture
1350 void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
1351 const Aspect_FillMethod theFillStyle)
1353 if (myBgTexture.TexId != 0)
1355 // delete existing texture
1356 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
1357 myBgTexture.TexId = 0;
1360 // load image from file
1361 Image_AlienPixMap anImageLoaded;
1362 if (!anImageLoaded.Load (theFilePath))
1367 Image_PixMap anImage;
1368 if (anImageLoaded.RowExtraBytes() == 0 &&
1369 (anImageLoaded.Format() == Image_PixMap::ImgRGB
1370 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
1371 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
1373 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
1374 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
1378 // convert image to RGB format
1379 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
1384 anImage.SetTopDown (false);
1385 Image_PixMapData<Image_ColorRGB>& aDataNew = anImage.EditData<Image_ColorRGB>();
1386 Quantity_Color aSrcColor;
1387 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
1389 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
1391 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
1392 Image_ColorRGB& aColor = aDataNew.ChangeValue (aRow, aCol);
1393 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
1394 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
1395 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
1398 anImageLoaded.Clear();
1401 // create MipMapped texture
1402 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
1404 GLuint aTextureId = 0;
1405 glGenTextures (1, &aTextureId);
1406 glBindTexture (GL_TEXTURE_2D, aTextureId);
1408 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1409 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1410 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1411 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1413 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
1414 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
1415 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
1416 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
1418 myBgTexture.TexId = aTextureId;
1419 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
1420 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
1421 myBgTexture.Style = theFillStyle;
1424 /*----------------------------------------------------------------------*/
1426 //call_togl_set_bg_texture_style
1427 void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
1429 myBgTexture.Style = AFillStyle;
1432 /*----------------------------------------------------------------------*/
1434 //call_togl_gradient_background
1435 void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
1436 const Quantity_Color& AColor2,
1437 const Aspect_GradientFillMethod AType)
1439 Standard_Real R,G,B;
1440 AColor1.Values( R, G, B, Quantity_TOC_RGB );
1441 myBgGradient.color1.rgb[0] = ( Tfloat )R;
1442 myBgGradient.color1.rgb[1] = ( Tfloat )G;
1443 myBgGradient.color1.rgb[2] = ( Tfloat )B;
1444 myBgGradient.color1.rgb[3] = 0.F;
1446 AColor2.Values( R, G, B, Quantity_TOC_RGB );
1447 myBgGradient.color2.rgb[0] = ( Tfloat )R;
1448 myBgGradient.color2.rgb[1] = ( Tfloat )G;
1449 myBgGradient.color2.rgb[2] = ( Tfloat )B;
1450 myBgGradient.color2.rgb[3] = 0.F;
1452 myBgGradient.type = AType;
1455 /*----------------------------------------------------------------------*/
1457 //call_togl_set_gradient_type
1458 void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
1460 myBgGradient.type = AType;
1463 //=======================================================================
1464 //function : AddZLayer
1466 //=======================================================================
1468 void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
1470 myZLayers.AddLayer (theLayerId);
1473 //=======================================================================
1474 //function : RemoveZLayer
1476 //=======================================================================
1478 void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
1480 myZLayers.RemoveLayer (theLayerId);
1483 //=======================================================================
1484 //function : DisplayStructure
1486 //=======================================================================
1488 void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
1489 const Standard_Integer thePriority)
1491 Standard_Integer aZLayer = theStructure->GetZLayer ();
1492 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
1495 //=======================================================================
1496 //function : EraseStructure
1498 //=======================================================================
1500 void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
1502 Standard_Integer aZLayer = theStructure->GetZLayer ();
1503 myZLayers.RemoveStructure (theStructure, aZLayer);
1506 //=======================================================================
1507 //function : ChangeZLayer
1509 //=======================================================================
1511 void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
1512 const Standard_Integer theNewLayerId)
1514 Standard_Integer anOldLayer = theStructure->GetZLayer ();
1515 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);