ElementArrayBuffer.hh 4.79 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6 7
#ifndef ACGL_OPENGL_OBJECTS_ELEMENTBUFFERDATA_HH
#define ACGL_OPENGL_OBJECTS_ELEMENTBUFFERDATA_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9 10
#include <ACGL/ACGL.hh>

11
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
12 13 14 15
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>

#include <tr1/memory>
Robert Menzel's avatar
Robert Menzel committed
16 17

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
18
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
19 20 21

class ElementArrayBuffer
{
22 23
    ACGL_NOT_COPYABLE(ElementArrayBuffer)

Robert Menzel's avatar
Robert Menzel committed
24 25 26 27 28 29 30 31 32 33 34 35 36
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    ElementArrayBuffer(
        GLenum _usage = GL_STATIC_DRAW,
        GLenum _mode = GL_TRIANGLES,
        GLenum _type = GL_UNSIGNED_INT)
    :   mContext(0),
        mUsage(_usage),
        mMode(_mode),
        mElements(0),
        mType(_type),
Robert Menzel's avatar
Robert Menzel committed
37
        mSizeOfType(getGLTypeSize(_type))
Robert Menzel's avatar
Robert Menzel committed
38 39
    {
        glGenBuffers(1, &mContext);
40 41 42 43
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate element array buffer!" << std::endl;
        }

Robert Menzel's avatar
Robert Menzel committed
44 45 46 47
    }

    virtual ~ElementArrayBuffer(void)
    {
48 49
        // buffer 0 will get ignored by OpenGL
        glDeleteBuffers(1, &mContext);
Robert Menzel's avatar
Robert Menzel committed
50 51 52 53 54 55
    }
    
    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
56
    inline GLuint  getContext    (void) const { return mContext;    }
Robert Menzel's avatar
Robert Menzel committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    inline GLenum  getUsage      (void) const { return mUsage;      }
    inline GLenum  getMode       (void) const { return mMode;       }
    inline GLsizei getElements   (void) const { return mElements;   }
    inline GLenum  getType       (void) const { return mType;       }
    inline GLint   getSizeOfType (void) const { return mSizeOfType; }

    // ==================================================================================================== \/
    // ============================================================================================ SETTERS \/
    // ==================================================================================================== \/
public:
    inline void setUsage (GLenum _usage) { mUsage = _usage; }
    inline void setMode  (GLenum _mode)  { mMode = _mode;   }

    inline void setType (GLenum _type)
    {
        mType = _type;
Robert Menzel's avatar
Robert Menzel committed
73
        mSizeOfType = getGLTypeSize(mType);
Robert Menzel's avatar
Robert Menzel committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }
    
    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
public:
    //! Bind this buffer
    inline void bind(void) const
    {
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext);
    }

    void draw(void) const
    {
        glDrawElements(mMode, mElements, mType, (GLvoid*)0);
    }

    //! Set data for this buffer
92
    //! Note: The function is not const, because it changes the corresponding GPU data
Robert Menzel's avatar
Robert Menzel committed
93 94 95
    inline void setData(
        const GLvoid* _pData)
    {
96
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext);
Robert Menzel's avatar
Robert Menzel committed
97 98 99 100 101 102 103 104 105 106 107 108 109
        glBufferData(
            GL_ELEMENT_ARRAY_BUFFER,
            mElements * mSizeOfType,
            _pData,
            mUsage);
    }

    //! Set data for this buffer
    inline void setData(
        const GLvoid* _pData,
        GLsizei _elements)
    {
        mElements = _elements;
110
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext);
Robert Menzel's avatar
Robert Menzel committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        glBufferData(
            GL_ELEMENT_ARRAY_BUFFER,
            mElements * mSizeOfType,
            _pData,
            mUsage);
    }

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
    GLuint  mContext;
    GLenum  mUsage;
    GLenum  mMode;
    GLsizei mElements;
    GLenum  mType;
    GLint   mSizeOfType;
};

130
ACGL_SHARED_TYPEDEF(ElementArrayBuffer)
Robert Menzel's avatar
Robert Menzel committed
131 132

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
133 134
} // ACGL

135
#endif // ACGL_OPENGL_OBJECTS_ELEMENTBUFFERDATA_HH