BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
renderTarget.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_VIEW_RENDERING_RENDERTARGET_H
6 #define BALL_VIEW_RENDERING_RENDERTARGET_H
7 
8 #include <BALL/COMMON/global.h>
10 
11 #include <assert.h>
12 #include <vector>
13 #include <boost/smart_ptr.hpp>
14 #include <boost/shared_array.hpp>
15 
17 
18 namespace BALL
19 {
20  namespace VIEW
21  {
22 
24  struct Resolution
25  {
28 
30  : width(0),
31  height(0)
32  { }
33 
35  : width(width),
36  height(height)
37  { }
38  };
39 
40 
45  {
46  public:
47 
50  : width(0),
51  height(0),
52  pitch(0),
53  pixelFormat(PixelFormat::RGB_32)
54  { }
55 
56  FrameBufferFormat(Size width, Size height, const PixelFormat &pixelFormat)
57  : width(width),
58  height(height),
59  pitch(width * pixelFormat.computeByteSize()),
60  pixelFormat(pixelFormat)
61  {
62  assert(pitch >= width * pixelFormat.computeByteSize());
63  }
64 
65  FrameBufferFormat(Size width, Size height, Size pitch, const PixelFormat &pixelFormat)
66  : width(width),
67  height(height),
68  pitch(pitch),
69  pixelFormat(pixelFormat)
70  {
71  assert(pitch >= width * pixelFormat.computeByteSize());
72  }
73 
74  bool operator==(const FrameBufferFormat& format) const
75  {
76  return width==format.width
77  && height==format.height
78  && pitch==format.pitch
79  && pixelFormat==format.pixelFormat;
80  }
81 
82  bool operator!=(const FrameBufferFormat& format) const
83  {
84  return !(*this == format);
85  }
86 
87  bool isValid() const { return width > 0 && height > 0 && pitch > 0; }
88 
92  unsigned int getWidth() const { return width; }
93 
97  void setWidth(unsigned int width) { this->width = width; }
98 
100  unsigned int getHeight() const { return height; }
101 
103  void setHeight(unsigned int height) { this->height = height; }
104 
106  unsigned int getPitch() const { return pitch; }
107 
109  void setPitch(unsigned int pitch) { this->pitch = pitch; }
110 
112  const PixelFormat &getPixelFormat() const { return pixelFormat; }
113 
115  void setPixelFormat(const PixelFormat &pixelFormat)
116  {
117  this->pixelFormat = pixelFormat;
118  }
119 
123  size_t computeSize() const
124  {
125  // pitch is in bytes
126  return isValid() ? (getPitch() * getHeight()) : 0;
127  }
128 
130  void resize(Size newWidth, Size newHeight)
131  {
132  this->width = newWidth;
133  this->height = newHeight;
134  }
135 
139  const FrameBufferFormat resized(Size newWidth, Size newHeight) const
140  {
141  return FrameBufferFormat(newWidth, newHeight, pitch, pixelFormat);
142  }
143 
144  private:
145 
147  unsigned int width;
148 
150  unsigned int height;
151 
158  unsigned int pitch;
159 
160  PixelFormat pixelFormat;
161  };
162 
163  inline std::ostream &operator<<(std::ostream &o, const FrameBufferFormat &f)
164  {
165  o << f.getWidth() << "x" << f.getHeight() << ", pitch " << f.getPitch()
166  << ", pixel format: " << f.getPixelFormat();
167  return o;
168  }
169 
170  typedef std::vector<FrameBufferFormat> BufferFormatList;
171 
172  //-----------------------------------------------------------------------------
173 
175  {
176  public:
177 
181  FrameBuffer(void* data, const FrameBufferFormat &format)
182  : data(data),
183  format(format)
184  {
185  }
186 
190  virtual ~FrameBuffer() {}
191 
193  void *getData() { return data; }
194 
196  const void *getData() const { return data; }
197 
199  const FrameBufferFormat &getFormat() const { return format; }
200 
201  protected:
202 
204  void setData(void *data) { this->data = data; }
205 
207  void setFormat(const FrameBufferFormat &format) { this->format = format; }
208 
209  private:
214  void *data;
215 
219  FrameBufferFormat format;
220  };
221 
222  typedef boost::shared_ptr<FrameBuffer> FrameBufferPtr;
223 
224  //-----------------------------------------------------------------------------
225 
227  {
228  public:
229 
230  virtual ~RenderTarget() { }
231 
239  virtual FrameBufferPtr getBuffer() throw(BALL::Exception::NoBufferAvailable) = 0;
240 
241  virtual FrameBufferFormat getFormat() const = 0;
242 
249  virtual void releaseBuffer(FrameBufferPtr buffer) = 0;
250 
253  virtual bool init() = 0;
254 
257  virtual bool resize(const unsigned int width, const unsigned int height) = 0;
258 
261  virtual void refresh() = 0;
262 
263  /* Prepare the window for rendering, e.g., make it current if necessary.
264  */
265  virtual void prepareRendering() = 0;
266 
272  virtual void setupStereo(float /*eye_separation*/, float /*focal_length*/) {};
273 
277  virtual bool doNotResize() const = 0;
278  };
279 
280  } //namespace VIEW
281 
282 } // namespace BALL
283 
284 #endif // BALL_VIEW_RENDERING_RENDERTARGET_H
virtual FrameBufferFormat getFormat() const =0
const FrameBufferFormat resized(Size newWidth, Size newHeight) const
Definition: renderTarget.h:139
void setPixelFormat(const PixelFormat &pixelFormat)
Definition: renderTarget.h:115
unsigned int computeByteSize() const
Definition: pixelFormat.h:157
std::vector< FrameBufferFormat > BufferFormatList
Definition: renderTarget.h:170
const void * getData() const
Definition: renderTarget.h:196
boost::shared_ptr< FrameBuffer > FrameBufferPtr
Definition: renderTarget.h:222
unsigned int getPitch() const
Definition: renderTarget.h:106
unsigned int getHeight() const
Definition: renderTarget.h:100
void setHeight(unsigned int height)
Definition: renderTarget.h:103
virtual void prepareRendering()=0
const FrameBufferFormat & getFormat() const
Definition: renderTarget.h:199
void resize(Size newWidth, Size newHeight)
Definition: renderTarget.h:130
virtual void releaseBuffer(FrameBufferPtr buffer)=0
void setPitch(unsigned int pitch)
Definition: renderTarget.h:109
const PixelFormat & getPixelFormat() const
Definition: renderTarget.h:112
FrameBuffer(void *data, const FrameBufferFormat &format)
Definition: renderTarget.h:181
unsigned int getWidth() const
Definition: renderTarget.h:92
virtual void setupStereo(float, float)
Definition: renderTarget.h:272
virtual void refresh()=0
bool operator==(const FrameBufferFormat &format) const
Definition: renderTarget.h:74
FrameBufferFormat(Size width, Size height, const PixelFormat &pixelFormat)
Definition: renderTarget.h:56
std::ostream & operator<<(std::ostream &o, const PixelFormat &f)
Definition: pixelFormat.h:209
void setWidth(unsigned int width)
Definition: renderTarget.h:97
virtual bool init()=0
virtual bool doNotResize() const =0
virtual FrameBufferPtr getBuffer()=0
bool operator!=(const FrameBufferFormat &format) const
Definition: renderTarget.h:82
FrameBufferFormat(Size width, Size height, Size pitch, const PixelFormat &pixelFormat)
Definition: renderTarget.h:65
void setFormat(const FrameBufferFormat &format)
Definition: renderTarget.h:207
Resolution(Size width, Size height)
Definition: renderTarget.h:34
virtual bool resize(const unsigned int width, const unsigned int height)=0
void setData(void *data)
Definition: renderTarget.h:204