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