]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.h
Drop Id tags and copyright notices from files
[libs/gl.git] / source / buffer.h
1 #ifndef MSP_GL_BUFFER_H_
2 #define MSP_GL_BUFFER_H_
3
4 #include "gl.h"
5
6 namespace Msp {
7 namespace GL {
8
9 enum BufferType
10 {
11         ARRAY_BUFFER         = GL_ARRAY_BUFFER_ARB,
12         ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER_ARB,
13         PIXEL_PACK_BUFFER    = GL_PIXEL_PACK_BUFFER_ARB,
14         PIXEL_UNPACK_BUFFER  = GL_PIXEL_UNPACK_BUFFER_ARB
15 };
16
17 enum BufferUsage
18 {
19         STREAM_DRAW  = GL_STREAM_DRAW_ARB,
20         STREAM_READ  = GL_STREAM_READ_ARB,
21         STREAM_COPY  = GL_STREAM_COPY_ARB,
22         STATIC_DRAW  = GL_STATIC_DRAW_ARB,
23         STATIC_READ  = GL_STATIC_READ_ARB,
24         STATIC_COPY  = GL_STATIC_COPY_ARB,
25         DYNAMIC_DRAW = GL_DYNAMIC_DRAW_ARB,
26         DYNAMIC_READ = GL_DYNAMIC_READ_ARB,
27         DYNAMIC_COPY = GL_DYNAMIC_COPY_ARB
28 };
29
30 /**
31 A buffer for storing data in GL memory.  Putting vertex and index data in
32 buffers can improve rendering performance.  The VertexArray and Mesh classes
33 contain built-in support for buffers.
34 */
35 class Buffer
36 {
37 private:
38         BufferType type;
39         BufferUsage usage;
40         unsigned id;
41
42         static const Buffer *bound[4];
43
44 public:
45         Buffer(BufferType);
46         ~Buffer();
47
48 private:
49         const Buffer *maybe_bind() const;
50
51 public:
52         /** Sets the usage hint of the buffer.  It will take effect the next time
53         the buffer's contents are defined. */
54         void set_usage(BufferUsage);
55
56         /** Uploads data into the buffer, completely replacing any previous
57         contents. */
58         void data(unsigned, const void *);
59
60         /** Overwrites part of the buffer data with new data.  The buffer size can
61         not be changed with this call. */
62         void sub_data(unsigned, unsigned, const void *);
63
64         /** Binds the buffer in its default slot. */
65         void bind() const { bind_to(type); }
66
67         /** Binds the buffer in an alternate slot. */
68         void bind_to(BufferType) const;
69
70         /** Unbinds the buffer from its default slot. */
71         void unbind() const { unbind_from(type); }
72
73         static const Buffer *current(BufferType t) { return binding(t); }
74         static void unbind_from(BufferType);
75 private:
76         static const Buffer *&binding(BufferType);
77         static void restore(const Buffer *, BufferType);
78 };
79
80 /**
81 An adaptor for Buffer to make it compatible with Bind.
82 */
83 template<BufferType T>
84 class BufferAlias
85 {
86 private:
87         const Buffer &buffer;
88
89 public:
90         BufferAlias(const Buffer &b): buffer(b) { }
91
92         void bind() const { buffer.bind_to(T); }
93         static const Buffer *current() { return Buffer::current(T); }
94         static void unbind() { Buffer::unbind_from(T); }
95 };
96
97 } // namespace GL
98 } // namespace Msp
99
100 #endif