summaryrefslogtreecommitdiffstats
path: root/vcl/inc/opengl/BufferObject.hxx
blob: 396de89de0ed5747a2f0178a481d01ef5aeacda8 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#ifndef INCLUDED_VCL_INC_OPENGL_BUFFEROBJECT_H
#define INCLUDED_VCL_INC_OPENGL_BUFFEROBJECT_H

namespace vcl
{

template<typename TYPE, GLenum BUFFER_TYPE>
class BufferObject
{
private:
    GLuint mId;

public:
    BufferObject()
        : mId(0)
    {
        glGenBuffers(1, &mId);
        CHECK_GL_ERROR();
    }

    virtual ~BufferObject()
    {
        if (mId)
        {
            glDeleteBuffers(1, &mId);
            CHECK_GL_ERROR();
            mId = 0;
        }
    }

    void bind()
    {
        if (mId)
        {
            glBindBuffer(BUFFER_TYPE, mId);
            CHECK_GL_ERROR();
        }
    }

    void unbind()
    {
        if (mId)
        {
            glBindBuffer(BUFFER_TYPE, 0);
            CHECK_GL_ERROR();
        }
    }

    void upload(const std::vector<TYPE>& rData)
    {
        if (mId)
        {
            bind();
            glBufferData(BUFFER_TYPE, sizeof(TYPE) * rData.size(), rData.data(), GL_STATIC_DRAW);
            CHECK_GL_ERROR();
        }
    }

};

template<typename TYPE>
class VertexBufferObject final : public BufferObject<TYPE, GL_ARRAY_BUFFER>
{
};

class IndexBufferObject final : public BufferObject<GLuint, GL_ELEMENT_ARRAY_BUFFER>
{
};

} // end vcl

#endif // INCLUDED_VCL_INC_OPENGL_BUFFEROBJECT_H

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */