vbo.cpp 7.09 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
#include <GL/glew.h>
47 48
#include <stdlib.h>
#include <iostream>
49 50 51 52
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
53
#include "vbo.hpp"
54
#include "Shader.hpp"
55

56 57 58 59

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

using namespace std;
60 61 62 63 64 65 66 67 68 69 70 71 72


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

Vbo::Vbo(){
73
    _nbVertex=0;
74 75
}

76 77
Vbo::Vbo(Shader *s){
    _shader = s;
78 79
    _nbVertex = 0;
}
80 81

Vbo::~Vbo(){
82 83 84 85
    glDeleteBuffers(1, &_vboID);/* release the buffer and free its memory inside the graphic card */
    _vboID = 0;
    glDeleteVertexArrays(1, &_vaoID);
    _vaoID = 0;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}




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

101 102 103 104 105 106 107
int Vbo::add(Element_pos x, Element_pos y, Element_col r, Element_col g, Element_col b){
    _vertex.push_back(x);
    _vertex.push_back(y);
    _colors.push_back(r);
    _colors.push_back(g);
    _colors.push_back(b);
    _nbVertex++;
108
}
109

Thibault Soucarre's avatar
Thibault Soucarre committed
110 111 112 113 114 115 116 117
int Vbo::add(Element_pos x, Element_pos y, Element_pos tx, Element_pos ty){
    _vertex.push_back(x);
    _vertex.push_back(y);
    _texture_coord.push_back(tx);
    _texture_coord.push_back(ty);
    _nbVertex++;
}

118
int Vbo::add(Element_pos x, Element_pos y, char b){
119 120
    _vertex.push_back(x);
    _vertex.push_back(y);
121
    _shaded.push_back(b);
122 123 124
    _nbVertex++;
}

125 126 127 128 129 130 131
int Vbo::add(Element_pos x, Element_pos y, float b){
    _vertex.push_back(x);
    _vertex.push_back(y);
    _shaded2.push_back(b);
    _nbVertex++;
}

132 133 134 135 136 137
int Vbo::add(Element_pos x, Element_pos y){
    _vertex.push_back(x);
    _vertex.push_back(y);
    _nbVertex++;
}

138
void Vbo::config(int glsl){
139 140 141
    std::cout << "Configuration du VBO. Nombre de points : "<< _vertex.size()/2 << std::endl;
    int vertex_size = _vertex.size() * sizeof(Element_pos);
    int colors_size = _colors.size() * sizeof(Element_col);
Thibault Soucarre's avatar
Thibault Soucarre committed
142
    int texture_size = _texture_coord.size() * sizeof(Element_pos);
143
    int shaded_size;
144 145
    Element_pos * vertex = &_vertex[0];
    Element_col * colors = &_colors[0];
Thibault Soucarre's avatar
Thibault Soucarre committed
146
    Element_pos * texture = &_texture_coord[0];
147 148 149 150 151 152 153 154 155 156
    char * shaded;
    float* shaded2;
    if(glsl<330){
        shaded_size = _shaded2.size() * sizeof(float);
        shaded2 = &_shaded2[0];
    }
    else{
        shaded_size = _shaded.size() * sizeof(char);
        shaded = &_shaded[0];
    }
157 158 159 160 161 162 163 164
    //Delete old vbo if necessary
    if(glIsBuffer(_vboID) == GL_TRUE)
        glDeleteBuffers(1, &_vboID);
    //Generate vbo
    glGenBuffers(1, &_vboID);
    //Select our VBO
    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    //Allocate memory inside graphic card
Thibault Soucarre's avatar
Thibault Soucarre committed
165
    glBufferData(GL_ARRAY_BUFFER, vertex_size+colors_size+shaded_size+texture_size, 0, GL_STATIC_DRAW);
166 167 168
    //Send datas
    glBufferSubData(GL_ARRAY_BUFFER, 0          , vertex_size, vertex);
    glBufferSubData(GL_ARRAY_BUFFER, vertex_size, colors_size, colors);
Thibault Soucarre's avatar
Thibault Soucarre committed
169
    glBufferSubData(GL_ARRAY_BUFFER, vertex_size+colors_size, texture_size, texture);
170 171 172 173
    if(glsl<330)
        glBufferSubData(GL_ARRAY_BUFFER, vertex_size+colors_size, shaded_size, shaded2);
    else
        glBufferSubData(GL_ARRAY_BUFFER, vertex_size+colors_size, shaded_size, shaded);
174 175
    //Deselect our VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
176
    //Clear RAM and realoccate vectors
177 178
    std::vector<Element_pos>().swap(_vertex);
    std::vector<Element_col>().swap(_colors);
179
    std::vector<char>().swap(_shaded);
180
    std::vector<float>().swap(_shaded2);
Thibault Soucarre's avatar
Thibault Soucarre committed
181
    std::vector<Element_pos>().swap(_texture_coord);
182 183 184 185 186 187 188 189 190 191 192 193
    //Delete old vao if necessary
    if(glIsVertexArray(_vaoID))
        glDeleteVertexArrays(1, &_vaoID);
    //Generate VAO
    glGenVertexArrays(1, &_vaoID);
    //Select our VAO
    glBindVertexArray(_vaoID);
    //The following instructions are stocked inside the VAO
    {
        //Select VBO
        glBindBuffer(GL_ARRAY_BUFFER, _vboID);
        //Send vertex
194
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
195 196
        glEnableVertexAttribArray(0);
        //Send colors
197 198 199 200
        if(colors_size>0){
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertex_size));
            glEnableVertexAttribArray(1);
        }
Thibault Soucarre's avatar
Thibault Soucarre committed
201 202 203 204
        if(texture_size>0){
            glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertex_size));
            glEnableVertexAttribArray(2);
        }
205
        if(shaded_size>0){
206 207 208 209 210 211 212 213
            if(glsl<330){
                glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertex_size+colors_size));
                glEnableVertexAttribArray(1);
            }
            else{
                glVertexAttribPointer(1, 1, GL_BYTE, GL_FALSE, 0, BUFFER_OFFSET(vertex_size+colors_size));
                glEnableVertexAttribArray(1);
            }
214
        }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        //Deselect VBO
        glBindBuffer(GL_ARRAY_BUFFER,0);
    }
    //Deselect VAO
    glBindVertexArray(0);
}


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

Thibault Soucarre's avatar
Thibault Soucarre committed
233

234 235
void Vbo::lock(){
    glBindVertexArray(_vaoID);
236 237
}

238 239 240
void Vbo::unlock(){
    glBindVertexArray(0);
}
241

242 243 244
int Vbo::getNbVertex(){
    return _nbVertex;
}
245

246 247 248 249
void Vbo::setNbVertex(int n){
    _nbVertex = n;
}

250 251 252 253 254 255 256 257
Shader * Vbo::get_shader(){
    return _shader;
}

void Vbo::delete_shader(){
    delete _shader;
    _shader = NULL;
}
258

259 260 261
void Vbo::set_shader(Shader *s){
    _shader = s;
}