| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | |
| 5 | #ifndef BALL_VIEW_RENDERING_RENDERERS_BUFFEREDRENDERER_H |
| 6 | #define BALL_VIEW_RENDERING_RENDERERS_BUFFEREDRENDERER_H |
| 7 | |
| 8 | #ifndef BALL_VIEW_RENDERING_RENDERERS_RENDERER_H |
| 9 | # include <BALL/VIEW/RENDERING/RENDERERS/renderer.h> |
| 10 | #endif |
| 11 | |
| 12 | #ifndef BALL_VIEW_RENDERING_RENDERTARGET_H |
| 13 | # include <BALL/VIEW/RENDERING/renderTarget.h> |
| 14 | #endif |
| 15 | |
| 16 | namespace BALL |
| 17 | { |
| 18 | namespace VIEW |
| 19 | { |
| 20 | /** Interface for Buffered Rendering. |
| 21 | * BufferedRenderers don't create their own window. Instead, they render the |
| 22 | * scene to a FrameBuffer which is provided by a RenderTarget. |
| 23 | * The FrameBuffer contains a pointer to a memory area where the results will |
| 24 | * be stored, as well as the pixel format and the resolution. |
| 25 | * Before rendering can start, you have to choose an output format using the |
| 26 | * setFrameBufferFormat() function. After that, you can call renderToBuffer(), |
| 27 | * as long as the format of the RenderTarget is the same that you set before. |
| 28 | */ |
| 29 | class BALL_VIEW_EXPORT BufferedRenderer |
| 30 | : public Renderer |
| 31 | { |
| 32 | public: |
| 33 | |
| 34 | BufferedRenderer() |
| 35 | : offset_(0), |
| 36 | stride_(0) |
| 37 | { } |
| 38 | |
| 39 | /** Tries to choose a format for buffered rendering. |
| 40 | * @return true if the format could be set, |
| 41 | * @return false if it is not supported. |
| 42 | */ |
| 43 | bool setFrameBufferFormat(const FrameBufferFormat &format) |
| 44 | { |
| 45 | if (supports(format)) |
| 46 | { |
| 47 | bufferFormat = format; |
| 48 | formatUpdated(); |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /** Render the current frame to the target's buffer using the \link Stage \endlink setup. |
| 57 | * Throws FrameBufferFormatException if the RenderTarget's format is not |
| 58 | * the one that you set before with setFrameBufferFormat(). |
| 59 | * @throw Exception::InvalidFormat if the FrameBufferFormat is not supported by the renderer |
| 60 | * @throw Exception::NoBufferAvailable if the frame buffer was not correctly initialized |
| 61 | */ |
| 62 | void renderToBuffer(RenderTarget* renderTarget, const Stage& stage) |
| 63 | { |
| 64 | prepareBufferedRendering(stage); |
| 65 | |
| 66 | FrameBufferPtr buffer = renderTarget->getBuffer(); |
| 67 | assert(buffer); |
| 68 | |
| 69 | if (buffer->getFormat() != bufferFormat) |
| 70 | { |
| 71 | throw Exception::InvalidFormat(__FILE__, __LINE__, "Buffer with wrong framebuffer format supplied"); |
| 72 | } |
| 73 | |
| 74 | renderToBufferImpl( buffer ); |
| 75 | |
| 76 | renderTarget->releaseBuffer(buffer); |
| 77 | } |
| 78 | |
| 79 | /** Checks if a particular PixelFormat is supported by the renderer. */ |
| 80 | virtual bool supports(const PixelFormat &format) const = 0; |
| 81 | |
| 82 | /** Returns a supported resolution within the given limits. |
| 83 | * The x resolution will be between min.x and max.x and the y resolution |
| 84 | * will be between min.y and max.y. |
| 85 | * The returned resolution must be supported by the renderer with the |
| 86 | * requested PixelFormat. |
| 87 | * @throw Exception::FormatUnsupported if the FrameBufferFormat is not supported by the renderer |
| 88 | */ |
| 89 | virtual Resolution getSupportedResolution(const Resolution &min, const Resolution &max, |
| 90 | const PixelFormat &format) const = 0; |
| 91 | |
| 92 | /** Set an offset into the data buffer |
| 93 | */ |
| 94 | virtual void setOffset(Size offset) {offset_ = offset;} |
| 95 | |
| 96 | /** Set a stride for the data buffer |
| 97 | */ |
| 98 | virtual void setStride(Size stride) {stride_ = stride;} |
| 99 | |
| 100 | protected: |
| 101 | |
| 102 | /** Checks if a particular FrameBufferFormat is supported by the renderer. |
| 103 | * This checks the combination of pixel format and resolution. |
| 104 | * If this returns true, the renderer is able to write to a buffer in |
| 105 | * this format. |
| 106 | */ |
| 107 | virtual bool supports(const FrameBufferFormat &format) const = 0; |
| 108 | |
| 109 | /** This is called once the FrameBufferFormat has been set so the renderer |
| 110 | * can perform any initialization steps that are needed. |
| 111 | */ |
| 112 | virtual void formatUpdated() = 0; |
| 113 | |
| 114 | /** Prepare for the rendering, taking into account settings in Stage, eg. Camera, Lights, Background colors, etc. |
| 115 | */ |
| 116 | virtual void prepareBufferedRendering(const Stage& stage) = 0; |
| 117 | |
| 118 | /** Render to the frame buffer. |
| 119 | * @pre The frame buffer's format is the one stored in bufferFormat and |
| 120 | * prepareBufferedRendering() has been called. |
| 121 | */ |
| 122 | virtual void renderToBufferImpl(FrameBufferPtr buffer) = 0; |
| 123 | |
| 124 | /** Returns the current FrameBufferFormat. */ |
| 125 | const FrameBufferFormat &getFrameBufferFormat() const { return bufferFormat; } |
| 126 | |
| 127 | Size offset_; |
| 128 | Size stride_; |
| 129 | |
| 130 | private: |
| 131 | |
| 132 | FrameBufferFormat bufferFormat; |
| 133 | }; |
| 134 | } //namespace VIEW |
| 135 | |
| 136 | } //namespace BALL |
| 137 | |
| 138 | |
| 139 | #endif // BALL_VIEW_RENDERING_BUFFEREDRENDERER_H |