0024552: Convertation of the generic classes to the non-generic (BndLib).
[occt.git] / src / OpenGl / OpenGl_View_1.cxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_GlCore11.hxx>
17
18 #include <OpenGl_View.hxx>
19
20 #include <Visual3d_Layer.hxx>
21
22 #include <OpenGl_tgl_funcs.hxx>
23 #include <OpenGl_PrinterContext.hxx>
24 #include <OpenGl_Workspace.hxx>
25
26 /*----------------------------------------------------------------------*/
27
28 //TelProjectionRaster in OpenGl_telem_util.cxx
29 Standard_Boolean OpenGl_View::ProjectObjectToRaster (const Standard_Integer w, const Standard_Integer h,
30                                                     const Standard_ShortReal x, const Standard_ShortReal y, const Standard_ShortReal z,
31                                                     Standard_ShortReal &xr, Standard_ShortReal &yr)
32 {
33   int i, j, k;
34
35   GLdouble modelMatrix[16];
36   for (k = 0, i = 0; i < 4; i++)
37     for (j = 0; j < 4; j++, k++)
38       modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
39
40   GLdouble projMatrix[16];
41   for (k = 0, i = 0; i < 4; i++)
42     for (j = 0; j < 4; j++, k++)
43       projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
44
45   GLint viewport[4];
46   viewport[0] = 0;
47   viewport[1] = 0;
48   viewport[2] = w;
49   viewport[3] = h;
50
51   /*
52   * glGetIntegerv (GL_VIEWPORT, viewport);
53   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
54   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
55   */
56
57   GLdouble winx, winy, winz;
58   if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
59   {
60     xr = ( Standard_ShortReal )winx;
61     yr = ( Standard_ShortReal )winy;
62     return Standard_True;
63   }
64
65   xr = 0.F;
66   yr = 0.F;
67   return Standard_False;
68 }
69
70 /*----------------------------------------------------------------------*/
71 //TelUnProjectionRaster
72 Standard_Boolean OpenGl_View::ProjectRasterToObject (const Standard_Integer w, const Standard_Integer h,
73                                                     const Standard_Integer xr, const Standard_Integer yr,
74                                                     Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z)
75 {
76   int i, j, k;
77
78   GLdouble modelMatrix[16];
79   for (k = 0, i = 0; i < 4; i++)
80     for (j = 0; j < 4; j++, k++)
81       modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
82
83   GLdouble projMatrix[16];
84   for (k = 0, i = 0; i < 4; i++)
85     for (j = 0; j < 4; j++, k++)
86       projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
87
88   GLint viewport[4];
89   viewport[0] = 0;
90   viewport[1] = 0;
91   viewport[2] = w;
92   viewport[3] = h;
93
94   /*
95   * glGetIntegerv (GL_VIEWPORT, viewport);
96   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
97   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
98   */
99
100   GLdouble objx, objy, objz;
101   if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
102   {
103     x = ( Standard_ShortReal )objx;
104     y = ( Standard_ShortReal )objy;
105     z = ( Standard_ShortReal )objz;
106     return Standard_True;
107   }
108
109   x = 0.F;
110   y = 0.F;
111   z = 0.F;
112   return Standard_False;
113 }
114
115 /*----------------------------------------------------------------------*/
116 //TelUnProjectionRasterWithRay
117 Standard_Boolean OpenGl_View::ProjectRasterToObjectWithRay (const Standard_Integer w, const Standard_Integer h,
118                                                            const Standard_Integer xr, const Standard_Integer yr,
119                                                            Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z,
120                                                            Standard_ShortReal &dx, Standard_ShortReal &dy, Standard_ShortReal &dz)
121 {
122   int i, j, k;
123
124   GLdouble modelMatrix[16];
125   for (k = 0, i = 0; i < 4; i++)
126     for (j = 0; j < 4; j++, k++)
127       modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
128
129   GLdouble projMatrix[16];
130   for (k = 0, i = 0; i < 4; i++)
131     for (j = 0; j < 4; j++, k++)
132       projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
133
134   GLint viewport[4];
135   viewport[0] = 0;
136   viewport[1] = 0;
137   viewport[2] = w;
138   viewport[3] = h;
139
140   /*
141   * glGetIntegerv (GL_VIEWPORT, viewport);
142   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
143   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
144   */
145
146   const GLdouble winx = ( GLdouble )xr;
147   const GLdouble winy = ( GLdouble )yr;
148
149   GLdouble objx, objy, objz;
150   if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
151   {
152     GLdouble objx1, objy1, objz1;
153     if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
154     {
155       x = ( Standard_ShortReal )objx;
156       y = ( Standard_ShortReal )objy;
157       z = ( Standard_ShortReal )objz;
158       dx = ( Standard_ShortReal )(objx-objx1);
159       dy = ( Standard_ShortReal )(objy-objy1);
160       dz = ( Standard_ShortReal )(objz-objz1);
161       return Standard_True;
162     }
163   }
164
165   x = 0.F;
166   y = 0.F;
167   z = 0.F;
168   dx = 0.F;
169   dy = 0.F;
170   dz = 0.F;
171   return Standard_False;
172 }
173
174 /*----------------------------------------------------------------------*/
175
176 //call_togl_inquiremat
177 void OpenGl_View::GetMatrices (TColStd_Array2OfReal&  theMatOrient,
178                                TColStd_Array2OfReal&  theMatMapping,
179                                const Standard_Boolean theIsCustom) const
180 {
181   int i, j;
182   if (theIsCustom)
183   {
184     // OCC18942: Trying to return the current matrices instead of calculating them anew.
185     // This in particular allows using application-defined matrices.
186     for (i = 0; i < 4; ++i)
187     {
188       for (j = 0; j < 4; ++j)
189       {
190         theMatOrient  (i, j) = myOrientationMatrix[j][i];
191         theMatMapping (i, j) = myMappingMatrix[j][i];
192       }
193     }
194     return;
195   }
196
197   int anErr = 0;
198   Tmatrix3 aMatOri;
199   Tmatrix3 aMatMapping;
200
201   view_map3 aViewMap;
202   memcpy (&aViewMap.win,     &myExtra.map.window,   sizeof(Tlimit));
203   memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
204   switch (myExtra.map.proj)
205   {
206     default:
207     case TelParallel:    aViewMap.proj_type = TYPE_PARAL;    break;
208     case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
209   }
210   aViewMap.proj_ref_point.x = myExtra.map.prp[0];
211   aViewMap.proj_ref_point.y = myExtra.map.prp[1];
212   aViewMap.proj_ref_point.z = myExtra.map.prp[2];
213   aViewMap.view_plane  = myExtra.map.vpd;
214   aViewMap.back_plane  = myExtra.map.bpd;
215   aViewMap.front_plane = myExtra.map.fpd;
216
217   call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
218                               (const vec3*   )myExtra.vpn,
219                               (const vec3*   )myExtra.vup,
220                               &anErr, aMatOri);
221   if (anErr == 0)
222     call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
223
224   if (anErr == 0)
225   {
226     for (i = 0; i < 4; ++i)
227     {
228       for (j = 0; j < 4; ++j)
229       {
230         theMatOrient  (i, j) = aMatOri[j][i];
231         theMatMapping (i, j) = aMatMapping[j][i];
232       }
233     }
234     return;
235   }
236
237   // return just identity matrices
238   for (i = 0; i < 4; ++i)
239   {
240     for (j = 0; j < 4; ++j)
241     {
242       if (i == j) {
243         theMatMapping (i, j) = 1.0;
244         theMatOrient  (i, j) = 1.0;
245       }
246       else {
247         theMatMapping (i, j)  = 0.0;
248         theMatOrient  (i, j)  = 0.0;
249       }
250     }
251   }
252 }
253
254 /*----------------------------------------------------------------------*/