0024070: OpenGL capped object-level clipping planes
[occt.git] / src / OpenGl / OpenGl_View_2.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
2166f0fa
SK
20#include <stdio.h>
21#include <stdlib.h>
22
5f8b738e 23#include <OpenGl_GlCore11.hxx>
2166f0fa 24#include <OpenGl_tgl_funcs.hxx>
2166f0fa 25
3c3131a0 26#include <Image_AlienPixMap.hxx>
2166f0fa
SK
27#include <Visual3d_Layer.hxx>
28
2166f0fa
SK
29#include <OpenGl_AspectLine.hxx>
30#include <OpenGl_Display.hxx>
31#include <OpenGl_Workspace.hxx>
32#include <OpenGl_View.hxx>
33#include <OpenGl_Trihedron.hxx>
34#include <OpenGl_GraduatedTrihedron.hxx>
35#include <OpenGl_PrinterContext.hxx>
59f45b7c 36#include <OpenGl_Structure.hxx>
2166f0fa 37
2166f0fa
SK
38#define EPSI 0.0001
39
2166f0fa
SK
40static const GLfloat default_amb[4] = { 0.F, 0.F, 0.F, 1.F };
41static const GLfloat default_sptdir[3] = { 0.F, 0.F, -1.F };
42static const GLfloat default_sptexpo = 0.F;
43static const GLfloat default_sptcutoff = 180.F;
44
45extern void InitLayerProp (const int AListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
46
47/*----------------------------------------------------------------------*/
48
49struct OPENGL_CLIP_PLANE
50{
51 GLboolean isEnabled;
52 GLdouble Equation[4];
1c35b92f 53 DEFINE_STANDARD_ALLOC
2166f0fa
SK
54};
55
56/*----------------------------------------------------------------------*/
57/*
58* Fonctions privees
59*/
60
61/*-----------------------------------------------------------------*/
62/*
63* Set des lumieres
64*/
65static void bind_light(const OpenGl_Light *lptr, int *gl_lid)
66{
67 // Only 8 lights in OpenGL...
68 if (*gl_lid > GL_LIGHT7) return;
69
70 // the light is a headlight ?
1d47d8d0 71 GLint cur_matrix = 0;
2166f0fa
SK
72 if (lptr->HeadLight)
73 {
74 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
75 glMatrixMode(GL_MODELVIEW);
76 glPushMatrix();
77 glLoadIdentity();
78 }
79
80 GLfloat data_amb[4];
81 GLfloat data_diffu[4];
82 GLfloat data_pos[4];
83 GLfloat data_sptdir[3];
84 GLfloat data_sptexpo;
85 GLfloat data_sptcutoff;
86 GLfloat data_constantattenuation;
87 GLfloat data_linearattenuation;
88
89 /* set la light en fonction de son type */
90 switch (lptr->type)
91 {
92 case TLightAmbient:
93 data_amb[0] = lptr->col.rgb[0];
94 data_amb[1] = lptr->col.rgb[1];
95 data_amb[2] = lptr->col.rgb[2];
96 data_amb[3] = 1.0;
97
98 /*------------------------- Ambient ---------------------------*/
99 /*
100 * The GL_AMBIENT parameter refers to RGBA intensity of the ambient
101 * light.
102 */
3c3131a0 103 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, data_amb);
2166f0fa
SK
104 break;
105
106
107 case TLightDirectional:
108 data_diffu[0] = lptr->col.rgb[0];
109 data_diffu[1] = lptr->col.rgb[1];
110 data_diffu[2] = lptr->col.rgb[2];
111 data_diffu[3] = 1.0;
112
113 /*------------------------- Direction ---------------------------*/
114 /* From Open GL Programming Rev 1 Guide Chapt 6 :
115 Lighting The Mathematics of Lighting ( p 168 )
116
117 Directional Light Source ( Infinite ) :
118 if the last parameter of GL_POSITION , w , is zero, the
119 corresponding light source is a Directional one.
120
121 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
3c3131a0 122 To create a realistic effect, set the GL_SPECULAR parameter
2166f0fa
SK
123 to the same value as the GL_DIFFUSE.
124 */
125
126 data_pos[0] = -lptr->dir[0];
127 data_pos[1] = -lptr->dir[1];
128 data_pos[2] = -lptr->dir[2];
129 data_pos[3] = 0.0;
130
131 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
132 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
133 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
134
135 glLightfv(*gl_lid, GL_POSITION, data_pos);
136 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
137 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
138 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
139 break;
140
141
142 case TLightPositional:
143 data_diffu[0] = lptr->col.rgb[0];
144 data_diffu[1] = lptr->col.rgb[1];
145 data_diffu[2] = lptr->col.rgb[2];
146 data_diffu[3] = 1.0;
147
148 /*------------------------- Position -----------------------------*/
149 /* From Open GL Programming Rev 1 Guide Chapt 6 :
150 Lighting The Mathematics of Lighting ( p 168 )
151 Positional Light Source :
152 if the last parameter of GL_POSITION , w , is nonzero,
153 the corresponding light source is a Positional one.
154
155 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
156
3c3131a0 157 To create a realistic effect, set the GL_SPECULAR parameter
2166f0fa
SK
158 to the same value as the GL_DIFFUSE.
159 */
160
161 data_pos[0] = lptr->pos[0];
162 data_pos[1] = lptr->pos[1];
163 data_pos[2] = lptr->pos[2];
164 data_pos[3] = 1.0;
165
166 data_constantattenuation = lptr->atten[0];
167 data_linearattenuation = lptr->atten[1];
168
169 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
170 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
171 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
172
173 glLightfv(*gl_lid, GL_POSITION, data_pos);
174 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
175 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
176 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
177 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
178 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
3c3131a0 179 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
2166f0fa
SK
180 break;
181
182
183 case TLightSpot:
184 data_diffu[0] = lptr->col.rgb[0];
185 data_diffu[1] = lptr->col.rgb[1];
186 data_diffu[2] = lptr->col.rgb[2];
187 data_diffu[3] = 1.0;
188
189 data_pos[0] = lptr->pos[0];
190 data_pos[1] = lptr->pos[1];
191 data_pos[2] = lptr->pos[2];
192 data_pos[3] = 1.0;
193
194 data_sptdir[0] = lptr->dir[0];
195 data_sptdir[1] = lptr->dir[1];
196 data_sptdir[2] = lptr->dir[2];
197
198 data_sptexpo = ( float )lptr->shine * 128.0F;
199 data_sptcutoff = ( float )(lptr->angle * 180.0F)/( float )M_PI;
200
201 data_constantattenuation = lptr->atten[0];
202 data_linearattenuation = lptr->atten[1];
203
204 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
205 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
3c3131a0 206 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
2166f0fa 207
3c3131a0 208 glLightfv(*gl_lid, GL_POSITION, data_pos);
2166f0fa
SK
209 glLightfv(*gl_lid, GL_SPOT_DIRECTION, data_sptdir);
210 glLightf(*gl_lid, GL_SPOT_EXPONENT, data_sptexpo);
211 glLightf(*gl_lid, GL_SPOT_CUTOFF, data_sptcutoff);
212 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
213 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
3c3131a0 214 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
2166f0fa
SK
215 break;
216 }
217
3c3131a0 218 if (lptr->type != TLightAmbient)
219 {
2166f0fa
SK
220 glEnable(*gl_lid);
221 (*gl_lid)++;
222 }
223
224 /* si la light etait une headlight alors restaure la matrice precedente */
225 if (lptr->HeadLight)
226 {
227 glPopMatrix();
228 glMatrixMode(cur_matrix);
229 }
230}
231
232/*----------------------------------------------------------------------*/
233/*
234* Prototypes
235*/
236
237static void call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
238 float *ox, float *oy, float *oz );
239static void call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c);
240
241/*----------------------------------------------------------------------*/
242/*
243* Fonctions externes
244*/
245
246/*
247* Evaluates orientation matrix.
248*/
249/* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
250void call_func_eval_ori_matrix3 (const point3* vrp, // view reference point
251 const vec3* vpn, // view plane normal
252 const vec3* vup, // view up vector
3c3131a0 253 int* err_ind,
2166f0fa
SK
254 float mout[4][4]) // OUT view orientation matrix
255{
256
257 /* Translate to VRP then change the basis.
258 * The old basis is: e1 = < 1, 0, 0>, e2 = < 0, 1, 0>, e3 = < 0, 0, 1>.
259 * The new basis is: ("x" means cross product)
260 * e3' = VPN / |VPN|
261 * e1' = VUP x VPN / |VUP x VPN|
262 * e2' = e3' x e1'
263 * Therefore the transform from old to new is x' = TAx, where:
264 *
265 * | e1'x e2'x e3'x 0 | | 1 0 0 0 |
266 * A = | e1'y e2'y e3'y 0 |, T = | 0 1 0 0 |
267 * | e1'z e2'z e3'z 0 | | 0 0 1 0 |
268 * | 0 0 0 1 | | -vrp.x -vrp.y -vrp.z 1 |
269 *
270 */
271
272 /*
273 * These ei's are really ei primes.
274 */
275 register float (*m)[4][4];
276 point3 e1, e2, e3, e4;
277 double s, v;
278
279 /*
280 * e1' = VUP x VPN / |VUP x VPN|, but do the division later.
281 */
282 e1.x = vup->delta_y * vpn->delta_z - vup->delta_z * vpn->delta_y;
283 e1.y = vup->delta_z * vpn->delta_x - vup->delta_x * vpn->delta_z;
284 e1.z = vup->delta_x * vpn->delta_y - vup->delta_y * vpn->delta_x;
285 s = sqrt( e1.x * e1.x + e1.y * e1.y + e1.z * e1.z);
286 e3.x = vpn->delta_x;
287 e3.y = vpn->delta_y;
288 e3.z = vpn->delta_z;
289 v = sqrt( e3.x * e3.x + e3.y * e3.y + e3.z * e3.z);
290 /*
291 * Check for vup and vpn colinear (zero dot product).
292 */
293 if ((s > -EPSI) && (s < EPSI))
294 *err_ind = 2;
295 else
296 /*
297 * Check for a normal vector not null.
298 */
299 if ((v > -EPSI) && (v < EPSI))
300 *err_ind = 3;
301 else {
302 /*
303 * Normalize e1
304 */
305 e1.x /= ( float )s;
306 e1.y /= ( float )s;
307 e1.z /= ( float )s;
308 /*
309 * e3 = VPN / |VPN|
310 */
311 e3.x /= ( float )v;
312 e3.y /= ( float )v;
313 e3.z /= ( float )v;
314 /*
315 * e2 = e3 x e1
316 */
317 e2.x = e3.y * e1.z - e3.z * e1.y;
318 e2.y = e3.z * e1.x - e3.x * e1.z;
319 e2.z = e3.x * e1.y - e3.y * e1.x;
320 /*
321 * Add the translation
322 */
323 e4.x = -( e1.x * vrp->x + e1.y * vrp->y + e1.z * vrp->z);
324 e4.y = -( e2.x * vrp->x + e2.y * vrp->y + e2.z * vrp->z);
325 e4.z = -( e3.x * vrp->x + e3.y * vrp->y + e3.z * vrp->z);
326 /*
327 * Homogeneous entries
328 *
329 * | e1.x e2.x e3.x 0.0 | | 1 0 0 0 |
330 * | e1.y e2.y e3.y 0.0 | * | 0 1 0 0 |
331 * | e1.z e2.z e3.z 0.0 | | a b 1 c |
332 * | e4.x e4.y e4.z 1.0 | | 0 0 0 1 |
333 */
334
335 m = (float (*)[4][4])mout;
336
337 (*m)[0][0] = e1.x;
338 (*m)[0][1] = e2.x;
339 (*m)[0][2] = e3.x;
340 (*m)[0][3] = ( float )0.0;
341
342 (*m)[1][0] = e1.y;
343 (*m)[1][1] = e2.y;
344 (*m)[1][2] = e3.y;
345 (*m)[1][3] = ( float )0.0;
346
347 (*m)[2][0] = e1.z;
348 (*m)[2][1] = e2.z;
349 (*m)[2][2] = e3.z;
350 (*m)[2][3] = ( float )0.0;
351
352 (*m)[3][0] = e4.x;
353 (*m)[3][1] = e4.y;
354 (*m)[3][2] = e4.z;
355 (*m)[3][3] = ( float )1.0;
356
357 *err_ind = 0;
358 }
359}
360
361/*----------------------------------------------------------------------*/
362/*
363* Evaluates mapping matrix.
364*/
365/* OCC18942: obsolete in OCCT6.3, might be removed in further versions! */
366void call_func_eval_map_matrix3(
3c3131a0 367 view_map3 *Map,
368 int *err_ind,
2166f0fa
SK
369 matrix3 mat)
370{
371 int i, j;
372 matrix3 Tpar, Spar;
373 matrix3 Tper, Sper;
374 matrix3 Shear;
375 matrix3 Scale;
376 matrix3 Tprp;
377 matrix3 aux_mat1, aux_mat2, aux_mat3;
378 point3 Prp;
379
380 *err_ind = 0;
381 for (i=0; i<4; i++)
382 for (j=0; j<4; j++)
383 Spar[i][j] = Sper[i][j] = aux_mat1[i][j] = aux_mat2[i][j] =
384 aux_mat3[i][j] = Tper[i][j] = Tpar[i][j] = Tprp[i][j] =
385 Shear[i][j] = Scale[i][j] = ( float )(i == j);
386
387 Prp.x = Map->proj_ref_point.x;
388 Prp.y = Map->proj_ref_point.y;
389 Prp.z = Map->proj_ref_point.z;
390
391 /*
392 * Type Parallele
3c3131a0 393 */
2166f0fa
SK
394 if (Map->proj_type == TYPE_PARAL)
395 {
396 float umid, vmid;
397 point3 temp;
398
399#ifdef FMN
400 float cx, cy, gx, gy, xsf, ysf, zsf;
401 float fpd, bpd;
402 float dopx, dopy, dopz;
403 matrix3 tmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
404 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
405 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
406 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
407 matrix3 smat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
408 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
409 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
410 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
411 matrix3 shmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
412 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
413 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
414 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
415 matrix3 tshmat = { { ( float )1.0, ( float )0.0, ( float )0.0, ( float )0.0 },
416 { ( float )0.0, ( float )1.0, ( float )0.0, ( float )0.0 },
417 { ( float )0.0, ( float )0.0, ( float )1.0, ( float )0.0 },
418 { ( float )0.0, ( float )0.0, ( float )0.0, ( float )1.0 } };
419
420 /* centers */
421 cx = Map->win.x_min + Map->win.x_max, cx /= ( float )2.0;
422 cy = Map->win.y_min + Map->win.y_max, cy /= ( float )2.0;
423
424 gx = 2.0/ (Map->win.x_max - Map->win.x_min);
425 gy = 2.0/ (Map->win.y_max - Map->win.y_min);
426
427 tmat[0][3] = -cx;
428 tmat[1][3] = -cy;
429 tmat[2][3] = (Map->front_plane + Map->back_plane)/(Map->front_plane - Map->back_plane);
430
431 smat[0][0] = gx;
432 smat[1][1] = gy;
433 smat[2][2] = -2./(Map->front_plane - Map->back_plane);
434
435 /* scale factors */
436 dopx = cx - Prp.x;
437 dopy = cy - Prp.y;
438 dopz = - Prp.z;
439
440 /* map matrix */
441 shmat[0][2] = -(dopx/dopz);
442 shmat[1][2] = -(dopy/dopz);
443
444 /* multiply to obtain mapping matrix */
445 call_util_mat_mul( tmat, shmat, tshmat );
446 call_util_mat_mul( smat, tshmat, mat );
447
3c3131a0 448 return;
2166f0fa
SK
449#endif
450
451 /* CAL */
452 Map->proj_vp.z_min = ( float )0.0;
453 Map->proj_vp.z_max = ( float )1.0;
454 /* CAL */
455
456 /* Shear matrix calculation */
457 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0;
458 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0;
459 if(Prp.z == Map->view_plane){
460 /* Projection reference point is on the view plane */
461 *err_ind = 1;
462 return;
463 }
464 Shear[2][0] = ( float )(-1.0) * ((Prp.x-umid)/(Prp.z-Map->view_plane));
465 Shear[2][1] = ( float )(-1.0) * ((Prp.y-vmid)/(Prp.z-Map->view_plane));
466
467 /*
468 * Calculate the lower left coordinate of the view plane
469 * after the Shearing Transformation.
470 */
471 call_util_apply_trans2(Map->win.x_min, Map->win.y_min,
472 Map->view_plane, Shear, &(temp.x), &(temp.y), &(temp.z));
473
474 /* Translate the back plane to the origin */
475 Tpar[3][0] = ( float )(-1.0) * temp.x;
476 Tpar[3][1] = ( float )(-1.0) * temp.y;
477 Tpar[3][2] = ( float )(-1.0) * Map->back_plane;
478
479 call_util_mat_mul(Shear, Tpar, aux_mat1);
480
481 /* Calculation of Scaling transformation */
482 Spar[0][0] = ( float )1.0 / (Map->win.x_max - Map->win.x_min);
483 Spar[1][1] = ( float )1.0 / (Map->win.y_max - Map->win.y_min);
484 Spar[2][2] = ( float )1.0 / (Map->front_plane - Map->back_plane );
485 call_util_mat_mul (aux_mat1, Spar, aux_mat2);
486 /* Atlast we transformed view volume to NPC */
487
488 /* Translate and scale the view plane to projection view port */
489 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
490 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
491 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
492 Map->proj_vp.x_min > Map->proj_vp.x_max ||
493 Map->proj_vp.y_min > Map->proj_vp.y_max ||
494 Map->proj_vp.z_min > Map->proj_vp.z_max){
495 *err_ind = 1;
496 return;
497 }
498 for(i=0; i<4; i++)
499 for(j=0; j<4; j++)
500 aux_mat1[i][j] = (float)(i==j);
501 aux_mat1[0][0] = Map->proj_vp.x_max-Map->proj_vp.x_min;
502 aux_mat1[1][1] = Map->proj_vp.y_max-Map->proj_vp.y_min;
503 aux_mat1[2][2] = Map->proj_vp.z_max-Map->proj_vp.z_min;
504 aux_mat1[3][0] = Map->proj_vp.x_min;
505 aux_mat1[3][1] = Map->proj_vp.y_min;
506 aux_mat1[3][2] = Map->proj_vp.z_min;
507 call_util_mat_mul (aux_mat2, aux_mat1, mat);
508
3c3131a0 509 return;
510 }
2166f0fa
SK
511
512 /*
513 * Type Perspective
3c3131a0 514 */
2166f0fa
SK
515 else if (Map->proj_type == TYPE_PERSPECT)
516 {
517 float umid, vmid;
518 float B, F, V;
519 float Zvmin;
520
521 /* CAL */
522 Map->proj_vp.z_min = ( float )0.0;
523 Map->proj_vp.z_max = ( float )1.0;
524 /* CAL */
525
526 B = Map->back_plane;
527 F = Map->front_plane;
528 V = Map->view_plane;
529
530 if(Prp.z == Map->view_plane){
531 /* Centre of Projection is on the view plane */
532 *err_ind = 1;
533 return;
534 }
535 if(Map->proj_vp.x_min < 0.0 || Map->proj_vp.y_min < 0.0 ||
536 Map->proj_vp.z_min < 0.0 || Map->proj_vp.x_max > 1.0 ||
537 Map->proj_vp.y_max > 1.0 || Map->proj_vp.z_max > 1.0 ||
538 Map->proj_vp.x_min > Map->proj_vp.x_max ||
539 Map->proj_vp.y_min > Map->proj_vp.y_max ||
540 Map->proj_vp.z_min > Map->proj_vp.z_max ||
541 F < B){
542 *err_ind = 1;
543 return;
544 }
545
546 /* This is the transformation to move VRC to Center Of Projection */
547 Tprp[3][0] = ( float )(-1.0)*Prp.x;
548 Tprp[3][1] = ( float )(-1.0)*Prp.y;
549 Tprp[3][2] = ( float )(-1.0)*Prp.z;
550
551 /* Calculation of Shear matrix */
552 umid = ( float )(Map->win.x_min+Map->win.x_max)/( float )2.0-Prp.x;
553 vmid = ( float )(Map->win.y_min+Map->win.y_max)/( float )2.0-Prp.y;
554 Shear[2][0] = ( float )(-1.0)*umid/(Map->view_plane-Prp.z);
555 Shear[2][1] = ( float )(-1.0)*vmid/(Map->view_plane-Prp.z);
556 call_util_mat_mul(Tprp, Shear, aux_mat3);
557
558 /* Scale the view volume to canonical view volume
559 * Centre of projection at origin.
560 * 0 <= N <= -1, -0.5 <= U <= 0.5, -0.5 <= V <= 0.5
561 */
562 Scale[0][0] = (( float )(-1.0)*Prp.z+V)/
563 ((Map->win.x_max-Map->win.x_min)*(( float )(-1.0)*Prp.z+B));
564 Scale[1][1] = (( float )(-1.0)*Prp.z+V)/
565 ((Map->win.y_max-Map->win.y_min)*(( float )(-1.0)*Prp.z+B));
566 Scale[2][2] = ( float )(-1.0) / (( float )(-1.0)*Prp.z+B);
567
568 call_util_mat_mul(aux_mat3, Scale, aux_mat1);
569
570 /*
571 * Transform the Perspective view volume into
572 * Parallel view volume.
573 * Lower left coordinate: (-0.5,-0.5, -1)
574 * Upper right coordinate: (0.5, 0.5, 1.0)
575 */
576 Zvmin = ( float )(-1.0*(-1.0*Prp.z+F)/(-1.0*Prp.z+B));
577 aux_mat2[2][2] = ( float )1.0/(( float )1.0+Zvmin);
578 aux_mat2[2][3] = ( float )(-1.0);
579 aux_mat2[3][2] = ( float )(-1.0)*Zvmin*aux_mat2[2][2];
580 aux_mat2[3][3] = ( float )0.0;
581 call_util_mat_mul(aux_mat1, aux_mat2, Shear);
582
583 for(i=0; i<4; i++)
584 for(j=0; j<4; j++)
585 aux_mat1[i][j] = aux_mat2[i][j] = (float)(i==j);
586
587 /* Translate and scale the view plane to projection view port */
588 aux_mat2[0][0] = (Map->proj_vp.x_max-Map->proj_vp.x_min);
589 aux_mat2[1][1] = (Map->proj_vp.y_max-Map->proj_vp.y_min);
590 aux_mat2[2][2] = (Map->proj_vp.z_max-Map->proj_vp.z_min);
591 aux_mat2[3][0] = aux_mat2[0][0]/( float )2.0+Map->proj_vp.x_min;
592 aux_mat2[3][1] = aux_mat2[1][1]/( float )2.0+Map->proj_vp.y_min;
593 aux_mat2[3][2] = aux_mat2[2][2]+Map->proj_vp.z_min;
594 call_util_mat_mul (Shear, aux_mat2, mat);
595
596 return;
597 }
598 else
599 *err_ind = 1;
600}
601
602/*----------------------------------------------------------------------*/
603
604static void
605call_util_apply_trans2( float ix, float iy, float iz, matrix3 mat,
606 float *ox, float *oy, float *oz )
607{
608 float temp;
609 *ox = ix*mat[0][0]+iy*mat[1][0]+iz*mat[2][0]+mat[3][0];
610 *oy = ix*mat[0][1]+iy*mat[1][1]+iz*mat[2][1]+mat[3][1];
611 *oz = ix*mat[0][2]+iy*mat[1][2]+iz*mat[2][2]+mat[3][2];
612 temp = ix * mat[0][3]+iy * mat[1][3]+iz * mat[2][3]+mat[3][3];
613 *ox /= temp;
614 *oy /= temp;
615 *oz /= temp;
616}
617
618/*----------------------------------------------------------------------*/
619
620static void
621call_util_mat_mul( matrix3 mat_a, matrix3 mat_b, matrix3 mat_c)
622{
623 int i, j, k;
624
625 for (i=0; i<4; i++)
626 for (j=0; j<4; j++)
627 for (mat_c[i][j] = ( float )0.0,k=0; k<4; k++)
628 mat_c[i][j] += mat_a[i][k] * mat_b[k][j];
629}
630
631/*----------------------------------------------------------------------*/
632
633//call_func_redraw_all_structs_proc
a174a3c5 634void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
635 const Handle(OpenGl_Workspace) &AWorkspace,
636 const Graphic3d_CView& ACView,
637 const Aspect_CLayer2d& ACUnderLayer,
638 const Aspect_CLayer2d& ACOverLayer)
2166f0fa 639{
2166f0fa 640 // Store and disable current clipping planes
4269bd1b 641 const Handle(OpenGl_Context)& aContext = AWorkspace->GetGlContext();
642 const Standard_Integer aMaxClipPlanes = aContext->MaxClipPlanes();
643 const GLenum lastid = GL_CLIP_PLANE0 + aMaxClipPlanes;
644 OPENGL_CLIP_PLANE *oldPlanes = new OPENGL_CLIP_PLANE[aMaxClipPlanes];
2166f0fa
SK
645 OPENGL_CLIP_PLANE *ptrPlane = oldPlanes;
646 GLenum planeid = GL_CLIP_PLANE0;
647 for ( ; planeid < lastid; planeid++, ptrPlane++ )
648 {
649 glGetClipPlane( planeid, ptrPlane->Equation );
650 if ( ptrPlane->isEnabled )
651 {
652 glDisable( planeid );
653 ptrPlane->isEnabled = GL_TRUE;
654 }
4269bd1b 655 else
656 {
2166f0fa 657 ptrPlane->isEnabled = GL_FALSE;
4269bd1b 658 }
2166f0fa
SK
659 }
660
661 /////////////////////////////////////////////////////////////////////////////
662 // Step 1: Prepare for redraw
663
664 // Render background
665 if ( (AWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
666 ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
667 {
668 const Standard_Integer aViewWidth = AWorkspace->Width();
669 const Standard_Integer aViewHeight = AWorkspace->Height();
670
671 glPushAttrib( GL_ENABLE_BIT | GL_TEXTURE_BIT );
672
673 glMatrixMode( GL_PROJECTION );
674 glPushMatrix();
675 glLoadIdentity();
676 glMatrixMode( GL_MODELVIEW );
677 glPushMatrix();
678 glLoadIdentity();
679
680 if ( glIsEnabled( GL_DEPTH_TEST ) )
681 glDisable( GL_DEPTH_TEST ); //push GL_ENABLE_BIT
682
f8b2ed36 683 // drawing bg gradient if:
684 // - gradient fill type is not Aspect_GFM_NONE and
685 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
686 if ( ( myBgGradient.type != Aspect_GFM_NONE ) &&
687 ( myBgTexture.TexId == 0 || myBgTexture.Style == Aspect_FM_CENTERED ||
688 myBgTexture.Style == Aspect_FM_NONE ) )
2166f0fa
SK
689 {
690 Tfloat* corner1 = 0;/* -1,-1*/
691 Tfloat* corner2 = 0;/* 1,-1*/
692 Tfloat* corner3 = 0;/* 1, 1*/
693 Tfloat* corner4 = 0;/* -1, 1*/
694 Tfloat dcorner1[3];
695 Tfloat dcorner2[3];
696
697 switch( myBgGradient.type )
698 {
699 case Aspect_GFM_HOR:
f8b2ed36 700 corner1 = myBgGradient.color1.rgb;
2166f0fa 701 corner2 = myBgGradient.color2.rgb;
f8b2ed36 702 corner3 = myBgGradient.color2.rgb;
2166f0fa
SK
703 corner4 = myBgGradient.color1.rgb;
704 break;
705 case Aspect_GFM_VER:
706 corner1 = myBgGradient.color2.rgb;
f8b2ed36 707 corner2 = myBgGradient.color2.rgb;
2166f0fa 708 corner3 = myBgGradient.color1.rgb;
f8b2ed36 709 corner4 = myBgGradient.color1.rgb;
2166f0fa
SK
710 break;
711 case Aspect_GFM_DIAG1:
712 corner2 = myBgGradient.color2.rgb;
3c3131a0 713 corner4 = myBgGradient.color1.rgb;
2166f0fa
SK
714 dcorner1 [0] = dcorner2[0] = 0.5F * (corner2[0] + corner4[0]);
715 dcorner1 [1] = dcorner2[1] = 0.5F * (corner2[1] + corner4[1]);
716 dcorner1 [2] = dcorner2[2] = 0.5F * (corner2[2] + corner4[2]);
717 corner1 = dcorner1;
3c3131a0 718 corner3 = dcorner2;
2166f0fa
SK
719 break;
720 case Aspect_GFM_DIAG2:
3c3131a0 721 corner1 = myBgGradient.color2.rgb;
722 corner3 = myBgGradient.color1.rgb;
2166f0fa
SK
723 dcorner1 [0] = dcorner2[0] = 0.5F * (corner1[0] + corner3[0]);
724 dcorner1 [1] = dcorner2[1] = 0.5F * (corner1[1] + corner3[1]);
725 dcorner1 [2] = dcorner2[2] = 0.5F * (corner1[2] + corner3[2]);
726 corner2 = dcorner1;
3c3131a0 727 corner4 = dcorner2;
2166f0fa
SK
728 break;
729 case Aspect_GFM_CORNER1:
f8b2ed36 730 corner1 = myBgGradient.color2.rgb;
2166f0fa
SK
731 corner2 = myBgGradient.color2.rgb;
732 corner3 = myBgGradient.color2.rgb;
f8b2ed36 733 corner4 = myBgGradient.color1.rgb;
2166f0fa
SK
734 break;
735 case Aspect_GFM_CORNER2:
736 corner1 = myBgGradient.color2.rgb;
f8b2ed36 737 corner2 = myBgGradient.color2.rgb;
738 corner3 = myBgGradient.color1.rgb;
2166f0fa
SK
739 corner4 = myBgGradient.color2.rgb;
740 break;
741 case Aspect_GFM_CORNER3:
742 corner1 = myBgGradient.color2.rgb;
f8b2ed36 743 corner2 = myBgGradient.color1.rgb;
744 corner3 = myBgGradient.color2.rgb;
2166f0fa
SK
745 corner4 = myBgGradient.color2.rgb;
746 break;
747 case Aspect_GFM_CORNER4:
f8b2ed36 748 corner1 = myBgGradient.color1.rgb;
2166f0fa
SK
749 corner2 = myBgGradient.color2.rgb;
750 corner3 = myBgGradient.color2.rgb;
f8b2ed36 751 corner4 = myBgGradient.color2.rgb;
2166f0fa
SK
752 break;
753 default:
754 //printf("gradient background type not right\n");
755 break;
756 }
757
758 // Save GL parameters
759 glDisable( GL_LIGHTING ); //push GL_ENABLE_BIT
760
761 GLint curSM;
762 glGetIntegerv( GL_SHADE_MODEL, &curSM );
763 if ( curSM != GL_SMOOTH )
764 glShadeModel( GL_SMOOTH ); //push GL_LIGHTING_BIT
765
766 glBegin(GL_TRIANGLE_FAN);
f8b2ed36 767 if( myBgGradient.type != Aspect_GFM_CORNER1 && myBgGradient.type != Aspect_GFM_CORNER3 )
2166f0fa
SK
768 {
769 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
770 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
771 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
772 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
3c3131a0 773 }
f8b2ed36 774 else //if ( myBgGradient.type == Aspect_GFM_CORNER1 || myBgGradient.type == Aspect_GFM_CORNER3 )
2166f0fa
SK
775 {
776 glColor3f(corner2[0],corner2[1],corner2[2]); glVertex2f( 1.,-1.);
777 glColor3f(corner3[0],corner3[1],corner3[2]); glVertex2f( 1., 1.);
778 glColor3f(corner4[0],corner4[1],corner4[2]); glVertex2f(-1., 1.);
779 glColor3f(corner1[0],corner1[1],corner1[2]); glVertex2f(-1.,-1.);
780 }
781 glEnd();
782
783 // Restore GL parameters
784 if ( curSM != GL_SMOOTH )
785 glShadeModel( curSM );
786 }
f8b2ed36 787 // drawing bg image if:
788 // - it is defined and
789 // - fill type is not Aspect_FM_NONE
790 if ( myBgTexture.TexId != 0 && myBgTexture.Style != Aspect_FM_NONE )
791 {
792 GLfloat texX_range = 1.F; // texture <s> coordinate
793 GLfloat texY_range = 1.F; // texture <t> coordinate
794
795 // Set up for stretching or tiling
796 GLfloat x_offset, y_offset;
797 if ( myBgTexture.Style == Aspect_FM_CENTERED )
798 {
799 x_offset = (GLfloat)myBgTexture.Width / (GLfloat)aViewWidth;
800 y_offset = (GLfloat)myBgTexture.Height / (GLfloat)aViewHeight;
801 }
802 else
803 {
804 x_offset = 1.F;
805 y_offset = 1.F;
806 if ( myBgTexture.Style == Aspect_FM_TILED )
807 {
808 texX_range = (GLfloat)aViewWidth / (GLfloat)myBgTexture.Width;
809 texY_range = (GLfloat)aViewHeight / (GLfloat)myBgTexture.Height;
810 }
811 }
812
1e743e91 813 // OCCT issue 0023000: Improve the way the gradient and textured
814 // background is managed in 3d viewer (note 0020339)
815 // Setting this coefficient to -1.F allows to tile textures relatively
816 // to the top-left corner of the view (value 1.F corresponds to the
817 // initial behaviour - tiling from the bottom-left corner)
818 GLfloat aCoef = -1.F;
819
f8b2ed36 820 glEnable( GL_TEXTURE_2D ); //push GL_ENABLE_BIT
821 glBindTexture( GL_TEXTURE_2D, myBgTexture.TexId ); //push GL_TEXTURE_BIT
822
823 glDisable( GL_BLEND ); //push GL_ENABLE_BIT
824
825 glColor3fv( AWorkspace->BackgroundColor().rgb );
826 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //push GL_TEXTURE_BIT
827
1e743e91 828 // Note that texture is mapped using GL_REPEAT wrapping mode so integer part
829 // is simply ignored, and negative multiplier is here for convenience only
830 // and does not result e.g. in texture mirroring
f8b2ed36 831 glBegin( GL_QUADS );
1e743e91 832 glTexCoord2f(0.F, 0.F); glVertex2f( -x_offset, -aCoef * y_offset );
833 glTexCoord2f(texX_range, 0.F); glVertex2f( x_offset, -aCoef * y_offset );
834 glTexCoord2f(texX_range, aCoef * texY_range); glVertex2f( x_offset, aCoef * y_offset );
835 glTexCoord2f(0.F, aCoef * texY_range); glVertex2f( -x_offset, aCoef * y_offset );
f8b2ed36 836 glEnd();
837 }
2166f0fa
SK
838
839 glPopMatrix();
840 glMatrixMode( GL_PROJECTION );
841 glPopMatrix();
842 glMatrixMode( GL_MODELVIEW );
843
844 glPopAttrib(); //GL_ENABLE_BIT | GL_TEXTURE_BIT
845
846 if ( AWorkspace->UseZBuffer() )
847 glEnable( GL_DEPTH_TEST );
848
849 /* GL_DITHER on/off pour le trace */
850 if (AWorkspace->Dither())
851 glEnable (GL_DITHER);
852 else
853 glDisable (GL_DITHER);
854 }
855
856 // Switch off lighting by default
857 glDisable(GL_LIGHTING);
858
859 /////////////////////////////////////////////////////////////////////////////
860 // Step 2: Draw underlayer
a174a3c5 861 RedrawLayer2d (thePrintContext, AWorkspace, ACView, ACUnderLayer);
2166f0fa
SK
862
863 /////////////////////////////////////////////////////////////////////////////
864 // Step 3: Redraw main plane
865
866 // Setup face culling
867 GLboolean isCullFace = GL_FALSE;
868 if ( myBackfacing )
869 {
870 isCullFace = glIsEnabled( GL_CULL_FACE );
871 if ( myBackfacing < 0 )
872 {
873 glEnable( GL_CULL_FACE );
874 glCullFace( GL_BACK );
875 }
876 else
877 glDisable( GL_CULL_FACE );
878 }
879
880 //TsmPushAttri(); /* save previous graphics context */
881
882 // if the view is scaled normal vectors are scaled to unit length for correct displaying of shaded objects
3c3131a0 883 if(myExtra.scaleFactors[0] != 1.F ||
2166f0fa
SK
884 myExtra.scaleFactors[1] != 1.F ||
885 myExtra.scaleFactors[2] != 1.F)
886 glEnable(GL_NORMALIZE);
3c3131a0 887 else if(glIsEnabled(GL_NORMALIZE))
2166f0fa
SK
888 glDisable(GL_NORMALIZE);
889
890 // Apply View Projection
891 // This routine activates the Projection matrix for a view.
892
893 glMatrixMode( GL_PROJECTION );
894
a174a3c5 895#ifdef _WIN32
2166f0fa 896 // add printing scale/tiling transformation
a174a3c5 897 if (!thePrintContext.IsNull())
2166f0fa 898 {
a174a3c5 899 thePrintContext->LoadProjTransformation();
2166f0fa
SK
900 }
901 else
902#endif
903 glLoadIdentity();
904
905 glMultMatrixf( (const GLfloat *) myMappingMatrix );
906
907 // Add translation necessary for the environnement mapping
908 if (mySurfaceDetail != Visual3d_TOD_NONE)
909 {
910 // OCC280: FitAll work incorrect for perspective view if the SurfaceDetail mode is V3d_TEX_ENVIRONMENT or V3d_TEX_ALL
911 // const GLfloat dep = vptr->vrep.extra.map.fpd * 0.5F;
912 const GLfloat dep = (myExtra.map.fpd + myExtra.map.bpd) * 0.5F;
913 glTranslatef(-dep*myExtra.vpn[0],-dep*myExtra.vpn[1],-dep*myExtra.vpn[2]);
914 }
915
916 // Apply matrix
917 AWorkspace->SetViewMatrix((const OpenGl_Matrix *)myOrientationMatrix);
918
919/*
920While drawing after a clipplane has been defined and enabled, each vertex
921is transformed to eye-coordinates, where it is dotted with the transformed
922clipping plane equation. Eye-coordinate vertexes whose dot product with
923the transformed clipping plane equation is positive or zero are in, and
924require no clipping. Those eye-coordinate vertexes whose dot product is
925negative are clipped. Because clipplane clipping is done in eye-
926coordinates, changes to the projection matrix have no effect on its
927operation.
928
929A point and a normal are converted to a plane equation in the following manner:
930
931point = [Px,Py,Pz]
932
933normal = |Nx|
934|Ny|
935|Nz|
936
937plane equation = |A|
938|B|
939|C|
940|D|
941A = Nx
942B = Ny
943C = Nz
944D = -[Px,Py,Pz] dot |Nx|
945|Ny|
946|Nz|
947
948*/
949
2166f0fa
SK
950 // Apply Fog
951 if ( myFog.IsOn )
952 {
953 const GLfloat ramp = myExtra.map.fpd - myExtra.map.bpd;
954 const GLfloat fog_start = myFog.Front * ramp - myExtra.map.fpd;
955 const GLfloat fog_end = myFog.Back * ramp - myExtra.map.fpd;
956
957 glFogi(GL_FOG_MODE, GL_LINEAR);
958 glFogf(GL_FOG_START, fog_start);
959 glFogf(GL_FOG_END, fog_end);
960 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
961 glEnable(GL_FOG);
962 }
963 else
964 glDisable(GL_FOG);
965
966 // Apply Lights
967 {
968 int i;
969
970 // Switch off all lights
971 for (i = GL_LIGHT0; i <= GL_LIGHT7; i++)
972 glDisable(i);
973 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, default_amb);
974
975 /* set les lights */
976 int gl_lid = GL_LIGHT0;
977 OpenGl_ListOfLight::Iterator itl(myLights);
978 for (; itl.More(); itl.Next())
979 {
980 const OpenGl_Light &alight = itl.Value();
981 bind_light(&alight, &gl_lid);
982 }
983
984 if (gl_lid != GL_LIGHT0) glEnable(GL_LIGHTING);
985 }
986
987 // Apply InteriorShadingMethod
988 glShadeModel( myIntShadingMethod == TEL_SM_FLAT ? GL_FLAT : GL_SMOOTH );
989
990 // Apply clipping planes
991 {
2166f0fa
SK
992 if ( myZClip.Back.IsOn || myZClip.Front.IsOn )
993 {
4269bd1b 994 Graphic3d_SetOfHClipPlane aZClipping;
2166f0fa
SK
995
996 const GLdouble ramp = myExtra.map.fpd - myExtra.map.bpd;
997
998 if ( myZClip.Back.IsOn )
999 {
1000 const GLdouble back = ramp * myZClip.Back.Limit + myExtra.map.bpd;
4269bd1b 1001 const Graphic3d_ClipPlane::Equation aBack(0.0, 0.0, 1.0, -back);
1002 aZClipping.Add (new Graphic3d_ClipPlane (aBack));
2166f0fa
SK
1003 }
1004
1005 if ( myZClip.Front.IsOn )
1006 {
1007 const GLdouble front = ramp * myZClip.Front.Limit + myExtra.map.bpd;
4269bd1b 1008 const Graphic3d_ClipPlane::Equation aFront (0.0, 0.0, -1.0, front);
1009 aZClipping.Add (new Graphic3d_ClipPlane (aFront));
2166f0fa
SK
1010 }
1011
4269bd1b 1012 aContext->ChangeClipping().Set (aZClipping, &OpenGl_IdentityMatrix);
2166f0fa
SK
1013 }
1014
1015 // Apply user clipping planes
4269bd1b 1016 Graphic3d_SetOfHClipPlane aPlanesOn;
1017 Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (myClipPlanes);
1018 for (; aPlaneIt.More(); aPlaneIt.Next())
1019 {
1020 const Handle(Graphic3d_ClipPlane)& aUserPln = aPlaneIt.Value();
1021 if (aUserPln->IsOn ())
1022 aPlanesOn.Add (aUserPln);
1023 }
1024
1025 if (aPlanesOn.Size() > 0)
2166f0fa 1026 {
4269bd1b 1027 aContext->ChangeClipping().Set (aPlanesOn);
2166f0fa
SK
1028 }
1029 }
1030
1031 // Apply AntiAliasing
1032 {
1033 if (myAntiAliasing)
1034 AWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
1035 else
1036 AWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
1037 }
1038
de75ed09 1039 // Clear status bitfields
1040 AWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
2166f0fa 1041
de75ed09 1042 // Added PCT for handling of textures
1043 switch (mySurfaceDetail)
2166f0fa 1044 {
de75ed09 1045 case Visual3d_TOD_NONE:
1046 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1047 AWorkspace->DisableTexture();
1048 // Render the view
1049 RenderStructs(AWorkspace);
1050 break;
1051
1052 case Visual3d_TOD_ENVIRONMENT:
1053 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1054 AWorkspace->EnableTexture (myTextureEnv);
1055 // Render the view
1056 RenderStructs(AWorkspace);
1057 AWorkspace->DisableTexture();
1058 break;
1059
1060 case Visual3d_TOD_ALL:
1061 // First pass
1062 AWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1063 // Render the view
1064 RenderStructs(AWorkspace);
1065 AWorkspace->DisableTexture();
1066
1067 // Second pass
1068 if (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
2166f0fa 1069 {
de75ed09 1070 AWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1071 AWorkspace->EnableTexture (myTextureEnv);
2166f0fa 1072
de75ed09 1073 /* sauvegarde de quelques parametres OpenGL */
1074 GLint blend_dst, blend_src;
1075 GLint zbuff_f;
1076 GLboolean zbuff_w;
1077 glGetBooleanv(GL_DEPTH_WRITEMASK, &zbuff_w);
1078 glGetIntegerv(GL_DEPTH_FUNC, &zbuff_f);
1079 glGetIntegerv(GL_BLEND_DST, &blend_dst);
1080 glGetIntegerv(GL_BLEND_SRC, &blend_src);
1081 GLboolean zbuff_state = glIsEnabled(GL_DEPTH_TEST);
1082 GLboolean blend_state = glIsEnabled(GL_BLEND);
2166f0fa 1083
de75ed09 1084 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1085 glEnable(GL_BLEND);
2166f0fa 1086
de75ed09 1087 glDepthFunc(GL_EQUAL);
1088 glDepthMask(GL_FALSE);
1089 glEnable(GL_DEPTH_TEST);
2166f0fa 1090
2166f0fa 1091 AWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
2166f0fa 1092
2166f0fa
SK
1093 // Render the view
1094 RenderStructs(AWorkspace);
bf75be98 1095 AWorkspace->DisableTexture();
2166f0fa 1096
de75ed09 1097 /* restauration des parametres OpenGL */
1098 glBlendFunc(blend_src, blend_dst);
1099 if (!blend_state) glDisable(GL_BLEND);
2166f0fa 1100
de75ed09 1101 glDepthFunc(zbuff_f);
1102 glDepthMask(zbuff_w);
1103 if (!zbuff_state) glDisable(GL_DEPTH_FUNC);
1104 }
1105 break;
2166f0fa
SK
1106 }
1107
26395493 1108 // Resetting GL parameters according to the default aspects
1109 // in order to synchronize GL state with the graphic driver state
1110 // before drawing auxiliary stuff (trihedrons, overlayer)
1111 // and invoking optional callbacks
1112 AWorkspace->ResetAppliedAspect();
2166f0fa 1113
4269bd1b 1114 // Unset clip planes managed by driver
1115 aContext->ChangeClipping().Unset (aContext->Clipping().Planes());
2166f0fa 1116
a174a3c5 1117 // display global trihedron
1118 if (myTrihedron != NULL)
1119 {
1120 myTrihedron->Render (AWorkspace);
1121 }
1122 if (myGraduatedTrihedron != NULL)
1123 {
1124 myGraduatedTrihedron->Render (AWorkspace);
1125 }
2166f0fa 1126
2166f0fa
SK
1127 // Restore face culling
1128 if ( myBackfacing )
1129 {
1130 if ( isCullFace )
1131 {
1132 glEnable ( GL_CULL_FACE );
1133 glCullFace ( GL_BACK );
1134 }
529afc1a 1135 else
2166f0fa
SK
1136 glDisable ( GL_CULL_FACE );
1137 }
1138
1139 /////////////////////////////////////////////////////////////////////////////
1140 // Step 6: Draw overlayer
529afc1a
K
1141 const int aMode = 0;
1142 AWorkspace->DisplayCallback (ACView, (aMode | OCC_PRE_OVERLAY));
2166f0fa 1143
a174a3c5 1144 RedrawLayer2d (thePrintContext, AWorkspace, ACView, ACOverLayer);
2166f0fa 1145
529afc1a 1146 AWorkspace->DisplayCallback (ACView, aMode);
2166f0fa
SK
1147
1148 // Restore clipping planes
1149 for ( ptrPlane = oldPlanes, planeid = GL_CLIP_PLANE0; planeid < lastid; planeid++, ptrPlane++ )
1150 {
1151 glClipPlane( planeid, ptrPlane->Equation );
1152 if ( ptrPlane->isEnabled )
1153 glEnable( planeid );
529afc1a 1154 else
2166f0fa
SK
1155 glDisable( planeid );
1156 }
1157 delete[] oldPlanes;
1158}
1159
1160/*----------------------------------------------------------------------*/
1161
1162//ExecuteViewDisplay
1163void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
1164{
59f45b7c 1165 if ( myZLayers.NbStructures() <= 0 )
1166 return;
2166f0fa
SK
1167
1168 glPushAttrib ( GL_DEPTH_BUFFER_BIT );
1169
2166f0fa
SK
1170 //TsmPushAttri(); /* save previous graphics context */
1171
1172 if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
1173 {
1174 const int antiAliasingMode = AWorkspace->GetDisplay()->AntiAliasingMode();
1175
1176 if ( !myAntiAliasing )
1177 {
1178 glDisable(GL_POINT_SMOOTH);
1179 glDisable(GL_LINE_SMOOTH);
1180 if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
1181 glBlendFunc (GL_ONE, GL_ZERO);
1182 glDisable (GL_BLEND);
1183 }
1184 else
1185 {
1186 glEnable(GL_POINT_SMOOTH);
1187 glEnable(GL_LINE_SMOOTH);
1188 if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
3c3131a0 1189 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2166f0fa
SK
1190 glEnable (GL_BLEND);
1191 }
1192 }
1193
59f45b7c 1194 myZLayers.Render (AWorkspace);
2166f0fa
SK
1195
1196 //TsmPopAttri(); /* restore previous graphics context; before update lights */
de75ed09 1197 glPopAttrib();
2166f0fa
SK
1198}
1199
1200/*----------------------------------------------------------------------*/
1201
1202//call_togl_redraw_layer2d
a174a3c5 1203void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
35e08fe8 1204 const Handle(OpenGl_Workspace)& /*AWorkspace*/,
a174a3c5 1205 const Graphic3d_CView& ACView,
1206 const Aspect_CLayer2d& ACLayer)
2166f0fa
SK
1207{
1208 if (&ACLayer == NULL
1209 || ACLayer.ptrLayer == NULL
1210 || ACLayer.ptrLayer->listIndex == 0) return;
1211
529afc1a
K
1212 GLsizei dispWidth = (GLsizei )ACLayer.viewport[0];
1213 GLsizei dispHeight = (GLsizei )ACLayer.viewport[1];
2166f0fa 1214
2166f0fa
SK
1215 glMatrixMode( GL_MODELVIEW );
1216 glPushMatrix ();
1217 glLoadIdentity ();
1218
1219 glMatrixMode (GL_PROJECTION);
1220 glPushMatrix ();
1221 glLoadIdentity ();
1222
1223 if (!ACLayer.sizeDependent)
1224 glViewport (0, 0, dispWidth, dispHeight);
1225
1226 float left = ACLayer.ortho[0];
1227 float right = ACLayer.ortho[1];
1228 float bottom = ACLayer.ortho[2];
1229 float top = ACLayer.ortho[3];
1230
1231 int attach = ACLayer.attach;
1232
1233 float ratio;
1234 if (!ACLayer.sizeDependent)
1235 ratio = (float) dispWidth/dispHeight;
1236 else
1237 ratio = ACView.DefWindow.dx/ACView.DefWindow.dy;
1238
1239 float delta;
c9d4eb9d 1240 if (ratio >= 1.0) {
2166f0fa
SK
1241 delta = (float )((top - bottom)/2.0);
1242 switch (attach) {
1243 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1244 top = bottom + 2*delta/ratio;
1245 break;
1246 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1247 top = bottom + 2*delta/ratio;
1248 break;
1249 case 2: /* Aspect_TOC_TOP_LEFT */
1250 bottom = top - 2*delta/ratio;
1251 break;
1252 case 3: /* Aspect_TOC_TOP_RIGHT */
1253 bottom = top - 2*delta/ratio;
1254 break;
1255 }
1256 }
c9d4eb9d 1257 else {
2166f0fa
SK
1258 delta = (float )((right - left)/2.0);
1259 switch (attach) {
1260 case 0: /* Aspect_TOC_BOTTOM_LEFT */
1261 right = left + 2*delta*ratio;
1262 break;
1263 case 1: /* Aspect_TOC_BOTTOM_RIGHT */
1264 left = right - 2*delta*ratio;
1265 break;
1266 case 2: /* Aspect_TOC_TOP_LEFT */
1267 right = left + 2*delta*ratio;
1268 break;
1269 case 3: /* Aspect_TOC_TOP_RIGHT */
1270 left = right - 2*delta*ratio;
1271 break;
1272 }
1273 }
1274
a174a3c5 1275#ifdef _WIN32
2166f0fa 1276 // Check printer context that exists only for print operation
a174a3c5 1277 if (!thePrintContext.IsNull())
2166f0fa
SK
1278 {
1279 // additional transformation matrix could be applied to
1280 // render only those parts of viewport that will be
1281 // passed to a printer as a current "frame" to provide
1282 // tiling; scaling of graphics by matrix helps render a
1283 // part of a view (frame) in same viewport, but with higher
1284 // resolution
a174a3c5 1285 thePrintContext->LoadProjTransformation();
2166f0fa
SK
1286
1287 // printing operation also assumes other viewport dimension
1288 // to comply with transformation matrix or graphics scaling
1289 // factors for tiling for layer redraw
1290 GLsizei anViewportX = 0;
1291 GLsizei anViewportY = 0;
a174a3c5 1292 thePrintContext->GetLayerViewport (anViewportX, anViewportY);
2166f0fa
SK
1293 if (anViewportX != 0 && anViewportY != 0)
1294 glViewport (0, 0, anViewportX, anViewportY);
1295 }
3c3131a0 1296#endif
2166f0fa
SK
1297
1298 glOrtho (left, right, bottom, top, -1.0, 1.0);
1299
2166f0fa
SK
1300 glPushAttrib (
1301 GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT |
1302 GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT );
c9d4eb9d 1303
2166f0fa 1304 glDisable (GL_DEPTH_TEST);
c9d4eb9d 1305 glDisable (GL_TEXTURE_1D);
1306 glDisable (GL_TEXTURE_2D);
1307 glDisable (GL_LIGHTING);
1308
1309 // TODO: Obsolete code, the display list is always empty now, to be removed
2166f0fa
SK
1310 glCallList (ACLayer.ptrLayer->listIndex);
1311
1312 //calling dynamic render of LayerItems
1313 if ( ACLayer.ptrLayer->layerData )
1314 {
1315 InitLayerProp(ACLayer.ptrLayer->listIndex);
1316 ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems();
1317 InitLayerProp(0);
1318 }
1319
1320 glPopAttrib ();
1321
2166f0fa
SK
1322 glMatrixMode (GL_PROJECTION);
1323 glPopMatrix ();
1324
1325 glMatrixMode( GL_MODELVIEW );
1326 glPopMatrix ();
1327
2166f0fa
SK
1328 if (!ACLayer.sizeDependent)
1329 glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
1330
1331 glFlush ();
2166f0fa
SK
1332}
1333
1334/*----------------------------------------------------------------------*/
1335
1336//call_togl_create_bg_texture
3c3131a0 1337void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath,
1338 const Aspect_FillMethod theFillStyle)
2166f0fa 1339{
3c3131a0 1340 if (myBgTexture.TexId != 0)
2166f0fa 1341 {
3c3131a0 1342 // delete existing texture
1343 glDeleteTextures (1, (GLuint* )&(myBgTexture.TexId));
2166f0fa
SK
1344 myBgTexture.TexId = 0;
1345 }
1346
3c3131a0 1347 // load image from file
1348 Image_AlienPixMap anImageLoaded;
1349 if (!anImageLoaded.Load (theFilePath))
1350 {
1351 return;
1352 }
1353
1354 Image_PixMap anImage;
1355 if (anImageLoaded.RowExtraBytes() == 0 &&
1356 (anImageLoaded.Format() == Image_PixMap::ImgRGB
1357 || anImageLoaded.Format() == Image_PixMap::ImgRGB32
1358 || anImageLoaded.Format() == Image_PixMap::ImgRGBA))
2166f0fa 1359 {
3c3131a0 1360 anImage.InitWrapper (anImageLoaded.Format(), anImageLoaded.ChangeData(),
1361 anImageLoaded.SizeX(), anImageLoaded.SizeY(), anImageLoaded.SizeRowBytes());
1362 }
1363 else
1364 {
1365 // convert image to RGB format
1366 if (!anImage.InitTrash (Image_PixMap::ImgRGB, anImageLoaded.SizeX(), anImageLoaded.SizeY()))
1367 {
1368 return;
1369 }
1370
1371 anImage.SetTopDown (false);
1372 Image_PixMapData<Image_ColorRGB>& aDataNew = anImage.EditData<Image_ColorRGB>();
1373 Quantity_Color aSrcColor;
1374 for (Standard_Size aRow = 0; aRow < anImage.SizeY(); ++aRow)
1375 {
1376 for (Standard_Size aCol = 0; aCol < anImage.SizeX(); ++aCol)
2166f0fa 1377 {
6a7d83c4 1378 aSrcColor = anImageLoaded.PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
3c3131a0 1379 Image_ColorRGB& aColor = aDataNew.ChangeValue (aRow, aCol);
8263fcd3 1380 aColor.r() = Standard_Byte(255.0 * aSrcColor.Red());
1381 aColor.g() = Standard_Byte(255.0 * aSrcColor.Green());
1382 aColor.b() = Standard_Byte(255.0 * aSrcColor.Blue());
2166f0fa 1383 }
3c3131a0 1384 }
1385 anImageLoaded.Clear();
1386 }
2166f0fa 1387
3c3131a0 1388 // create MipMapped texture
1389 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
2166f0fa 1390
3c3131a0 1391 GLuint aTextureId = 0;
1392 glGenTextures (1, &aTextureId);
1393 glBindTexture (GL_TEXTURE_2D, aTextureId);
2166f0fa 1394
3c3131a0 1395 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1396 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1397 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1398 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
2166f0fa 1399
3c3131a0 1400 const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
1401 gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
1402 GLint(anImage.SizeX()), GLint(anImage.SizeY()),
1403 aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
2166f0fa 1404
3c3131a0 1405 myBgTexture.TexId = aTextureId;
1406 myBgTexture.Width = (Standard_Integer )anImage.SizeX();
1407 myBgTexture.Height = (Standard_Integer )anImage.SizeY();
1408 myBgTexture.Style = theFillStyle;
2166f0fa
SK
1409}
1410
1411/*----------------------------------------------------------------------*/
1412
1413//call_togl_set_bg_texture_style
1414void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod AFillStyle)
1415{
f8b2ed36 1416 myBgTexture.Style = AFillStyle;
2166f0fa
SK
1417}
1418
1419/*----------------------------------------------------------------------*/
1420
1421//call_togl_gradient_background
1422void OpenGl_View::SetBackgroundGradient (const Quantity_Color& AColor1,
1423 const Quantity_Color& AColor2,
1424 const Aspect_GradientFillMethod AType)
1425{
1426 Standard_Real R,G,B;
1427 AColor1.Values( R, G, B, Quantity_TOC_RGB );
1428 myBgGradient.color1.rgb[0] = ( Tfloat )R;
1429 myBgGradient.color1.rgb[1] = ( Tfloat )G;
1430 myBgGradient.color1.rgb[2] = ( Tfloat )B;
1431 myBgGradient.color1.rgb[3] = 0.F;
1432
1433 AColor2.Values( R, G, B, Quantity_TOC_RGB );
1434 myBgGradient.color2.rgb[0] = ( Tfloat )R;
1435 myBgGradient.color2.rgb[1] = ( Tfloat )G;
1436 myBgGradient.color2.rgb[2] = ( Tfloat )B;
1437 myBgGradient.color2.rgb[3] = 0.F;
1438
1439 myBgGradient.type = AType;
1440}
1441
1442/*----------------------------------------------------------------------*/
1443
1444//call_togl_set_gradient_type
1445void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod AType)
1446{
f8b2ed36 1447 myBgGradient.type = AType;
2166f0fa
SK
1448}
1449
59f45b7c 1450//=======================================================================
1451//function : AddZLayer
3c3131a0 1452//purpose :
59f45b7c 1453//=======================================================================
1454
1455void OpenGl_View::AddZLayer (const Standard_Integer theLayerId)
1456{
1457 myZLayers.AddLayer (theLayerId);
1458}
1459
1460//=======================================================================
1461//function : RemoveZLayer
3c3131a0 1462//purpose :
59f45b7c 1463//=======================================================================
1464
1465void OpenGl_View::RemoveZLayer (const Standard_Integer theLayerId)
1466{
1467 myZLayers.RemoveLayer (theLayerId);
1468}
1469
1470//=======================================================================
1471//function : DisplayStructure
3c3131a0 1472//purpose :
59f45b7c 1473//=======================================================================
1474
1475void OpenGl_View::DisplayStructure (const OpenGl_Structure *theStructure,
1476 const Standard_Integer thePriority)
1477{
1478 Standard_Integer aZLayer = theStructure->GetZLayer ();
1479 myZLayers.AddStructure (theStructure, aZLayer, thePriority);
1480}
1481
1482//=======================================================================
1483//function : EraseStructure
3c3131a0 1484//purpose :
59f45b7c 1485//=======================================================================
1486
1487void OpenGl_View::EraseStructure (const OpenGl_Structure *theStructure)
1488{
1489 Standard_Integer aZLayer = theStructure->GetZLayer ();
1490 myZLayers.RemoveStructure (theStructure, aZLayer);
1491}
1492
1493//=======================================================================
1494//function : ChangeZLayer
1495//purpose :
1496//=======================================================================
1497
1498void OpenGl_View::ChangeZLayer (const OpenGl_Structure *theStructure,
1499 const Standard_Integer theNewLayerId)
1500{
1501 Standard_Integer anOldLayer = theStructure->GetZLayer ();
1502 myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId);
1503}