1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2024 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include "GLCanvas.h"
#include "gl-select.h"
#include "gl-render.h"
#include "gl2ps-print.h"
#include "graphics.h"
#include "interpreter.h"
OCTAVE_BEGIN_NAMESPACE(octave)
GLWidget::GLWidget (Canvas& parent_canvas, QWidget *parent)
: QOpenGLWidget (parent), m_parent_canvas (parent_canvas),
m_glfcns (), m_renderer (m_glfcns)
{
setFocusPolicy (Qt::ClickFocus);
setFocus ();
setUpdateBehavior (QOpenGLWidget::PartialUpdate);
}
GLWidget::~GLWidget () { }
void
GLWidget::initializeGL ()
{
// The qopengl_functions object (part of Octave, not Qt) is just
// wrapper around QOpenGLFunctions_1_1. Does initialization really
// need to be deferred until initializeGL is called?
m_glfcns.init ();
// All other resources we need are currently (supposed to be)
// managed by the QOpenGLWidget object so there is else nothing to
// do here. If we used custom shader programs, then we would need
// to initialize them here.
}
void
GLWidget::draw (graphics_object go)
{
if (go)
{
begin_rendering ();
unwind_action reset_current ([=] () { end_rendering (); });
graphics_object fig = go.get_ancestor ("figure");
double dpr = fig.get ("__device_pixel_ratio__").double_value ();
m_renderer.set_viewport (dpr * width (), dpr * height ());
m_renderer.set_device_pixel_ratio (dpr);
m_renderer.draw (go);
}
}
uint8NDArray
GLWidget::do_getPixels (graphics_object go)
{
uint8NDArray retval;
if (go && go.isa ("figure"))
{
Matrix pos = go.get ("position").matrix_value ();
double dpr = go.get ("__device_pixel_ratio__").double_value ();
pos(2) *= dpr;
pos(3) *= dpr;
begin_rendering ();
unwind_action reset_current ([=] () { end_rendering (); });
// When the figure is not visible or its size is frozen for printing,
// we use a framebuffer object to make sure we are rendering on a
// suitably large frame.
if (go.get ("visible").string_value () == "off"
|| go.get ("__printing__").string_value () == "on")
{
QOpenGLFramebufferObject
fbo (pos(2), pos(3),
QOpenGLFramebufferObject::Attachment::Depth);
fbo.bind ();
unwind_action release_fbo ([&] () { fbo.release (); });
m_renderer.set_viewport (pos(2), pos(3));
m_renderer.set_device_pixel_ratio (dpr);
m_renderer.draw (go);
retval = m_renderer.get_pixels (pos(2), pos(3));
}
else
{
m_renderer.set_viewport (pos(2), pos(3));
m_renderer.set_device_pixel_ratio (dpr);
m_renderer.draw (go);
retval = m_renderer.get_pixels (pos(2), pos(3));
}
}
return retval;
}
void
GLWidget::do_print (const QString& file_cmd, const QString& term,
graphics_object go)
{
if (go.valid_object ())
{
begin_rendering ();
unwind_action reset_current ([=] () { end_rendering (); });
graphics_object fig (go.get_ancestor ("figure"));
if (fig.get ("visible").string_value () == "on")
octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
term.toStdString ());
else
{
// When the figure is not visible, we use a framebuffer object
// to make sure we are rendering on a suitably large frame.
Matrix pos = fig.get ("position").matrix_value ();
double dpr = fig.get ("__device_pixel_ratio__").double_value ();
pos(2) *= dpr;
pos(3) *= dpr;
QOpenGLFramebufferObject
fbo (pos(2), pos(3),
QOpenGLFramebufferObject::Attachment::Depth);
fbo.bind ();
unwind_action release_fbo ([&] () { fbo.release (); });
octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
term.toStdString ());
}
}
}
graphics_object
GLWidget::selectFromAxes (const graphics_object& ax, const QPoint& pt)
{
if (ax)
{
begin_rendering ();
unwind_action reset_current ([=] () { end_rendering (); });
octave::opengl_selector s (m_glfcns);
s.set_viewport (width (), height ());
return s.select (ax, pt.x (), height () - pt.y (),
octave::select_ignore_hittest);
}
return graphics_object ();
}
void
GLWidget::drawZoomBox (const QPoint& p1, const QPoint& p2)
{
Matrix overlaycolor (3, 1);
overlaycolor(0) = 0.45;
overlaycolor(1) = 0.62;
overlaycolor(2) = 0.81;
double overlayalpha = 0.1;
Matrix bordercolor = overlaycolor;
double borderalpha = 0.9;
double borderwidth = 1.5;
begin_rendering ();
unwind_action reset_current ([=] () { end_rendering (); });
m_renderer.draw_zoom_box (width (), height (),
p1.x (), p1.y (), p2.x (), p2.y (),
overlaycolor, overlayalpha,
bordercolor, borderalpha, borderwidth);
}
void
GLWidget::paintGL ()
{
m_parent_canvas.canvasPaintEvent ();
}
void
GLWidget::mouseDoubleClickEvent (QMouseEvent *xevent)
{
m_parent_canvas.canvasMouseDoubleClickEvent (xevent);
}
void
GLWidget::mouseMoveEvent (QMouseEvent *xevent)
{
m_parent_canvas.canvasMouseMoveEvent (xevent);
}
void
GLWidget::mousePressEvent (QMouseEvent *xevent)
{
m_parent_canvas.canvasMousePressEvent (xevent);
}
void
GLWidget::mouseReleaseEvent (QMouseEvent *xevent)
{
m_parent_canvas.canvasMouseReleaseEvent (xevent);
}
void
GLWidget::wheelEvent (QWheelEvent *xevent)
{
m_parent_canvas.canvasWheelEvent (xevent);
}
void
GLWidget::keyPressEvent (QKeyEvent *xevent)
{
if (! m_parent_canvas.canvasKeyPressEvent (xevent))
QOpenGLWidget::keyPressEvent (xevent);
}
void
GLWidget::keyReleaseEvent (QKeyEvent *xevent)
{
if (! m_parent_canvas.canvasKeyReleaseEvent (xevent))
QOpenGLWidget::keyReleaseEvent (xevent);
}
bool
GLWidget::begin_rendering ()
{
bool retval = true;
if (! isValid ())
{
// FIXME: Is this really the right way to manager offscreen
// rendering for printing?
static bool os_ctx_ok = true;
if (os_ctx_ok && ! m_os_context.isValid ())
{
// Try to initialize offscreen context
m_os_surface.create ();
if (! m_os_context.create ())
{
os_ctx_ok = false;
return false;
}
}
retval = m_os_context.makeCurrent (&m_os_surface);
}
else
makeCurrent ();
return retval;
}
void
GLWidget::end_rendering ()
{
doneCurrent ();
}
GLCanvas::GLCanvas (octave::interpreter& interp,
const graphics_handle& gh, QWidget *parent)
: Canvas (interp, gh), m_glwidget (new GLWidget (*this, parent))
{ }
GLCanvas::~GLCanvas ()
{
delete m_glwidget;
}
void
GLCanvas::draw (const graphics_handle& gh)
{
gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
octave::autolock guard (gh_mgr.graphics_lock ());
graphics_object go = gh_mgr.get_object (gh);
m_glwidget->draw (go);
}
uint8NDArray
GLCanvas::do_getPixels (const graphics_handle& gh)
{
uint8NDArray retval;
gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
graphics_object go = gh_mgr.get_object (gh);
return m_glwidget->do_getPixels (go);
}
void
GLCanvas::do_print (const QString& file_cmd, const QString& term,
const graphics_handle& handle)
{
gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
octave::autolock guard (gh_mgr.graphics_lock ());
graphics_object go = gh_mgr.get_object (handle);
try
{
m_glwidget->do_print (file_cmd, term, go);
}
catch (octave::execution_exception& ee)
{
emit interpreter_event
([=] ()
{
// INTERPRETER THREAD
throw ee;
});
}
}
graphics_object
GLCanvas::selectFromAxes (const graphics_object& ax, const QPoint& pt)
{
return m_glwidget->selectFromAxes (ax, pt);
}
void
GLCanvas::drawZoomBox (const QPoint& p1, const QPoint& p2)
{
m_glwidget->drawZoomBox (p1, p2);
}
bool
GLCanvas::begin_rendering ()
{
return m_glwidget->begin_rendering ();
}
void
GLCanvas::end_rendering ()
{
m_glwidget->end_rendering ();
}
OCTAVE_END_NAMESPACE(octave)
|