vbo.cpp 5.84 KB
Newer Older
Mathieu Faverge's avatar
Mathieu Faverge committed
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 34 35 36 37 38 39 40 41 42
/*
** This file is part of the ViTE project.
**
** This software is governed by the CeCILL-A license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
** 
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
** 
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
** 
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-A license and that you accept its terms.
**
**
** ViTE developpers are (for version 0.* to 1.0):
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
**        - VUCHENER Clément 
**
*/
43 44 45
/*!
 *\file vbo.cpp
 */
46 47 48 49
 #include <GL/glew.h>
#include <stdlib.h>
#include <iostream>
#include "vbo.hpp"
50

51 52 53 54

#define ERROR(m) printf("%s", m);

using namespace std;
55

56 57
const int Vbo::VBO_OK              = 0;
const int Vbo::VBO_OUT_OF_MEMORY   = 1;
58
const int Vbo::VBO_UNDEFINED_ERROR = 2;
59 60 61 62 63 64 65 66 67 68 69 70

/***********************************
 *
 *
 *
 * Constructor and destructor.
 *
 *
 *
 **********************************/

Vbo::Vbo(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
71 72
}

73

74
Vbo::Vbo(int nb_object, Type object_type){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
75

76 77
    /* init class attributes */

78
    _nb_coordinates_per_vertex = 2;/* 2 dimensions (x and y) */
79
    _cur = 0;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
80
    _id = 0;
81

Olivier Lagrasse's avatar
Olivier Lagrasse committed
82
    _nb_object = nb_object;
83 84 85 86 87 88 89 90 91 92 93 94 95 96

    switch (object_type){
    case TRIANGLES:
        _nb_verteces_per_object = 3;
        _object_type = GL_TRIANGLES;
        break;
    case QUADS:
        _nb_verteces_per_object = 4;
        _object_type = GL_QUADS;
        break;
    case LINES:
        _nb_verteces_per_object = 2;
        _object_type = GL_LINES;
    }
97 98
    
    /* There is _nb_object*(number_of_vertices_per_object) elements. */
99
    _size =  _nb_coordinates_per_vertex*_nb_verteces_per_object*_nb_object;
100

101
    _client_state = GL_VERTEX_ARRAY;
102

103 104 105

    /* init OpenGL */

Olivier Lagrasse's avatar
Olivier Lagrasse committed
106
    glGenBuffers(1, &_id);/* create the buffer an put its value to _id */
107

Olivier Lagrasse's avatar
Olivier Lagrasse committed
108 109
    glBindBuffer(GL_ARRAY_BUFFER, _id);/* select our vbo */
    
110
    glBufferData(GL_ARRAY_BUFFER, _size*sizeof(Data_type), NULL, GL_STREAM_DRAW);/* allocate memory space inside graphic card */
111
       
Olivier Lagrasse's avatar
Olivier Lagrasse committed
112
    glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
113 114 115 116 117

}


Vbo::~Vbo(){
118
    glDeleteBuffers(1, &_id);/* release the buffer and free its memory inside the graphic card */
119
    _id = 0;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}




/***********************************
 *
 *
 *
 * Buffer filling.
 *
 *
 *
 **********************************/

135
int Vbo::add(const unsigned int data_size,  const Data_type* const data){
136

Olivier Lagrasse's avatar
Olivier Lagrasse committed
137 138
    glBindBuffer(GL_ARRAY_BUFFER, _id);/* select our vbo */

139 140 141 142 143 144 145
    /* Take care:
     *
     * For performance, please use glMapBuffer() if data amount is more than 32Kbits,
     * else use glBufferSubData().
     *
     */

Olivier Lagrasse's avatar
Olivier Lagrasse committed
146
    if ( (_cur+data_size) <= _size){
147
        
148
        glBufferSubData(GL_ARRAY_BUFFER, _cur*sizeof(Data_type), data_size*sizeof(Data_type), data);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
149
        _cur += data_size;
150
   
Olivier Lagrasse's avatar
Olivier Lagrasse committed
151 152 153 154 155 156 157
    }else{
        glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
        return Vbo::VBO_OUT_OF_MEMORY;
    }
    
    glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */

158

Olivier Lagrasse's avatar
Olivier Lagrasse committed
159 160 161 162
    return Vbo::VBO_OK;
}


163
int Vbo::get_remaining_size() const{
164
    return (_size-_cur)/(_nb_verteces_per_object*_nb_coordinates_per_vertex);
165 166 167 168
}



Olivier Lagrasse's avatar
Olivier Lagrasse committed
169 170 171 172 173 174 175 176 177 178 179

 /***********************************
  *
  *
  *
  * Buffer display.
  *
  *
  *
  **********************************/

180
void Vbo::display() const{
Olivier Lagrasse's avatar
Olivier Lagrasse committed
181 182 183 184

    glBindBuffer(GL_ARRAY_BUFFER, _id);/* select our vbo */


185 186 187 188 189 190 191 192
    /* glVertexPointer(A, B, C, D)
     *
     * A: coordinate number to identify a vertex (2 in 2 dimension space, 3 in 3 dimension space).
     * B: the coordinate type (GL_INT, GL_FLOAT, GL_DOUBLE, etc.)
     * C: stride -> the case offset between two vertex group (and not coordinate !) . Use to interleaves to kind of objects (for example, verteces and colors).
     * D: the vertex buffer address in the VRAM. Use 0 with the VBO_BUFFER_OFFSET macro to convert a number into an address.
     */
    glVertexPointer(_nb_coordinates_per_vertex, GL_DOUBLE, 0, VBO_BUFFER_OFFSET(0));/* give to OpenGL the VBO address */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
193

194 195
    glEnableClientState(_client_state);
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
196

197 198 199 200 201 202 203 204 205 206 207 208
    /* glDrawArrays(A, B, C)
     *
     * A: an OpenGL primitive (GL_LINES, GL_TRIANGLES, etc.)
     * B: the first vertex index of the vertex buffer which will be printed.
     * C: the whole number of verteces which will be drawn.
     *
     * Please take care that a vertex is represented by _nb_coordinates_per_vertex cases of the _cur array.
     */
    glDrawArrays(_object_type, 0, _cur/_nb_coordinates_per_vertex);/* RENDERING */
        
    glDisableClientState(_client_state);
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
209
    glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}




 /***********************************
  *
  *
  *
  * Debug tools.
  *
  *
  *
  **********************************/

void Vbo::display_info(){

    GLvoid* _read_buf = NULL;
228
 
Olivier Lagrasse's avatar
Olivier Lagrasse committed
229
}