1 /**
2 Copyright: Copyright (c) 2013-2017 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module voxelman.world.mesh.chunkmesh;
8 
9 import anchovy.vao;
10 import anchovy.vbo;
11 import voxelman.math;
12 import voxelman.model.vertex;
13 import voxelman.graphics;
14 
15 struct ChunkMesh
16 {
17 	vec3 position;
18 
19 	private Vao vao;
20 	private Vbo vbo;
21 
22 	void del()
23 	{
24 		vbo.del;
25 		vao.del;
26 	}
27 
28 	void uploadMeshData(MeshVertex[] data)
29 	{
30 		vao.gen;
31 		vao.bind;
32 			vbo.gen;
33 			vbo.bind;
34 
35 			vbo.uploadData(data);
36 			MeshVertex.setAttributes();
37 
38 			vbo.unbind;
39 		vao.unbind;
40 	}
41 
42 	void render() const
43 	{
44 		vao.bind;
45 		vao.drawArrays(PrimitiveType.TRIANGLES, 0, cast(uint)numVertexes());
46 		vao.unbind;
47 	}
48 
49 	bool empty() const { return vbo.uploadedBytes == 0; }
50 
51 	ulong numVertexes() const {
52 		return vbo.uploadedBytes/MeshVertex.sizeof;
53 	}
54 
55 	ulong numTris() const {
56 		return numVertexes/3;
57 	}
58 
59 	size_t uploadedBytes() const {
60 		return vbo.uploadedBytes;
61 	}
62 }
63 
64 alias MeshVertex = VertexPosColor!(float, 3, ubyte, 4);
65 
66 void freeChunkMeshData(ref MeshVertex[] data)
67 {
68 	import std.experimental.allocator;
69 	import std.experimental.allocator.mallocator;
70 	Mallocator.instance.dispose(data);
71 	data = null;
72 }