1 // File: OpenGl_View_1.cxx
2 // Created: 20 September 2011
3 // Author: Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
6 #include <OpenGl_GlCore11.hxx>
8 #include <OpenGl_View.hxx>
10 #include <Visual3d_Layer.hxx>
12 #include <OpenGl_tgl_funcs.hxx>
13 #include <OpenGl_PrinterContext.hxx>
14 #include <OpenGl_Workspace.hxx>
16 #include <GL/glu.h> // gluProject(), gluUnProject()
18 /*----------------------------------------------------------------------*/
20 //TelProjectionRaster in OpenGl_telem_util.cxx
21 Standard_Boolean OpenGl_View::ProjectObjectToRaster (const Standard_Integer w, const Standard_Integer h,
22 const Standard_ShortReal x, const Standard_ShortReal y, const Standard_ShortReal z,
23 Standard_ShortReal &xr, Standard_ShortReal &yr)
27 GLdouble modelMatrix[16];
28 for (k = 0, i = 0; i < 4; i++)
29 for (j = 0; j < 4; j++, k++)
30 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
32 GLdouble projMatrix[16];
33 for (k = 0, i = 0; i < 4; i++)
34 for (j = 0; j < 4; j++, k++)
35 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
44 * glGetIntegerv (GL_VIEWPORT, viewport);
45 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
46 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
49 GLdouble winx, winy, winz;
50 if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
52 xr = ( Standard_ShortReal )winx;
53 yr = ( Standard_ShortReal )winy;
59 return Standard_False;
62 /*----------------------------------------------------------------------*/
63 //TelUnProjectionRaster
64 Standard_Boolean OpenGl_View::ProjectRasterToObject (const Standard_Integer w, const Standard_Integer h,
65 const Standard_Integer xr, const Standard_Integer yr,
66 Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z)
70 GLdouble modelMatrix[16];
71 for (k = 0, i = 0; i < 4; i++)
72 for (j = 0; j < 4; j++, k++)
73 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
75 GLdouble projMatrix[16];
76 for (k = 0, i = 0; i < 4; i++)
77 for (j = 0; j < 4; j++, k++)
78 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
87 * glGetIntegerv (GL_VIEWPORT, viewport);
88 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
89 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
92 GLdouble objx, objy, objz;
93 if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
95 x = ( Standard_ShortReal )objx;
96 y = ( Standard_ShortReal )objy;
97 z = ( Standard_ShortReal )objz;
104 return Standard_False;
107 /*----------------------------------------------------------------------*/
108 //TelUnProjectionRasterWithRay
109 Standard_Boolean OpenGl_View::ProjectRasterToObjectWithRay (const Standard_Integer w, const Standard_Integer h,
110 const Standard_Integer xr, const Standard_Integer yr,
111 Standard_ShortReal &x, Standard_ShortReal &y, Standard_ShortReal &z,
112 Standard_ShortReal &dx, Standard_ShortReal &dy, Standard_ShortReal &dz)
116 GLdouble modelMatrix[16];
117 for (k = 0, i = 0; i < 4; i++)
118 for (j = 0; j < 4; j++, k++)
119 modelMatrix[k] = ( GLdouble )myOrientationMatrix[i][j];
121 GLdouble projMatrix[16];
122 for (k = 0, i = 0; i < 4; i++)
123 for (j = 0; j < 4; j++, k++)
124 projMatrix[k] = ( GLdouble )myMappingMatrix[i][j];
133 * glGetIntegerv (GL_VIEWPORT, viewport);
134 * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
135 * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
138 const GLdouble winx = ( GLdouble )xr;
139 const GLdouble winy = ( GLdouble )yr;
141 GLdouble objx, objy, objz;
142 if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
144 GLdouble objx1, objy1, objz1;
145 if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
147 x = ( Standard_ShortReal )objx;
148 y = ( Standard_ShortReal )objy;
149 z = ( Standard_ShortReal )objz;
150 dx = ( Standard_ShortReal )(objx-objx1);
151 dy = ( Standard_ShortReal )(objy-objy1);
152 dz = ( Standard_ShortReal )(objz-objz1);
153 return Standard_True;
163 return Standard_False;
166 /*----------------------------------------------------------------------*/
168 //call_togl_inquiremat
169 void OpenGl_View::GetMatrices (TColStd_Array2OfReal& theMatOrient,
170 TColStd_Array2OfReal& theMatMapping,
171 const Standard_Boolean theIsCustom) const
176 // OCC18942: Trying to return the current matrices instead of calculating them anew.
177 // This in particular allows using application-defined matrices.
178 for (i = 0; i < 4; ++i)
180 for (j = 0; j < 4; ++j)
182 theMatOrient (i, j) = myOrientationMatrix[j][i];
183 theMatMapping (i, j) = myMappingMatrix[j][i];
191 Tmatrix3 aMatMapping;
194 memcpy (&aViewMap.win, &myExtra.map.window, sizeof(Tlimit));
195 memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
196 switch (myExtra.map.proj)
199 case TelParallel: aViewMap.proj_type = TYPE_PARAL; break;
200 case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
202 aViewMap.proj_ref_point.x = myExtra.map.prp[0];
203 aViewMap.proj_ref_point.y = myExtra.map.prp[1];
204 aViewMap.proj_ref_point.z = myExtra.map.prp[2];
205 aViewMap.view_plane = myExtra.map.vpd;
206 aViewMap.back_plane = myExtra.map.bpd;
207 aViewMap.front_plane = myExtra.map.fpd;
209 call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
210 (const vec3* )myExtra.vpn,
211 (const vec3* )myExtra.vup,
214 call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
218 for (i = 0; i < 4; ++i)
220 for (j = 0; j < 4; ++j)
222 theMatOrient (i, j) = aMatOri[j][i];
223 theMatMapping (i, j) = aMatMapping[j][i];
229 // return just identity matrices
230 for (i = 0; i < 4; ++i)
232 for (j = 0; j < 4; ++j)
235 theMatMapping (i, j) = 1.0;
236 theMatOrient (i, j) = 1.0;
239 theMatMapping (i, j) = 0.0;
240 theMatOrient (i, j) = 0.0;
246 /*----------------------------------------------------------------------*/