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