gl-render.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 Michael Goffioul
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (gl_render_h)
00024 #define gl_render_h 1
00025 
00026 #ifdef HAVE_WINDOWS_H
00027 #define WIN32_LEAN_AND_MEAN
00028 #include <windows.h>
00029 #endif
00030 
00031 #ifdef HAVE_GL_GL_H
00032 #include <GL/gl.h>
00033 #elif defined HAVE_OPENGL_GL_H || defined HAVE_FRAMEWORK_OPENGL
00034 #include <OpenGL/gl.h>
00035 #endif
00036 
00037 #ifdef HAVE_GL_GLU_H
00038 #include <GL/glu.h>
00039 #elif defined HAVE_OPENGL_GLU_H || defined HAVE_FRAMEWORK_OPENGL
00040 #include <OpenGL/glu.h>
00041 #endif
00042 
00043 #include "graphics.h"
00044 #include "txt-eng-ft.h"
00045 
00046 class
00047 OCTINTERP_API
00048 opengl_renderer
00049 {
00050 public:
00051   opengl_renderer (void)
00052     : toolkit (), xform (), xmin (), xmax (), ymin (), ymax (),
00053     zmin (), zmax (), xZ1 (), xZ2 (), marker_id (), filled_marker_id (),
00054     camera_pos (), camera_dir ()
00055 #if HAVE_FREETYPE
00056     , text_renderer ()
00057 #endif
00058   { }
00059 
00060   virtual ~opengl_renderer (void) { }
00061 
00062   virtual void draw (const graphics_object& go, bool toplevel = true);
00063 
00064   virtual void draw (const Matrix& hlist, bool toplevel = false)
00065     {
00066       int len = hlist.length ();
00067 
00068       for (int i = len-1; i >= 0; i--)
00069         {
00070           graphics_object obj = gh_manager::get_object (hlist(i));
00071 
00072           if (obj)
00073             draw (obj, toplevel);
00074         }
00075     }
00076 
00077   virtual void set_viewport (int w, int h);
00078   virtual graphics_xform get_transform (void) const { return xform; }
00079 
00080 protected:
00081   virtual void draw_figure (const figure::properties& props);
00082   virtual void draw_axes (const axes::properties& props);
00083   virtual void draw_line (const line::properties& props);
00084   virtual void draw_surface (const surface::properties& props);
00085   virtual void draw_patch (const patch::properties& props);
00086   virtual void draw_hggroup (const hggroup::properties& props);
00087   virtual void draw_text (const text::properties& props);
00088   virtual void draw_image (const image::properties& props);
00089   virtual void draw_uipanel (const uipanel::properties& props,
00090                              const graphics_object& go);
00091 
00092   virtual void init_gl_context (bool enhanced, const Matrix& backgroundColor);
00093 
00094   virtual void set_color (const Matrix& c);
00095   virtual void set_polygon_offset (bool on, double offset = 0.0);
00096   virtual void set_linewidth (float w);
00097   virtual void set_linestyle (const std::string& s, bool stipple = false);
00098   virtual void set_clipbox (double x1, double x2, double y1, double y2,
00099                             double z1, double z2);
00100   virtual void set_clipping (bool on);
00101   virtual void set_font (const base_properties& props);
00102 
00103   virtual void init_marker (const std::string& m, double size, float width);
00104   virtual void end_marker (void);
00105   virtual void draw_marker (double x, double y, double z,
00106                             const Matrix& lc, const Matrix& fc);
00107 
00108   virtual void text_to_pixels (const std::string& txt,
00109                                uint8NDArray& pixels,
00110                                Matrix& bbox,
00111                                int halign = 0, int valign = 0,
00112                                double rotation = 0.0);
00113 
00114   virtual Matrix render_text (const std::string& txt,
00115                               double x, double y, double z,
00116                               int halign, int valign, double rotation = 0.0);
00117 
00118   virtual void draw_pixels (GLsizei w, GLsizei h, GLenum format,
00119                             GLenum type, const GLvoid *data);
00120 
00121   virtual void render_grid (const std::string& gridstyle, const Matrix& ticks,
00122                             double lim1, double lim2,
00123                             double p1, double p1N, double p2, double p2N,
00124                             int xyz, bool is_3D);
00125 
00126   virtual void render_tickmarks (const Matrix& ticks, double lim1, double lim2,
00127                                  double p1, double p1N, double p2, double p2N,
00128                                  double dx, double dy, double dz,
00129                                  int xyz, bool doubleside);
00130 
00131   virtual void render_ticktexts (const Matrix& ticks,
00132                                  const string_vector& ticklabels,
00133                                  double lim1, double lim2,
00134                                  double p1, double p2,
00135                                  int xyz, int ha, int va,
00136                                  int& wmax, int& hmax);
00137 
00138 private:
00139   opengl_renderer (const opengl_renderer&)
00140     : toolkit (), xform (), xmin (), xmax (), ymin (), ymax (),
00141     zmin (), zmax (), xZ1 (), xZ2 (), marker_id (), filled_marker_id (),
00142     camera_pos (), camera_dir ()
00143 #if HAVE_FREETYPE
00144     , text_renderer ()
00145 #endif
00146     { }
00147 
00148   opengl_renderer& operator = (const opengl_renderer&)
00149     { return *this; }
00150 
00151   bool is_nan_or_inf (double x, double y, double z) const
00152     {
00153       return (xisnan (x) || xisnan (y) || xisnan (z)
00154               || xisinf (x) || xisinf (y) || xisinf (z));
00155     }
00156 
00157   octave_uint8 clip_code (double x, double y, double z) const
00158     {
00159       return ((x < xmin ? 1 : 0)
00160               | (x > xmax ? 1 : 0) << 1
00161               | (y < ymin ? 1 : 0) << 2
00162               | (y > ymax ? 1 : 0) << 3
00163               | (z < zmin ? 1 : 0) << 4
00164               | (z > zmax ? 1 : 0) << 5
00165               | (is_nan_or_inf (x, y, z) ? 0 : 1) << 6);
00166     }
00167 
00168   unsigned int make_marker_list (const std::string& m, double size,
00169                                  bool filled) const;
00170 
00171   void setup_opengl_transformation (const axes::properties& props);
00172 
00173   void draw_axes_planes (const axes::properties& props);
00174   void draw_axes_boxes (const axes::properties& props);
00175 
00176   void draw_axes_x_grid (const axes::properties& props);
00177   void draw_axes_y_grid (const axes::properties& props);
00178   void draw_axes_z_grid (const axes::properties& props);
00179 
00180   void draw_axes_children (const axes::properties& props);
00181 
00182 private:
00183   // The graphics toolkit associated with the figure being rendered.
00184   graphics_toolkit toolkit;
00185 
00186   // axes transformation data
00187   graphics_xform xform;
00188 
00189   // axis limits in model scaled coordinate
00190   double xmin, xmax;
00191   double ymin, ymax;
00192   double zmin, zmax;
00193 
00194   // Z projection limits in windows coordinate
00195   double xZ1, xZ2;
00196 
00197   // call lists identifiers for markers
00198   unsigned int marker_id, filled_marker_id;
00199 
00200   // camera information for primitive sorting
00201   ColumnVector camera_pos, camera_dir;
00202 
00203 #if HAVE_FREETYPE
00204   // freetype render, used for text rendering
00205   ft_render text_renderer;
00206 #endif
00207 
00208 private:
00209   class patch_tesselator;
00210 };
00211 
00212 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines