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