0023022: This is desirable to access OpenGl extensions and core API (1.2+) in one...
[occt.git] / src / OpenGl / OpenGl_View_1.cxx
1 // File:      OpenGl_View_1.cxx
2 // Created:   20 September 2011
3 // Author:    Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
5
6 #include <OpenGl_GlCore11.hxx>
7
8 #include <OpenGl_View.hxx>
9
10 #include <Visual3d_Layer.hxx>
11
12 #include <OpenGl_tgl_funcs.hxx>
13 #include <OpenGl_PrinterContext.hxx>
14 #include <OpenGl_Workspace.hxx>
15
16 #include <GL/glu.h> // gluProject(), gluUnProject()
17
18 /*----------------------------------------------------------------------*/
19
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)
24 {
25   int i, j, k;
26
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];
31
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];
36
37   GLint viewport[4];
38   viewport[0] = 0;
39   viewport[1] = 0;
40   viewport[2] = w;
41   viewport[3] = h;
42
43   /*
44   * glGetIntegerv (GL_VIEWPORT, viewport);
45   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
46   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
47   */
48
49   GLdouble winx, winy, winz;
50   if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
51   {
52     xr = ( Standard_ShortReal )winx;
53     yr = ( Standard_ShortReal )winy;
54     return Standard_True;
55   }
56
57   xr = 0.F;
58   yr = 0.F;
59   return Standard_False;
60 }
61
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)
67 {
68   int i, j, k;
69
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];
74
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];
79
80   GLint viewport[4];
81   viewport[0] = 0;
82   viewport[1] = 0;
83   viewport[2] = w;
84   viewport[3] = h;
85
86   /*
87   * glGetIntegerv (GL_VIEWPORT, viewport);
88   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
89   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
90   */
91
92   GLdouble objx, objy, objz;
93   if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
94   {
95     x = ( Standard_ShortReal )objx;
96     y = ( Standard_ShortReal )objy;
97     z = ( Standard_ShortReal )objz;
98     return Standard_True;
99   }
100
101   x = 0.F;
102   y = 0.F;
103   z = 0.F;
104   return Standard_False;
105 }
106
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)
113 {
114   int i, j, k;
115
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];
120
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];
125
126   GLint viewport[4];
127   viewport[0] = 0;
128   viewport[1] = 0;
129   viewport[2] = w;
130   viewport[3] = h;
131
132   /*
133   * glGetIntegerv (GL_VIEWPORT, viewport);
134   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
135   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
136   */
137
138   const GLdouble winx = ( GLdouble )xr;
139   const GLdouble winy = ( GLdouble )yr;
140
141   GLdouble objx, objy, objz;
142   if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
143   {
144     GLdouble objx1, objy1, objz1;
145     if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
146     {
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;
154     }
155   }
156
157   x = 0.F;
158   y = 0.F;
159   z = 0.F;
160   dx = 0.F;
161   dy = 0.F;
162   dz = 0.F;
163   return Standard_False;
164 }
165
166 /*----------------------------------------------------------------------*/
167
168 //call_togl_inquiremat
169 void OpenGl_View::GetMatrices (TColStd_Array2OfReal&  theMatOrient,
170                                TColStd_Array2OfReal&  theMatMapping,
171                                const Standard_Boolean theIsCustom) const
172 {
173   int i, j;
174   if (theIsCustom)
175   {
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)
179     {
180       for (j = 0; j < 4; ++j)
181       {
182         theMatOrient  (i, j) = myOrientationMatrix[j][i];
183         theMatMapping (i, j) = myMappingMatrix[j][i];
184       }
185     }
186     return;
187   }
188
189   int anErr = 0;
190   Tmatrix3 aMatOri;
191   Tmatrix3 aMatMapping;
192
193   view_map3 aViewMap;
194   memcpy (&aViewMap.win,     &myExtra.map.window,   sizeof(Tlimit));
195   memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
196   switch (myExtra.map.proj)
197   {
198     default:
199     case TelParallel:    aViewMap.proj_type = TYPE_PARAL;    break;
200     case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
201   }
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;
208
209   call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
210                               (const vec3*   )myExtra.vpn,
211                               (const vec3*   )myExtra.vup,
212                               &anErr, aMatOri);
213   if (anErr == 0)
214     call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
215
216   if (anErr == 0)
217   {
218     for (i = 0; i < 4; ++i)
219     {
220       for (j = 0; j < 4; ++j)
221       {
222         theMatOrient  (i, j) = aMatOri[j][i];
223         theMatMapping (i, j) = aMatMapping[j][i];
224       }
225     }
226     return;
227   }
228
229   // return just identity matrices
230   for (i = 0; i < 4; ++i)
231   {
232     for (j = 0; j < 4; ++j)
233     {
234       if (i == j) {
235         theMatMapping (i, j) = 1.0;
236         theMatOrient  (i, j) = 1.0;
237       }
238       else {
239         theMatMapping (i, j)  = 0.0;
240         theMatOrient  (i, j)  = 0.0;
241       }
242     }
243   }
244 }
245
246 /*----------------------------------------------------------------------*/