0023428: Extend OpenGl_Context to use Geometry Shaders extension
[occt.git] / src / OpenGl / OpenGl_View_1.cxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20
21 #include <OpenGl_GlCore11.hxx>
22
23 #include <OpenGl_View.hxx>
24
25 #include <Visual3d_Layer.hxx>
26
27 #include <OpenGl_tgl_funcs.hxx>
28 #include <OpenGl_PrinterContext.hxx>
29 #include <OpenGl_Workspace.hxx>
30
31 #include <GL/glu.h> // gluProject(), gluUnProject()
32
33 /*----------------------------------------------------------------------*/
34
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)
39 {
40   int i, j, k;
41
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];
46
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];
51
52   GLint viewport[4];
53   viewport[0] = 0;
54   viewport[1] = 0;
55   viewport[2] = w;
56   viewport[3] = h;
57
58   /*
59   * glGetIntegerv (GL_VIEWPORT, viewport);
60   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
61   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
62   */
63
64   GLdouble winx, winy, winz;
65   if (gluProject (( GLdouble )x, ( GLdouble )y, ( GLdouble )z, modelMatrix, projMatrix, viewport, &winx, &winy, &winz))
66   {
67     xr = ( Standard_ShortReal )winx;
68     yr = ( Standard_ShortReal )winy;
69     return Standard_True;
70   }
71
72   xr = 0.F;
73   yr = 0.F;
74   return Standard_False;
75 }
76
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)
82 {
83   int i, j, k;
84
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];
89
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];
94
95   GLint viewport[4];
96   viewport[0] = 0;
97   viewport[1] = 0;
98   viewport[2] = w;
99   viewport[3] = h;
100
101   /*
102   * glGetIntegerv (GL_VIEWPORT, viewport);
103   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
104   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
105   */
106
107   GLdouble objx, objy, objz;
108   if (gluUnProject (( GLdouble )xr, ( GLdouble )yr, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
109   {
110     x = ( Standard_ShortReal )objx;
111     y = ( Standard_ShortReal )objy;
112     z = ( Standard_ShortReal )objz;
113     return Standard_True;
114   }
115
116   x = 0.F;
117   y = 0.F;
118   z = 0.F;
119   return Standard_False;
120 }
121
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)
128 {
129   int i, j, k;
130
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];
135
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];
140
141   GLint viewport[4];
142   viewport[0] = 0;
143   viewport[1] = 0;
144   viewport[2] = w;
145   viewport[3] = h;
146
147   /*
148   * glGetIntegerv (GL_VIEWPORT, viewport);
149   * glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
150   * glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
151   */
152
153   const GLdouble winx = ( GLdouble )xr;
154   const GLdouble winy = ( GLdouble )yr;
155
156   GLdouble objx, objy, objz;
157   if (gluUnProject (winx, winy, 0.0, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))
158   {
159     GLdouble objx1, objy1, objz1;
160     if (gluUnProject (winx, winy, -10.0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz1))
161     {
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;
169     }
170   }
171
172   x = 0.F;
173   y = 0.F;
174   z = 0.F;
175   dx = 0.F;
176   dy = 0.F;
177   dz = 0.F;
178   return Standard_False;
179 }
180
181 /*----------------------------------------------------------------------*/
182
183 //call_togl_inquiremat
184 void OpenGl_View::GetMatrices (TColStd_Array2OfReal&  theMatOrient,
185                                TColStd_Array2OfReal&  theMatMapping,
186                                const Standard_Boolean theIsCustom) const
187 {
188   int i, j;
189   if (theIsCustom)
190   {
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)
194     {
195       for (j = 0; j < 4; ++j)
196       {
197         theMatOrient  (i, j) = myOrientationMatrix[j][i];
198         theMatMapping (i, j) = myMappingMatrix[j][i];
199       }
200     }
201     return;
202   }
203
204   int anErr = 0;
205   Tmatrix3 aMatOri;
206   Tmatrix3 aMatMapping;
207
208   view_map3 aViewMap;
209   memcpy (&aViewMap.win,     &myExtra.map.window,   sizeof(Tlimit));
210   memcpy (&aViewMap.proj_vp, &myExtra.map.viewport, sizeof(Tlimit3));
211   switch (myExtra.map.proj)
212   {
213     default:
214     case TelParallel:    aViewMap.proj_type = TYPE_PARAL;    break;
215     case TelPerspective: aViewMap.proj_type = TYPE_PERSPECT; break;
216   }
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;
223
224   call_func_eval_ori_matrix3 ((const point3* )myExtra.vrp,
225                               (const vec3*   )myExtra.vpn,
226                               (const vec3*   )myExtra.vup,
227                               &anErr, aMatOri);
228   if (anErr == 0)
229     call_func_eval_map_matrix3 (&aViewMap, &anErr, aMatMapping);
230
231   if (anErr == 0)
232   {
233     for (i = 0; i < 4; ++i)
234     {
235       for (j = 0; j < 4; ++j)
236       {
237         theMatOrient  (i, j) = aMatOri[j][i];
238         theMatMapping (i, j) = aMatMapping[j][i];
239       }
240     }
241     return;
242   }
243
244   // return just identity matrices
245   for (i = 0; i < 4; ++i)
246   {
247     for (j = 0; j < 4; ++j)
248     {
249       if (i == j) {
250         theMatMapping (i, j) = 1.0;
251         theMatOrient  (i, j) = 1.0;
252       }
253       else {
254         theMatMapping (i, j)  = 0.0;
255         theMatOrient  (i, j)  = 0.0;
256       }
257     }
258   }
259 }
260
261 /*----------------------------------------------------------------------*/