openglcanvas.cpp 26.2 KB
Newer Older
Luis Peñaranda's avatar
Luis Peñaranda committed
1 2
#include <QtGui>
#ifdef NEEDGLEE
3
  #include "GLee.h"
Luis Peñaranda's avatar
Luis Peñaranda committed
4 5 6
#endif
#include "openglcanvas.h"
#include <cmath>
7 8
#include <sys/types.h>  // for fstat, getpwuid, getuid
#include <sys/stat.h>   // for fstat
Luis Penaranda's avatar
Luis Penaranda committed
9 10 11 12
#ifdef _WIN32
  #include <stdlib.h>   // for getenv (TODO: use getenv_s)
#else
  #include <pwd.h>      // for getpwuid
13
#endif
14
#include <cstdio>       // for getc
15
#include <cstring>
Luis Peñaranda's avatar
Luis Peñaranda committed
16

17
#include "files.h"
18

19
#define VERT_SHADER_FILE "vertex_shader.vert"
20
#define FRAG_SHADER_FILE "fragment_shader.frag"
Luis Peñaranda's avatar
Luis Peñaranda committed
21

22 23 24 25 26 27 28 29
#define CONST_PI        3.141592653589793115997963468544185161590576171875
#define CONST_PI_2      1.5707963267948965579989817342720925807952880859375
#define CONST_PI_F      3.1415927410125732421875f
#define CONST_PI_2_F    1.57079637050628662109375f
//#define CONST_PI        (0x1.921fb54442d18p+1)
//#define CONST_PI_2      (0x1.921fb54442d18p+0)
//#define CONST_PI_F      (0x1.921fb6p+1f)
//#define CONST_PI_2_F    (0x1.921fb6p+0f)
Luis Peñaranda's avatar
Luis Peñaranda committed
30

Luis Peñaranda's avatar
Luis Peñaranda committed
31 32 33 34 35
OpenGLCanvas::OpenGLCanvas(QWidget *parent) :
    QGLWidget(parent)
{
    setFormat(QGL::DoubleBuffer | QGL::DepthBuffer);
    fov = 60.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
36
    fov_max = 60.f; // TODO: I only use floats here because Leo did... check
Luis Peñaranda's avatar
Luis Peñaranda committed
37 38 39
    scale = 1.0f;
    center_lambda = 0.f;
    center_phi = 0.f;
40
    fov_scale_relation = "Naive";
41
    visualization = "Moebius";
42
    auto_fov_max=false;
43 44
    zblambda=.1f; // Zorin-Barr transformation lambda
    zbR=1.f; // Zorin-Barr transformation R
Luis Peñaranda's avatar
Luis Peñaranda committed
45 46 47 48 49 50 51 52 53 54 55 56 57

    time_frames = 0;
    time_timer.setInterval(0);
    connect(&time_timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
    time_start = time_time.time();
}

void OpenGLCanvas::slotTimer(void) {
    updateGL();
}

void OpenGLCanvas::change_fov(double f){

Luis Peñaranda's avatar
Luis Peñaranda committed
58 59 60 61
    if (fov!=f && f>=1.f && f<=360.f)
        fov=f;
    if (fov<=fov_max)
        scale=1.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
62 63
    //else if (fov>295.f)
    //    scale = 0.02f; // TODO: check this value wrt fov_max
Luis Peñaranda's avatar
Luis Peñaranda committed
64
    else {
Luis Peñaranda's avatar
Luis Peñaranda committed
65 66 67
        if (fov_scale_relation == "Naive")
            scale=fov_max/fov;
        else if (fov_scale_relation == "Square Root")
68
            scale=sqrtf((360.f-fov_max-fov)/(360.-2*fov_max));
Luis Peñaranda's avatar
Luis Peñaranda committed
69
        else if (fov_scale_relation == "Linear")
70
            scale=(360.f-fov_max-fov)/(360.f-2.f*fov_max);
Luis Peñaranda's avatar
Luis Peñaranda committed
71
        else if (fov_scale_relation == "Square Power")
72
            scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),2.f);
Luis Peñaranda's avatar
Luis Peñaranda committed
73
        else if (fov_scale_relation == "Cubic Power")
74
            scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),3.f);
Luis Peñaranda's avatar
Luis Peñaranda committed
75
        else if (fov_scale_relation == "Logarithm")
76
            scale=logf(expf(1.f)+(1.f-expf(1.f))*(fov-fov_max)/(360.f-2.f*fov_max));
Luis Peñaranda's avatar
Luis Peñaranda committed
77 78 79 80
    }

//    scale = 0.3f;

Luis Peñaranda's avatar
Luis Peñaranda committed
81
    fprintf(stderr,"change fov, fov=%f, fov_max=%f, scale=%f\n",fov,fov_max,scale);
82
    emit fov_changed((int)fov);
Luis Peñaranda's avatar
Luis Peñaranda committed
83

Luis Peñaranda's avatar
Luis Peñaranda committed
84 85 86 87
    updateGL();

}

88 89
void OpenGLCanvas::automaxbutton(bool autovalue){
    auto_fov_max=autovalue;
90
    change_fov_max(compute_auto_fov_max(fov));
91 92
}

93
void OpenGLCanvas::change_fov(int new_fov){
94 95 96 97
    if(new_fov<=360&&new_fov>=1){
        change_fov((double)new_fov);
        if(auto_fov_max)
            change_fov_max(compute_auto_fov_max(new_fov));
98
    }
99 100 101
}

void OpenGLCanvas::change_fov_max(int new_fov_max){
102
    if(new_fov_max<=360.f&&new_fov_max>=1)
Luis Peñaranda's avatar
Luis Peñaranda committed
103
            fov_max=(double)new_fov_max;
104 105
    if (fov<=fov_max)
        scale=1.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
106 107
    //else if (fov>295.f)
    //    scale = 0.02f; // TODO: check this value wrt fov_max
108 109 110 111
    else {
        if (fov_scale_relation == "Naive")
            scale=fov_max/fov;
        else if (fov_scale_relation == "Square Root")
112
            scale=sqrtf((360.f-fov_max-fov)/(360.f-2.f*fov_max));
113
        else if (fov_scale_relation == "Linear")
114
            scale=(360.f-fov_max-fov)/(360.f-2.f*fov_max);
115
        else if (fov_scale_relation == "Square Power")
116
            scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),2.f);
117
        else if (fov_scale_relation == "Cubic Power")
118
            scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),3.f);
119
        else if (fov_scale_relation == "Logarithm")
120
            scale=logf(expf(1.f)+(1.f-expf(1.f))*(fov-fov_max)/(360.f-2.f*fov_max));
121
    }
Luis Peñaranda's avatar
Luis Peñaranda committed
122
    fprintf(stderr,"change fov_max, fov=%f, fov_max=%f, new scale=%f\n",fov,fov_max,scale);
123
    emit max_fov_changed((int)fov_max);
Luis Peñaranda's avatar
Luis Peñaranda committed
124 125 126
    updateGL();
}

Luis Peñaranda's avatar
Luis Peñaranda committed
127 128 129 130 131 132 133 134 135
//void OpenGLCanvas::change_scale(double s){

//    if (scale!=s && s>=0.0 && s<=1.0) scale = s;
//    updateGL();

//}

void OpenGLCanvas::change_center_lambda(double lambda){

136
    if (center_lambda!=lambda && lambda>=-CONST_PI_F && lambda<=CONST_PI_F) {
Luis Peñaranda's avatar
Luis Peñaranda committed
137 138 139 140 141 142 143 144
        center_lambda = lambda;
        updateGL();
    }

}

void OpenGLCanvas::change_center_phi(double phi){

145
    if (center_phi!=phi && phi>=-CONST_PI_2_F && phi<=CONST_PI_2_F) {
Luis Peñaranda's avatar
Luis Peñaranda committed
146 147 148 149 150 151
        center_phi = phi;
        updateGL();
    }

}

Luis Peñaranda's avatar
Luis Peñaranda committed
152 153 154 155 156 157
void OpenGLCanvas::re_center(){
    center_phi=.0f;
    center_lambda=.0f;
    updateGL();
}

Luis Peñaranda's avatar
Luis Peñaranda committed
158 159 160
void OpenGLCanvas::change_fov_scale_relation(QString name){

   fov_scale_relation = name;
Luis Peñaranda's avatar
Luis Peñaranda committed
161 162
   if (fov<fov_max) scale = 1.f;
   //else if (fov>295.f) scale = 0.01f;
Luis Peñaranda's avatar
Luis Peñaranda committed
163
   else{
Luis Peñaranda's avatar
Luis Peñaranda committed
164 165 166
       if (fov_scale_relation == "Naive")
           scale=fov_max/fov;
       else if (fov_scale_relation == "Square Root")
167
           scale=sqrtf((360.f-fov_max-fov)/(360.f-2.f*fov_max));
Luis Peñaranda's avatar
Luis Peñaranda committed
168
       else if (fov_scale_relation == "Linear")
169
           scale=(360.f-fov_max-fov)/(360.f-2.f*fov_max);
Luis Peñaranda's avatar
Luis Peñaranda committed
170
       else if (fov_scale_relation == "Square Power")
171
           scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),2.f);
Luis Peñaranda's avatar
Luis Peñaranda committed
172
       else if (fov_scale_relation == "Cubic Power")
173
           scale=powf((360.f-fov_max-fov)/(360.f-2.f*fov_max),3.f);
Luis Peñaranda's avatar
Luis Peñaranda committed
174
       else if (fov_scale_relation == "Logarithm")
175
           scale=logf(expf(1.f)+(1.f-expf(1.f))*(fov-fov_max)/(360.f-2.f*fov_max));
Luis Peñaranda's avatar
Luis Peñaranda committed
176
   }
177
   fprintf(stderr,"changed scale relation, scale=%f, fov_max=%f\n",scale,fov_max);
Luis Peñaranda's avatar
Luis Peñaranda committed
178 179 180 181 182 183 184
   updateGL();

}

void OpenGLCanvas::change_visualization(QString name){

    visualization = name;
185
    updateGL();
Luis Peñaranda's avatar
Luis Peñaranda committed
186 187 188

}

189 190 191
// This function reads the contents of the ~/.panorc file and stores the
// options in private variables.
void OpenGLCanvas::read_config_file(){
192
#ifdef _WIN32
Luis Penaranda's avatar
Luis Penaranda committed
193 194
    char* envvar=(char*)malloc(12*sizeof(char));
    strcpy(envvar,"USERPROFILE\0");
195
    char *filepath=(char*)malloc(512*sizeof(char));
Luis Penaranda's avatar
Luis Penaranda committed
196 197 198
    strcpy(filepath,getenv(envvar));
    free(envvar);
    strcat(filepath,"\\.panorc");
199
#else
200 201 202
    struct passwd *pw=getpwuid(getuid());
    char *filepath=pw->pw_dir;
    strcat(filepath,"/.panorc");
203
#endif
204 205 206 207
    shader_dir=(char*)malloc(512*sizeof(char));
    shader_dir[0]='\0';
    input_image_file=(char*)malloc(512*sizeof(char));
    input_image_file[0]='\0';
208 209
    input_image_dir=(char*)malloc(512*sizeof(char));
    input_image_dir[0]='\0';
210
    char *read_line=(char*)malloc(64*sizeof(char));
211 212
    struct stat testbuf;
    if(stat(filepath,&testbuf)){
Luis Penaranda's avatar
Luis Penaranda committed
213
        fprintf(stderr,"%s does not exist\n",filepath);
214
    }else{
Luis Penaranda's avatar
Luis Penaranda committed
215 216
        FILE *rcfile;
        FOPEN_RO(rcfile,filepath);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        char c;
        char *line=(char*)malloc(512*sizeof(char));
        while((c=getc(rcfile))!=EOF){
            while(c=='\n') // discard empty lines
                c=getc(rcfile);
            if(c==EOF)
                break;
            line[0]=c; // first char on the line was already read
            if(!fgets(line+1,511,rcfile)){
                fprintf(stderr,"error reading rcfile\n");
                exit(-1);
            }
            // check for 'shader_dir' option
            if(!strncmp(line,"shader_dir=",11)){
                strcpy(shader_dir,line+11);
                shader_dir[strlen(line)-12]='\0';
                fprintf(stderr,"shader_dir=%s\n",shader_dir);
            }
            // check for 'image_file' option
            if(!strncmp(line,"image_file=",11)){
                strcpy(input_image_file,line+11);
                input_image_file[strlen(line)-12]='\0';
                fprintf(stderr,"input_image_file=%s\n",input_image_file);
            }
241 242 243 244 245 246
            // check for 'image_dir' option
            if(!strncmp(line,"image_dir=",10)){
                strcpy(input_image_dir,line+10);
                input_image_dir[strlen(line)-11]='\0';
                fprintf(stderr,"input_image_dir=%s\n",input_image_dir);
            }
Luis Peñaranda's avatar
Luis Peñaranda committed
247 248
            // check for 'max_fov' option
            if(!strncmp(line,"max_fov=",8)){
249 250 251
                strcpy(read_line,line+8);
                read_line[strlen(line)-9]='\0';
                fov_max=atof(read_line);
Luis Peñaranda's avatar
Luis Peñaranda committed
252 253
                fprintf(stderr,"max_fov=%f\n",fov_max);
            }
254 255 256 257 258 259 260
            // check for 'fov' option
            if(!strncmp(line,"fov=",4)){
                strcpy(read_line,line+4);
                read_line[strlen(line)-5]='\0';
                fov=atof(read_line);
                fprintf(stderr,"fov=%f\n",fov);
            }
261 262 263 264 265 266 267
            // check for 'auto_max_fov' option
            if(!strncmp(line,"auto_max_fov=",13)){
                strcpy(read_line,line+13);
                read_line[strlen(line)-14]='\0';
                auto_fov_max=atof(read_line);
                fprintf(stderr,"auto_max_fov=%d\n",auto_fov_max);
            }
268 269 270 271 272 273 274 275 276 277 278 279 280
            // check for the Zorin-Barr transformation parameters, lambda and R
            if(!strncmp(line,"zblambda=",9)){
                strcpy(read_line,line+9);
                read_line[strlen(line)-10]='\0';
                zblambda=atof(read_line);
                fprintf(stderr,"zblambda=%f\n",zblambda);
            }
            if(!strncmp(line,"zbR=",4)){
                strcpy(read_line,line+4);
                read_line[strlen(line)-5]='\0';
                zbR=atof(read_line);
                fprintf(stderr,"zbR=%f\n",zbR);
            }
281 282 283
        }
        fclose(rcfile);
    }
Luis Penaranda's avatar
Luis Penaranda committed
284 285 286
#ifdef _WIN32
    free(filepath);
#endif
Luis Peñaranda's avatar
Luis Peñaranda committed
287 288
    emit fov_changed((int)fov);
    emit max_fov_changed((int)fov_max);
289 290
}

291
void OpenGLCanvas::load_image(const char *new_image){
292
    unsigned char *textureBytes=read_image(new_image,&image_size_x,&image_size_y);
293 294 295 296 297 298
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    GLuint tex;
    glGenTextures(1,&tex);
    glBindTexture(GL_TEXTURE_2D,tex);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
299 300 301 302 303 304 305 306 307
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGB,
                 image_size_x,
                 image_size_y,
                 0,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 textureBytes);
308 309
}

310
void OpenGLCanvas::change_input_image(){
311
        const char *fname=QFileDialog::getOpenFileName(this,tr("Choose Panorama File"),input_image_dir,FileRead::image_types).toStdString().c_str();
Luis Penaranda's avatar
Luis Penaranda committed
312 313 314 315
        if(strlen(fname)>0){
                load_image(fname);
                updateGL();
        }
316 317
}

Luis Peñaranda's avatar
Luis Peñaranda committed
318 319 320 321 322 323 324 325
void OpenGLCanvas::initializeGL(){
    glShadeModel(GL_SMOOTH);
    glClearColor(1.0f,1.0f,1.0f,0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

326
#ifndef _MSC_VER
327
#ifdef __APPLE__
Luis Peñaranda's avatar
Luis Peñaranda committed
328
    const char * const progname = "PROJ_ROOT_DIR";
329 330 331 332 333
#else
    // progname is a file name or a path???
    const char * const progname = (char*)(PROGNAME);
#endif
    fprintf(stderr,"progname=%s\n",progname);
334 335
#endif // MSC_VER

336 337
    read_config_file();
    // If the input file does not exist or was not specified.
338
    struct stat testbuf;
339
    if(stat(input_image_file,&testbuf)||!strcmp(input_image_file,"")){
340
        load_image(QFileDialog::getOpenFileName(this,tr("Choose Panorama File"),input_image_dir,FileRead::image_types).toStdString().c_str());
341
    }else{
342
        load_image(input_image_file);
343
    }
344
    free(input_image_file);
Luis Peñaranda's avatar
Luis Peñaranda committed
345 346 347 348 349 350 351 352 353 354 355

    // mesh resolution
    int m,n;
    m = n = 100;

    //defining texture coordinates
    int meshNumTexCoord = m*n;
    float *texCoord = (float *)malloc(2*meshNumTexCoord*sizeof(float));
    if (texCoord == NULL){
        printf("problem allocating memory for texture coordinates \n");
    }
Luis Peñaranda's avatar
Luis Peñaranda committed
356
    define_texture_coordinates(texCoord, m, n, -CONST_PI_2_F, CONST_PI_2_F, -CONST_PI_F, CONST_PI_F);
Luis Peñaranda's avatar
Luis Peñaranda committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

    //defining positions of the sphere vertices
    int meshNumVertices = m*n;
    float* positions = (float *)malloc(3*meshNumVertices*sizeof(float));
    if (positions == NULL){
        printf("problem allocating memory for positions \n");
    }
//    vertex_transformation(positions, m, n, center_lambda, center_phi, fov_rads, scale); //passar pelo vertex shader
    load_sphere_mesh(positions, m, n); //colocar essa e funcoes para textura e triangulos no initializeGL

    //defining triagle indices
    unsigned int meshNumFaces = 2*(m-1)*(n-1);
    unsigned int meshNumIndices = 3*meshNumFaces;
    unsigned int * indices = (unsigned int *)malloc(meshNumIndices*sizeof(unsigned int));
    define_triangle_indices(indices, m, n);

    // draw setup
    verticesPositions = positions;
    textureCoordinates = texCoord;
    numberOfIndices = meshNumIndices;
    triangleIndices = indices;

    setShaders();
}

void OpenGLCanvas::define_texture_coordinates(float *texCoord, int m, int n, float min_phi, float max_phi, float min_lambda, float max_lambda){

    float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1));
    float delta_phi = (max_phi-min_phi)/(1.0*(m-1));

    for (int i = 0; i<m; i++){
        for (int j = 0; j<n; j++){
Luis Peñaranda's avatar
Luis Peñaranda committed
389 390
            texCoord[2*(j+i*n)] = (min_lambda+delta_lambda*j)/(2*CONST_PI_F) + 0.5;
            texCoord[2*(j+i*n)+1] = (min_phi+delta_phi*i)/(CONST_PI_F) + 0.5;
Luis Peñaranda's avatar
Luis Peñaranda committed
391 392 393 394 395
        }
    }

}

396
// This function makes the same computation GLSL does. It is never called.
Luis Peñaranda's avatar
Luis Peñaranda committed
397 398
void OpenGLCanvas::vertex_transformation(float *positions, int m, int n, float center_lambda, float center_phi, float fov_rads, float scale){

Luis Peñaranda's avatar
Luis Peñaranda committed
399 400 401 402
    float min_lambda = -CONST_PI_F;
    float max_lambda = CONST_PI_F;
    float min_phi = -CONST_PI_2_F;
    float max_phi = CONST_PI_2_F;
Luis Peñaranda's avatar
Luis Peñaranda committed
403 404 405 406 407 408 409

    float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1));
    float delta_phi = (max_phi-min_phi)/(1.0*(m-1));

    float lambda, phi, x, y, z, u, v, r, theta;

    //calculating the extent of the projection for the given FOV
410 411
    lambda=fov_rads;
    phi=0.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
412
    // OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    y=sinf(phi);
    x=-sinf(lambda)*cosf(phi);
    z = -cosf(lambda)*cosf(phi);
    u=2.f*x/(1.f-z);
    v=2.f*y/(1.f-z);
    r=hypotf(u,v);
    theta=atan2f(u,v);
    r*=scale;
    u=-r*sinf(theta);
    v=r*cosf(theta);
    x=(4.f*u)/(u*u+v*v+4.f);
    y=(4.f*v)/(u*u+v*v+4.f);
    z=(u*u+v*v-4.f)/(u*u+v*v+4.f);
    u=x/(-z);
    v=y/(-z);
    float extent=u;
Luis Peñaranda's avatar
Luis Peñaranda committed
429 430 431 432

    for (int i = 0; i<m; i++){
        for (int j = 0; j<n; j++){

433 434
            lambda=(min_lambda+delta_lambda*j);
            phi=(min_phi+delta_phi*i);
Luis Peñaranda's avatar
Luis Peñaranda committed
435 436

            // OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes
437 438 439
            y=sinf(phi);
            x=-sinf(lambda)*cosf(phi);
            z=-cosf(lambda)*cosf(phi);
Luis Peñaranda's avatar
Luis Peñaranda committed
440 441

            //Rotation 1: (-center_lambda)-rotation on the xz-plane
442 443 444 445
            float x_copy=x;
            x=cosf(-center_lambda)*x-sinf(-center_lambda)*z;
            //y=1.f*y;
            z=sinf(-center_lambda)*x_copy+cosf(-center_lambda)*z;
Luis Peñaranda's avatar
Luis Peñaranda committed
446 447

            //Rotation 2: (-center_phi)-rotation on the yz-plane
448 449 450 451
            float y_copy=y;
            //x = 1.f*x;
            y=cosf(-center_phi)*y-sinf(-center_phi)*z;
            z=sinf(-center_phi)*y_copy+cosf(-center_phi)*z;
Luis Peñaranda's avatar
Luis Peñaranda committed
452

453 454
            u=2.f*x/(1.f-z);
            v=2.f*y/(1.f-z);
Luis Peñaranda's avatar
Luis Peñaranda committed
455

456 457
            r=hypotf(u,v);
            theta=atan2f(u,v);
Luis Peñaranda's avatar
Luis Peñaranda committed
458 459

            // scaling the complex plane according to scale specified in the interface (relate it to FOV)
460
            r*=scale;
Luis Peñaranda's avatar
Luis Peñaranda committed
461

462 463
            u=-r*sinf(theta);
            v=r*cosf(theta);
Luis Peñaranda's avatar
Luis Peñaranda committed
464

465 466 467
            x=(4.f*u)/(u*u+v*v+4.f);
            y=(4.f*v)/(u*u+v*v+4.f);
            z=(u*u+v*v-4.f)/(u*u+v*v+4.f);
Luis Peñaranda's avatar
Luis Peñaranda committed
468

469 470
            lambda=atan2f(x,-z)/CONST_PI_F;
            phi=asinf(y)/CONST_PI_2_F;
Luis Peñaranda's avatar
Luis Peñaranda committed
471

472 473 474
            if (visualization=="Moebius"){
                u = x/(-z);
                v = y/(-z);
Luis Peñaranda's avatar
Luis Peñaranda committed
475 476 477 478
                positions[3*(j+i*n)] = u/extent;
                positions[3*(j+i*n)+1] = v/extent;
                positions[3*(j+i*n)+2] = z;
            }
479
            else if (visualization=="3D Sphere"){
Luis Peñaranda's avatar
Luis Peñaranda committed
480 481 482 483
                positions[3*(j+i*n)] = 0.9f*x;
                positions[3*(j+i*n)+1] = 0.9f*y;
                positions[3*(j+i*n)+2] = z;
            }
484
            else if (visualization=="Equi-Rectangular"){
Luis Peñaranda's avatar
Luis Peñaranda committed
485 486 487 488
                positions[3*(j+i*n)] = lambda;
                positions[3*(j+i*n)+1] = phi;
                positions[3*(j+i*n)+2] = z;
            }
489
            else if (visualization=="Stereographic"){
490 491 492 493 494 495
                u = 2*x/(-z+1);
                v = 2*y/(-z+1);
                positions[3*(j+i*n)] = u/extent;
                positions[3*(j+i*n)+1] = v/extent;
                positions[3*(j+i*n)+2] = z;
            }
496 497 498
            else if (visualization=="Mercator"){
                u=lambda;
                v=logf((1.0/cosf(phi))+tanf(phi));
499 500 501 502
                positions[3*(j+i*n)] = u/extent;
                positions[3*(j+i*n)+1] = v/extent;
                positions[3*(j+i*n)+2] = z;
            }
503
            else if (visualization=="Zorin-Barr"){
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
                // perspective
                u = x/(-z);
                v = y/(-z);
                // apply Z-B transformation to (u,v)
                float lambda=.1f;
                float R=1.f;
                float alpha=atanf(v/u);
                float r=hypotf(u,v);
                float rhoprime=(lambda*r/R)+(1.f-lambda)*(R*(sqrtf(r*r+1.f)-1.f))/(r*(sqrtf(R*R+1.f)-1.f));
                u=rhoprime*cosf(alpha);
                v=rhoprime*sinf(alpha);
                //
                positions[3*(j+i*n)] = u/extent;
                positions[3*(j+i*n)+1] = v/extent;
                positions[3*(j+i*n)+2] = z;
            }
520 521 522 523 524 525
            else if (visualization=="Orthographic"){
                u=x;
                v=y;
                positions[3*(j+i*n)]=x/extent;
                positions[3*(j+i*n)+1]=y/extent;
                positions[3*(j+i*n)+2]=z;
Luis Penaranda's avatar
Luis Penaranda committed
526
            }
Luis Peñaranda's avatar
Luis Peñaranda committed
527 528 529 530 531 532
        }
    }
}

void OpenGLCanvas::load_sphere_mesh(float *positions, int m, int n){

Luis Peñaranda's avatar
Luis Peñaranda committed
533 534 535 536
    float min_lambda = -CONST_PI_F;
    float max_lambda = CONST_PI_F;
    float min_phi = -CONST_PI_2_F;
    float max_phi = CONST_PI_2_F;
Luis Peñaranda's avatar
Luis Peñaranda committed
537 538 539 540 541 542 543 544 545

    float delta_lambda = (max_lambda-min_lambda)/(1.0*(n-1));
    float delta_phi = (max_phi-min_phi)/(1.0*(m-1));

    float lambda, phi, x, y, z;

    for (int i = 0; i<m; i++){
        for (int j = 0; j<n; j++){

546 547
            lambda=(min_lambda+delta_lambda*j);
            phi=(min_phi+delta_phi*i);
Luis Peñaranda's avatar
Luis Peñaranda committed
548 549

            // OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes
550 551 552
            y=sinf(phi);
            x=-sinf(lambda)*cosf(phi);
            z=-cosf(lambda)*cosf(phi);
Luis Peñaranda's avatar
Luis Peñaranda committed
553 554 555 556 557 558 559 560 561 562 563

            positions[3*(j+i*n)] = x;
            positions[3*(j+i*n)+1] = y;
            positions[3*(j+i*n)+2] = z;

        }
    }

}

float OpenGLCanvas::calculate_extent(float fov_rads){
564
    float lambda, phi, x, y, z, u, v, r, theta;
Luis Peñaranda's avatar
Luis Peñaranda committed
565
    //calculating the extent of the projection for the given FOV
566 567
    lambda=fov_rads;
    phi=0.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
568
    // OpenGL: x is the vertical axes pointg downwards, and y is horizontal axes
569 570 571 572 573 574 575 576 577 578 579 580 581
    y=sinf(phi);
    x=-sinf(lambda)*cosf(phi);
    z=-cosf(lambda)*cosf(phi);
    u=2.f*x/(1.f-z);
    v=2.f*y/(1.f-z);
    r=hypotf(u,v);//sqrt(u*u+v*v);
    theta=atan2f(u,v);
    r*=scale;
    u=-r*sinf(theta);
    v=r*cosf(theta);
    x=(4.f*u)/(u*u+v*v+4.f);
    y=(4.f*v)/(u*u+v*v+4.f);
    z=(u*u+v*v-4.f)/(u*u+v*v+4.f);
582
    if (visualization=="Moebius"){
583 584
        u=x/(-z);
        v=y/(-z);
585 586
    }
    else if (visualization=="Stereographic"){
587 588
        u=2.f*x/(1.f-z);
        v=2.f*y/(1.f-z);
589 590
    }
    else if (visualization=="Mercator"){
591
        u=fov_rads;
592 593 594 595
        // Warning: this extent calculation is wrong.
        // Write now it's olny showing the entire panorama intead of
        // the corresponging FOV.
    }
596 597
    if (visualization=="Zorin-Barr"){
        // TODO: check whether this is correct
598 599
        u=x/(-z);
        v=y/(-z);
600
    }
Luis Penaranda's avatar
Luis Penaranda committed
601
    if (visualization=="Orthographic"){
602
        if(z<0.f){
Luis Penaranda's avatar
Luis Penaranda committed
603 604 605 606 607 608
            u=x;
            v=y;
        }else{
            u=v=1.f;
        }
    }
Luis Peñaranda's avatar
Luis Peñaranda committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    return u;
}

void OpenGLCanvas::define_triangle_indices(unsigned int * indices, int m, int n){

    for (int i = 0; i<m-1; i++){
        for (int j = 0; j<n-1; j++){

            unsigned int index = (j+i*n);

            indices[3*(2*(j+i*(n-1)))] = index;
            indices[3*(2*(j+i*(n-1)))+1] = index+1;
            indices[3*(2*(j+i*(n-1)))+2] = index+n;

            indices[3*(2*(j+i*(n-1))+1)] = index+1;
            indices[3*(2*(j+i*(n-1))+1)+1] = index+n+1;
            indices[3*(2*(j+i*(n-1))+1)+2] = index+n;

        }
    }

}

632 633 634 635 636
// This function computes a new value of the maximum fov and must be called
// when the fov is changed and the auto setting is enabled.
int OpenGLCanvas::compute_auto_fov_max(int new_fov){
        if(new_fov<60)
            return 60;
637
        if(new_fov>179)
638 639 640 641
            return 1;
        //if(new_fov>60)
            return (90-new_fov/2);
}
Luis Penaranda's avatar
Luis Penaranda committed
642

Luis Peñaranda's avatar
Luis Peñaranda committed
643
void OpenGLCanvas::resizeGL(int w, int h){
644 645 646 647
    if(w>h)
        glViewport(0,(h-w)/2,w,w);
    else
        glViewport((w-h)/2,0,h,h);
Luis Peñaranda's avatar
Luis Peñaranda committed
648 649 650 651 652 653 654
}

char * OpenGLCanvas::textFileRead(char *fn) {

    FILE *fp;
    char *content = NULL;
    int f, count;
655 656
    f = OPEN_FILE(fn, O_RDONLY);
    count = LSEEK_FD(f, 0, SEEK_END);
Luis Peñaranda's avatar
Luis Peñaranda committed
657 658
//    close(f);
    if (fn != NULL) {
Luis Penaranda's avatar
Luis Penaranda committed
659
        FOPEN_RO(fp,fn);
Luis Peñaranda's avatar
Luis Peñaranda committed
660 661 662 663 664 665
        if (fp != NULL) {
            if (count > 0) {
                content = (char *)malloc(sizeof(char) * (count+1));
                count = fread(content,sizeof(char),count,fp);
                content[count] = '\0';
            }
Luis Penaranda's avatar
Luis Penaranda committed
666
            fclose(fp); // maybe this line must be outside the {}
Luis Peñaranda's avatar
Luis Peñaranda committed
667 668 669 670 671 672 673 674 675
        }
    }
    return content;
}

void OpenGLCanvas::setShaders() {

    char *vs,*fs;

Luis Peñaranda's avatar
Luis Peñaranda committed
676 677
#ifndef __APPLE__
  #ifdef GLEW_VERSION_1_5
678 679 680 681 682
    GLenum err=glewInit();
    if(err!=GLEW_OK){
        fprintf(stderr,"error in GLEW initialization: %s\n",glewGetString(err));
        exit(-1);
    }
Luis Peñaranda's avatar
Luis Peñaranda committed
683
  #endif
684 685
#endif

Luis Peñaranda's avatar
Luis Peñaranda committed
686 687 688
    GLuint v = glCreateShader(GL_VERTEX_SHADER);
    GLuint f = glCreateShader(GL_FRAGMENT_SHADER);

689
    // Configure vertex and fragment shader files.
690 691
    char *vs_file=(char*)malloc(512*sizeof(char*));
    char *fs_file=(char*)malloc(512*sizeof(char*));
692 693 694 695 696 697 698 699 700 701 702 703 704 705
    if(!strcmp(shader_dir,"")){ // if shader_dir was not configured
        if(!GET_WORKDIR(vs_file,512)||!GET_WORKDIR(fs_file,512)){
            fprintf(stderr,"error reading shader files\n");
            exit(-1);
        }
        strcat(vs_file,"/shaders/");
        strcat(fs_file,"/shaders/");
    }else{;
        strcpy(vs_file,shader_dir);
        strcat(vs_file,"/");
        strcpy(fs_file,shader_dir);
        strcat(fs_file,"/");
    }
    strcat(vs_file,VERT_SHADER_FILE);
706
    strcat(fs_file,FRAG_SHADER_FILE);
707
    fprintf(stderr,"vs_file=%s\nfs_file=%s\n",vs_file,fs_file);
708 709 710 711 712 713 714 715

    struct stat vs_testbuf,fs_testbuf;
    if(stat(vs_file,&vs_testbuf)||stat(fs_file,&fs_testbuf)){
        fprintf(stderr,"a shader file does not exist!\n");
        free(vs_file);
        free(fs_file);
        exit(-1);
    }
Luis Peñaranda's avatar
Luis Peñaranda committed
716

717 718
    vs=textFileRead(vs_file);
    fs=textFileRead(fs_file);
Luis Peñaranda's avatar
Luis Peñaranda committed
719 720 721 722 723 724 725 726

    const char * vv = vs;
    const char * ff = fs;

    glShaderSource(v, 1, &vv,NULL);
    glShaderSource(f, 1, &ff,NULL);

    free(vs);free(fs);
727
    free(vs_file);free(fs_file);
Luis Peñaranda's avatar
Luis Peñaranda committed
728 729 730 731 732 733

    glCompileShader(v);
    glCompileShader(f);

    GLuint p = glCreateProgram();

734 735 736 737 738 739 740 741 742 743 744
    // Bind attributes zblambda and zbR to the vertex shader. Nvidia hardware
    // only leaves attributes 1 and 7 unreserved; attributes 8 to 15 are
    // reserved for textures.
    // TODO: we use attributes 14 and 15, which work for Nvidia; we need to
    // test with other hardware.
#define ZBL_ATTR 14
#define ZBR_ATTR 15
    glVertexAttrib1f(ZBL_ATTR,zblambda);
    glBindAttribLocation(p,ZBL_ATTR,"zblambda");
    glVertexAttrib1f(ZBR_ATTR,zbR);
    glBindAttribLocation(p,ZBR_ATTR,"zbR");
745

Luis Peñaranda's avatar
Luis Peñaranda committed
746 747 748 749 750 751 752 753
    glAttachShader(p,v);
    glAttachShader(p,f);

    glLinkProgram(p);
    glUseProgram(p);

}

Luis Peñaranda's avatar
Luis Peñaranda committed
754 755 756 757 758 759 760 761 762
void OpenGLCanvas::mousePressEvent(QMouseEvent *event){
    lastPos=event->pos();
    //fprintf(stderr,"mouse click\n");
}

void OpenGLCanvas::mouseMoveEvent(QMouseEvent *event){
    // scroll with the left button
    if(event->buttons()==Qt::LeftButton){
        // compute the delta and move the image
Luis Peñaranda's avatar
Luis Peñaranda committed
763 764
        center_lambda+=(event->x()-lastPos.x())*CONST_PI_F/image_size_x;
        center_phi+=(event->y()-lastPos.y())*CONST_PI_F/image_size_y;
Luis Peñaranda's avatar
Luis Peñaranda committed
765 766 767 768 769
        lastPos=event->pos();
        updateGL();
    }
}

770 771
void OpenGLCanvas::wheelEvent(QWheelEvent *event){
    if(event->orientation()==Qt::Vertical){
772
        if(event->modifiers()==Qt::ShiftModifier){
773 774
            change_fov_max(fov_max+((double)event->delta())/30);
        }else{
775 776
            int new_fov=fov+event->delta()/30;
            change_fov((double)new_fov);
777 778
            if(auto_fov_max)
                change_fov_max(compute_auto_fov_max(new_fov));
779
        }
780 781 782
    }
}

Luis Peñaranda's avatar
Luis Peñaranda committed
783 784
void OpenGLCanvas::paintGL(){

785
    float fov_rads = (fov/360.f)*CONST_PI_F;
Luis Peñaranda's avatar
Luis Peñaranda committed
786 787 788 789 790

//    // changing scale to generate the figures for the paper (remove it after)
//    scale = 0.8;

    // defining transformation parameters (that will be passed to the vertex shader)
791 792 793 794 795 796 797 798 799
    float extent=calculate_extent(fov_rads);
    float vis_mode=.0f;
    if (visualization=="Moebius") vis_mode=1.f;
    else if (visualization=="3D Sphere") vis_mode=2.f;
    else if (visualization=="Equi-Rectangular") vis_mode=3.f;
    else if (visualization=="Stereographic") vis_mode=4.f;
    else if (visualization=="Mercator") vis_mode=5.f;
    else if (visualization=="Zorin-Barr") vis_mode=6.f;
    else if (visualization=="Orthographic") vis_mode=7.f;
Luis Peñaranda's avatar
Luis Peñaranda committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 2.0/extent, 0.0, 2.0/scale, 0.0, -2.0/vis_mode);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(0.0, 2.0/center_lambda, 0.0, 2.0/center_phi, -1.0, 1.0);

    // drawing the mesh
    glClearColor(1.0, 1.0, 1.0, 1.0);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glColor3f(1, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, verticesPositions);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates);

    glDrawElements(GL_TRIANGLES, numberOfIndices, GL_UNSIGNED_INT, triangleIndices);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    time_frames++;
//    if (time_frames > 0) {
        double dt = time_time.elapsed();
//        if (dt > 0.5) {
            time_fps = time_frames/dt;
            time_frames = 0;
            time_time.reset();
Luis Peñaranda's avatar
Luis Peñaranda committed
833 834
            emit fps(QString("%1 fps").arg((int)(time_fps+0.5)));
            //printf("fps = %d ", (int)(time_fps+0.5));
Luis Peñaranda's avatar
Luis Peñaranda committed
835 836 837 838
//        }
//    }

}