Render_alternate.cpp 50.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*
** 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".
Thibault Soucarre's avatar
Thibault Soucarre committed
9
**
10 11 12 13 14
** 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.
Thibault Soucarre's avatar
Thibault Soucarre committed
15
**
16 17 18 19 20 21 22 23 24 25
** 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.
Thibault Soucarre's avatar
Thibault Soucarre committed
26
**
27 28 29 30 31 32 33 34 35 36 37 38 39
** 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 developers are (for version 0.* to 1.0):
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
Thibault Soucarre's avatar
Thibault Soucarre committed
40
**        - VUCHENER Clément
41 42 43 44 45
**
*/
/*!
 *\file Render_alternate.cpp
 */
46
#include <assert.h>
47
#include "common/common.hpp"
48
#include "common/Info.hpp"
49
#include "common/Message.hpp"
50 51
/* -- */

52
#include <map>
53 54
#include <GL/glew.h>
/* -- */
55 56 57
#include <QFile> // For loading the wait image
#include <QDate>
#include <QTimer>
58
#include <QImage>
59
#include <QEventLoop>
60
/* -- */
61
#include "interface/resource.hpp"
62 63 64 65 66 67
#include "interface/Interface.hpp"
/* -- */
#include "common/common.hpp"
#include "common/Info.hpp"
#include "common/Message.hpp"
/* -- */
68 69 70 71 72 73 74
#include "trace/values/Values.hpp"
#include "trace/tree/Interval.hpp"
#include "trace/tree/Node.hpp"
#include "trace/tree/BinaryTree.hpp"
#include "trace/EntityValue.hpp"
#include "trace/EntityTypes.hpp"
#include "trace/Entitys.hpp"
75 76
#include "trace/Trace.hpp"
/* -- */
77 78
#include "render/Ruler.hpp"
#include "render/Render_alternate.hpp"
79
#include "render/GanttDiagram.hpp"
80
#include "render/Shader.hpp"
81
#include "render/vbo.hpp"
82
/* -- */
83 84
#include "core/Core.hpp"
#include <iostream>
85

86
#define PI 3.14159265
Thibault Soucarre's avatar
Thibault Soucarre committed
87
#define NB_STEPS 20
88 89 90

using namespace std;

91 92 93 94 95 96 97
extern "C" {
    void checkGlError(){
        int rc = glGetError();
        assert(rc == GL_NO_ERROR );
    }
}

98 99 100 101

#define message *Message::get_instance() << "(" << __FILE__ << " l." << __LINE__ << "): "

static bool _draw_container;
Thibault Soucarre's avatar
Thibault Soucarre committed
102
static bool _draw_states;
103 104 105
static bool _draw_ruler;
static bool _draw_arrow;
static bool _draw_event;
Thibault Soucarre's avatar
Thibault Soucarre committed
106
static bool _draw_counter;
107
//const int Render_alternate::DRAWING_TIMER_DEFAULT = 10;
108 109 110 111 112 113 114 115 116 117 118 119

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

Render_alternate::Render_alternate(Core* core, QWidget *parent, const QGLFormat& format)
120 121
    : Hook_event(this, core, parent, format),
      _glsl(0),
122 123
      _containers(NULL),
      _counters(NULL),
124
      _arrows3(NULL),
125 126
      _ruler(NULL),
      _wait(NULL),
127 128
      _selection(NULL),
      _time_line(NULL),
129 130 131
      _modelview(glm::mat4(1.0)), _projection(glm::mat4(1.0)),
      _container_height(_DRAWING_CONTAINER_HEIGHT_DEFAULT)
{
132 133 134
    _texts.clear();
    _variable_texts.clear();
    _links.clear();
Mathieu Faverge's avatar
Mathieu Faverge committed
135
    vertical_line = 0;
136 137 138
    /*int error = FT_Init_FreeType(&library);
    if(error != 0)
     std::cout << "an error occured during freetype initialization" << std::endl;*/
139 140 141 142
}


Render_alternate::~Render_alternate(){
Mathieu Faverge's avatar
Mathieu Faverge committed
143
    delete _wait;
144
    delete _shader;
145 146 147 148 149 150 151 152 153 154 155 156 157
}

/***********************************
 *
 *
 *
 * Default QGLWidget functions.
 *
 *
 *
 **********************************/

void  Render_alternate::initializeGL() {
158 159
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
160
    if(err!=GLEW_OK){
161
        std::cout << "ERROR : GlewInit failed" << std::endl;
162
    }
163 164 165 166 167 168
#ifdef USE_QT5
    // Reset the error code set to GL_INVALID_ENUM by glewInit()
    glGetError();
#endif

    checkGlError();
169
    glClearColor(0.5f, 0.5f, 0.55f, 0.0f);
170
    glEnable(GL_DEPTH_TEST);
171
    checkGlError();
172
    glClearStencil(0);
173
    checkGlError();
174 175

    // Check for OpenGL version support
176
    std::cout << "init" << std::endl;
177
    const GLubyte * version = glGetString(GL_SHADING_LANGUAGE_VERSION);
178
    checkGlError();
179 180 181 182 183 184
    if (version==NULL)
        std::cout << "ERROR : could not detect your GLSL version" << std::endl;
    else
        std::cout << "Version GLSL : " << version << std::endl;
    _glsl = (version[0]-'0')*100 + (version[2]-'0')*10 + version[3]-'0';
    std::cout << _glsl << std::endl;
185

186 187
    _shader = new Shader(_glsl);
    _shader->charger();
Thibault Soucarre's avatar
Thibault Soucarre committed
188
    draw_wait();
189
    setAutoFillBackground(false);
190 191 192 193
    if(_core == NULL)
        std::cout << "_core = NULL" << std::endl;
    _core->waitGUIInit->quit();
    std::cout << "init ok" << std::endl;
194 195
    _modelview = glm::scale(_modelview, glm::vec3(1,-1,1));
    _modelview = glm::translate(_modelview, glm::vec3(0,-Info::Render::height, 0));
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    /* code from test on freetype library
     see http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_02*/
    /*face = FT_New_Face(library,
                       "/usr/share/fonts/truetype/freefont/FreeSerif.ttf",
                       0,
                       &face);

    if(error)
        std::cout << "an error occured when loading font" << std::endl;

    error = FT_Set_Char_Size(face,
                             0,
                             16*64,
                             1920,
                             1080);

    if(error)
        std::cout << "an error occured when changing character size" << std::endl;

 FT_Glyphslot g = face->glyph;
 int w=0, h=0;
 for(int i = 32; i < 128; i++) {
     if(FT_Load_Char(face, i, FT_LOAD_RENDER)) {
         fprintf(stderr, "Loading character %c failed!\n", i);
         continue;
  }

     w += g->bitmap.width;
     h = std::max(h, g->bitmap.rows);
     char_info[*p].ax = g->advance.x >> 6;
     char_info[*p].ay = g->advance.y >> 6;
     char_info[*p].bw = g->bitmap.width;
     char_info[*p].bh = g->bitmap.rows;
     char_info[*p].bl = g->bitmap_left;
     char_info[*p].bt = g->bitmap_top;
     char_info[*p].tx = (float)x / w;

     /* you might as well save this value as it is needed later on */

     /*}
 atlas_width = w;
 GLuint tex;
 glActiveTexture(GL_TEXTURE0);
 glGenTextures(1, &tex);
 glBindTexture(GL_TEXTURE_2D, tex);
 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);
int x = 0;
for(int i = 32; i < 128; i++) {
  if(FT_Load_Char(face, i, FT_LOAD_RENDER))
    continue;
  glTexSubImage2D(GL_TEXTURE_2D, 0, x, 0, g->bitmap.width, g->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
  x += g->bitmap.width;
      }*/

}

void Render_alternate::render_text(QString text, float x, float y, float w, float h){
    /* Code trying to create text on QImage and create a texture from this. Then render the text thanks to this texture. Not working yet, the texture seems to be empty*/
    std::cout << "render_text" << std::endl;
    GLuint texture;
    checkGlError();
    glGenTextures(2, &texture);
    checkGlError();
    glBindTexture(GL_TEXTURE_2D, texture);
    checkGlError();
    QImage   *image = new QImage(512, 512, QImage::Format_RGB32);
    image->fill( Qt::transparent );
    QPainter *p = new QPainter(image);
    //p.beginNativePainting();
    p->drawText(0, 0, 512, 512, Qt::AlignHCenter|Qt::AlignVCenter, text);
    delete p;
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512, 512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);
    checkGlError();
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_ALPHA, GL_UNSIGNED_BYTE, image->bits());
    checkGlError();
    glBindTexture(GL_TEXTURE_2D, 0);
    //p.endNativePainting();
    Vbo v(NULL);
    v.add(x  , y,   0, 1);
    v.add(x+w, y,   1, 1);
    v.add(x+w, y+h, 1, 0);
    v.add(x  , y,   0, 1);
    v.add(x  , y+h, 0, 0);
    v.add(x+w, y+h, 1, 0);
    v.config(_glsl);
Mathieu Faverge's avatar
Mathieu Faverge committed
282
    glUseProgram(_wait->get_shader()->getProgramID());
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    v.lock();
    glBindTexture(GL_TEXTURE_2D, texture);
    glDrawArrays(GL_TRIANGLES, 0, v.getNbVertex());
    glDisable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, 0);
    v.unlock();
    glUseProgram(0);
    delete image;
    /*trying freetype*/
    /*    int n=0;
    for(char*p = text; *p; p++){
        float x2 =  x + c[*p].bl * sx;
        float y2 = -y - c[*p].bt * sy;
        float w = c[*p].bw * sx;
        float h = c[*p].bh * sy;
        /* Advance the cursor to the start of the next character */
        /*  x += c[*p].ax * sx;
        y += c[*p].ay * sy;

        /* Skip glyphs that have no pixels */
        /*if(!w || !h)
            continue;
        _text.add(x2, y2, c[*p].tx, 0);
        _text.add(x2+w, -y2, c[*p].tx + c[*p].bw / atlas_width, 0);
        _text.add(x2, -y2 - h, c[*p].tx, c[*p].bh / atlas_height); //remember: each glyph occupies a different amount of vertical space
        _text.add(x2 + w, -y2    , c[*p].tx + c[*p].bw / atlas_width,   0);
        _text.add(x2,     -y2 - h, c[*p].tx,                                          c[*p].bh / atlas_height);
        _text.add(x2 + w, -y2 - h, c[*p].tx + c[*p].bw / atlas_width, c[*p].bh / atlas_height);
         }*/
314 315 316 317
}

void  Render_alternate::resizeGL(int width, int height) {
    glViewport(0, 0, width, height);
318
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
319

320 321 322
    /* update informations about widget size */
    Info::Screen::width  = width;
    Info::Screen::height = height;
323 324 325
    if(_state == DRAWING_STATE_WAITING)
        _projection = glm::ortho(-50, 50, -50, 50);//, 0, 1);
    else if(_state == DRAWING_STATE_DRAWING)
326
        _projection = glm::ortho(0.f, Info::Render::width, 0.f, Info::Render::height, 0.f, 100.f);
327 328 329 330
    else{
     message << tr("Undefined value for the drawing state attribute - Render area").toStdString() << Message::ende;
     }

331 332
}

333

334
void  Render_alternate::paintGL(){
335
    glGetError();
336
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
337
    glClearDepth(1.0);
338
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
339
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
340 341
    checkGlError();

342
    /*Draw the home screen*/
Thibault Soucarre's avatar
Thibault Soucarre committed
343
    if(DRAWING_STATE_WAITING == _state){
Mathieu Faverge's avatar
Mathieu Faverge committed
344
        glUseProgram(_wait->get_shader()->getProgramID());
345 346
        checkGlError();

Thibault Soucarre's avatar
Thibault Soucarre committed
347
        glEnable(GL_BLEND);
348 349
        checkGlError();

Thibault Soucarre's avatar
Thibault Soucarre committed
350
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
351 352
        checkGlError();

Mathieu Faverge's avatar
Mathieu Faverge committed
353
        _wait->lock();
354

Thibault Soucarre's avatar
Thibault Soucarre committed
355
        glBindTexture(GL_TEXTURE_2D, _textureID);
356 357
        checkGlError();

Thibault Soucarre's avatar
Thibault Soucarre committed
358
        std::cout << "draw wait\n";
359 360
        // GL_QUADS is deprecated for Opengl greater than 3., so an invalide enum is returned by glGetError and needs to be reset
        // TODO: fix that by replacing by GL_TRIANGLES
Mathieu Faverge's avatar
Mathieu Faverge committed
361
        glDrawArrays(GL_TRIANGLES, 0, _wait->getNbVertex()); /*glGetError();*/
362
        checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
363
        glBindTexture(GL_TEXTURE_2D, 0);
364
        checkGlError();
Mathieu Faverge's avatar
Mathieu Faverge committed
365
        _wait->unlock();
Thibault Soucarre's avatar
Thibault Soucarre committed
366
        glUseProgram(0);
367
        checkGlError();
368
        return;
Thibault Soucarre's avatar
Thibault Soucarre committed
369
    }
370
    /*Else, draw the trace*/
Thibault Soucarre's avatar
Thibault Soucarre committed
371

372
    resizeGL(Render_alternate::QGLWidget::width(), Render_alternate::QGLWidget::height());
373
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
374 375
    checkGlError();

376
    //select shader program
377
    glUseProgram(_shader->getProgramID());
378
    checkGlError();
379
    glm::mat4 tmp  = _modelview;
380 381
    glm::mat4 tmp2 = _modelview;
    glm::mat4 mvp;
382 383 384
    start_ruler();
    call_ruler();
    end_ruler();
385
    _ruler.config(_glsl);
386
    _ruler.lock();
387
    _modelview = glm::translate(_modelview, glm::vec3(0.0, 0.0, _z_ruler));
388
    mvp = _projection * _modelview;
389
    glUniformMatrix4fv(glGetUniformLocation(_shader->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
390
    checkGlError();
391
    /*The first 8 elements of the vbo are the coordinates of the quads, others elements are coordinates of the graduation*/
392
    glDrawArrays(GL_TRIANGLES, 0, 12);  /*glGetError();*/
393
    checkGlError();
394 395
    _modelview = tmp;
    _modelview = glm::translate(_modelview, glm::vec3(0.0, 0.0, _z_ruler_over));
396
    mvp = _projection * _modelview;
397
    glUniformMatrix4fv(glGetUniformLocation(_shader->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
398
    checkGlError();
399
    glDrawArrays(GL_LINES, 12, _ruler.getNbVertex()-12);
400
    checkGlError();
401
    _ruler.unlock();
402 403
    _modelview = tmp;

404
    /* drawing containers*/
405 406
    Shader*s = _containers.get_shader();
    glUseProgram(s->getProgramID());
407
    _containers.lock();
408
    _modelview = glm::translate(_modelview, glm::vec3(0.0, _ruler_y + _ruler_height - _y_state_translate, _z_container));
Thibault Soucarre's avatar
Thibault Soucarre committed
409
    _modelview = glm::scale(_modelview, glm::vec3(_x_scale_container_state/0.20, _y_state_scale, 1.0));
410
    mvp = _projection * _modelview;
411
    glUniformMatrix4fv(glGetUniformLocation(_shader->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
412 413
    glDrawArrays(GL_TRIANGLES, 0, _containers.getNbVertex());  /*glGetError();*/
    checkGlError();
414
    _containers.unlock();
415
    _modelview = tmp;
416
    glUseProgram(0);
417
    /*drawing states*/
418
    _modelview = glm::translate(_modelview, glm::vec3(_default_entity_x_translate - _x_state_translate, _ruler_y + _ruler_height - _y_state_translate, _z_state));
419
    _modelview = glm::scale(_modelview, glm::vec3(_x_state_scale, _y_state_scale, 1));
420
    mvp = _projection * _modelview;
421
    std::map<EntityValue*, Vbo*>::iterator it_state;
422 423
    it_state = _states.begin();
    while(it_state!=_states.end()){
424
        Shader *s = it_state->second->get_shader();
425
        glUseProgram(s->getProgramID());
426
        it_state->second->lock();
427
        glUniformMatrix4fv(glGetUniformLocation(s->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
428
        if(it_state->first==NULL || it_state->first->get_visible()) {
429 430
            glDrawArrays(GL_TRIANGLES, 0, it_state->second->getNbVertex());  /*glGetError();*/
            checkGlError();
431
        }
432
        it_state->second->unlock();
433 434
        it_state++;
    }
435
    checkGlError();
436

Thibault Soucarre's avatar
Thibault Soucarre committed
437
    /*drawing counters*/
438 439 440
    glUseProgram(_counters.get_shader()->getProgramID());
    glUniformMatrix4fv(glGetUniformLocation(_counters.get_shader()->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));

Thibault Soucarre's avatar
Thibault Soucarre committed
441 442 443
    _counters.lock();
    glDrawArrays(GL_LINES, 0, _counters.getNbVertex());
    _counters.unlock();
444
    _modelview = tmp;
445
    checkGlError();
446

447
    /*drawing links*/
Thibault Soucarre's avatar
Thibault Soucarre committed
448
    if(false == Info::Render::_no_arrows){
449
        //matrix change that are necessary for each link
450 451
        _modelview = glm::translate(_modelview, glm::vec3(_default_entity_x_translate - _x_state_translate, _ruler_y + _ruler_height - _y_state_translate , _z_arrow));
        _modelview = glm::scale(_modelview, glm::vec3(_x_state_scale, _y_state_scale, 1.0));
452
        mvp = _projection * _modelview;
453
        //draw the lines
454 455
        std::map<EntityValue*, std::pair<Vbo *, Vbo*> >::iterator it_arrow = _arrows.begin();
        while(it_arrow != _arrows.end()){
456
            Shader *s = it_arrow->second.first->get_shader();
457
            glUseProgram(s->getProgramID());
458
            it_arrow->second.first->lock();
459
            glUniformMatrix4fv(glGetUniformLocation(s->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
460 461 462 463 464 465
            if(it_arrow->first == NULL || it_arrow->first->get_visible())
                glDrawArrays(GL_LINES, 0, it_arrow->second.first->getNbVertex());
            it_arrow->second.first->unlock();
            if(Info::Render::_arrows_shape == 1){ //head = points
                std::cout << "head of the arrow "  << it_arrow->second.second->getNbVertex() << " " << it_arrow->second.first->getNbVertex() << std::endl;
                it_arrow->second.second->lock();
466 467 468
                glEnable(GL_PROGRAM_POINT_SIZE);
                glEnable(GL_POINT_SMOOTH);
                glPointSize(5);
469 470 471
                if(it_arrow->first == NULL || it_arrow->first->get_visible())
                    glDrawArrays(GL_POINTS, 0, it_arrow->second.second->getNbVertex());
                it_arrow->second.second->unlock();
472
            }
473
            it_arrow++;
474
        }
475 476 477 478 479 480
        if(Info::Render::_arrows_shape == 0){ //triangles
                std::map<EntityValue*, std::vector<Element_pos> >::iterator it_link = _links.begin();
                _modelview = glm::scale(_modelview, glm::vec3(1/_x_state_scale, 1/_y_state_scale, 1));
                tmp2 = _modelview;
                _arrows3.lock();
                while(it_link != _links.end()){
481
                    Shader *s = _arrows[it_link->first].first->get_shader();
482 483 484 485 486 487 488 489 490
                    glUseProgram(s->getProgramID());
                    std::vector<Element_pos> v = it_link->second;
                    int n = v.size()/3;
                    if(it_link->first != NULL && !it_link->first->get_visible()){
                        it_link++;
                        continue;
                    }
                    for(int i=0 ; i<n ; i++){
                        _modelview = glm::translate(_modelview, glm::vec3(_x_state_scale * v[3*i], _y_state_scale * v[3*i+1], 0));
491 492 493
                        _modelview = glm::rotate(_modelview, v[3*i+2], glm::vec3(0, 0, 1));
                        mvp = _projection * _modelview;
                        glUniformMatrix4fv(glGetUniformLocation(s->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
494

495 496
                        glDrawArrays(GL_TRIANGLES, 0, _arrows3.getNbVertex());
                        _modelview = tmp2;
497 498 499 500
                    }
                    it_link++;
                }
                _arrows3.unlock();
501
        }
502
        _modelview = tmp;
503
    }
504
    checkGlError();
505

Thibault Soucarre's avatar
Thibault Soucarre committed
506 507
    /*drawing events*/
    if(false == Info::Render::_no_events){
508 509
        _modelview = glm::translate(_modelview, glm::vec3(_default_entity_x_translate - _x_state_translate, _ruler_y + _ruler_height - _y_state_translate , _z_event));
        _modelview = glm::scale(_modelview, glm::vec3(_x_state_scale, _y_state_scale, 1.0));
510 511 512
        mvp = _projection * _modelview;
        std::map<EntityValue*, std::pair<Vbo*, Vbo*> >::iterator it_event = _events.begin();
        while(it_event != _events.end()){
513
            Shader *s = it_event->second.first->get_shader();
514 515
            glUseProgram(s->getProgramID());
            it_event->second.first->lock();
516
            glUniformMatrix4fv(glGetUniformLocation(s->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
517
            if(it_event->first==NULL || it_event->first->get_visible()){
518 519
                //glDisable(GL_PROGRAM_POINT_SIZE);
                //draw points as circles instead of squares
520 521 522
                glEnable(GL_POINT_SMOOTH);
                glPointSize(5);
                glDrawArrays(GL_POINTS, 0, it_event->second.first->getNbVertex());
523
                it_event->second.first->unlock();
524 525 526 527 528 529 530 531 532
            }
            it_event->second.second->lock();
            glLineWidth(2);
            if(it_event->first==NULL || it_event->first->get_visible())
                glDrawArrays(GL_LINES, 0, it_event->second.second->getNbVertex());
            it_event->second.second->unlock();
            it_event++;
        }
        glLineWidth(1);
533

534
    }
535

536
    _modelview = tmp;
537
    glUseProgram(_shader->getProgramID());
538 539


540 541
    /*draw selection if necessary*/
    if (_mouse_pressed && (Info::Render::_key_ctrl == false) && !_mouse_pressed_inside_container){
542 543 544
        //allow transparency
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
545 546
        Element_pos x0, x1, y0, y1;
        x0 = screen_to_render_x(_mouse_x);
547
        y0 = screen_to_render_y(_mouse_y);
548
        x1 = screen_to_render_x(_new_mouse_x);
549
        y1 = screen_to_render_y(_new_mouse_y);
550 551 552 553 554 555 556 557 558 559
        _selection.add(x0, y0, 1.0, 1.0, 1.0);
        _selection.add(x0, y1, 1.0, 1.0, 1.0);
        _selection.add(x1, y1, 1.0, 1.0, 1.0);
        _selection.add(x1, y1, 1.0, 1.0, 1.0);
        _selection.add(x1, y0, 1.0, 1.0, 1.0);
        _selection.add(x0, y0, 1.0, 1.0, 1.0);
        _selection.config(_glsl);
        _selection.lock();
        glDrawArrays(GL_TRIANGLES, 0, _selection.getNbVertex());
        _selection.unlock();
560
        glDisable(GL_BLEND);
561
        _modelview = tmp;
562
    }
Thibault Soucarre's avatar
Thibault Soucarre committed
563
    draw_vertical_line();
564
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
565

566 567
    //deselect shader
    glUseProgram(0);
568
    checkGlError();
569 570
    QFont arial_font = QFont("Arial", 10);
    const QFontMetrics metric(arial_font);
Mathieu Faverge's avatar
Mathieu Faverge committed
571
    QString t = QString().setNum(vertical_line);
572 573
    // draw time corresponding to the vertical line position
    qglColor(Qt::red);
574
    checkGlError();
575 576 577
    renderText ( render_to_screen_x(trace_to_render_x(vertical_line))-metric.size(Qt::TextSingleLine,t).width() -5,
              metric.height()-1,
               t,
578 579 580 581 582 583 584 585 586 587 588 589
     arial_font);
    /*render_text(t,
                render_to_screen_x(trace_to_render_x(vertical_line))-metric.size(Qt::TextSingleLine,t).width() -5,
                metric.height()-1,
                100,
     100);*/
    glGetError();
    /*render_text("Test",
                vertical_line,
                0,
                1,
     1);*/
590

591
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
592 593 594 595 596
    //draw texts for variables
    std::map<long int, Variable_text_>::const_iterator it=_variable_texts.begin();
    const std::map<long int, Variable_text_>::const_iterator it_end=_variable_texts.end();

    for(;it!=it_end;it++){
597
        renderText ( render_to_screen_x(trace_to_render_x(vertical_line))+3,
598 599 600 601
                     render_to_screen_y( trace_to_render_y((*it).second.y) + 0.5),
                     QString().setNum((*it).second.value),
                     arial_font);
     }
602
    checkGlError();
Thibault Soucarre's avatar
Thibault Soucarre committed
603

604 605

    qglColor(Qt::white);
606 607 608 609 610 611 612 613 614
    //we calculate the height of the interline we want : max height of the font + 1 pixel to avoid overlapping (metric.height() returns a bigger value, a bit too much)
    int height = metric.tightBoundingRect("fg").height()+1;

    /* Draw container text */
    const unsigned int texts_size = _texts.size();
    std::map<Element_pos,Element_pos> previous_by_column;


    //int skipped,displayed=0;
615
    for (unsigned int i=0 ; i<texts_size ; i++){
616 617 618 619 620 621 622 623 624 625



            if (trace_to_render_y(_texts[i].y) + 0.5 < 9) continue;/* Do not display text if it is on the ruler area */

            //check if ye are not too close to another container to properly display the text
            std::map<Element_pos,Element_pos>::const_iterator it = previous_by_column.find(_texts[i].x);
            const std::map<Element_pos,Element_pos>::const_iterator it_end = previous_by_column.end();

            if (it==it_end || render_to_screen_y(trace_to_render_y(_texts[i].y)) - render_to_screen_y(trace_to_render_y((*it).second))> height ){
626
                const QString text_elided = metric.elidedText(_texts[i].value.c_str(), Qt::ElideRight, 0.8 * _x_scale_container_state*Info::Screen::width/(Info::Trace::depth+1.));
627
                renderText ( render_to_screen_x(_texts[i].x * _x_scale_container_state/0.20),
628 629 630
                             render_to_screen_y( trace_to_render_y(_texts[i].y) + 0.5),
                             text_elided,
                             arial_font);
631

632
                //push only displayed values in the map
633 634 635 636
                previous_by_column[_texts[i].x]=_texts[i].y;
                //displayed++;
            }//else{skipped++;}
        }
Thibault Soucarre's avatar
Thibault Soucarre committed
637
        /*Draw ruler text*/
638 639 640 641 642 643 644 645 646 647 648 649 650 651
        std::ostringstream buf_txt;
        Element_pos graduation_diff;
        Element_pos coeff_prefix;
        graduation_diff = Ruler::get_graduation_diff(Info::Render::_x_min_visible, Info::Render::_x_max_visible);
        coeff_prefix    = Ruler::get_coeff_for_common_prefix(Info::Render::_x_min_visible, Info::Render::_x_max_visible);

        arial_font.setPointSize(14);

        buf_txt.str("");/* flush the buffer */
        buf_txt << "min: " << (double)Info::Render::_x_min_visible;

        renderText (render_to_screen_x(trace_to_render_x(Info::Render::_x_min_visible)),
                    render_to_screen_y(3),
                    buf_txt.str().c_str(),
652
         arial_font);
653 654 655 656 657 658 659

        buf_txt.str("");/* flush the buffer */
        buf_txt << "max: " << (double)Info::Render::_x_max_visible;

        renderText (render_to_screen_x(trace_to_render_x(Info::Render::_x_max_visible))-130,
                    render_to_screen_y(3),
                    buf_txt.str().c_str(),
660
         arial_font);
661 662 663 664 665 666 667 668

        buf_txt.str("");
        buf_txt << Ruler::get_common_part_string(Info::Render::_x_min_visible, coeff_prefix) << "--";

        renderText (render_to_screen_x(trace_to_render_x((Info::Render::_x_min_visible +
                                                          Info::Render::_x_max_visible) / 2)),
                    render_to_screen_y(3),
                    buf_txt.str().c_str(),
669 670 671
         arial_font);
        QString q(buf_txt.str().c_str());
        //glGetError();
672 673 674 675 676 677 678 679 680 681 682

        for (Element_pos i = Info::Render::_x_min_visible ;
             i < Info::Render::_x_max_visible ;
             i+=graduation_diff){

            buf_txt.str("");/* flush the buffer */
            buf_txt << Ruler::get_variable_part(i, coeff_prefix, 2);

            renderText (render_to_screen_x(trace_to_render_x(i)+1),
                        render_to_screen_y(8),
                        buf_txt.str().c_str(),
683
             arial_font);
684

685
    }
686

687
    checkGlError();
688 689 690 691 692 693 694 695 696 697 698 699 700 701
}
/***********************************
 *
 *
 *
 * Building functions.
 *
 *
 *
 **********************************/



bool Render_alternate::build(){
Thibault Soucarre's avatar
Thibault Soucarre committed
702

703 704 705 706 707 708 709 710 711 712
    _state = DRAWING_STATE_DRAWING;/* change the drawing state */

    /* disable some OpenGL features to enhance the rendering */
    glDisable(GL_BLEND);
    replace_scale(1);/* for states scaling */
    _x_state_translate = 0;/* for states translation */
    _y_state_scale = 1;/* for states scaling */
    _y_state_translate = 0;/* for states translation */
    _x_scroll_pos = 0;/* horizontal bar placed on 0 */
    _y_scroll_pos = 0;/* vertical bar placed on 0 */
713
    checkGlError();
714 715 716 717 718 719
    if (NULL == _render_instance)
        return true;

    return true;
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
/* trying to render text with qpainter outside paintGL
 Not working yet*/
/*void Render_alternate::paintEvent(QPaintEvent *Event){
    paintGL();
    QPainter p(this);
    QFont arial_font = QFont("Arial", 10);
    const QFontMetrics metric(arial_font);
    QString t =QString().setNum(vertical_line);
    p.drawText(render_to_screen_x(trace_to_render_x(vertical_line))-metric.size(Qt::TextSingleLine,t).width() -5,
               metric.height()-1,
               t);
    glGetError();
    checkGlError();
 }*/

735 736 737


bool Render_alternate::unbuild(){
Thibault Soucarre's avatar
Thibault Soucarre committed
738

739

740 741 742 743 744 745
    /**********************
     *
     * Init OpenGL features
     *
     **********************/

746 747 748 749 750 751
    /*enable some OpenGL features*/
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glEnable(GL_BLEND);/* enable blending for the alpha color */
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor3d(1.0, 1.0, 1.0);/* init color to white */
752 753


754 755 756 757 758 759 760 761 762
    /*****************************
     *
     * Init render area attributes
     *
     *****************************/

    _state = DRAWING_STATE_WAITING;/* change the drawing state */


763
    /* clear lists and maps*/
764 765 766
    _text_pos.clear();
    _text_value.clear();
    clear_text();
767 768 769 770
    _links.clear();
    _states.clear();
    _arrows.clear();
    _events.clear();
771

772
    return true;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
}





/***********************************
 *
 *
 *
 * Drawing function for the wait screen.
 *
 *
 *
 **********************************/



GLuint Render_alternate::draw_wait() {
Mathieu Faverge's avatar
Mathieu Faverge committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    if (_wait == NULL) {
        Shader *s = new Shader(_glsl, 0);

        _wait = new Vbo();
        s->charger();
        _wait->set_shader(s);

        glGenTextures(1, &_textureID);
        glBindTexture(GL_TEXTURE_2D, _textureID);

        QFile texture_file(QString(":/img/img/logo") +  QDate::currentDate().toString("MMdd") + QString(".png"));
        if (true == texture_file.exists())/* The texture exists */
            _textureID = bindTexture(QPixmap(texture_file.fileName()), GL_TEXTURE_2D);
        else/* use the default picture */
            _textureID = bindTexture(QPixmap(":/img/img/logo.png"), GL_TEXTURE_2D);

        /* Coordinates of the two triangles storing the texture */
        _wait->add(-0.25, -0.25, 0, 0);
        _wait->add( 0.25, -0.25, 1, 0);
        _wait->add( 0.25,  0.25, 1, 1);
        _wait->add( 0.25,  0.25, 1, 1);
        _wait->add(-0.25,  0.25, 0, 1);
        _wait->add(-0.25, -0.25, 0, 0);
        _wait->config(_glsl);
    }
817 818 819
}


820
/* Function called by paintGL. Config ruler vbo*/
821
void Render_alternate::call_ruler(){
822
    _ruler.setNbVertex(0);
823
    Element_pos graduation_diff;
824
    //Element_pos coeff_prefix;
825 826 827 828
    const Element_pos offset_x = _default_entity_x_translate;
    const Element_pos offset_y = _ruler_y + _ruler_height+3.5;

    update_visible_interval_value();
Thibault Soucarre's avatar
Thibault Soucarre committed
829

830
    graduation_diff = Ruler::get_graduation_diff(Info::Render::_x_min_visible, Info::Render::_x_max_visible);
831
    //coeff_prefix    = Ruler::get_coeff_for_common_prefix(Info::Render::_x_min_visible, Info::Render::_x_max_visible);
832 833
    //draw quads
    set_color(0.0, 0.0, 1.0);
834
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 3, _r, _g, _b);
835 836
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 8, _r, _g, _b);
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 8, _r, _g, _b);
837
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 8, _r, _g, _b);
838
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 3, _r, _g, _b);
839
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 3, _r, _g, _b);
840 841 842 843
    set_color(0.0, 0.0, 0.0);
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 0, _r, _g, _b);
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 3, _r, _g, _b);
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 3, _r, _g, _b);
844
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 3, _r, _g, _b);
845
    _ruler.add(trace_to_render_x(Info::Render::_x_max_visible), 0, _r, _g, _b);
846
    _ruler.add(trace_to_render_x(Info::Render::_x_min_visible), 0, _r, _g, _b);
847 848

    //draw graduations
849
    set_color(1.0, 1.0, 1.0);
Thibault Soucarre's avatar
Thibault Soucarre committed
850

851 852 853
    for (Element_pos i = Info::Render::_x_min_visible ; i < Info::Render::_x_max_visible ; i+=graduation_diff){
        const Element_pos grad_div_by_5 = graduation_diff/5;
        for (Element_pos j = (i+grad_div_by_5) ; j<(i+graduation_diff) ; j+= grad_div_by_5){
854 855
            draw_line( trace_to_render_x(j) + offset_x, 3,
                       trace_to_render_x(j) + offset_x, 5.5, _z_ruler);
856 857
        }

858 859
        draw_line( trace_to_render_x(i) + offset_x, 3,
                   trace_to_render_x(i) + offset_x, 8, _z_ruler);
860
    }
Thibault Soucarre's avatar
Thibault Soucarre committed
861

862
     return;
Thibault Soucarre's avatar
Thibault Soucarre committed
863
}
864 865 866 867



void Render_alternate::set_color(float r, float g, float b){
868 869 870
    _r = r;
    _g = g;
    _b = b;
871 872
}

Thibault Soucarre's avatar
Thibault Soucarre committed
873
void Render_alternate::draw_text(const Element_pos x, const Element_pos y, const Element_pos size, const std::string s){
874
    if (_draw_ruler) return;/* do not draw text for ruler */
Thibault Soucarre's avatar
Thibault Soucarre committed
875 876
    Container_text_ buf;
    buf.x     = x;
877
    buf.y     = render_to_trace_y(y);/* Cancel previous transformation. */
Thibault Soucarre's avatar
Thibault Soucarre committed
878 879 880
    buf.value = s;
    buf.size = size;
    _texts.push_back(buf);
881 882 883
}

void Render_alternate::draw_quad(Element_pos x, Element_pos y, Element_pos z, Element_pos w, Element_pos h){
884
    checkGlError();
885 886 887 888
    Element_pos offset_x;
    const Element_pos offset_y = -_ruler_y - _ruler_height;

    offset_x = 0;
889 890 891 892 893
    if(_draw_container){
        std::cout << "container : " << x << " " << y << " " << w << " " << h <<std::endl;
        _containers.add(x+offset_x  , y+offset_y  , _r,_g,_b);
        _containers.add(x+offset_x+w, y+offset_y  , _r,_g,_b);
        _containers.add(x+offset_x+w, y+offset_y+h, _r,_g,_b);
894
        _containers.add(x+offset_x+w, y+offset_y+h, _r,_g,_b);
895
        _containers.add(x+offset_x  , y+offset_y+h, _r,_g,_b);
896
        _containers.add(x+offset_x  , y+offset_y  , _r,_g,_b);
897 898
    }
    else if(_draw_states){
899
        offset_x = -_default_entity_x_translate;
900
        if(_glsl<330) { //float for color gradient
901 902 903
            _current->add(x+offset_x  , y+offset_y, 2.0f);
            _current->add(x+offset_x+w, y+offset_y, 1.0f);
            _current->add(x+offset_x+w, y+offset_y+h, 1.0f);
904
            _current->add(x+offset_x+w, y+offset_y+h, 1.0f);
905
            _current->add(x+offset_x  , y+offset_y+h, 2.0f);
906
            _current->add(x+offset_x  , y+offset_y, 2.0f);
907
        }
908
        else{ //char for color gradient
909 910
            char c1 = 1;
            char c2 = 2;
911 912 913
            _current->add(x+offset_x  , y+offset_y, c2);
            _current->add(x+offset_x+w, y+offset_y, c1);
            _current->add(x+offset_x+w, y+offset_y+h, c1);
914
            _current->add(x+offset_x+w, y+offset_y+h, c1);
915
            _current->add(x+offset_x  , y+offset_y+h, c2);
916
            _current->add(x+offset_x  , y+offset_y, c2);
917
        }
918 919 920
    }
    else if(_draw_ruler){
    }
921
    checkGlError();
922 923 924 925 926 927 928 929 930 931 932 933

}

void Render_alternate::draw_triangle(Element_pos , Element_pos ,
                                  Element_pos , Element_pos ){
}

void Render_alternate::draw_line(Element_pos x1, Element_pos y1, Element_pos x2, Element_pos y2, Element_pos z){

    const Element_pos offset_x = -_default_entity_x_translate;
    const Element_pos offset_y = -_ruler_y - _ruler_height;

934
    if(_draw_counter){
935 936
        _counters.add(x1+offset_x,y1+offset_y);//,_r,_g,_b);
        _counters.add(x2+offset_x,y2+offset_y);//,_r,_g,_b);
937
    }
938
    else if(_draw_ruler){
939 940
        _ruler.add(x1+offset_x, y1, _r, _g, _b);
        _ruler.add(x2+offset_x, y2, _r, _g, _b);
941
    }
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
}

void Render_alternate::draw_circle(Element_pos /*x*/, Element_pos /*y*/, Element_pos /*z*/, Element_pos /*r*/){
}


void Render_alternate::start_draw(){
    _draw_ruler = false;
    _draw_container = false;
    _draw_arrow = false;
    _draw_event = false;
}

void Render_alternate::start_draw_containers(){
    _draw_container = true;
957 958 959
    Shader *s = new Shader(_glsl);
    s->charger();
    _containers.set_shader(s);
960
    _containers.setNbVertex(0);
961 962 963 964 965 966 967 968 969 970
}

void Render_alternate::draw_container(const Element_pos , const Element_pos , const Element_pos , const Element_pos ){
}

void Render_alternate::draw_container_text(const Element_pos , const Element_pos , const std::string ){
}

void Render_alternate::end_draw_containers(){
    _draw_container = false;
971
    checkGlError();
972
    _containers.config(_glsl);
973
    cout << "end_draw_containers" << endl;
974
    checkGlError();
975 976 977
}

void Render_alternate::start_draw_states(){
978
    _draw_states = true;
979 980
}

981 982 983 984 985 986 987 988
void Render_alternate::draw_state( const Element_pos x,
                                   const Element_pos y,
                                   const Element_pos z,
                                   const Element_pos w,
                                   const Element_pos h,
                                   EntityValue *value )
{
    if( _states.count(value) == 0 ) { //if there is no vbo corresponding to value
989
        //we create a new vbo and a new shader corresponding to the current color*/
990
        Shader *s = new Shader(_glsl, _r, _g, _b, true);
991
        s->charger();
992
        //assert(value); // TODO: check why busy state exists and uncomment this assert
993
        Vbo *v = new Vbo(s);
994
        std::pair<EntityValue*, Vbo*> p2(value, v);
995
        _states.insert(p2);
996 997 998

        connect( value, SIGNAL(changedColor( EntityValue * )),
                 this, SLOT(update_ev( EntityValue *)) );
999
    }
1000
    //add the state to the vbo
1001
    _current = _states[value];
1002
    draw_quad(x, y, z, w, h);
1003 1004 1005 1006
}

void Render_alternate::end_draw_states(){

1007
    _draw_states = false;
1008
    std::map<EntityValue*, Vbo*>::iterator it;
1009 1010
    it = _states.begin();
    while(it!=_states.end()){
1011
        //std::cout << it->first->get_name().get_name() << std::endl;
1012
        it->second->config(_glsl);
1013 1014
        it++;
    }
1015
    cout << "end_draw_states" << endl;
1016
    checkGlError();
1017 1018 1019 1020 1021 1022 1023
}

void Render_alternate::start_draw_arrows(){
    _draw_arrow = true;

}

1024
 void Render_alternate::draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height, const Element_col red, const Element_col green, const Element_col blue, EntityValue* value){
1025 1026
     if(_arrows.count(value) == 0){ //if there is no vbo corresponding to value
         /* Create a new vbo and a new shader*/
1027 1028
         Shader *s = new Shader(_glsl, _r, _g, _b, false);
         s->charger();
1029 1030 1031 1032 1033
         Vbo* v1 = new Vbo(s);
         Vbo* v2 = new Vbo(s);
         std::pair<Vbo*, Vbo*> p1(v1, v2);
         std::pair<EntityValue*, std::pair<Vbo*, Vbo*> > p2(value, p1);
         _arrows.insert(p2);
1034
         std::vector<Element_pos> v;
1035 1036
         std::pair<EntityValue*, std::vector<Element_pos> > p3(value, v);
         _links.insert(p3);
1037 1038 1039

        connect( value, SIGNAL(changedColor( EntityValue * )),
                 this, SLOT(update_ev( EntityValue *)) );
1040
     }
1041
     //store data in vbo
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
     const Element_pos offset_x = -_default_entity_x_translate;
     const Element_pos offset_y = -_ruler_y - _ruler_height;
     Element_pos x0, x1, y0, y1, l, alpha, cosa, sina;
     x0 = start_time   + offset_x;
     x1 = end_time     + offset_x;
     y0 = start_height + offset_y;
     y1 = end_height   + offset_y;
     //size of the arrow
     l = sqrt(pow(x1-x0,2)+pow(y1-y0,2));
     //calculate angle beetween arrow and horizontal axe
     cosa = (x1-x0)/l;
     sina = (y1-y0)/l;
     alpha = acos(cosa);
     if(sina<0) alpha*= -1;
     _current = _arrows[value].first;
     _current->add(x0, y0);
     _current->add(x1, y1);
     _current = _arrows[value].second;
     _current->add(x1, y1);
1061
     /* Need this to draw triangles heads. Necessary for matrix operations*/
1062 1063 1064 1065
     std::vector<Element_pos> *v = &_links[value];
     v->push_back(x1);
     v->push_back(y1);
     v->push_back(alpha);
1066
 }
1067 1068

void Render_alternate::end_draw_arrows(){
1069
    //coordinates that we use for draw the head all arrows when parameter arrows_shape is triangle, we will change the angle and scale of using the modelview matrix
1070
    _draw_arrow = false;
1071 1072 1073
    _arrows3.add(0,0,1,1,1);
    _arrows3.add(-1.2, -0.4, 1, 1, 1);
    _arrows3.add(-1.2, 0.4, 1, 1, 1);
1074
    _arrows3.setNbVertex(3);
1075
    //send datas to vbo
1076 1077
    std::map<EntityValue*, std::pair<Vbo*, Vbo*> >::iterator it = _arrows.begin();
    while(it != _arrows.end()){
1078
        std::cout << it->first->get_name() << std::endl;
1079 1080 1081 1082 1083 1084
        it->second.first->config(_glsl);
        it->second.second->config(_glsl);
        it++;
    }
    //_arrows.config(_glsl);
    //_arrows2.config(_glsl);
1085
    _arrows3.config(_glsl);
1086
    cout << "end_draw_arrows " << _links.size() << endl;
1087
    checkGlError();
1088 1089 1090 1091 1092 1093
}

void Render_alternate::start_draw_events(){
    _draw_event = true;
}

1094 1095 1096 1097
void Render_alternate::draw_event(const Element_pos time, const Element_pos height,  const Element_pos container_height, EntityValue* value){
    if(_events.count(value) == 0){
        Shader *s = new Shader(_glsl, _r, _g, _b, false);
        s->charger();
1098 1099 1100 1101 1102
        Vbo* v1 = new Vbo(s);
        Vbo* v2 = new Vbo(s);
        std::pair<Vbo*, Vbo*> p1(v1, v2);
        std::pair<EntityValue*, std::pair<Vbo*, Vbo*> > p2(value, p1);
        _events.insert(p2);
1103 1104 1105

        connect( value, SIGNAL(changedColor( EntityValue * )),
                 this, SLOT(update_ev( EntityValue *)) );
1106
    }
1107 1108
    const Element_pos offset_x = -_default_entity_x_translate;
    const Element_pos offset_y = -_ruler_y - _ruler_height;
1109 1110 1111 1112 1113
    _current = _events[value].first;
    _current->add(time+offset_x, height + offset_y);
    _current = _events[value].second;
    _current->add(time+offset_x, height + offset_y);
    _current->add(time+offset_x, height+offset_y+container_height/2);
1114 1115 1116 1117
}


void Render_alternate::end_draw_events(){
1118 1119 1120
    _draw_event = false;
    std::map<EntityValue*, std::pair<Vbo*, Vbo*> >::iterator it = _events.begin();
    while(it != _events.end()){
1121
        std::cout << it->first->get_name() << std::endl;
1122 1123 1124 1125
        it->second.first->config(_glsl);
        it->second.second->config(_glsl);
        it++;
    }
1126
    _draw_event = false;
1127
    cout << "end_draw_events" << endl;
1128
    checkGlError();
1129 1130 1131
}

void Render_alternate::start_draw_counter(){
1132
    _draw_counter = true;
1133
    /* create shader for counters : all vertex are white */
1134 1135 1136
    Shader *s = new Shader(_glsl, 1, 1, 1, false);
    s->charger();
    _counters.set_shader(s);
1137 1138 1139 1140 1141 1142
}

void Render_alternate::draw_counter(const Element_pos , const Element_pos ){
}

void Render_alternate::end_draw_counter(){
1143
    _draw_counter = false;
1144
    _counters.config(_glsl);
1145
    cout << "end_draw_counters" << endl;
1146
    checkGlError();
1147 1148 1149 1150 1151 1152 1153
}

void Render_alternate::start_ruler(){
    _draw_ruler = true;
}

void Render_alternate::end_ruler(){
1154
    checkGlError();
1155 1156 1157 1158
    _draw_ruler = false;
}

void Render_alternate::end_draw(){
1159
    cout << "end_draw" << endl;
1160
    checkGlError();
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
}


void Render_alternate::draw_stored_texts(){
}

void Render_alternate::draw_stored_arrows(){
}

void Render_alternate::draw_stored_circles(){
}
1172 1173 1174 1175 1176 1177

void Render_alternate::release(){
    std::cout << "release" << std::endl;
}

void Render_alternate::draw_text_value(long int id,double text, double y){
Thibault Soucarre's avatar
Thibault Soucarre committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    if(y!=-1){
        Variable_text_ buf;
        buf.y=y;
        buf.value=text;
       // printf("adding %f at %f for %p\n", text, y, (void*)id);
        _variable_texts[id]=buf;
    }else{
        //it's an update
        Variable_text_ buf=_variable_texts[id];
        buf.value=text;
        //printf("updating %f at %f for %p\n", text, y, (void*)id);
        _variable_texts[id]=buf;
    }
1191 1192
}

Thibault Soucarre's avatar
Thibault Soucarre committed
1193

1194 1195 1196 1197 1198 1199 1200 1201
void Render_alternate::show_minimap(){
}

/*!
 * \brief draws the vertical helper line
 */

void Render_alternate::draw_vertical_line(){
1202
    glm::mat4 mvp;
Thibault Soucarre's avatar
Thibault Soucarre committed
1203 1204
    if(vertical_line==0)
        return;
1205 1206 1207 1208
    _time_line.add(trace_to_render_x(vertical_line), 0.0, 1.0, 0.0, 0.0);
    _time_line.add(trace_to_render_x(vertical_line), Info::Render::height, 1.0, 0.0, 0.0);
    _time_line.config(_glsl);
    _time_line.lock();
1209
    mvp = _projection * _modelview;
1210
    checkGlError();
1211
    glUniformMatrix4fv(glGetUniformLocation(_shader->getProgramID(), "MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
Thibault Soucarre's avatar
Thibault Soucarre committed
1212
    glDrawArrays(GL_LINES, 0, 2);
1213 1214
    _time_line.unlock();
     checkGlError();
1215 1216 1217 1218 1219 1220
}

/*!
 * \brief slot connected to the simple click event
 */
void Render_alternate::update_vertical_line(){
Thibault Soucarre's avatar
Thibault Soucarre committed
1221 1222 1223 1224 1225
    if (_mouse_pressed_inside_container)
            set_vertical_line(0);
    else
        set_vertical_line(render_to_trace_x( screen_to_render_x(_mouse_x)));
    updateGL();
1226 1227 1228
}

/*!
1229 1230 1231
 * \brief set the vertical line offset
 * \param l the line offset.
 */
Thibault Soucarre's avatar
Thibault Soucarre committed
1232 1233 1234
void Render_alternate::set_vertical_line(Element_pos new_coord){
    if(new_coord==vertical_line)vertical_line=0;
    else vertical_line=new_coord;
1235
}
Thibault Soucarre's avatar
Thibault Soucarre committed
1236 1237 1238 1239 1240

Element_pos Render_alternate::get_vertical_line(){
        return vertical_line;
}

1241 1242 1243 1244
void Render_alternate::clear_text (){
    _texts.clear();
    _variable_texts.clear();
}
1245

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
/* Change the shader associated to entityType entity */
void
Render_alternate::update_ev_single( EntityValue *ev,
                                    std::map<EntityValue*, Vbo*> *evmap,
                                    bool shaded )
{
    std::map<EntityValue*, Vbo*>::iterator it;

    it = evmap->find( ev );
    if ( it != evmap->end() ) {
        Vbo   *vbo = it->second;
        Color *c = ev->get_used_color();

        //delete previous shader
        vbo->delete_shader();

        //create and load the new shader*/
        Shader *s = new Shader(_glsl,
                               c->get_red(),
                               c->get_green(),
                               c->get_blue(),
                               shaded );
        s->charger();
        vbo->set_shader(s);
    }
}

void
Render_alternate::update_ev_couple( EntityValue *ev,
                                    std::map<EntityValue*, std::