1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2021 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 (octave_gl_render_h)
27 #define octave_gl_render_h 1
28 
29 #include "octave-config.h"
30 
31 #include "graphics.h"
32 #include "text-renderer.h"
33 
34 namespace octave
35 {
36   class opengl_functions;
37 
38   class
39   OCTINTERP_API
40   opengl_renderer
41   {
42   public:
43 
44     opengl_renderer (opengl_functions& glfcns);
45 
46     // No copying!
47 
48     opengl_renderer (const opengl_renderer&) = delete;
49 
50     opengl_renderer& operator = (const opengl_renderer&) = delete;
51 
52     virtual ~opengl_renderer (void) = default;
53 
get_opengl_functions(void)54     opengl_functions& get_opengl_functions (void) const { return m_glfcns; }
55 
56     virtual void draw (const graphics_object& go, bool toplevel = true);
57 
58     // The following version of the draw method is not declared virtual
59     // because no derived class overrides it.
60 
61     void draw (const Matrix& hlist, bool toplevel = false);
62 
63     virtual void set_viewport (int w, int h);
set_device_pixel_ratio(double dpr)64     virtual void set_device_pixel_ratio (double dpr) { m_devpixratio = dpr; }
65     virtual Matrix get_viewport_scaled (void) const;
get_transform(void)66     virtual graphics_xform get_transform (void) const { return xform; }
67     virtual uint8NDArray get_pixels (int width, int height);
68 
69     virtual void draw_zoom_box (int width, int height,
70                                 int x1, int y1, int x2, int y2,
71                                 const Matrix& overlaycolor,
72                                 double overlayalpha,
73                                 const Matrix& bordercolor,
74                                 double borderalpha, double borderwidth);
75 
76     virtual void finish (void);
77 
78   protected:
79     virtual void draw_figure (const figure::properties& props);
80     virtual void draw_axes (const axes::properties& props);
81     virtual void draw_line (const line::properties& props);
82     virtual void draw_surface (const surface::properties& props);
83     virtual void draw_patch (const patch::properties& props);
84     virtual void draw_light (const light::properties& props);
85     virtual void draw_hggroup (const hggroup::properties& props);
86     virtual void draw_text (const text::properties& props);
87     virtual void draw_text_background (const text::properties& props,
88                                        bool do_rotate = false);
89     virtual void draw_image (const image::properties& props);
90     virtual void draw_uipanel (const uipanel::properties& props,
91                                const graphics_object& go);
92     virtual void draw_uibuttongroup (const uibuttongroup::properties& props,
93                                      const graphics_object& go);
94     virtual void init_gl_context (bool enhanced, const Matrix& backgroundColor);
95     virtual void setup_opengl_transformation (const axes::properties& props);
96 
97     virtual void set_clipbox (double x1, double x2, double y1, double y2,
98                               double z1, double z2);
99     virtual void set_clipping (bool on);
100     virtual void set_font (const base_properties& props);
101     virtual void set_color (const Matrix& c);
set_interpreter(const caseless_str & interp)102     virtual void set_interpreter (const caseless_str& interp)
103     {
104       interpreter = interp;
105     }
106     virtual void set_linewidth (float w);
107     virtual void set_linestyle (const std::string& s, bool stipple = false,
108                                 double linewidth = 0.5);
set_linecap(const std::string &)109     virtual void set_linecap (const std::string&) { };
set_linejoin(const std::string &)110     virtual void set_linejoin (const std::string&) { };
111     virtual void set_polygon_offset (bool on, float offset = 0.0f);
set_selecting(bool on)112     virtual void set_selecting (bool on)
113     {
114       selecting = on;
115     }
116 
117     virtual void init_marker (const std::string& m, double size, float width);
118     virtual void end_marker (void);
119     virtual void draw_marker (double x, double y, double z,
120                               const Matrix& lc, const Matrix& fc);
121 
122     virtual void text_to_pixels (const std::string& txt,
123                                  uint8NDArray& pixels,
124                                  Matrix& bbox,
125                                  int halign = 0, int valign = 0,
126                                  double rotation = 0.0);
127 
128     virtual void text_to_strlist (const std::string& txt,
129                                   std::list<text_renderer::string>& lst,
130                                   Matrix& bbox,
131                                   int halign = 0, int valign = 0,
132                                   double rotation = 0.0);
133 
134     virtual Matrix render_text (const std::string& txt,
135                                 double x, double y, double z,
136                                 int halign, int valign, double rotation = 0.0);
137 
138     virtual void render_grid (const double linewidth,
139                               const std::string& gridstyle,
140                               const Matrix& gridcolor, const double gridalpha,
141                               const Matrix& ticks, double lim1, double lim2,
142                               double p1, double p1N, double p2, double p2N,
143                               int xyz, bool is_3D);
144 
145     virtual void render_tickmarks (const Matrix& ticks, double lim1, double lim2,
146                                    double p1, double p1N, double p2, double p2N,
147                                    double dx, double dy, double dz,
148                                    int xyz, bool doubleside);
149 
150     virtual void render_ticktexts (const Matrix& ticks,
151                                    const string_vector& ticklabels,
152                                    double lim1, double lim2,
153                                    double p1, double p2,
154                                    int xyz, int ha, int va,
155                                    int& wmax, int& hmax);
156 
157     virtual void draw_zoom_rect (int x1, int y1, int x2, int y2);
158 
159   private:
160 
161     void init_maxlights (void);
162 
163     std::string get_string (unsigned int id) const;
164 
is_nan_or_inf(double x,double y,double z)165     bool is_nan_or_inf (double x, double y, double z) const
166     {
167       return (math::isnan (x) || math::isnan (y)
168               || math::isnan (z)
169               || math::isinf (x) || math::isinf (y)
170               || math::isinf (z));
171     }
172 
clip_code(double x,double y,double z)173     uint8_t clip_code (double x, double y, double z) const
174     {
175       return ((x < xmin ? 1 : 0)
176               | (x > xmax ? 1 : 0) << 1
177               | (y < ymin ? 1 : 0) << 2
178               | (y > ymax ? 1 : 0) << 3
179               | (z < zmin ? 1 : 0) << 4
180               | (z > zmax ? 1 : 0) << 5
181               | (is_nan_or_inf (x, y, z) ? 0 : 1) << 6);
182     }
183 
184     void set_normal (int bfl_mode, const NDArray& n, int j, int i);
185 
186     double points_to_pixels (const double val) const;
187 
188     unsigned int make_marker_list (const std::string& m, double size,
189                                    bool filled) const;
190 
191     void draw_axes_planes (const axes::properties& props);
192     void draw_axes_boxes (const axes::properties& props);
193 
194     void draw_axes_grids (const axes::properties& props);
195     void draw_axes_x_grid (const axes::properties& props);
196     void draw_axes_y_grid (const axes::properties& props);
197     void draw_axes_z_grid (const axes::properties& props);
198 
199     void draw_axes_children (const axes::properties& props);
200 
201     void draw_all_lights (const base_properties& props,
202                           std::list<graphics_object>& obj_list);
203 
204   protected:
205 
206     opengl_functions& m_glfcns;
207 
208     // axis limits in model scaled coordinate
209     double xmin, xmax;
210     double ymin, ymax;
211     double zmin, zmax;
212 
213     // Factor used for translating Octave pixels to actual device pixels
214     double m_devpixratio;
215 
216     // axes transformation data
217     graphics_xform xform;
218 
219   private:
220 
221     // The graphics toolkit associated with the figure being rendered.
222     graphics_toolkit toolkit;
223 
224     // Z projection limits in windows coordinate
225     double xZ1, xZ2;
226 
227     // call lists identifiers for markers
228     unsigned int marker_id, filled_marker_id;
229 
230     // camera information for primitive sorting and lighting
231     ColumnVector camera_pos, camera_dir, view_vector;
232 
233     // interpreter to be used by text_to_pixels
234     caseless_str interpreter;
235 
236     text_renderer txt_renderer;
237 
238     // light object present and visible
239     unsigned int m_current_light;
240     unsigned int m_max_lights;
241 
242     // Indicate we are drawing for selection purpose
243     bool selecting;
244 
245     // Indicate we are drawing for printing purpose
246     bool m_printing;
247 
248   private:
249     class patch_tessellator;
250   };
251 }
252 
253 #endif
254