diff --git a/README b/README index 81d42217b66d75574bda26282ffa869a55484741..073e855ebe19ccb987f59a9705604c6824594b40 100644 --- a/README +++ b/README @@ -9,3 +9,11 @@ it was not tested). It requires: -glut or freeglut. To build, simply run qmake and make. + +To configure, you can set in the file ~/.panorc the following options. + shader_dir= + image_file= +Lines in the file must start with option names, and no spaces should go +between option names, equal signs and values. If no shader path is set, the +default is ./shaders/. If no input file is set, or the file is missing, a +file chooser will be shown at startup. diff --git a/openglcanvas.cpp b/openglcanvas.cpp index 2ed9039cefebe571ff5a2a181bf130b4c98f8f54..27a64627533860042295c9109bf476dc4fdbb5ac 100644 --- a/openglcanvas.cpp +++ b/openglcanvas.cpp @@ -4,20 +4,23 @@ #endif #include "openglcanvas.h" #include +#include // for getcwd, getpwuid, getuid +#include // for fstat, getpwuid, getuid +#include // for fstat +#include // for getpwuid (TODO: windows?) +#include // for fopen, fclose, getc +#include #ifdef WINDOWS #include #define GET_WORKDIR _getcwd #else - #include + // unistd was included above #define GET_WORKDIR getcwd #endif -#include // for fstat -#include // for fstat #define PROGNAME "pano_interface" -#define INPUT_IMAGE_FILE "input_images/image_4_input.pnm" -#define VERT_SHADER_FILE "shaders/test_vertex_shader.vert" -#define FRAG_SHADER_FILE "shaders/fragment_shader.frag" +#define VERT_SHADER_FILE "test_vertex_shader.vert" +#define FRAG_SHADER_FILE "fragment_shader.frag" OpenGLCanvas::OpenGLCanvas(QWidget *parent) : QGLWidget(parent) @@ -107,6 +110,51 @@ void OpenGLCanvas::change_visualization(QString name){ } +// This function reads the contents of the ~/.panorc file and stores the +// options in private variables. +// TODO: windows +void OpenGLCanvas::read_config_file(){ + struct passwd *pw=getpwuid(getuid()); + char *filepath=pw->pw_dir; + strcat(filepath,"/.panorc"); + shader_dir=(char*)malloc(512*sizeof(char)); + shader_dir[0]='\0'; + input_image_file=(char*)malloc(512*sizeof(char)); + input_image_file[0]='\0'; + struct stat testbuf; + if(stat(filepath,&testbuf)){ + fprintf(stderr,"~/.panorc does not exist\n"); + }else{ + FILE *rcfile=fopen(filepath,"r"); + 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); + } + } + fclose(rcfile); + } +} + void OpenGLCanvas::load_image(const char *new_image){ const char * const progname=(char*)(PROGNAME); int textureSize=getTextureSize(progname,new_image); @@ -135,33 +183,23 @@ void OpenGLCanvas::initializeGL(){ glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); - char *input_image=(char*)malloc(512*sizeof(char*)); - if(!GET_WORKDIR(input_image,512)){ - fprintf(stderr,"too long pathname\n"); - exit(-1); - }; - strcat(input_image,"/\0"); - strcat(input_image,INPUT_IMAGE_FILE); #ifdef __APPLE__ const char * const progname = "PROJ_ROOT_DIR"; - //const char * input_image = INPUT_IMAGE_FILE; #else // progname is a file name or a path??? const char * const progname = (char*)(PROGNAME); - //const char * input_image = (char*)(INPUT_IMAGE_FILE); #endif fprintf(stderr,"progname=%s\n",progname); - fprintf(stderr,"default image=%s\n",input_image); - // what to do if the input file does not exist + read_config_file(); + // If the input file does not exist or was not specified. struct stat testbuf; - if(stat(input_image,&testbuf)){ - fprintf(stderr,"the default image file does not exist!\n"); + if(stat(input_image_file,&testbuf)||!strcmp(input_image_file,"")){ load_image(QFileDialog::getOpenFileName(this,tr("Choose Panorama File")).toStdString().c_str()); }else{ - load_image(input_image); + load_image(input_image_file); } - free(input_image); + free(input_image_file); // mesh resolution int m,n; @@ -499,23 +537,25 @@ void OpenGLCanvas::setShaders() { GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); + // Configure vertex and fragment shader files. char *vs_file=(char*)malloc(512*sizeof(char*)); - if(!GET_WORKDIR(vs_file,512)){ - fprintf(stderr,"too long pathname\n"); - exit(-1); - }; - strcat(vs_file,"/\0"); - strcat(vs_file,VERT_SHADER_FILE); - fprintf(stderr,"vs_file=%s\n",vs_file); - char *fs_file=(char*)malloc(512*sizeof(char*)); - if(!GET_WORKDIR(fs_file,512)){ - fprintf(stderr,"too long pathname\n"); - exit(-1); - }; - strcat(fs_file,"/\0"); + 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); strcat(fs_file,FRAG_SHADER_FILE); - fprintf(stderr,"fs_file=%s\n",fs_file); + fprintf(stderr,"vs_file=%s\nfs_file=%s\n",vs_file,fs_file); struct stat vs_testbuf,fs_testbuf; if(stat(vs_file,&vs_testbuf)||stat(fs_file,&fs_testbuf)){ @@ -525,15 +565,8 @@ void OpenGLCanvas::setShaders() { exit(-1); } -//#ifdef __APPLE__ - // in Mac - //vs = textFileRead(VERT_SHADER_FILE); - //fs = textFileRead(FRAG_SHADER_FILE); -//#else - // in Linux (I think this would also work in mac, I just need to try) vs=textFileRead(vs_file); fs=textFileRead(fs_file); -//#endif const char * vv = vs; const char * ff = fs; diff --git a/openglcanvas.h b/openglcanvas.h index 88fdc7b132107c8c8e4ac098cd6e644cc3eee9e3..88dced1c7929508ed8db2306af347a1745324ffa 100644 --- a/openglcanvas.h +++ b/openglcanvas.h @@ -41,6 +41,7 @@ public: explicit OpenGLCanvas(QWidget *parent = 0); protected: + void read_config_file(); void load_image(const char *new_image); void initializeGL(); void resizeGL(int w, int h); @@ -88,6 +89,10 @@ private: float * textureCoordinates; //float windowWidth; //float windowHeight; + + // options set in the rc file + char* shader_dir; + char* input_image_file; }; #endif // OPENGLCANVAS_H