1 /**
2 Copyright: Copyright (c) 2017 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module anchovy.vbo;
7 
8 import derelict.opengl3.gl3;
9 import anchovy.glerrors;
10 
11 
12 struct Vbo
13 {
14 	import core.atomic;
15 	shared static size_t numAllocated;
16 
17 	private GLuint handle_;
18 	private size_t uploadedBytes_;
19 
20 	GLuint handle() const {
21 		return handle_;
22 	}
23 
24 	// data size in bytes
25 	size_t uploadedBytes() const {
26 		return uploadedBytes_;
27 	}
28 
29 	bool isGenerated() const {
30 		return handle_ != 0;
31 	}
32 
33 	bool empty() const {
34 		return uploadedBytes_ == 0;
35 	}
36 
37 	void gen() {
38 		if (isGenerated) return;
39 		checkgl!glGenBuffers(1, &handle_);
40 		atomicOp!"+="(numAllocated, 1);
41 	}
42 
43 	void del() {
44 		if (handle_ == 0) return;
45 		atomicOp!"-="(numAllocated, 1);
46 		checkgl!glDeleteBuffers(1, &handle_);
47 		uploadedBytes_ = 0;
48 	}
49 
50 	void bind() const {
51 		assert(isGenerated);
52 		checkgl!glBindBuffer(GL_ARRAY_BUFFER, handle_);
53 	}
54 
55 	static void unbind() {
56 		checkgl!glBindBuffer(GL_ARRAY_BUFFER, 0);
57 	}
58 
59 	// requires binding
60 	void uploadData(Vert)(const Vert[] data) {
61 		uploadedBytes_ = data.length*Vert.sizeof;
62 		checkgl!glBufferData(GL_ARRAY_BUFFER, uploadedBytes_, data.ptr, GL_STATIC_DRAW);
63 	}
64 }