GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
GLCanvas.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "GLCanvas.h"
31 #include "gl-select.h"
32 
33 #include "gl-render.h"
34 #include "gl2ps-print.h"
35 #include "graphics.h"
36 #include "interpreter.h"
37 
39 
40 #if defined (HAVE_QOPENGLWIDGET)
41 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
42 #else
43 # if defined (Q_OS_WIN32)
44 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
45  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel \
46  | QGL::IndirectRendering),
47 # else
48 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
49  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel),
50 # endif
51 #endif
52 
53 GLCanvas::GLCanvas (octave::base_qobject& oct_qobj,
54  octave::interpreter& interp,
55  const graphics_handle& gh, QWidget *xparent)
57  Canvas (oct_qobj, interp, gh), m_glfcns (), m_renderer (m_glfcns)
58 {
59  setFocusPolicy (Qt::ClickFocus);
60  setFocus ();
61 }
62 
64 { }
65 
66 void
68 {
69  m_glfcns.init ();
70 }
71 
72 void
74 {
75  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
76 
77  octave::autolock guard (gh_mgr.graphics_lock ());
78 
79  graphics_object go = gh_mgr.get_object (gh);
80 
81  if (go)
82  {
83  graphics_object fig = go.get_ancestor ("figure");
84  double dpr = fig.get ("__device_pixel_ratio__").double_value ();
85  m_renderer.set_viewport (dpr * width (), dpr * height ());
86  m_renderer.set_device_pixel_ratio (dpr);
87  m_renderer.draw (go);
88  }
89 }
90 
93 {
94  uint8NDArray retval;
95 
96  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
97 
98  graphics_object go = gh_mgr.get_object (gh);
99 
100  if (go && go.isa ("figure"))
101  {
102  Matrix pos = go.get ("position").matrix_value ();
103  double dpr = go.get ("__device_pixel_ratio__").double_value ();
104  pos(2) *= dpr;
105  pos(3) *= dpr;
106 
107  // Make sure we have a valid current context
108  if (! begin_rendering ())
109  return retval;
110 
111  // When the figure is not visible or its size is frozen for printing,
112  // we use a framebuffer object to make sure we are rendering on a
113  // suitably large frame.
114  if (go.get ("visible").string_value () == "off"
115  || go.get ("__printing__").string_value () == "on")
116  {
117  OCTAVE_QT_OPENGL_FBO
118  fbo (pos(2), pos(3),
119  OCTAVE_QT_OPENGL_FBO::Attachment::Depth);
120 
121  fbo.bind ();
122 
123  m_renderer.set_viewport (pos(2), pos(3));
124  m_renderer.set_device_pixel_ratio (dpr);
125  m_renderer.draw (go);
126  retval = m_renderer.get_pixels (pos(2), pos(3));
127 
128  fbo.release ();
129  }
130  else
131  {
132  m_renderer.set_viewport (pos(2), pos(3));
133  m_renderer.set_device_pixel_ratio (dpr);
134  m_renderer.draw (go);
135  retval = m_renderer.get_pixels (pos(2), pos(3));
136  }
137 
138  end_rendering ();
139  }
140 
141  return retval;
142 }
143 
144 void
145 GLCanvas::do_print (const QString& file_cmd, const QString& term,
146  const graphics_handle& handle)
147 {
148  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
149 
150  octave::autolock guard (gh_mgr.graphics_lock ());
151 
152  graphics_object go = gh_mgr.get_object (handle);
153 
154  if (go.valid_object ())
155  {
156  graphics_object fig (go.get_ancestor ("figure"));
157 
158  // Make sure we have a valid current context
159  if (! begin_rendering ())
160  error ("print: no valid OpenGL offscreen context");
161 
162  try
163  {
164  if (fig.get ("visible").string_value () == "on")
165  octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
166  term.toStdString ());
167  else
168  {
169  // When the figure is not visible, we use a framebuffer object
170  // to make sure we are rendering on a suitably large frame.
171  Matrix pos = fig.get ("position").matrix_value ();
172  double dpr = fig.get ("__device_pixel_ratio__").double_value ();
173  pos(2) *= dpr;
174  pos(3) *= dpr;
175 
176  OCTAVE_QT_OPENGL_FBO
177  fbo (pos(2), pos(3),
178  OCTAVE_QT_OPENGL_FBO::Attachment::Depth);
179 
180  fbo.bind ();
181 
182  octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
183  term.toStdString ());
184 
185  fbo.release ();
186  }
187  }
188  catch (octave::execution_exception& ee)
189  {
190  emit interpreter_event
191  ([=] (void)
192  {
193  // INTERPRETER THREAD
194  throw ee;
195  });
196  }
197 
198  end_rendering ();
199  }
200 }
201 
202 graphics_object
203 GLCanvas::selectFromAxes (const graphics_object& ax, const QPoint& pt)
204 {
205  makeCurrent ();
206 
207  if (ax)
208  {
209  octave::opengl_selector s (m_glfcns);
210 
211  s.set_viewport (width (), height ());
212  return s.select (ax, pt.x (), height () - pt.y (),
214  }
215 
216  return graphics_object ();
217 }
218 
219 void
220 GLCanvas::drawZoomBox (const QPoint& p1, const QPoint& p2)
221 {
222  Matrix overlaycolor (3, 1);
223  overlaycolor(0) = 0.45;
224  overlaycolor(1) = 0.62;
225  overlaycolor(2) = 0.81;
226  double overlayalpha = 0.1;
227  Matrix bordercolor = overlaycolor;
228  double borderalpha = 0.9;
229  double borderwidth = 1.5;
230 
231  m_renderer.draw_zoom_box (width (), height (),
232  p1.x (), p1.y (), p2.x (), p2.y (),
233  overlaycolor, overlayalpha,
234  bordercolor, borderalpha, borderwidth);
235 }
236 
237 void
239 {
240  canvasPaintEvent ();
241 }
242 
243 void
244 GLCanvas::mouseDoubleClickEvent (QMouseEvent *xevent)
245 {
247 }
248 
249 void
250 GLCanvas::mouseMoveEvent (QMouseEvent *xevent)
251 {
252  canvasMouseMoveEvent (xevent);
253 }
254 
255 void
256 GLCanvas::mousePressEvent (QMouseEvent *xevent)
257 {
258  canvasMousePressEvent (xevent);
259 }
260 
261 void
262 GLCanvas::mouseReleaseEvent (QMouseEvent *xevent)
263 {
264  canvasMouseReleaseEvent (xevent);
265 }
266 
267 void
268 GLCanvas::wheelEvent (QWheelEvent *xevent)
269 {
270  canvasWheelEvent (xevent);
271 }
272 
273 void
274 GLCanvas::keyPressEvent (QKeyEvent *xevent)
275 {
276  if (! canvasKeyPressEvent (xevent))
277  OCTAVE_QT_OPENGL_WIDGET::keyPressEvent (xevent);
278 }
279 
280 void
281 GLCanvas::keyReleaseEvent (QKeyEvent *xevent)
282 {
283  if (! canvasKeyReleaseEvent (xevent))
284  OCTAVE_QT_OPENGL_WIDGET::keyReleaseEvent (xevent);
285 }
286 
287 bool
289 {
290  bool retval = true;
291 
292  if (! isValid ())
293  {
294 # if defined (HAVE_QT_OFFSCREEN)
295  static bool os_ctx_ok = true;
296  if (os_ctx_ok && ! m_os_context.isValid ())
297  {
298  // Try to initialize offscreen context
299  m_os_surface.create ();
300  if (! m_os_context.create ())
301  {
302  os_ctx_ok = false;
303  return false;
304  }
305  }
306 
307  retval = m_os_context.makeCurrent (&m_os_surface);
308 # else
309  retval = false;
310 # endif
311  }
312  else
313  makeCurrent ();
314 
315  return retval;
316 }
317 
318 void
320 {
321  doneCurrent ();
322 }
323 
OCTAVE_END_NAMESPACE(octave)
#define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
Definition: GLCanvas.cc:48
Definition: Canvas.h:50
void canvasMouseReleaseEvent(QMouseEvent *event)
Definition: Canvas.cc:787
octave::interpreter & m_interpreter
Definition: Canvas.h:153
void canvasPaintEvent(void)
Definition: Canvas.cc:275
void canvasMouseDoubleClickEvent(QMouseEvent *event)
Definition: Canvas.cc:541
bool canvasKeyReleaseEvent(QKeyEvent *event)
Definition: Canvas.cc:1064
void interpreter_event(const octave::fcn_callback &fcn)
void canvasWheelEvent(QWheelEvent *event)
Definition: Canvas.cc:897
void canvasMousePressEvent(QMouseEvent *event)
Definition: Canvas.cc:574
void canvasMouseMoveEvent(QMouseEvent *event)
Definition: Canvas.cc:439
bool canvasKeyPressEvent(QKeyEvent *event)
Definition: Canvas.cc:1033
void end_rendering(void)
Definition: GLCanvas.cc:319
bool begin_rendering(void)
Definition: GLCanvas.cc:288
void mouseReleaseEvent(QMouseEvent *event)
Definition: GLCanvas.cc:262
void initializeGL(void)
Definition: GLCanvas.cc:67
void paintGL(void)
Definition: GLCanvas.cc:238
octave::opengl_renderer m_renderer
Definition: GLCanvas.h:95
GLCanvas(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_handle &handle, QWidget *parent)
Definition: GLCanvas.cc:53
void keyReleaseEvent(QKeyEvent *event)
Definition: GLCanvas.cc:281
graphics_object selectFromAxes(const graphics_object &ax, const QPoint &pt)
Definition: GLCanvas.cc:203
void mousePressEvent(QMouseEvent *event)
Definition: GLCanvas.cc:256
void wheelEvent(QWheelEvent *event)
Definition: GLCanvas.cc:268
~GLCanvas(void)
Definition: GLCanvas.cc:63
uint8NDArray do_getPixels(const graphics_handle &handle)
Definition: GLCanvas.cc:92
void keyPressEvent(QKeyEvent *event)
Definition: GLCanvas.cc:274
void mouseDoubleClickEvent(QMouseEvent *event)
Definition: GLCanvas.cc:244
void draw(const graphics_handle &handle)
Definition: GLCanvas.cc:73
void mouseMoveEvent(QMouseEvent *event)
Definition: GLCanvas.cc:250
octave::qopengl_functions m_glfcns
Definition: GLCanvas.h:94
void drawZoomBox(const QPoint &p1, const QPoint &p2)
Definition: GLCanvas.cc:220
void do_print(const QString &file_cmd, const QString &term, const graphics_handle &handle)
Definition: GLCanvas.cc:145
Definition: dMatrix.h:42
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
@ select_ignore_hittest
Definition: gl-select.h:39
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)