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>
46 static const GLfloat default_amb[4] = { 0.F, 0.F, 0.F, 1.F };
47 static const GLfloat default_sptdir[3] = { 0.F, 0.F, -1.F };
48 static const GLfloat default_sptexpo = 0.F;
49 static const GLfloat default_sptcutoff = 180.F;
51 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
53 /*----------------------------------------------------------------------*/
55 struct OPENGL_CLIP_PLANE
62 /*----------------------------------------------------------------------*/
67 /*-----------------------------------------------------------------*/
71 static void bind_light(const OpenGl_Light *lptr, int *gl_lid)
73 // Only 8 lights in OpenGL...
74 if (*gl_lid > GL_LIGHT7) return;
76 // the light is a headlight ?
80 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
81 glMatrixMode(GL_MODELVIEW);
87 GLfloat data_diffu[4];
89 GLfloat data_sptdir[3];
91 GLfloat data_sptcutoff;
92 GLfloat data_constantattenuation;
93 GLfloat data_linearattenuation;
95 /* set la light en fonction de son type */
99 data_amb[0] = lptr->col.rgb[0];
100 data_amb[1] = lptr->col.rgb[1];
101 data_amb[2] = lptr->col.rgb[2];
104 /*------------------------- Ambient ---------------------------*/
106 * The GL_AMBIENT parameter refers to RGBA intensity of the ambient
109 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, data_amb);
113 case TLightDirectional:
114 data_diffu[0] = lptr->col.rgb[0];
115 data_diffu[1] = lptr->col.rgb[1];
116 data_diffu[2] = lptr->col.rgb[2];
119 /*------------------------- Direction ---------------------------*/
120 /* From Open GL Programming Rev 1 Guide Chapt 6 :
121 Lighting The Mathematics of Lighting ( p 168 )
123 Directional Light Source ( Infinite ) :
124 if the last parameter of GL_POSITION , w , is zero, the
125 corresponding light source is a Directional one.
127 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
128 To create a realistic effect, set the GL_SPECULAR parameter
129 to the same value as the GL_DIFFUSE.
132 data_pos[0] = -lptr->dir[0];
133 data_pos[1] = -lptr->dir[1];
134 data_pos[2] = -lptr->dir[2];
137 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
138 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
139 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
141 glLightfv(*gl_lid, GL_POSITION, data_pos);
142 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
143 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
144 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
148 case TLightPositional:
149 data_diffu[0] = lptr->col.rgb[0];
150 data_diffu[1] = lptr->col.rgb[1];
151 data_diffu[2] = lptr->col.rgb[2];
154 /*------------------------- Position -----------------------------*/
155 /* From Open GL Programming Rev 1 Guide Chapt 6 :
156 Lighting The Mathematics of Lighting ( p 168 )
157 Positional Light Source :
158 if the last parameter of GL_POSITION , w , is nonzero,
159 the corresponding light source is a Positional one.
161 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
163 To create a realistic effect, set the GL_SPECULAR parameter
164 to the same value as the GL_DIFFUSE.
167 data_pos[0] = lptr->pos[0];
168 data_pos[1] = lptr->pos[1];
169 data_pos[2] = lptr->pos[2];
172 data_constantattenuation = lptr->atten[0];
173 data_linearattenuation = lptr->atten[1];
175 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
176 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
177 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
179 glLightfv(*gl_lid, GL_POSITION, data_pos);
180 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
181 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
182 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
183 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
184 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
185 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
190 data_diffu[0] = lptr->col.rgb[0];
191 data_diffu[1] = lptr->col.rgb[1];
192 data_diffu[2] = lptr->col.rgb[2];
195 data_pos[0] = lptr->pos[0];
196 data_pos[1] = lptr->pos[1];
197 data_pos[2] = lptr->pos[2];
200 data_sptdir[0] = lptr->dir[0];
201 data_sptdir[1] = lptr->dir[1];
202 data_sptdir[2] = lptr->dir[2];
204 data_sptexpo = ( float )lptr->shine * 128.0F;
205 data_sptcutoff = ( float )(lptr->angle * 180.0F)/( float )M_PI;
207 data_constantattenuation = lptr->atten[0];
208 data_linearattenuation = lptr->atten[1];
210 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
211 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
212 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
214 glLightfv(*gl_lid, GL_POSITION, data_pos);
215 glLightfv(*gl_lid, GL_SPOT_DIRECTION, data_sptdir);
216 glLightf(*gl_lid, GL_SPOT_EXPONENT, data_sptexpo);
217 glLightf(*gl_lid, GL_SPOT_CUTOFF, data_sptcutoff);
218 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
219 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
220 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
224 if (lptr->type != TLightAmbient)
230 /* si la light etait une headlight alors restaure la matrice precedente */
234 glMatrixMode(cur_matrix);
238 /*----------------------------------------------------------------------*/
243 static void call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
244 float *ox, float *oy, float *oz );
245 static void call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c);
247 /*----------------------------------------------------------------------*/
253 * Evaluates orientation matrix.
255 /* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
256 void call_func_eval_ori_matrix3 (const point3* vrp, // view reference point
257 const vec3* vpn, // view plane normal
258 const vec3* vup, // view up vector
260 float mout[4][4]) // OUT view orientation matrix
263 /* Translate to VRP then change the basis.
264 * The old basis is: e1 = < 1, 0, 0>, e2 = < 0, 1, 0>, e3 = < 0, 0, 1>.
265 * The new basis is: ("x" means cross product)
267 * e1' = VUP x VPN / |VUP x VPN|
269 * Therefore the transform from old to new is x' = TAx, where:
271 * | e1'x e2'x e3'x 0 | | 1 0 0 0 |
272 * A = | e1'y e2'y e3'y 0 |, T = | 0 1 0 0 |
273 * | e1'z e2'z e3'z 0 | | 0 0 1 0 |
274 * | 0 0 0 1 | | -vrp.x -vrp.y -vrp.z 1 |
279 * These ei's are really ei primes.
281 register float (*m)[4][4];
282 point3 e1, e2, e3, e4;
286 * e1' = VUP x VPN / |VUP x VPN|, but do the division later.
288 e1.x = vup->delta_y * vpn->delta_z - vup->delta_z * vpn->delta_y;
289 e1.y = vup->delta_z * vpn->delta_x - vup->delta_x * vpn->delta_z;
290 e1.z = vup->delta_x * vpn->delta_y - vup->delta_y * vpn->delta_x;
291 s = sqrt( e1.x * e1.x + e1.y * e1.y + e1.z * e1.z);
295 v = sqrt( e3.x * e3.x + e3.y * e3.y + e3.z * e3.z);
297 * Check for vup and vpn colinear (zero dot product).
299 if ((s > -EPSI) && (s < EPSI))
303 * Check for a normal vector not null.
305 if ((v > -EPSI) && (v < EPSI))
323 e2.x = e3.y * e1.z - e3.z * e1.y;
324 e2.y = e3.z * e1.x - e3.x * e1.z;
325 e2.z = e3.x * e1.y - e3.y * e1.x;
327 * Add the translation
329 e4.x = -( e1.x * vrp->x + e1.y * vrp->y + e1.z * vrp->z);
330 e4.y = -( e2.x * vrp->x + e2.y * vrp->y + e2.z * vrp->z);
331 e4.z = -( e3.x * vrp->x + e3.y * vrp->y + e3.z * vrp->z);
333 * Homogeneous entries
335 * | e1.x e2.x e3.x 0.0 | | 1 0 0 0 |
336 * | e1.y e2.y e3.y 0.0 | * | 0 1 0 0 |
337 * | e1.z e2.z e3.z 0.0 | | a b 1 c |
338 * | e4.x e4.y e4.z 1.0 | | 0 0 0 1 |
341 m = (float (*)[4][4])mout;
346 (*m)[0][3] = ( float )0.0;
351 (*m)[1][3] = ( float )0.0;
356 (*m)[2][3] = ( float )0.0;
361 (*m)[3][3] = ( float )1.0;
367 /*----------------------------------------------------------------------*/
369 * Evaluates mapping matrix.
371 /* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
372 void call_func_eval_map_matrix3(
383 matrix3 aux_mat1, aux_mat2, aux_mat3;
389 Spar[i][j] = Sper[i][j] = aux_mat1[i][j] = aux_mat2[i][j] =
390 aux_mat3[i][j] = Tper[i][j] = Tpar[i][j] = Tprp[i][j] =
391 Shear[i][j] = Scale[i][j] = ( float )(i == j);
393 Prp.x = Map->proj_ref_point.x;
394 Prp.y = Map->proj_ref_point.y;
395 Prp.z = Map->proj_ref_point.z;
400 if (Map->proj_type == TYPE_PARAL)
406 float cx, cy, gx, gy, xsf, ysf, zsf;
408 float dopx, dopy, dopz;
409 matrix3 tmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
410 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
411 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
412 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
413 matrix3 smat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
414 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
415 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
416 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
417 matrix3 shmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
418 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
419 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
420 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
421 matrix3 tshmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
422 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
423 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
424 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
427 cx = Map->win.x_min + Map->win.x_max, cx /= ( float )2.0;
428 cy = Map->win.y_min + Map->win.y_max, cy /= ( float )2.0;
430 gx = 2.0/ (Map->win.x_max - Map->win.x_min);
431 gy = 2.0/ (Map->win.y_max - Map->win.y_min);
435 tmat[2][3] = (Map->front_plane + Map->back_plane)/(Map->front_plane - Map->back_plane);
439 smat[2][2] = -2./(Map->front_plane - Map->back_plane);
447 shmat[0][2] = -(dopx/dopz);
448 shmat[1][2] = -(dopy/dopz);
450 /* multiply to obtain mapping matrix */
451 call_util_mat_mul( tmat, shmat, tshmat );
452 call_util_mat_mul( smat, tshmat, mat );
458 Map->proj_vp.z_min = ( float )0.0;
459 Map->proj_vp.z_max = ( float )1.0;
462 /* Shear matrix calculation */
463 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0;
464 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0;
465 if(Prp.z == Map->view_plane){
466 /* Projection reference point is on the view plane */
470 Shear[2][0] = ( float )(-1.0) * ((Prp.x-umid)/(Prp.z-Map->view_plane));
471 Shear[2][1] = ( float )(-1.0) * ((Prp.y-vmid)/(Prp.z-Map->view_plane));
474 * Calculate the lower left coordinate of the view plane
475 * after the Shearing Transformation.
477 call_util_apply_trans2(Map->win.x_min, Map->win.y_min,
478 Map->view_plane, Shear, &(temp.x), &(temp.y), &(temp.z));
480 /* Translate the back plane to the origin */
481 Tpar[3][0] = ( float )(-1.0) * temp.x;
482 Tpar[3][1] = ( float )(-1.0) * temp.y;
483 Tpar[3][2] = ( float )(-1.0) * Map->back_plane;
485 call_util_mat_mul(Shear, Tpar, aux_mat1);
487 /* Calculation of Scaling transformation */
488 Spar[0][0] = ( float )1.0 / (Map->win.x_max - Map->win.x_min);
489 Spar[1][1] = ( float )1.0 / (Map->win.y_max - Map->win.y_min);
490 Spar[2][2] = ( float )1.0 / (Map->front_plane - Map->back_plane );
491 call_util_mat_mul (aux_mat1, Spar, aux_mat2);
492 /* Atlast we transformed view volume to NPC */
494 /* Translate and scale the view plane to projection view port */
495 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
496 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
497 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
498 Map->proj_vp.x_min > Map->proj_vp.x_max ||
499 Map->proj_vp.y_min > Map->proj_vp.y_max ||
500 Map->proj_vp.z_min > Map->proj_vp.z_max){
506 aux_mat1[i][j] = (float)(i==j);
507 aux_mat1[0][0] = Map->proj_vp.x_max-Map->proj_vp.x_min;
508 aux_mat1[1][1] = Map->proj_vp.y_max-Map->proj_vp.y_min;
509 aux_mat1[2][2] = Map->proj_vp.z_max-Map->proj_vp.z_min;
510 aux_mat1[3][0] = Map->proj_vp.x_min;
511 aux_mat1[3][1] = Map->proj_vp.y_min;
512 aux_mat1[3][2] = Map->proj_vp.z_min;
513 call_util_mat_mul (aux_mat2, aux_mat1, mat);
521 else if (Map->proj_type == TYPE_PERSPECT)
528 Map->proj_vp.z_min = ( float )0.0;
529 Map->proj_vp.z_max = ( float )1.0;
533 F = Map->front_plane;
536 if(Prp.z == Map->view_plane){
537 /* Centre of Projection is on the view plane */
541 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
542 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
543 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
544 Map->proj_vp.x_min > Map->proj_vp.x_max ||
545 Map->proj_vp.y_min > Map->proj_vp.y_max ||
546 Map->proj_vp.z_min > Map->proj_vp.z_max ||
552 /* This is the transformation to move VRC to Center Of Projection */
553 Tprp[3][0] = ( float )(-1.0)*Prp.x;
554 Tprp[3][1] = ( float )(-1.0)*Prp.y;
555 Tprp[3][2] = ( float )(-1.0)*Prp.z;
557 /* Calculation of Shear matrix */
558 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0-Prp.x;
559 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0-Prp.y;
560 Shear[2][0] = ( float )(-1.0)*umid/(Map->view_plane-Prp.z);
561 Shear[2][1] = ( float )(-1.0)*vmid/(Map->view_plane-Prp.z);
562 call_util_mat_mul(Tprp, Shear, aux_mat3);
564 /* Scale the view volume to canonical view volume
565 * Centre of projection at origin.
566 * 0 <= N <= -1, -0.5 <= U <= 0.5, -0.5 <= V <= 0.5
568 Scale[0][0] = (( float )(-1.0)*Prp.z+V)/
569 ((Map->win.x_max-Map->win.x_min)*(( float )(-1.0)*Prp.z+B));
570 Scale[1][1] = (( float )(-1.0)*Prp.z+V)/
571 ((Map->win.y_max-Map->win.y_min)*(( float )(-1.0)*Prp.z+B));
572 Scale[2][2] = ( float )(-1.0) / (( float )(-1.0)*Prp.z+B);
574 call_util_mat_mul(aux_mat3, Scale, aux_mat1);
577 * Transform the Perspective view volume into
578 * Parallel view volume.
579 * Lower left coordinate: (-0.5,-0.5, -1)
580 * Upper right coordinate: (0.5, 0.5, 1.0)
582 Zvmin = ( float )(-1.0*(-1.0*Prp.z+F)/(-1.0*Prp.z+B));
583 aux_mat2[2][2] = ( float )1.0/(( float )1.0+Zvmin);
584 aux_mat2[2][3] = ( float )(-1.0);
585 aux_mat2[3][2] = ( float )(-1.0)*Zvmin*aux_mat2[2][2];
586 aux_mat2[3][3] = ( float )0.0;
587 call_util_mat_mul(aux_mat1, aux_mat2, Shear);
591 aux_mat1[i][j] = aux_mat2[i][j] = (float)(i==j);
593 /* Translate and scale the view plane to projection view port */
594 aux_mat2[0][0] = (Map->proj_vp.x_max-Map->proj_vp.x_min);
595 aux_mat2[1][1] = (Map->proj_vp.y_max-Map->proj_vp.y_min);
596 aux_mat2[2][2] = (Map->proj_vp.z_max-Map->proj_vp.z_min);
597 aux_mat2[3][0] = aux_mat2[0][0]/( float )2.0+Map->proj_vp.x_min;
598 aux_mat2[3][1] = aux_mat2[1][1]/( float )2.0+Map->proj_vp.y_min;
599 aux_mat2[3][2] = aux_mat2[2][2]+Map->proj_vp.z_min;
600 call_util_mat_mul (Shear, aux_mat2, mat);
608 /*----------------------------------------------------------------------*/
611 call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
612 float *ox, float *oy, float *oz )
615 *ox = ix*mat[0][0]+iy*mat[1][0]+iz*mat[2][0]+mat[3][0];
616 *oy = ix*mat[0][1]+iy*mat[1][1]+iz*mat[2][1]+mat[3][1];
617 *oz = ix*mat[0][2]+iy*mat[1][2]+iz*mat[2][2]+mat[3][2];
618 temp = ix * mat[0][3]+iy * mat[1][3]+iz * mat[2][3]+mat[3][3];
624 /*----------------------------------------------------------------------*/
627 call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c)
633 for (mat_c[i][j] = ( float )0.0,k=0; k<4; k++)
634 mat_c[i][j] += mat_a[i][k] * mat_b[k][j];
637 /*----------------------------------------------------------------------*/
639 //call_func_redraw_all_structs_proc
640 void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
641 const Handle(OpenGl_Workspace) &AWorkspace,
642 const Graphic3d_CView& ACView,
643 const Aspect_CLayer2d& ACUnderLayer,
644 const Aspect_CLayer2d& ACOverLayer)
646 // Store and disable current clipping planes
647 const Handle(OpenGl_Context)& aContext = AWorkspace->GetGlContext();
648 const Standard_Integer aMaxClipPlanes = aContext->MaxClipPlanes();
649 const GLenum lastid = GL_CLIP_PLANE0 + aMaxClipPlanes;
650 OPENGL_CLIP_PLANE *oldPlanes = new OPENGL_CLIP_PLANE[aMaxClipPlanes];
651 OPENGL_CLIP_PLANE *ptrPlane = oldPlanes;
652 GLenum planeid = GL_CLIP_PLANE0;
653 for ( ; planeid < lastid; planeid++, ptrPlane++ )
655 glGetClipPlane( planeid, ptrPlane->Equation );
656 if ( ptrPlane->isEnabled )
658 glDisable( planeid );
659 ptrPlane->isEnabled = GL_TRUE;
663 ptrPlane->isEnabled = GL_FALSE;
667 // Set OCCT state uniform variables
669 if (!aContext->ShaderManager()->IsEmpty())
671 if (myLightSourcesChanged)
673 aContext->ShaderManager()->UpdateLightSourceStateTo (&myLights);
674 myLightSourcesChanged = Standard_False;
677 if (myViewMappingChanged)
679 aContext->ShaderManager()->UpdateProjectionStateTo (myMappingMatrix);
680 myViewMappingChanged = Standard_False;
683 if (myOrientationChanged)
685 aContext->ShaderManager()->UpdateWorldViewStateTo (myOrientationMatrix);
686 myOrientationChanged = Standard_False;
689 if (aContext->ShaderManager()->ModelWorldState().Index() == 0)
691 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
692 { 0.f, 1.f, 0.f, 0.f },
693 { 0.f, 0.f, 1.f, 0.f },
694 { 0.f, 0.f, 0.f, 1.f } };
696 aContext->ShaderManager()->UpdateModelWorldStateTo (aModelWorldState);
700 /////////////////////////////////////////////////////////////////////////////
701 // Step 1: Prepare for redraw
704 if ( (AWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
705 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
707 const Standard_Integer aViewWidth = AWorkspace->Width();
708 const Standard_Integer aViewHeight = AWorkspace->Height();
710 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
712 glMatrixMode( GL_PROJECTION );
715 glMatrixMode( GL_MODELVIEW );
719 if ( glIsEnabled( GL_DEPTH_TEST ) )
720 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
722 // drawing bg gradient if:
723 // - gradient fill type is not Aspect_GFM_NONE and
724 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
725 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
726 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
727 myBgTexture.Style == Aspect_FM_NONE ) )
729 Tfloat* corner1 = 0;/* -1,-1*/
730 Tfloat* corner2 = 0;/* 1,-1*/
731 Tfloat* corner3 = 0;/* 1, 1*/
732 Tfloat* corner4 = 0;/* -1, 1*/
736 switch( myBgGradient.type )
739 corner1 = myBgGradient.color1.rgb;
740 corner2 = myBgGradient.color2.rgb;
741 corner3 = myBgGradient.color2.rgb;
742 corner4 = myBgGradient.color1.rgb;
745 corner1 = myBgGradient.color2.rgb;
746 corner2 = myBgGradient.color2.rgb;
747 corner3 = myBgGradient.color1.rgb;
748 corner4 = myBgGradient.color1.rgb;
750 case Aspect_GFM_DIAG1:
751 corner2 = myBgGradient.color2.rgb;
752 corner4 = myBgGradient.color1.rgb;
753 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
754 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
755 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
759 case Aspect_GFM_DIAG2:
760 corner1 = myBgGradient.color2.rgb;
761 corner3 = myBgGradient.color1.rgb;
762 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
763 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
764 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
768 case Aspect_GFM_CORNER1:
769 corner1 = myBgGradient.color2.rgb;
770 corner2 = myBgGradient.color2.rgb;
771 corner3 = myBgGradient.color2.rgb;
772 corner4 = myBgGradient.color1.rgb;
774 case Aspect_GFM_CORNER2:
775 corner1 = myBgGradient.color2.rgb;
776 corner2 = myBgGradient.color2.rgb;
777 corner3 = myBgGradient.color1.rgb;
778 corner4 = myBgGradient.color2.rgb;
780 case Aspect_GFM_CORNER3:
781 corner1 = myBgGradient.color2.rgb;
782 corner2 = myBgGradient.color1.rgb;
783 corner3 = myBgGradient.color2.rgb;
784 corner4 = myBgGradient.color2.rgb;
786 case Aspect_GFM_CORNER4:
787 corner1 = myBgGradient.color1.rgb;
788 corner2 = myBgGradient.color2.rgb;
789 corner3 = myBgGradient.color2.rgb;
790 corner4 = myBgGradient.color2.rgb;
793 //printf("gradient background type not right\n");
797 // Save GL parameters
798 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
801 glGetIntegerv( GL_SHADE_MODEL, &curSM );
802 if ( curSM != GL_SMOOTH )
803 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
805 glBegin(GL_TRIANGLE_FAN);
806 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
808 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
809 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
810 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
811 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
813 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
815 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
816 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
817 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
818 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
822 // Restore GL parameters
823 if ( curSM != GL_SMOOTH )
824 glShadeModel( curSM );
826 // drawing bg image if:
827 // - it is defined and
828 // - fill type is not Aspect_FM_NONE
829 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
831 GLfloat texX_range = 1.F; // texture <s> coordinate
832 GLfloat texY_range = 1.F; // texture <t> coordinate
834 // Set up for stretching or tiling
835 GLfloat x_offset, y_offset;
836 if ( myBgTexture.Style == Aspect_FM_CENTERED )
838 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
839 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
845 if ( myBgTexture.Style == Aspect_FM_TILED )
847 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
848 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
852 // OCCT issue 0023000: Improve the way the gradient and textured
853 // background is managed in 3d viewer (note 0020339)
854 // Setting this coefficient to -1.F allows to tile textures relatively
855 // to the top-left corner of the view (value 1.F corresponds to the
856 // initial behaviour - tiling from the bottom-left corner)
857 GLfloat aCoef = -1.F;
859 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
860 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
862 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
864 glColor3fv( AWorkspace->BackgroundColor().rgb );
865 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
867 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
868 // is simply ignored, and negative multiplier is here for convenience only
869 // and does not result e.g. in texture mirroring
871 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
872 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
873 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
874 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
879 glMatrixMode( GL_PROJECTION );
881 glMatrixMode( GL_MODELVIEW );
883 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
885 if ( AWorkspace->UseZBuffer() )
886 glEnable( GL_DEPTH_TEST );
888 /* GL_DITHER on/off pour le trace */
889 if (AWorkspace->Dither())
890 glEnable (GL_DITHER);
892 glDisable (GL_DITHER);
895 // Switch off lighting by default
896 glDisable(GL_LIGHTING);
898 /////////////////////////////////////////////////////////////////////////////
899 // Step 2: Draw underlayer
900 RedrawLayer2d (thePrintContext, ACView, ACUnderLayer);
902 /////////////////////////////////////////////////////////////////////////////
903 // Step 3: Redraw main plane
905 // Setup face culling
906 GLboolean isCullFace = GL_FALSE;
909 isCullFace = glIsEnabled( GL_CULL_FACE );
910 if ( myBackfacing < 0 )
912 glEnable( GL_CULL_FACE );
913 glCullFace( GL_BACK );
916 glDisable( GL_CULL_FACE );
919 //TsmPushAttri(); /* save previous graphics context */
921 // if the view is scaled normal vectors are scaled to unit length for correct displaying of shaded objects
922 if(myExtra.scaleFactors[0] != 1.F ||
923 myExtra.scaleFactors[1] != 1.F ||
924 myExtra.scaleFactors[2] != 1.F)
925 glEnable(GL_NORMALIZE);
926 else if(glIsEnabled(GL_NORMALIZE))
927 glDisable(GL_NORMALIZE);
929 // Apply View Projection
930 // This routine activates the Projection matrix for a view.
932 glMatrixMode( GL_PROJECTION );
935 // add printing scale/tiling transformation
936 if (!thePrintContext.IsNull())
938 thePrintContext->LoadProjTransformation();
944 glMultMatrixf( (const GLfloat *) myMappingMatrix );
946 // Add translation necessary for the environnement mapping
947 if (mySurfaceDetail != Visual3d_TOD_NONE)
949 // OCC280: FitAll work incorrect for perspective view if the SurfaceDetail mode is V3d_TEX_ENVIRONMENT or V3d_TEX_ALL
950 // const GLfloat dep = vptr->vrep.extra.map.fpd * 0.5F;
951 const GLfloat dep = (myExtra.map.fpd + myExtra.map.bpd) * 0.5F;
952 glTranslatef(-dep*myExtra.vpn[0],-dep*myExtra.vpn[1],-dep*myExtra.vpn[2]);
956 AWorkspace->SetViewMatrix((const OpenGl_Matrix *)myOrientationMatrix);
959 While drawing after a clipplane has been defined and enabled, each vertex
960 is transformed to eye-coordinates, where it is dotted with the transformed
961 clipping plane equation. Eye-coordinate vertexes whose dot product with
962 the transformed clipping plane equation is positive or zero are in, and
963 require no clipping. Those eye-coordinate vertexes whose dot product is
964 negative are clipped. Because clipplane clipping is done in eye-
965 coordinates, changes to the projection matrix have no effect on its
968 A point and a normal are converted to a plane equation in the following manner:
983 D = -[Px,Py,Pz] dot |Nx|
992 const GLfloat ramp = myExtra.map.fpd - myExtra.map.bpd;
993 const GLfloat fog_start = myFog.Front * ramp - myExtra.map.fpd;
994 const GLfloat fog_end = myFog.Back * ramp - myExtra.map.fpd;
996 glFogi(GL_FOG_MODE, GL_LINEAR);
997 glFogf(GL_FOG_START, fog_start);
998 glFogf(GL_FOG_END, fog_end);
999 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
1009 // Switch off all lights
1010 for (i = GL_LIGHT0; i <= GL_LIGHT7; i++)
1012 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, default_amb);
1014 /* set les lights */
1015 int gl_lid = GL_LIGHT0;
1016 OpenGl_ListOfLight::Iterator itl(myLights);
1017 for (; itl.More(); itl.Next())
1019 const OpenGl_Light &alight = itl.Value();
1020 bind_light(&alight, &gl_lid);
1023 if (gl_lid != GL_LIGHT0) glEnable(GL_LIGHTING);
1026 // Apply InteriorShadingMethod
1027 glShadeModel( myIntShadingMethod == TEL_SM_FLAT ? GL_FLAT : GL_SMOOTH );
1029 // Apply clipping planes
1031 const Handle(OpenGl_Context)& aContext = AWorkspace->GetGlContext();
1033 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1035 const GLdouble ramp = myExtra.map.fpd - myExtra.map.bpd;
1037 Handle(Graphic3d_ClipPlane) aPlaneBack;
1038 Handle(Graphic3d_ClipPlane) aPlaneFront;
1040 if (myZClip.Back.IsOn)
1042 const GLdouble back = ramp * myZClip.Back.Limit + myExtra.map.bpd;
1043 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, -back);
1044 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1047 if (myZClip.Front.IsOn)
1049 const GLdouble front = ramp * myZClip.Front.Limit + myExtra.map.bpd;
1050 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, front);
1051 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1054 // do some "memory allocation"-wise optimization
1055 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1057 Graphic3d_SetOfHClipPlane aSlicingPlanes;
1058 if (!aPlaneBack.IsNull())
1060 aSlicingPlanes.Add (aPlaneBack);
1063 if (!aPlaneFront.IsNull())
1065 aSlicingPlanes.Add (aPlaneFront);
1068 // add planes at loaded view matrix state
1069 aContext->ChangeClipping().AddView (aSlicingPlanes, AWorkspace);
1073 // Apply user clipping planes
1074 if (!myClipPlanes.IsEmpty())
1076 Graphic3d_SetOfHClipPlane aUserPlanes;
1077 Graphic3d_SetOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1078 for (; aClippingIt.More(); aClippingIt.Next())
1080 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1081 if (aClipPlane->IsOn())
1083 aUserPlanes.Add (aClipPlane);
1087 if (!aUserPlanes.IsEmpty())
1089 // add planes at actual matrix state.
1090 aContext->ChangeClipping().AddWorld (aUserPlanes);
1094 if (!aContext->ShaderManager()->IsEmpty())
1096 aContext->ShaderManager()->UpdateClippingState();
1100 // Apply AntiAliasing
1103 AWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
1105 AWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
1108 // Clear status bitfields
1109 AWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1111 // Added PCT for handling of textures
1112 switch (mySurfaceDetail)
1114 case Visual3d_TOD_NONE:
1115 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1116 AWorkspace->DisableTexture();
1118 RenderStructs(AWorkspace);
1121 case Visual3d_TOD_ENVIRONMENT:
1122 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1123 AWorkspace->EnableTexture (myTextureEnv);
1125 RenderStructs(AWorkspace);
1126 AWorkspace->DisableTexture();
1129 case Visual3d_TOD_ALL:
1131 AWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1133 RenderStructs(AWorkspace);
1134 AWorkspace->DisableTexture();
1137 if (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1139 AWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1140 AWorkspace->EnableTexture (myTextureEnv);
1142 /* sauvegarde de quelques parametres OpenGL */
1143 GLint blend_dst, blend_src;
1146 glGetBooleanv(GL_DEPTH_WRITEMASK, &zbuff_w);
1147 glGetIntegerv(GL_DEPTH_FUNC, &zbuff_f);
1148 glGetIntegerv(GL_BLEND_DST, &blend_dst);
1149 glGetIntegerv(GL_BLEND_SRC, &blend_src);
1150 GLboolean zbuff_state = glIsEnabled(GL_DEPTH_TEST);
1151 GLboolean blend_state = glIsEnabled(GL_BLEND);
1153 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1156 glDepthFunc(GL_EQUAL);
1157 glDepthMask(GL_FALSE);
1158 glEnable(GL_DEPTH_TEST);
1160 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1163 RenderStructs(AWorkspace);
1164 AWorkspace->DisableTexture();
1166 /* restauration des parametres OpenGL */
1167 glBlendFunc(blend_src, blend_dst);
1168 if (!blend_state) glDisable(GL_BLEND);
1170 glDepthFunc(zbuff_f);
1171 glDepthMask(zbuff_w);
1172 if (!zbuff_state) glDisable(GL_DEPTH_FUNC);
1177 // Resetting GL parameters according to the default aspects
1178 // in order to synchronize GL state with the graphic driver state
1179 // before drawing auxiliary stuff (trihedrons, overlayer)
1180 // and invoking optional callbacks
1181 AWorkspace->ResetAppliedAspect();
1183 aContext->ChangeClipping().RemoveAll();
1185 if (!aContext->ShaderManager()->IsEmpty())
1187 aContext->ShaderManager()->ResetMaterialStates();
1188 aContext->ShaderManager()->RevertClippingState();
1191 // display global trihedron
1192 if (myTrihedron != NULL)
1194 myTrihedron->Render (AWorkspace);
1196 if (myGraduatedTrihedron != NULL)
1198 myGraduatedTrihedron->Render (AWorkspace);
1201 // Restore face culling
1206 glEnable ( GL_CULL_FACE );
1207 glCullFace ( GL_BACK );
1210 glDisable ( GL_CULL_FACE );
1213 /////////////////////////////////////////////////////////////////////////////
1214 // Step 6: Draw overlayer
1215 const int aMode = 0;
1216 AWorkspace->DisplayCallback (ACView, (aMode | OCC_PRE_OVERLAY));
1218 RedrawLayer2d (thePrintContext, ACView, ACOverLayer);
1220 AWorkspace->DisplayCallback (ACView, aMode);
1222 // Restore clipping planes
1223 for ( ptrPlane = oldPlanes, planeid = GL_CLIP_PLANE0; planeid < lastid; planeid++, ptrPlane++ )
1225 glClipPlane( planeid, ptrPlane->Equation );
1226 if ( ptrPlane->isEnabled )
1227 glEnable( planeid );
1229 glDisable( planeid );
1234 /*----------------------------------------------------------------------*/
1236 //ExecuteViewDisplay
1237 void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
1239 if ( myZLayers.NbStructures() <= 0 )
1242 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
1244 //TsmPushAttri(); /* save previous graphics context */
1246 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
1248 const int antiAliasingMode = AWorkspace->GetDisplay()->AntiAliasingMode();
1250 if ( !myAntiAliasing )
1252 glDisable(GL_POINT_SMOOTH);
1253 glDisable(GL_LINE_SMOOTH);
1254 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
1255 glBlendFunc (GL_ONE, GL_ZERO);
1256 glDisable (GL_BLEND);
1260 glEnable(GL_POINT_SMOOTH);
1261 glEnable(GL_LINE_SMOOTH);
1262 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
1263 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1264 glEnable (GL_BLEND);
1268 myZLayers.Render (AWorkspace);
1270 //TsmPopAttri(); /* restore previous graphics context; before update lights */
1274 /*----------------------------------------------------------------------*/
1276 //call_togl_redraw_layer2d
1277 void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
1278 const Graphic3d_CView& ACView,
1279 const Aspect_CLayer2d& ACLayer)
1281 if (&ACLayer == NULL
1282 || ACLayer.ptrLayer == NULL
1283 || ACLayer.ptrLayer->listIndex == 0) return;
1285 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
1286 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
1288 glMatrixMode( GL_MODELVIEW );
1292 glMatrixMode (GL_PROJECTION);
1296 if (!ACLayer.sizeDependent)
1297 glViewport (0, 0, dispWidth, dispHeight);
1299 float left = ACLayer.ortho[0];
1300 float right = ACLayer.ortho[1];
1301 float bottom = ACLayer.ortho[2];
1302 float top = ACLayer.ortho[3];
1304 int attach = ACLayer.attach;
1307 if (!ACLayer.sizeDependent)
1308 ratio = (float) dispWidth/dispHeight;
1310 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
1314 delta = (float )((top - bottom)/2.0);
1316 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1317 top = bottom + 2*delta/ratio;
1319 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1320 top = bottom + 2*delta/ratio;
1322 case 2: /* Aspect_TOC_TOP_LEFT */
1323 bottom = top - 2*delta/ratio;
1325 case 3: /* Aspect_TOC_TOP_RIGHT */
1326 bottom = top - 2*delta/ratio;
1331 delta = (float )((right - left)/2.0);
1333 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1334 right = left + 2*delta*ratio;
1336 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1337 left = right - 2*delta*ratio;
1339 case 2: /* Aspect_TOC_TOP_LEFT */
1340 right = left + 2*delta*ratio;
1342 case 3: /* Aspect_TOC_TOP_RIGHT */
1343 left = right - 2*delta*ratio;
1349 // Check printer context that exists only for print operation
1350 if (!thePrintContext.IsNull())
1352 // additional transformation matrix could be applied to
1353 // render only those parts of viewport that will be
1354 // passed to a printer as a current "frame" to provide
1355 // tiling; scaling of graphics by matrix helps render a
1356 // part of a view (frame) in same viewport, but with higher
1358 thePrintContext->LoadProjTransformation();
1360 // printing operation also assumes other viewport dimension
1361 // to comply with transformation matrix or graphics scaling
1362 // factors for tiling for layer redraw
1363 GLsizei anViewportX = 0;
1364 GLsizei anViewportY = 0;
1365 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
1366 if (anViewportX != 0 && anViewportY != 0)
1367 glViewport (0, 0, anViewportX, anViewportY);
1371 glOrtho (left, right, bottom, top, -1.0, 1.0);
1374 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
1375 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
1377 glDisable (GL_DEPTH_TEST);
1378 glDisable (GL_TEXTURE_1D);
1379 glDisable (GL_TEXTURE_2D);
1380 glDisable (GL_LIGHTING);
1382 // TODO: Obsolete code, the display list is always empty now, to be removed
1383 glCallList (ACLayer.ptrLayer->listIndex);
1385 //calling dynamic render of LayerItems
1386 if ( ACLayer.ptrLayer->layerData )
1388 InitLayerProp (ACLayer.ptrLayer->listIndex);
1389 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
1395 glMatrixMode (GL_PROJECTION);
1398 glMatrixMode( GL_MODELVIEW );
1401 if (!ACLayer.sizeDependent)
1402 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
1407 /*----------------------------------------------------------------------*/
1409 //call_togl_create_bg_texture
1410 void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
1411 const Aspect_FillMethod theFillStyle)
1413 if (myBgTexture.TexId != 0)
1415 // delete existing texture
1416 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
1417 myBgTexture.TexId = 0;
1420 // load image from file
1421 Image_AlienPixMap anImageLoaded;
1422 if (!anImageLoaded.Load (theFilePath))
1427 Image_PixMap anImage;
1428 if (anImageLoaded.RowExtraBytes() == 0 &&
1429 (anImageLoaded.Format() == Image_PixMap::ImgRGB
1430 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
1431 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
1433 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
1434 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
1438 // convert image to RGB format
1439 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
1444 anImage.SetTopDown (false);
1445 Image_PixMapData<Image_ColorRGB>& aDataNew = anImage.EditData<Image_ColorRGB>();
1446 Quantity_Color aSrcColor;
1447 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
1449 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
1451 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
1452 Image_ColorRGB& aColor = aDataNew.ChangeValue (aRow, aCol);
1453 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
1454 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
1455 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
1458 anImageLoaded.Clear();
1461 // create MipMapped texture
1462 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
1464 GLuint aTextureId = 0;
1465 glGenTextures (1, &aTextureId);
1466 glBindTexture (GL_TEXTURE_2D, aTextureId);
1468 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1469 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1470 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1471 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1473 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
1474 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
1475 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
1476 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
1478 myBgTexture.TexId = aTextureId;
1479 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
1480 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
1481 myBgTexture.Style = theFillStyle;
1484 /*----------------------------------------------------------------------*/
1486 //call_togl_set_bg_texture_style
1487 void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
1489 myBgTexture.Style = AFillStyle;
1492 /*----------------------------------------------------------------------*/
1494 //call_togl_gradient_background
1495 void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
1496 const Quantity_Color& AColor2,
1497 const Aspect_GradientFillMethod AType)
1499 Standard_Real R,G,B;
1500 AColor1.Values( R, G, B, Quantity_TOC_RGB );
1501 myBgGradient.color1.rgb[0] = ( Tfloat )R;
1502 myBgGradient.color1.rgb[1] = ( Tfloat )G;
1503 myBgGradient.color1.rgb[2] = ( Tfloat )B;
1504 myBgGradient.color1.rgb[3] = 0.F;
1506 AColor2.Values( R, G, B, Quantity_TOC_RGB );
1507 myBgGradient.color2.rgb[0] = ( Tfloat )R;
1508 myBgGradient.color2.rgb[1] = ( Tfloat )G;
1509 myBgGradient.color2.rgb[2] = ( Tfloat )B;
1510 myBgGradient.color2.rgb[3] = 0.F;
1512 myBgGradient.type = AType;
1515 /*----------------------------------------------------------------------*/
1517 //call_togl_set_gradient_type
1518 void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
1520 myBgGradient.type = AType;
1523 //=======================================================================
1524 //function : AddZLayer
1526 //=======================================================================
1528 void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
1530 myZLayers.AddLayer (theLayerId);
1533 //=======================================================================
1534 //function : RemoveZLayer
1536 //=======================================================================
1538 void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
1540 myZLayers.RemoveLayer (theLayerId);
1543 //=======================================================================
1544 //function : DisplayStructure
1546 //=======================================================================
1548 void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
1549 const Standard_Integer thePriority)
1551 Standard_Integer aZLayer = theStructure->GetZLayer ();
1552 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
1555 //=======================================================================
1556 //function : EraseStructure
1558 //=======================================================================
1560 void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
1562 Standard_Integer aZLayer = theStructure->GetZLayer ();
1563 myZLayers.RemoveStructure (theStructure, aZLayer);
1566 //=======================================================================
1567 //function : ChangeZLayer
1569 //=======================================================================
1571 void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
1572 const Standard_Integer theNewLayerId)
1574 Standard_Integer anOldLayer = theStructure->GetZLayer ();
1575 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);