a2e67352512059d1b7f8d6cbbf071bf1c892c977
[sdk] / ecere / src / gfx / drivers / gl3 / glab.ec
1 // GL Array Buffer Manipulation
2
3 #if defined(_GLES)
4  #define ES1_1
5 #else
6 // #define SHADERS
7 #endif
8
9 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
10 #  if defined(SHADERS)
11 #     include "gl_core_3_3.h"
12 #  else
13 #     include "gl_compat_4_4.h"
14 #  endif
15 #endif
16
17 import "Display"
18
19 // Kept public for now
20 public void GLABDeleteBuffers(int count, GLAB * buffers)
21 {
22    int i;
23    for(i = 0; i < count; i++)
24    {
25       uint buffer = buffers[i].buffer;
26       if(buffer)
27       {
28          if(buffer == glabCurArrayBuffer)
29             GLABBindBuffer(GL_ARRAY_BUFFER, 0);
30          else if(buffer == glabCurElementBuffer)
31             GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
32       }
33    }
34    if(count && buffers[0].buffer)
35       glDeleteBuffers(count, (GLuint *)buffers);
36 }
37
38 void GLABBindBuffer(int target, uint buffer)
39 {
40    glBindBuffer(target, buffer);
41    if(target == GL_ARRAY_BUFFER)
42       glabCurArrayBuffer = buffer;
43    else if(target == GL_ELEMENT_ARRAY_BUFFER)
44       glabCurElementBuffer = buffer;
45 }
46
47 public enum GLBufferContents { vertex, normal, texCoord, color };
48
49 public define noAB = GLAB { 0 };
50
51 uint glabCurArrayBuffer;
52
53 public struct GLAB
54 {
55    uint buffer;
56
57    void upload(uint size, void * data)
58    {
59       if(this != null)
60       {
61          if(!buffer)
62             glGenBuffers(1, &buffer);
63          if(glabCurArrayBuffer != buffer)
64             GLABBindBuffer(GL_ARRAY_BUFFER, buffer);
65          glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);  //GL_DYNAMIC_DRAW);
66       }
67    }
68
69    void free()
70    {
71       if(this != null && buffer)
72       {
73          GLABDeleteBuffers(1, this);
74          buffer = 0;
75       }
76    }
77
78    void use(GLBufferContents contents, int n, int type, uint stride, void * pointer)
79    {
80       if(glabCurArrayBuffer != ((this != null) ? buffer : 0))
81          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
82 #ifdef SHADERS
83       glVertexAttribPointer(contents, n, type, GL_FALSE, stride, pointer);
84 #else
85       switch(contents)
86       {
87          case normal:   glNormalPointer(type, stride, pointer); break;
88          case vertex:   glVertexPointer(n, type, stride, pointer); break;
89          case texCoord: glTexCoordPointer(n, type, stride, pointer); break;
90          case color:    glColorPointer(n, type, stride, pointer); break;
91       }
92 #endif
93    }
94
95    void useVertTrans(uint count, int n, int type, uint stride, void * pointer)
96    {
97 #if defined(_GLES) || defined(ES1_1)
98       if(glabCurArrayBuffer != ((this != null) ? buffer : 0))
99          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
100       if(type == GL_INT)
101          glimtkVertexPointeri(n, stride, pointer, count);
102       else if(type == GL_DOUBLE)
103       {
104 #ifdef SHADERS
105          glVertexAttribPointer(GLBufferContents::vertex, n, GL_DOUBLE, GL_FALSE, stride, pointer);
106 #else
107          glVertexPointer(n, GL_DOUBLE, stride, pointer);
108 #endif
109       }
110 #else
111       use(vertex, n, type, stride, pointer);
112 #endif
113    }
114 };
115
116 uint glabCurElementBuffer;
117
118 public define noEAB = GLEAB { 0 };
119
120 public struct GLEAB
121 {
122    uint buffer;
123
124    void upload(uint size, void * data)
125    {
126       if(this != null)
127       {
128          if(!buffer)
129             glGenBuffers(1, &buffer);
130
131          if(glabCurElementBuffer != buffer)
132             GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
133          glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);  //GL_DYNAMIC_DRAW);
134       }
135    }
136
137    void free()
138    {
139       if(this != null && buffer)
140       {
141          GLABDeleteBuffers(1, (GLAB *)this);
142          buffer = 0;
143       }
144    }
145
146    void draw(int primType, int count, int type, void * indices)
147    {
148       if(glabCurElementBuffer != ((this != null) ? buffer : 0))
149          GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
150 #if defined(_GLES) || defined(ES1_1)
151       type = GL_UNSIGNED_SHORT;
152 #endif
153       glDrawElements(primType, count, type, indices);
154    }
155 };