FrameBuffer.h 6.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef __GPU__FRAMEBUFFER_H__
#define __GPU__FRAMEBUFFER_H__




#include "GPUdefs.h"
#include "Texture2D.h"
#include "TextureCube.h"
#include <map>
#include <vector>


/*
 *
 *  CLASS DECLARATION.
 *
 */

namespace GPU
{
    class RenderBuffer : public InstantiatedObject
    {
        /********************\
        | Member variable(s) |
        \********************/
    private:
        GLuint              m_Id;
        unsigned int        m_Width;
        unsigned int        m_Height;

        /*****************************\
        | Constructor(s) / destructor |
        \*****************************/
    public:
        inline              RenderBuffer() :
            m_Id(0),
            m_Width(0),
            m_Height(0)
        {}
        inline              ~RenderBuffer()                 { Release(); }

        /********************\
        | Member function(s) |
        \********************/
    protected:
        bool                Allocate();
        bool                Unallocate();

    public:
        inline bool         Create( const GLenum internalFormat,
                                    const unsigned int w,
                                    const unsigned int h )  { GLenum f[] = {internalFormat,GL_NONE};  return Create(f,w,h); }
        bool                Create( const GLenum internalFormats[],
                                    const unsigned int w,
                                    const unsigned int h );

        inline unsigned int Width() const                   { return m_Width; }
        inline unsigned int Height() const                  { return m_Height; }
        inline GLuint       Id() const                      { return m_Id; }
        GLint               InternalFormat() const;
    };


    class FrameBuffer
    {
        /******************\
        | Internal type(s) |
        \******************/
    private:
        typedef std::map<GLuint,InstantiatedObject*> BufferMap;

        /********************\
        | Member variable(s) |
        \********************/
    private:
        mutable GLint       m_PrevViewport[4];
        mutable GLint       m_PrevBuffer;
        GLuint              m_Id;
        GLuint              m_Width;
        GLuint              m_Height;
        BufferMap           m_Buffers;
        std::vector<GLenum> m_EnabledBuffers;

        /*****************************\
        | Constructor(s) / destructor |
        \*****************************/
    public:
        inline  FrameBuffer() :
            m_Id(0),
            m_Width(0),
            m_Height(0)
        {}
        inline  FrameBuffer( const unsigned int w,
                             const unsigned int h ) :
            m_Id(0),
            m_Width(0),
            m_Height(0)
        {
            Create(w,h);
        }
        inline  ~FrameBuffer()                                          { Release(); }

        /********************\
        | Member function(s) |
        \********************/
    public:
        bool                Create( const unsigned int w,
                                    const unsigned int h );
        inline bool         IsCreated() const                           { return m_Id != 0; }
        void                Release();

        inline GLuint       Width() const                               { return m_Width; }
        inline GLuint       Height() const                              { return m_Height; }
        inline GLuint       Id() const                                  { return m_Id; }

125 126 127 128
        inline bool         Attach( const GLuint attachment,
                                    const GLenum internalFormat )       { GLenum f[] = {internalFormat,GL_NONE}; return Attach(attachment,f); }
        inline bool         Attach( const GLuint attachment,
                                    const GLenum internalFormats[] )    { RenderBuffer buff; return buff.Create(internalFormats,m_Width,m_Height) && Attach(attachment,buff); }
129 130 131
        inline bool         Attach( const GLuint attachment,
                                    const GPU::RenderBuffer *buff )     { return buff? Attach(attachment,*buff) : false; }
        bool                Attach( const GLuint attachment,
132
                                    const GPU::RenderBuffer &buff );
133 134 135
        inline bool         Attach( const GLuint attachment,
                                    const GPU::Texture2D *tex )         { return tex? Attach(attachment,*tex) : false; }
        bool                Attach( const GLuint attachment,
136
                                    const GPU::Texture2D &tex );
137 138 139
        inline bool         Attach( const GLuint attachment,
                                    const GPU::TextureCube *tex,
                                    const GLenum targetCubeFace )       { return tex? Attach(attachment,*tex,targetCubeFace) : false; }
140 141 142
        bool                Attach( const GLuint attachment,
                                    const GPU::TextureCube &tex,
                                    const GLenum targetCubeFace );
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        void                Detach( const GLuint attachment );

        void                Enable ( const GLuint attachment );
        void                Disable( const GLuint attachment );

        void                Bind() const;
        void                Unbind() const;

        inline bool         DumpTo( const GLuint attachment,
                                    void *buffer,
                                    const GLuint format,
                                    const GLuint type,
                                    const unsigned int stride = 1 ) const   { return DumpTo( attachment, buffer, 0, 0, m_Width, m_Height, format, type, stride ); }
        bool                DumpTo( const GLuint attachment,
                                    void *buffer,
                                    const unsigned int x,
                                    const unsigned int y,
                                    const unsigned int w,
                                    const unsigned int h,
                                    const GLuint format,
                                    const GLuint type,
                                    const unsigned int stride = 1 ) const;

        const Texture2D*    AsTexture2D( const GLuint attachment ) const;
        const TextureCube*  AsTextureCube( const GLuint attachment ) const;
        const RenderBuffer* AsBuffer( const GLuint attachment ) const;
    };
};




#endif //__GPU__FRAMEBUFFER_H__