gKit2 light

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

buffer.cpp
1
2 #include <cstdio>
3
4 #include "buffer.h"
5
6
7 GLuint make_buffer( const GLenum target, const size_t data_size, const void *data, const GLenum usage )
8 {
9 if(data_size == 0)
10 {
11 printf("[error] 0B buffer...\n");
12 return 0;
13 }
14
15 GLuint buffer;
16 glGenBuffers(1, &buffer);
17 glBindBuffer(target, buffer);
18 glBufferData(target, data_size, data, usage);
19 return buffer;
20 }
21
22
23 GLuint create_vertex_format( )
24 {
25 GLuint vao;
26 glGenVertexArrays(1, &vao);
27 glBindVertexArray(vao);
28 return vao;
29 }
30
31 void release_vertex_format( const GLuint vao )
32 {
33 if(vao == 0)
34 return;
35
36 glBindVertexArray(vao);
37
38 GLint n= 0;
39 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &n);
40
41 // recupere les vertex buffers associes au vao
42 GLuint buffer= 0;
43 for(int i= 0; i < n; i++)
44 {
45 glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, (GLint *) &buffer);
46 if(buffer > 0)
47 glDeleteBuffers(1, &buffer);
48 }
49
50 // et l'index buffer
51 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, (GLint *) &buffer);
52 if(buffer > 0)
53 glDeleteBuffers(1, &buffer);
54
55 glDeleteVertexArrays(1, &vao);
56 }
57
58
59 GLuint make_vertex_buffer( const GLuint vao, const GLint attribute,
60 const int size, const GLenum type, const size_t data_size, const void *data )
61 {
62 if(attribute < 0)
63 return 0;
64
65 #ifndef GK_RELEASE
66 // verifie que le vertex array est selectionne
67 GLuint current;
68 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint *) &current);
69 if(current != vao)
70 {
71 printf("[error] wrong vertex array %u...\n", current);
72 glBindVertexArray(vao);
73 }
74 #endif
75
76 GLuint buffer= make_buffer(GL_ARRAY_BUFFER, data_size, data);
77 if(buffer > 0)
78 {
79 // configure l'utilisation du buffer
80 glVertexAttribPointer(attribute, size, type, GL_FALSE, 0, 0);
81 glEnableVertexAttribArray(attribute);
82 }
83 return buffer;
84 }
85
86 GLuint make_index_buffer( const GLuint vao, const size_t data_size, const void *data )
87 {
88 #ifndef GK_RELEASE
89 // verifie que le vertex array est selectionne
90 GLuint current;
91 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint *) &current);
92 if(current != vao)
93 {
94 printf("[error] wrong vertex array %u...\n", current);
95 glBindVertexArray(vao);
96 }
97 #endif
98
99 GLuint buffer= make_buffer(GL_ELEMENT_ARRAY_BUFFER, data_size, data);
100 return buffer;
101 }
102
103
104 bool update_buffer( const GLenum target, const size_t data_size, const void *data )
105 {
106 #ifndef GK_RELEASE
107 GLint buffer_size= 0;
108 glGetBufferParameteriv(target, GL_BUFFER_SIZE, &buffer_size);
109 if((size_t) buffer_size < data_size)
110 {
111 printf("[error] updating small buffer %d < %d...\n", (int) buffer_size, (int) data_size);
112 glBufferData(target, data_size, data, GL_STATIC_DRAW);
113 return false;
114 }
115 #endif
116
117 glBufferSubData(target, 0, data_size, data);
118 return true;
119 }
120
121 bool update_vertex_buffer( const GLuint vao, const GLint attribute, const size_t data_size, const void *data )
122 {
123 #ifndef GK_RELEASE
124 // verifie que le vertex array est selectionne
125 GLuint current;
126 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint *) &current);
127 if(current != vao)
128 {
129 printf("[error] wrong vertex array %u...\n", current);
130 glBindVertexArray(vao);
131 }
132 #endif
133
134 GLuint buffer= 0;
135 glGetVertexAttribiv(attribute, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, (GLint *) &buffer);
136 if(buffer == 0)
137 return false;
138
139 glBindBuffer(GL_ARRAY_BUFFER, buffer);
140 return update_buffer(GL_ARRAY_BUFFER, data_size, data);
141 }
142
143 bool update_index_buffer( const GLuint vao, const size_t data_size, const void *data )
144 {
145 #ifndef GK_RELEASE
146 // verifie que le vertex array est selectionne
147 GLuint current;
148 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint *) &current);
149 if(current != vao)
150 {
151 printf("[error] wrong vertex array %u...\n", current);
152 glBindVertexArray(vao);
153 }
154 #endif
155
156 // recupere l'index buffer
157 GLuint buffer;
158 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, (GLint *) &buffer);
159 if(buffer == 0)
160 return false;
161
162 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
163 return update_buffer(GL_ELEMENT_ARRAY_BUFFER, data_size, data);
164 }
165
166
make_buffer
GLuint make_buffer(const GLenum target, const size_t data_size, const void *data, const GLenum usage)
cree un buffer. a detruire avec glDeleteBuffers( ).
Definition: buffer.cpp:7
update_vertex_buffer
bool update_vertex_buffer(const GLuint vao, const GLint attribute, const size_t data_size, const void *data)
modifie le contenu d'un vertex buffer associe au vertex format, vao.
Definition: buffer.cpp:121
release_vertex_format
void release_vertex_format(const GLuint vao)
detruit le vertex array vao et les buffers associes, crees par make_vertex_buffer() et make_index_buf...
Definition: buffer.cpp:31
create_vertex_format
GLuint create_vertex_format()
cree un vertex array. a detruire avec release_vertex_format( ).
Definition: buffer.cpp:23
make_index_buffer
GLuint make_index_buffer(const GLuint vao, const size_t data_size, const void *data)
cree un index buffer et configure le vertex array vao. detruit par release_vertex_format( )...
Definition: buffer.cpp:86
update_buffer
bool update_buffer(const GLenum target, const size_t data_size, const void *data)
modifie le contenu d'un buffer.
Definition: buffer.cpp:104
update_index_buffer
bool update_index_buffer(const GLuint vao, const size_t data_size, const void *data)
modifie le contenu d'un index buffer associe au vertex format, vao
Definition: buffer.cpp:143
printf
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
buffer.h
make_vertex_buffer
GLuint make_vertex_buffer(const GLuint vao, const GLint attribute, const int size, const GLenum type, const size_t data_size, const void *data)
cree un vertex buffer et configure le vertex array vao. detruit par release_vertex_format( )...
Definition: buffer.cpp:59