]> git.tdb.fi Git - libs/gl.git/blob - source/buffer.cpp
Style update: add spaces around assignment operators
[libs/gl.git] / source / buffer.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "arb_vertex_buffer_object.h"
9 #include "extension.h"
10 #include "buffer.h"
11
12 namespace Msp {
13 namespace GL {
14
15 Buffer::Buffer(BufferType t):
16         type(t),
17         usage(STATIC_DRAW)
18 {
19         static RequireExtension _req_vbo("GL_ARB_vertex_buffer_object");
20         if(type==PIXEL_PACK_BUFFER || type==PIXEL_UNPACK_BUFFER)
21                 static RequireExtension _req_pbo("GL_ARB_pixel_buffer_object");
22
23         glGenBuffersARB(1, &id);
24 }
25
26 Buffer::~Buffer()
27 {
28         glDeleteBuffersARB(1, &id);
29 }
30
31 void Buffer::bind(BufferType t) const
32 {
33         glBindBufferARB(t, id);
34         binding(t) = this;
35 }
36
37 void Buffer::maybe_bind() const
38 {
39         if(binding(type)!=this)
40                 bind();
41 }
42
43 void Buffer::set_usage(BufferUsage u)
44 {
45         usage = u;
46 }
47
48 void Buffer::data(unsigned size, const void *d)
49 {
50         maybe_bind();
51         glBufferDataARB(type, size, d, usage);
52 }
53
54 void Buffer::sub_data(unsigned offset, unsigned size, const void *d)
55 {
56         maybe_bind();
57         glBufferSubDataARB(type, offset, size, d);
58 }
59
60 void Buffer::unbind(BufferType type)
61 {
62         const Buffer *&ptr = binding(type);
63         if(ptr)
64         {
65                 glBindBufferARB(type, 0);
66                 ptr = 0;
67         }
68 }
69
70 const Buffer *&Buffer::binding(BufferType type)
71 {
72         switch(type)
73         {
74         case ARRAY_BUFFER:         return bound[0];
75         case ELEMENT_ARRAY_BUFFER: return bound[1];
76         case PIXEL_PACK_BUFFER:    return bound[2];
77         case PIXEL_UNPACK_BUFFER:  return bound[3];
78         default: throw InvalidParameterValue("Invalid buffer type");
79         }
80 }
81
82 const Buffer *Buffer::bound[4] = { 0, 0, 0, 0 };
83
84 } // namespace GL
85 } // namespace Msp