#ifndef STIM_GL_TEXTURE_H #define STIM_GL_TEXTURE_H #include #include #include #include "../grids/image_stack.h" //Visual Studio requires GLEW #ifdef _WIN32 #include #endif //#include #include namespace stim{ /* class gl_texture Uses image_stack class in order to create a texture object. */ template class gl_texture : public virtual image_stack { protected: //std::string path; GLuint texID; //OpenGL object GLenum texture_type; //1D, 2D, 3D GLint interpolation; GLint wrap; GLenum cpu_type; GLenum gpu_type; GLenum format; //format for the texture (GL_RGBA, GL_LUMINANCE, etc.) using image_stack::R; //using image_stack::S; using image_stack::ptr; /// Sets the internal texture_type, based on the data dimensions void setTextureType(){ if (R[3] > 1) //if the third dimension is greater than 1 texture_type = GL_TEXTURE_3D; //this is a 3D texture else if (R[2] > 1) //if the second dimension is greater than 1 texture_type = GL_TEXTURE_2D; //this is a 2D texture else if (R[1] > 1) //if the dimension value is greater than 1 texture_type = GL_TEXTURE_1D; //this is a 1D texture } //initializes important variables void init() { texID = 0; //initialize texture ID to zero, default if OpenGL returns an error //memset(R, 0, sizeof(size_t)); //memset(grid::S, 0, sizeof(F)); } //guesses the color format of the texture GLenum guess_format(){ size_t channels = R[0]; switch(channels){ case 1: return GL_LUMINANCE; case 2: return GL_RG; case 3: return GL_RGB; case 4: return GL_RGBA; default: std::cout<<"Error in stim::gl_texture - unable to guess texture format based on number of channels" << std::endl; exit(1); } } //guesses the OpenGL CPU data type based on T GLenum guess_cpu_type(){ // The following is C++ 11 code, but causes problems on some compilers (ex. nvcc). Below is my best approximation to a solution //if(std::is_same::value) return CV_MAKETYPE(CV_8U, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_8S, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_16U, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_16S, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_32S, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_32F, (int)C()); //if(std::is_same::value) return CV_MAKETYPE(CV_64F, (int)C()); if(typeid(T) == typeid(unsigned char)) return GL_UNSIGNED_BYTE; if(typeid(T) == typeid(char)) return GL_BYTE; if(typeid(T) == typeid(unsigned short)) return GL_UNSIGNED_SHORT; if(typeid(T) == typeid(short)) return GL_SHORT; if(typeid(T) == typeid(unsigned int)) return GL_UNSIGNED_INT; if(typeid(T) == typeid(int)) return GL_INT; if(typeid(T) == typeid(float)) return GL_FLOAT; std::cout<<"ERROR in stim::gl_texture - no valid data type found"<() { init(); //initialize the texture with NULL values interpolation = interp; //store the interpolation type wrap = twrap; //store the wrap type } ///@param is a mask indicating the files to load ///Creates an instance of the gl_texture object and initializes it with a file list gl_texture(std::string file_mask, GLint interp = GL_LINEAR, GLint twrap = GL_REPEAT){ init(); interpolation = interp; //store interpolation type wrap = twrap; //store wrap type load_images(file_mask); } ///Creates an instance of gl_texture and initializes with a file list ///@param file_list is a list of files ///@param interp is the type of texture interpolation (GL_LINEAR, GL_NEAREST) ///@param twrap is the type of texture wrapping gl_texture(std::vector file_list, GLint interp = GL_LINEAR, GLint twrap = GL_REPEAT){ init(); interpolation = interp; wrap = twrap; load_images(file_list); } ///Attaches the texture to the current OpenGL context and makes it ready to render void attach(){ if(texID == 0) generate_texture(); //generate the texture if it doesn't already exist else{ std::cout<<"Texture has already been attached to a context."< getSize(){ stim::vec size(R[1], R[2], R[3]); return size; } void getSize(size_t& x, size_t& y, size_t& z) { x = R[0]; y = R[1]; z = R[2]; } ///@param x size of the voxel in x direction ///@param y size of the voxel in y direction ///@param z size of the voxel in z direction /// Sets the dimenstions of the voxels. void setSpacing(float sx, float sy, float sz){ grid::S[1] = sx; grid::S[2] = sy; grid::S[3] = sz; } ///Returns a stim::vec that contains the x, y, z sizes of the voxel. vec getDims(){ vec dims(grid::S[1], grid::S[2], grid::S[3]); return dims; } /// Loads a series of files specified by a list of strings /// @param file_list is the vector of file names as strings void load_images(std::vector file_list){ image_stack::load_images(file_list); //load the images guess_parameters(); } ///@param file_mask specifies the file(s) to be loaded /// Sets the path and calls the loader on that path. void load_images(std::string file_mask){ image_stack::load_images(file_mask); //load images guess_parameters(); } /// Returns the GLuint id of the texture created by/associated with the /// instance of the gl_texture class. GLuint getTexture(){ return texID; } T* getData(){ return ptr; } void setData(T* rts) { } }; } #endif