1 module prova.graphics.primitives.mesh;
2 
3 import prova.graphics,
4        prova.math;
5 
6 ///
7 class Mesh
8 {
9   ///
10   uint VAO;
11   private uint VBO = -1;
12   private uint IBO = -1;
13   package(prova) int indexCount;
14 
15   ///
16   this()
17   {
18     glGenVertexArrays(1, &VAO);
19   }
20 
21   ///
22   final void setVBO(float[] vertices, int dimensions)
23   {
24     glBindVertexArray(VAO);
25     createBuffer(VBO);
26     glBindBuffer(GL_ARRAY_BUFFER, VBO);
27     glBufferData(GL_ARRAY_BUFFER, vertices.length * float.sizeof, vertices.ptr, GL_STATIC_DRAW);
28     glVertexAttribPointer(0, dimensions, GL_FLOAT, GL_FALSE, 0, null);
29     glEnableVertexAttribArray(0);
30   }
31 
32   ///
33   final void setIBO(uint[] indexes)
34   {
35     indexCount = cast(int) indexes.length;
36 
37     glBindVertexArray(VAO);
38     createBuffer(IBO);
39     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
40     glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexes.length * uint.sizeof, indexes.ptr, GL_STATIC_DRAW);
41   }
42 
43   private void createBuffer(ref uint buffer)
44   {
45     // delete old buffers
46     if(buffer != -1)
47       glDeleteBuffers(1, &buffer);
48 
49     // create a new buffer
50     glGenBuffers(1, &buffer);
51   }
52 
53   ~this()
54   {
55     glDeleteVertexArrays(1, &VAO);
56 
57     if(VBO != -1)
58       glDeleteBuffers(1, &VBO);
59     if(IBO != -1)
60       glDeleteBuffers(1, &IBO);
61   }
62 }