vertex-buffer.h 8.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* ============================================================================
 * Freetype GL - A C OpenGL Freetype engine
 * Platform:    Any
 * WWW:         http://code.google.com/p/freetype-gl/
 * ----------------------------------------------------------------------------
 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of Nicolas P. Rougier.
 * ============================================================================
 */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
34 35
#ifndef __VERTEX_BUFFER_H__
#define __VERTEX_BUFFER_H__
36 37 38 39 40

#ifdef __cplusplus
extern "C" {
#endif

41
#include "opengl.h"
Nicolas.Rougier's avatar
Nicolas.Rougier committed
42
#include "vector.h"
43
#include "vertex-attribute.h"
44

Nicolas.Rougier's avatar
Nicolas.Rougier committed
45

46
/**
47 48 49 50 51 52 53
 * @file   vertex-buffer.h
 * @author Nicolas Rougier (Nicolas.Rougier@inria.fr)
 * @date   April, 2012
 *
 * @defgroup vertex-buffer Vertex buffer
 *
 * @{
Nicolas.Rougier's avatar
Nicolas.Rougier committed
54
 */
55 56 57 58 59


/**
 * Generic vertex buffer.
 */
60
typedef struct vertex_buffer_t
Nicolas.Rougier's avatar
Nicolas.Rougier committed
61
{
62 63 64 65
    /** Format of the vertex buffer. */
    char * format;

    /** Vector of vertices. */
66
    vector_t * vertices;
67

68
    /** GL identity of the vertices buffer. */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
69
    GLuint vertices_id;
70 71

    /** Vector of indices. */
72
    vector_t * indices;
73

74
    /** GL identity of the indices buffer. */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
75
    GLuint indices_id;
76

77 78 79 80 81 82
    /** Current size of the vertices buffer in GPU */
    size_t GPU_vsize;

    /** Current size of the indices buffer in GPU*/
    size_t GPU_isize;

83 84 85
    /** GL primitives to render. */
    GLenum mode;

86
    /** Whether the vertex buffer needs to be uploaded to GPU memory. */
87
    char state;
88

89 90 91
    /** Individual items */
    vector_t * items;

92
    /** Array of attributes. */
93 94
    vertex_attribute_t *attributes[MAX_VERTEX_ATTRIBUTE];
} vertex_buffer_t;
95 96 97 98 99 100 101 102


/**
 * Creates an empty vertex buffer.
 *
 * @param  format a string describing vertex format.
 * @return        an empty vertex buffer.
 */
103
  vertex_buffer_t *
Nicolas.Rougier's avatar
Nicolas.Rougier committed
104
  vertex_buffer_new( const char *format );
105 106 107


/**
108
 * Deletes vertex buffer and releases GPU memory.
109
 *
110
 * @param  self  a vertex buffer
111
 */
112 113
  void
  vertex_buffer_delete( vertex_buffer_t * self );
114

Nicolas.Rougier's avatar
Nicolas.Rougier committed
115

116
/**
117
 *  Returns the number of items in the vertex buffer
118
 *
119 120
 *  @param  self  a vertex buffer
 *  @return       number of items
121
 */
122 123
  size_t
  vertex_buffer_size( const vertex_buffer_t *self );
124

125

126 127 128 129 130 131 132 133
/**
 *  Returns vertex format
 *
 *  @param  self  a vertex buffer
 *  @return       vertex format
 */
  const char *
  vertex_buffer_format( const vertex_buffer_t *self );
134

135 136 137 138 139 140 141

/**
 * Print information about a vertex buffer
 *
 * @param  self  a vertex buffer
 */
  void
142
  vertex_buffer_print( vertex_buffer_t * self );
143 144


Nicolas.Rougier's avatar
Nicolas.Rougier committed
145
/**
146
 * Prepare vertex buffer for render.
Nicolas.Rougier's avatar
Nicolas.Rougier committed
147 148 149 150 151
 *
 * @param  self  a vertex buffer
 * @param  mode  render mode
 */
  void
152
  vertex_buffer_render_setup ( vertex_buffer_t *self,
153
                               GLenum mode );
154 155 156


/**
157
 * Finish rendering by setting back modified states
158
 *
Nicolas.Rougier's avatar
Nicolas.Rougier committed
159
 * @param  self  a vertex buffer
160
 */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
161
  void
162
  vertex_buffer_render_finish ( vertex_buffer_t *self );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
163

164 165 166 167 168 169 170 171

/**
 * Render vertex buffer.
 *
 * @param  self  a vertex buffer
 * @param  mode  render mode
 */
  void
172
  vertex_buffer_render ( vertex_buffer_t *self,
173
                         GLenum mode );
174

Nicolas.Rougier's avatar
Nicolas.Rougier committed
175

176
/**
177
 * Render a specified item from the vertex buffer.
178
 *
179 180
 * @param  self   a vertex buffer
 * @param  index index of the item to be rendered 
181 182
 */
  void
183 184
  vertex_buffer_render_item ( vertex_buffer_t *self,
                              size_t index );
185 186


Nicolas.Rougier's avatar
Nicolas.Rougier committed
187
/**
188
 * Upload buffer to GPU memory.
Nicolas.Rougier's avatar
Nicolas.Rougier committed
189 190 191 192
 *
 * @param  self  a vertex buffer
 */
  void
193
  vertex_buffer_upload( vertex_buffer_t *self );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
194 195


196
/**
197
 * Clear all items.
198 199 200 201
 *
 * @param  self  a vertex buffer
 */
  void
202
  vertex_buffer_clear( vertex_buffer_t *self );
203

Nicolas.Rougier's avatar
Nicolas.Rougier committed
204

205 206 207
/**
 * Appends indices at the end of the buffer.
 *
208 209 210 211 212
 * @param  self     a vertex buffer
 * @param  indices  indices to be appended
 * @param  icount   number of indices to be appended
 *
 * @private
213 214
 */
  void
215
  vertex_buffer_push_back_indices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
216 217
                                    const GLuint * indices,
                                    const size_t icount );
218

Nicolas.Rougier's avatar
Nicolas.Rougier committed
219

220 221 222
/**
 * Appends vertices at the end of the buffer.
 *
223 224
 * @note Internal use
 *
225 226
 * @param  self     a vertex buffer
 * @param  vertices vertices to be appended
227 228 229
 * @param  vcount   number of vertices to be appended
 *
 * @private
230 231
 */
  void
232
  vertex_buffer_push_back_vertices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
233 234
                                     const void * vertices,
                                     const size_t vcount );
235

Nicolas.Rougier's avatar
Nicolas.Rougier committed
236

237
/**
238
 * Insert indices in the buffer.
239 240 241 242
 *
 * @param  self    a vertex buffer
 * @param  index   location before which to insert indices
 * @param  indices indices to be appended
243 244 245
 * @param  icount  number of indices to be appended
 *
 * @private
246 247
 */
  void
248
  vertex_buffer_insert_indices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
249 250 251
                                 const size_t index,
                                 const GLuint *indices,
                                 const size_t icount );
252

Nicolas.Rougier's avatar
Nicolas.Rougier committed
253

254
/**
255
 * Insert vertices in the buffer.
256 257 258 259
 *
 * @param  self     a vertex buffer
 * @param  index    location before which to insert vertices
 * @param  vertices vertices to be appended
260
 * @param  vcount   number of vertices to be appended
261
 *
262
 * @private
263 264
 */
  void
265
  vertex_buffer_insert_vertices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
266 267 268
                                  const size_t index,
                                  const void *vertices,
                                  const size_t vcount );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
269

270
/**
271
 * Erase indices in the buffer.
272
 *
273 274 275
 * @param  self   a vertex buffer
 * @param  first  the index of the first index to be erased
 * @param  last   the index of the last index to be erased
276
 *
277
 * @private
278 279
 */
  void
280
  vertex_buffer_erase_indices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
281 282
                                const size_t first,
                                const size_t last );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
283

284
/**
285
 * Erase vertices in the buffer.
286
 *
287 288 289
 * @param  self   a vertex buffer
 * @param  first  the index of the first vertex to be erased
 * @param  last   the index of the last vertex to be erased
290
 *
291
 * @private
292 293
 */
  void
294
  vertex_buffer_erase_vertices ( vertex_buffer_t *self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
295 296
                                 const size_t first,
                                 const size_t last );
297

Nicolas.Rougier's avatar
Nicolas.Rougier committed
298

299
/**
300
 * Append a new item to the collection.
301
 *
302 303 304 305 306
 * @param  self   a vertex buffer
 * @param  vcount   number of vertices
 * @param  vertices raw vertices data
 * @param  icount   number of indices
 * @param  indices  raw indices data
307
 */
308 309
  size_t
  vertex_buffer_push_back( vertex_buffer_t * self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
310 311
                           const void * vertices, const size_t vcount,  
                           const GLuint * indices, const size_t icount );
312

Nicolas.Rougier's avatar
Nicolas.Rougier committed
313

314
/**
315
 * Insert a new item into the vertex buffer.
316
 *
317
 * @param  self      a vertex buffer
318 319 320 321 322
 * @param  index     location before which to insert item
 * @param  vertices  raw vertices data
 * @param  vcount    number of vertices
 * @param  indices   raw indices data
 * @param  icount    number of indices
323
 */
324 325
  size_t
  vertex_buffer_insert( vertex_buffer_t * self,
326
                        const size_t index,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
327 328
                        const void * vertices, const size_t vcount,  
                        const GLuint * indices, const size_t icount );
329 330 331 332 333 334 335 336 337

/**
 * Erase an item from the vertex buffer.
 *
 * @param  self     a vertex buffer
 * @param  index    index of the item to be deleted
 */
  void
  vertex_buffer_erase( vertex_buffer_t * self,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
338
                       const size_t index );
339

340
/** @} */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
341

342 343 344 345
#ifdef __cplusplus
}
#endif

Nicolas.Rougier's avatar
Nicolas.Rougier committed
346
#endif /* __VERTEX_BUFFER_H__ */