1 /**
2 Copyright: Copyright (c) 2013-2018 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 voxelman.graphics.vao;
10 import voxelman.graphics.vbo;
11 import voxelman.math;
12 import voxelman.model.vertex;
13 import voxelman.graphics;
14 public import voxelman.world.mesh.vertex;
15 
16 struct ChunkMesh
17 {
18 	vec3 position;
19 
20 	private Vao vao;
21 	private Vbo vbo;
22 
23 	void del()
24 	{
25 		vbo.del;
26 		vao.del;
27 	}
28 
29 	void uploadMeshData(MeshVertex[] data)
30 	{
31 		vao.gen;
32 		vao.bind;
33 			vbo.gen;
34 			vbo.bind;
35 
36 			vbo.uploadData(data);
37 			MeshVertex.setAttributes();
38 
39 			vbo.unbind;
40 		vao.unbind;
41 	}
42 
43 	void render() const
44 	{
45 		vao.bind;
46 		vao.drawArrays(PrimitiveType.TRIANGLES, 0, cast(uint)numVertexes());
47 		vao.unbind;
48 	}
49 
50 	bool empty() const { return vbo.uploadedBytes == 0; }
51 
52 	ulong numVertexes() const {
53 		return vbo.uploadedBytes/MeshVertex.sizeof;
54 	}
55 
56 	ulong numTris() const {
57 		return numVertexes/3;
58 	}
59 
60 	size_t uploadedBytes() const {
61 		return vbo.uploadedBytes;
62 	}
63 }
64 
65 void freeChunkMeshData(ref MeshVertex[] data)
66 {
67 	import std.experimental.allocator;
68 	import std.experimental.allocator.mallocator;
69 	Mallocator.instance.dispose(data);
70 	data = null;
71 }