Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (77 sloc) 2.394 kb
f329a53 @rsebbe imported
rsebbe authored
1 //
2 // GLBuffer.m
3 // CeedGL
4 //
5 // Created by Raphael Sebbe on 01/11/10.
6 // Copyright (c) 2010 Creaceed. All rights reserved.
7 //
8
a9c9108 @rsebbe Added GLAllocator to (optionally) manage GL objects more easily. Moderni...
rsebbe authored
9 #import <CeedGL/GLBuffer.h>
f329a53 @rsebbe imported
rsebbe authored
10
11
12 @implementation GLBuffer
13
14 @synthesize usage = mUsage, size = mSize;
15
a9c9108 @rsebbe Added GLAllocator to (optionally) manage GL objects more easily. Moderni...
rsebbe authored
16 + (instancetype)buffer
f329a53 @rsebbe imported
rsebbe authored
17 {
1adedbc @rsebbe updated targets (all libs, iOS/Mac) to use ARC.
rsebbe authored
18 return [[self alloc] init];
f329a53 @rsebbe imported
rsebbe authored
19 }
20
a9c9108 @rsebbe Added GLAllocator to (optionally) manage GL objects more easily. Moderni...
rsebbe authored
21 - (instancetype)init {
f329a53 @rsebbe imported
rsebbe authored
22 if ((self = [super init])) {
23 // Initialization code here.
24 }
25
26 return self;
27 }
28
29 #pragma mark -
30 #pragma mark Handle creation/destruction
31 - (void)createHandle
32 {
33 GL_EXCEPT(mHandle, @"Trying to create a new handle over an existing one");
34
35 glGenBuffers(1, &mHandle);
36
37 GL_EXCEPT(!mHandle, @"Could not create handle (no current GL context?)");
38 }
39 - (void)destroyHandle
40 {
b75961e @rsebbe additional consistency checks (no destroy if handle owner is set) + unbi...
rsebbe authored
41 GL_EXCEPT(mHandle != 0 && mHandleOwner != nil, @"Cannot destroy a handle if lifetime is managed by an owner");
f329a53 @rsebbe imported
rsebbe authored
42 GL_EXCEPT(!mHandle, @"Trying to destroy a NULL handle");
43
44 glDeleteBuffers(1, &mHandle);
45 mHandle = 0;
46 }
47 - (void)setFromExistingHandle:(GLuint)handle size:(GLsizeiptr)size usage:(GLenum)usage
48 {
2a45a44 @rsebbe added CoreVideo-specific category to GLTexture to derive textures from C...
rsebbe authored
49 [self setFromExistingHandle:handle size:size usage:usage owner:nil];
50 }
51 - (void)setFromExistingHandle:(GLuint)handle size:(GLsizeiptr)size usage:(GLenum)usage owner:(id)owner
52 {
f329a53 @rsebbe imported
rsebbe authored
53 mHandle = handle;
54 mSize = size;
55 mUsage = usage;
2a45a44 @rsebbe added CoreVideo-specific category to GLTexture to derive textures from C...
rsebbe authored
56 self.handleOwner = owner;
f329a53 @rsebbe imported
rsebbe authored
57 }
58 - (void)loadData:(const GLvoid*)data size:(GLsizeiptr)size usage:(GLenum)usage target:(GLenum)target
59 {
60 if(mHandle == 0)
61 [self createHandle];
62
63 glBindBuffer(target, mHandle);
64 glBufferData(target, size, data, usage);
65 glBindBuffer(target, 0);
66
67 mSize = size;
68 mUsage = usage;
69 }
70 - (void)loadSubData:(const GLvoid*)data offset:(GLintptr)offset size:(GLsizeiptr)size target:(GLenum)target
71 {
72 GL_EXCEPT(mHandle == 0, @"Trying to load subdata on non-existing buffer");
73 glBindBuffer(target, mHandle);
74 glBufferSubData(target, offset, size, data);
75 glBindBuffer(target, 0);
76 }
77 - (void)loadDataFromSource:(id<GLBufferDataSource>)source usage:(GLenum)usage target:(GLenum)target
78 {
79 [self loadData:[source bufferData] size:[source bufferDataSize] usage:usage target:target];
80 }
81 - (void)loadSubDataFromSource:(id<GLBufferDataSource>)source offset:(GLintptr)offset size:(GLsizeiptr)size target:(GLenum)target
82 {
83 [self loadSubData:[source bufferData]+offset offset:offset size:size target:target];
84 }
85
86 - (void)bind:(GLenum)target
87 {
88 GL_EXCEPT(mHandle == 0, @"Trying to bind non-existing buffer");
89 glBindBuffer(target, mHandle);
90 }
91
92 @end
Something went wrong with that request. Please try again.