1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_GlCore11.hxx>
18 #include <OpenGl_View.hxx>
20 #include <Visual3d_Layer.hxx>
22 #include <OpenGl_tgl_funcs.hxx>
23 #include <OpenGl_PrinterContext.hxx>
24 #include <OpenGl_Workspace.hxx>
26 /*----------------------------------------------------------------------*/
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)
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];
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];
52 * glGetIntegerv (GL_VIEWPORT, viewport);
53 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
54 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
57 GLdouble winx, winy, winz;
58 if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
60 xr = ( Standard_ShortReal )winx;
61 yr = ( Standard_ShortReal )winy;
67 return Standard_False;
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)
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];
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];
95 * glGetIntegerv (GL_VIEWPORT, viewport);
96 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
97 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
100 GLdouble objx, objy, objz;
101 if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
103 x = ( Standard_ShortReal )objx;
104 y = ( Standard_ShortReal )objy;
105 z = ( Standard_ShortReal )objz;
106 return Standard_True;
112 return Standard_False;
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)
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];
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];
141 * glGetIntegerv (GL_VIEWPORT, viewport);
142 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
143 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
146 const GLdouble winx = ( GLdouble )xr;
147 const GLdouble winy = ( GLdouble )yr;
149 GLdouble objx, objy, objz;
150 if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
152 GLdouble objx1, objy1, objz1;
153 if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
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;
171 return Standard_False;
174 /*----------------------------------------------------------------------*/
176 //call_togl_inquiremat
177 void OpenGl_View::GetMatrices (TColStd_Array2OfReal& theMatOrient,
178 TColStd_Array2OfReal& theMatMapping,
179 const Standard_Boolean theIsCustom) const
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)
188 for (j = 0; j < 4; ++j)
190 theMatOrient (i, j) = myOrientationMatrix[j][i];
191 theMatMapping (i, j) = myMappingMatrix[j][i];
199 Tmatrix3 aMatMapping;
202 memcpy (&aViewMap.win, &myExtra.map.window, sizeof(Tlimit));
203 memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
204 switch (myExtra.map.proj)
207 case TelParallel: aViewMap.proj_type = TYPE_PARAL; break;
208 case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
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;
217 call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
218 (const vec3* )myExtra.vpn,
219 (const vec3* )myExtra.vup,
222 call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
226 for (i = 0; i < 4; ++i)
228 for (j = 0; j < 4; ++j)
230 theMatOrient (i, j) = aMatOri[j][i];
231 theMatMapping (i, j) = aMatMapping[j][i];
237 // return just identity matrices
238 for (i = 0; i < 4; ++i)
240 for (j = 0; j < 4; ++j)
243 theMatMapping (i, j) = 1.0;
244 theMatOrient (i, j) = 1.0;
247 theMatMapping (i, j) = 0.0;
248 theMatOrient (i, j) = 0.0;
254 /*----------------------------------------------------------------------*/