GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
gl-select.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "gl-select.h"
28 
29 #include <iostream>
30 
31 void
33 {
34  GLdouble p_matrix[16];
35  GLint viewport[4];
36 
37  glGetDoublev (GL_PROJECTION_MATRIX, p_matrix);
38  glGetIntegerv (GL_VIEWPORT, viewport);
39  glMatrixMode (GL_PROJECTION);
40  glLoadIdentity ();
41  gluPickMatrix (xp, yp, size, size, viewport);
42  glMultMatrixd (p_matrix);
43  glMatrixMode (GL_MODELVIEW);
44 }
45 
46 void
48 {
49  opengl_renderer::setup_opengl_transformation (props);
51 }
52 
53 void
54 opengl_selector::init_marker (const std::string& m, double sz, float width)
55 {
56  opengl_renderer::init_marker (m, sz, width);
58 }
59 
60 # define BUFFER_SIZE 128
61 
63 opengl_selector::select (const graphics_object& ax, int x, int y, int flags)
64 {
65  glEnable (GL_DEPTH_TEST);
66  glDepthFunc (GL_LEQUAL);
67 
68  xp = x;
69  yp = y;
70 
71  GLuint select_buffer[BUFFER_SIZE];
72 
73  glSelectBuffer (BUFFER_SIZE, select_buffer);
74  glRenderMode (GL_SELECT);
75  glInitNames ();
76 
77  object_map.clear ();
78 
79  draw (ax);
80 
81  int hits = glRenderMode (GL_RENDER);
82  graphics_object obj;
83 
84  if (hits > 0)
85  {
86  GLuint current_minZ = 0xffffffff;
87  GLuint current_name = 0xffffffff;
88 
89  for (int i = 0, j = 0; i < hits && j < BUFFER_SIZE-3; i++)
90  {
91  GLuint n = select_buffer[j++],
92  minZ = select_buffer[j++];
93 
94  j++; // skip maxZ
95  if (((flags & select_last) == 0 && (minZ <= current_minZ))
96  || ((flags & select_last) != 0 && (minZ >= current_minZ)))
97  {
98  bool candidate = true;
99  GLuint name =
100  select_buffer[std::min (j + n, GLuint (BUFFER_SIZE)) - 1];
101 
102  if ((flags & select_ignore_hittest) == 0)
103  {
104  graphics_object go = object_map[name];
105 
106  if (! go.get_properties ().is_hittest ())
107  candidate = false;
108  }
109 
110  if (candidate)
111  {
112  current_minZ = minZ;
113  current_name = name;
114  }
115 
116  j += n;
117  }
118  else
119  j += n;
120  }
121 
122  if (current_name != 0xffffffff)
123  obj = object_map[current_name];
124  }
125  else if (hits < 0)
126  warning ("opengl_selector::select: selection buffer overflow");
127 
128  object_map.clear ();
129 
130  return obj;
131 }
132 
133 void
134 opengl_selector::draw (const graphics_object& go, bool toplevel)
135 {
136  GLuint name = object_map.size ();
137 
138  object_map[name] = go;
139  glPushName (name);
140  opengl_renderer::draw (go, toplevel);
141  glPopName ();
142 }
143 
144 void
145 opengl_selector::fake_text (double x, double y, double z, const Matrix& bbox,
146  bool use_scale)
147 {
148  ColumnVector xpos, xp1, xp2;
149 
150  xpos = get_transform ().transform (x, y, z, use_scale);
151 
152  xp1 = xp2 = xpos;
153  xp1(0) += bbox(0);
154  xp1(1) -= bbox(1);
155  xp2(0) += (bbox(0) + bbox(2));
156  xp2(1) -= (bbox(1) + bbox(3));
157 
158  ColumnVector p1, p2, p3, p4;
159 
160  p1 = get_transform ().untransform (xp1(0), xp1(1), xp1(2), false);
161  p2 = get_transform ().untransform (xp2(0), xp1(1), xp1(2), false);
162  p3 = get_transform ().untransform (xp2(0), xp2(1), xp1(2), false);
163  p4 = get_transform ().untransform (xp1(0), xp2(1), xp1(2), false);
164 
165  glBegin (GL_QUADS);
166  glVertex3dv (p1.data ());
167  glVertex3dv (p2.data ());
168  glVertex3dv (p3.data ());
169  glVertex3dv (p4.data ());
170  glEnd ();
171 }
172 
173 void
175 {
176  if (props.get_string ().is_empty ())
177  return;
178 
179  Matrix pos = props.get_data_position ();
180  const Matrix bbox = props.get_extent_matrix ();
181 
182  fake_text (pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0, bbox);
183 }
184 
185 Matrix
186 opengl_selector::render_text (const std::string& txt,
187  double x, double y, double z,
188  int halign, int valign, double rotation)
189 {
190 #if HAVE_FREETYPE
191  uint8NDArray pixels;
192  Matrix bbox;
193 
194  // FIXME: probably more efficient to only compute bbox instead
195  // of doing full text rendering...
196  text_to_pixels (txt, pixels, bbox, halign, valign, rotation);
197  fake_text (x, y, z, bbox, false);
198 
199  return bbox;
200 #else
201  return Matrix (1, 4, 0.0);
202 #endif
203 }
204 
205 void
207 {
208  Matrix xd = props.get_xdata ().matrix_value ();
209  octave_idx_type nc = props.get_cdata ().matrix_value ().columns ();
210  double x_pix_size = (xd(1) - xd(0)) / (nc - 1);
211 
212  Matrix yd = props.get_ydata ().matrix_value ();
213  octave_idx_type nr = props.get_cdata ().matrix_value ().rows ();
214  double y_pix_size = (yd(1) - yd(0)) / (nr - 1);
215 
216  ColumnVector p1(3, 0.0), p2(3, 0.0), p3(3, 0.0), p4(3, 0.0);
217  p1(0) = xd(0) - x_pix_size/2;
218  p1(1) = yd(0) - y_pix_size/2;
219 
220  p2(0) = xd(1) + x_pix_size/2;
221  p2(1) = yd(0) - y_pix_size/2;
222 
223  p3(0) = xd(1) + x_pix_size/2;
224  p3(1) = yd(1) + y_pix_size/2;
225 
226  p4(0) = xd(0) - x_pix_size/2;
227  p4(1) = yd(1) + y_pix_size/2;
228 
229  glBegin (GL_QUADS);
230  glVertex3dv (p1.data ());
231  glVertex3dv (p2.data ());
232  glVertex3dv (p3.data ());
233  glVertex3dv (p4.data ());
234  glEnd ();
235 }
#define BUFFER_SIZE
Definition: gl-select.cc:60
Matrix get_data_position(void) const
Definition: graphics.cc:8098
octave_value get_cdata(void) const
Definition: graphics.h:8608
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
virtual Matrix render_text(const std::string &txt, double x, double y, double z, int halign, int valign, double rotation=0.0)
Definition: gl-select.cc:186
virtual void draw_image(const image::properties &props)
Definition: gl-select.cc:206
octave_value get_xdata(void) const
Definition: graphics.h:8618
octave_idx_type rows(void) const
Definition: Array.h:313
void fake_text(double x, double y, double z, const Matrix &bbox, bool use_scale=true)
Definition: gl-select.cc:145
octave_value get_ydata(void) const
Definition: graphics.h:8620
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-select.cc:134
bool is_hittest(void) const
Definition: graphics.h:2736
virtual void draw_text(const text::properties &props)
Definition: gl-select.cc:174
Matrix get_extent_matrix(void) const
Definition: graphics.cc:8109
virtual void setup_opengl_transformation(const axes::properties &props)
Definition: gl-select.cc:47
void apply_pick_matrix(void)
Definition: gl-select.cc:32
const T * data(void) const
Definition: Array.h:479
base_properties & get_properties(void)
Definition: graphics.h:3377
Definition: dMatrix.h:35
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
octave_value get_string(void) const
Definition: graphics.h:7956
void warning(const char *fmt,...)
Definition: error.cc:681
bool is_empty(void) const
Definition: ov.h:526
std::map< GLuint, graphics_object > object_map
Definition: gl-select.h:75
virtual void init_marker(const std::string &m, double size, float width)
Definition: gl-select.cc:54
octave_idx_type columns(void) const
Definition: Array.h:322
F77_RET_T const double * x
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:210
graphics_object select(const graphics_object &ax, int x, int y, int flags=0)
Definition: gl-select.cc:63