iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageRenderer.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_GRAPHICS_IMAGERENDERER_H
36 #define INCLUDE_NRT_GRAPHICS_IMAGERENDERER_H
37 
38 #include <nrt/Core/Geometry/Dims.H>
42 #include <nrt/Core/Typing/Enum.H>
44 
45 // need to test for these at config...
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #ifdef None
49 int const X11_None = None;
50 #undef None
51 #endif
52 #include <X11/keysym.h>
53 
54 // Expose OpenGL 3.0 prototypes, which are used in particular by VBO:
55 #define GL_GLEXT_PROTOTYPES 1
56 #define GL3_PROTOTYPES 1
57 
58 #include <GL/gl.h>
59 #include <GL/glx.h>
60 
61 namespace nrt
62 {
63  namespace graphics
64  {
65  //! Namespace for ShapeRenderer parameters
66  namespace imagerenderer
67  {
68  //! Parameter category definition
69  static nrt::ParameterCategory const ParamCateg("ImageRenderer Options");
70 
71  //! Parameter definition for display dims
72  NRT_DECLARE_PARAMETER_WITH_CALLBACK(dims, nrt::Dims<nrt::int32>, "Rendered display dimemsions in pixels",
73  nrt::Dims<nrt::int32>(640, 480), ParamCateg);
74 
75  //! Parameter definition for window title
76  NRT_DECLARE_PARAMETER_WITH_CALLBACK(windowTitle, std::string, "Title of the window",
77  "ImageRenderer Window", ParamCateg);
78 
79  //! ImageRenderer resize methods
80  /*! @class ResizePolicy
81 
82  When resizing an image, several options are avialble:
83 
84  @c nrt::ImageRendererResizePolicy::stretch - Stretches an image to fill the window, ignoring the aspect ratio.
85  When an image is replaced, no resizing of the window occurs.
86 
87  @c nrt::ImageRendererResizePolicy::best - Makes the image as large as possible in the confines of the current
88  window size while preserving the original aspect ratio of the image.
89 
90  @c nrt::ImageRendererResizePolicy::actual - Keeps the image at its actual size regardless of other settings
91  such as initial window size. New images will resize the window appropriately.
92 
93  @c nrt::ImageRendererResizePolicy::preserve - Forces the aspect ratio of the window to preserve the original
94  image ratio. New images will resize the window to its actual size.
95 
96  @relates ImageRenderer */
97  NRT_DEFINE_ENUM_CLASS(ResizePolicy, (stretch) (best) (actual) (preserve) );
98 
99  //! Parameter definition for resizing options
100  NRT_DECLARE_PARAMETER_WITH_CALLBACK(resizePolicy, ResizePolicy, "The window resizing policy",
101  ResizePolicy::best, ResizePolicy_Values, ParamCateg);
102 
103  //! Parameter definition for window background color
104  NRT_DECLARE_PARAMETER_WITH_CALLBACK(backgroundColor, PixRGBA<float>, "The background color for the window",
105  PixRGBA<float>( 0.0f, 0.0f, 0.0f, 1.0f ), ParamCateg);
106  }
107 
108  //! Render 2D images into a basic X11/OpenGL window
109  /*! \ingroup graphics */
111  public nrt::Parameter<imagerenderer::dims,
112  imagerenderer::windowTitle,
113  imagerenderer::resizePolicy,
114  imagerenderer::backgroundColor>
115  {
116  public:
117  //! Default constructor
118  /*! This should do very little, most initialization should happen in preInit(). \see Component */
119  ImageRenderer(std::string const & instanceName = "");
120 
121  //! Virtual destructor for safe inheritance
122  virtual ~ImageRenderer();
123 
124  //! \defgroup irinterface Interface functions that should all be called from a same thread
125  /*! An OpenGL context is attached to a given thread, hence all of the renderer's display functions should be
126  called from the same thread. Typically, that would teh the run() thread of an nrt::Component, as follows:
127  @code
128 void MyModule::run()
129 {
130  // Get our renderer going:
131  itsRenderer->createContext();
132 
133  // Main loop:
134  while (running())
135  {
136  // Reset our frame to blank:
137  itsRenderer->initFrame();
138 
139  // Now set the image to render (needs not be done in the loop)
140  itsRenderer->setImage( GenericImage(myImage) );
141 
142  // Finalize rendering of the frame:
143  itsRenderer->renderFrame();
144 
145  // Maybe sleep a bit, or compute FPS, etc
146  std::this_thread::sleep_for(std::chrono::milliseconds(10));
147  }
148 
149  // Time to cleanup and close down:
150  itsRenderer->destroyContext();
151 }
152  @endcode */
153  //!@{
154  //! Create a drawing context
155  void createContext();
156 
157  //! Optional, activate the context (after it is created by createContext()); initFrame() does this too
158  void activateContext();
159 
160  //! Reset our display buffer to blank
161  void initFrame();
162 
163  //! Sets the image to use
164  void setImage( GenericImage const & image );
165 
166  //! Sets the image to use
167  void setImage( Image<PixRGBA<float>> const image );
168 
169  //! Render the display buffer to screen
170  void renderFrame();
171 
172  //! Destroy our drawing context
173  void destroyContext();
174 
175  //!@}
176 
177  protected:
178  //! Open the window and do initial setups
179  virtual void preStart();
180 
181  //! Function called when a window resize event is received, or user changes the dims parameter
182  virtual void resizeCallback(int width, int height);
183 
184  //! Close down the window
185  virtual void postStop();
186 
187  Display *itsDisplay;
188  XVisualInfo *itsVisualInfo;
189  Window itsWindow;
190  GLXFBConfig itsFBConfig;
191  GLXContext itsCtx;
192  XSetWindowAttributes itsSwa;
193 
194  private:
195  void onParamChange(nrt::graphics::imagerenderer::dims const & param, nrt::Dims<nrt::int32> const & di);
196  void onParamChange(nrt::graphics::imagerenderer::backgroundColor const & param,
197  nrt::PixRGBA<float> const & color);
198  void onParamChange(nrt::graphics::imagerenderer::resizePolicy const & param,
199  nrt::graphics::imagerenderer::ResizePolicy const & policy);
200  void onParamChange(nrt::graphics::imagerenderer::windowTitle const & param, std::string const & title);
201  void handleEvents();
202 
203  Atom itsDeleteMessage; // used for catching window closure
204 
205  struct ContextLock
206  {
207  ContextLock(ImageRenderer * sr_);
208  ~ContextLock();
209  private:
210  ImageRenderer * sr;
211  std::lock_guard<std::mutex> lg;
212  };
213 
214  friend struct ContextLock;
215 
216  mutable std::mutex itsMutex;
217  bool itsHasContext;
218 
219  Image<PixRGBA<float>> itsImage;
220  std::array<GLfloat, 4> itsBackgroundColorValues;
221 
222  TextureManager itsTmanager;
223  };
224  } // namespace graphics
225 } // namespace nrt
226 
227 #endif // INCLUDE_NRT_GRAPHICS_IMAGERENDERER_H_