1/*
2 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
3 The widget drawing code is based on the NanoVG demo application
4 by Mikko Mononen.
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE.txt file.
8*/
9/**
10 * \file nanogui/glcanvas.h
11 *
12 * \brief Canvas widget for rendering OpenGL content. This widget was
13 * contributed by Jan Winkler.
14 */
15
16#pragma once
17
18#include <iostream>
19
20#include <nanogui/widget.h>
21#include <nanogui/opengl.h>
22#include <nanogui/glutil.h>
23
24NAMESPACE_BEGIN(nanogui)
25
26/**
27 * \class GLCanvas glcanvas.h nanogui/glcanvas.h
28 *
29 * \brief Canvas widget for rendering OpenGL content. This widget was
30 * contributed by Jan Winkler.
31 *
32 * Canvas widget that can be used to display arbitrary OpenGL content. This is
33 * useful to display and manipulate 3D objects as part of an interactive
34 * application. The implementation uses scissoring to ensure that rendered
35 * objects don't spill into neighboring widgets.
36 *
37 * \rst
38 * **Usage**
39 * Override :func:`nanogui::GLCanvas::drawGL` in subclasses to provide
40 * custom drawing code. See :ref:`nanogui_example_4`.
41 *
42 * \endrst
43 */
44class NANOGUI_EXPORT GLCanvas : public Widget {
45public:
46 /**
47 * Creates a GLCanvas attached to the specified parent.
48 *
49 * \param parent
50 * The Widget to attach this GLCanvas to.
51 */
52 GLCanvas(Widget *parent);
53
54 /// Returns the background color.
55 const Color &backgroundColor() const { return mBackgroundColor; }
56
57 /// Sets the background color.
58 void setBackgroundColor(const Color &backgroundColor) { mBackgroundColor = backgroundColor; }
59
60 /// Set whether to draw the widget border or not.
61 void setDrawBorder(const bool bDrawBorder) { mDrawBorder = bDrawBorder; }
62
63 /// Return whether the widget border gets drawn or not.
64 const bool &drawBorder() const { return mDrawBorder; }
65
66 /// Draw the canvas.
67 virtual void draw(NVGcontext *ctx) override;
68
69 /// Draw the GL scene. Override this method to draw the actual GL content.
70 virtual void drawGL() {}
71
72 /// Save the state of this GLCanvas to the specified Serializer.
73 virtual void save(Serializer &s) const override;
74
75 /// Set the state of this GLCanvas from the specified Serializer.
76 virtual bool load(Serializer &s) override;
77
78protected:
79 /// Internal helper function for drawing the widget border
80 void drawWidgetBorder(NVGcontext* ctx) const;
81
82protected:
83 /// The background color (what is used with ``glClearColor``).
84 Color mBackgroundColor;
85
86 /// Whether to draw the widget border or not.
87 bool mDrawBorder;
88
89public:
90 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
91};
92
93NAMESPACE_END(nanogui)
94