1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <OpenGl_GlCore11.hxx>
23 #include <OpenGl_View.hxx>
25 #include <Visual3d_Layer.hxx>
27 #include <OpenGl_tgl_funcs.hxx>
28 #include <OpenGl_PrinterContext.hxx>
29 #include <OpenGl_Workspace.hxx>
31 #include <GL/glu.h> // gluProject(), gluUnProject()
33 /*----------------------------------------------------------------------*/
35 //TelProjectionRaster in OpenGl_telem_util.cxx
36 Standard_Boolean OpenGl_View::ProjectObjectToRaster (const Standard_Integer w, const Standard_Integer h,
37 const Standard_ShortReal x, const Standard_ShortReal y, const Standard_ShortReal z,
38 Standard_ShortReal &xr, Standard_ShortReal &yr)
42 GLdouble modelMatrix[16];
43 for (k = 0, i = 0; i < 4; i++)
44 for (j = 0; j < 4; j++, k++)
45 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
47 GLdouble projMatrix[16];
48 for (k = 0, i = 0; i < 4; i++)
49 for (j = 0; j < 4; j++, k++)
50 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
59 * glGetIntegerv (GL_VIEWPORT, viewport);
60 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
61 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
64 GLdouble winx, winy, winz;
65 if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
67 xr = ( Standard_ShortReal )winx;
68 yr = ( Standard_ShortReal )winy;
74 return Standard_False;
77 /*----------------------------------------------------------------------*/
78 //TelUnProjectionRaster
79 Standard_Boolean OpenGl_View::ProjectRasterToObject (const Standard_Integer w, const Standard_Integer h,
80 const Standard_Integer xr, const Standard_Integer yr,
81 Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z)
85 GLdouble modelMatrix[16];
86 for (k = 0, i = 0; i < 4; i++)
87 for (j = 0; j < 4; j++, k++)
88 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
90 GLdouble projMatrix[16];
91 for (k = 0, i = 0; i < 4; i++)
92 for (j = 0; j < 4; j++, k++)
93 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
102 * glGetIntegerv (GL_VIEWPORT, viewport);
103 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
104 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
107 GLdouble objx, objy, objz;
108 if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
110 x = ( Standard_ShortReal )objx;
111 y = ( Standard_ShortReal )objy;
112 z = ( Standard_ShortReal )objz;
113 return Standard_True;
119 return Standard_False;
122 /*----------------------------------------------------------------------*/
123 //TelUnProjectionRasterWithRay
124 Standard_Boolean OpenGl_View::ProjectRasterToObjectWithRay (const Standard_Integer w, const Standard_Integer h,
125 const Standard_Integer xr, const Standard_Integer yr,
126 Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z,
127 Standard_ShortReal &dx, Standard_ShortReal &dy, Standard_ShortReal &dz)
131 GLdouble modelMatrix[16];
132 for (k = 0, i = 0; i < 4; i++)
133 for (j = 0; j < 4; j++, k++)
134 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
136 GLdouble projMatrix[16];
137 for (k = 0, i = 0; i < 4; i++)
138 for (j = 0; j < 4; j++, k++)
139 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
148 * glGetIntegerv (GL_VIEWPORT, viewport);
149 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
150 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
153 const GLdouble winx = ( GLdouble )xr;
154 const GLdouble winy = ( GLdouble )yr;
156 GLdouble objx, objy, objz;
157 if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
159 GLdouble objx1, objy1, objz1;
160 if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
162 x = ( Standard_ShortReal )objx;
163 y = ( Standard_ShortReal )objy;
164 z = ( Standard_ShortReal )objz;
165 dx = ( Standard_ShortReal )(objx-objx1);
166 dy = ( Standard_ShortReal )(objy-objy1);
167 dz = ( Standard_ShortReal )(objz-objz1);
168 return Standard_True;
178 return Standard_False;
181 /*----------------------------------------------------------------------*/
183 //call_togl_inquiremat
184 void OpenGl_View::GetMatrices (TColStd_Array2OfReal& theMatOrient,
185 TColStd_Array2OfReal& theMatMapping,
186 const Standard_Boolean theIsCustom) const
191 // OCC18942: Trying to return the current matrices instead of calculating them anew.
192 // This in particular allows using application-defined matrices.
193 for (i = 0; i < 4; ++i)
195 for (j = 0; j < 4; ++j)
197 theMatOrient (i, j) = myOrientationMatrix[j][i];
198 theMatMapping (i, j) = myMappingMatrix[j][i];
206 Tmatrix3 aMatMapping;
209 memcpy (&aViewMap.win, &myExtra.map.window, sizeof(Tlimit));
210 memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
211 switch (myExtra.map.proj)
214 case TelParallel: aViewMap.proj_type = TYPE_PARAL; break;
215 case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
217 aViewMap.proj_ref_point.x = myExtra.map.prp[0];
218 aViewMap.proj_ref_point.y = myExtra.map.prp[1];
219 aViewMap.proj_ref_point.z = myExtra.map.prp[2];
220 aViewMap.view_plane = myExtra.map.vpd;
221 aViewMap.back_plane = myExtra.map.bpd;
222 aViewMap.front_plane = myExtra.map.fpd;
224 call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
225 (const vec3* )myExtra.vpn,
226 (const vec3* )myExtra.vup,
229 call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
233 for (i = 0; i < 4; ++i)
235 for (j = 0; j < 4; ++j)
237 theMatOrient (i, j) = aMatOri[j][i];
238 theMatMapping (i, j) = aMatMapping[j][i];
244 // return just identity matrices
245 for (i = 0; i < 4; ++i)
247 for (j = 0; j < 4; ++j)
250 theMatMapping (i, j) = 1.0;
251 theMatOrient (i, j) = 1.0;
254 theMatMapping (i, j) = 0.0;
255 theMatOrient (i, j) = 0.0;
261 /*----------------------------------------------------------------------*/