GLuint TextureRenderable::texture(const uint32_t i) const { #if defined(_DEBUG) || defined(DEBUG) if (i > num_textures_) { throw wruntime_error(L"TextureRenderable::texture() - i out of bounds"); } #endif return textures_[i]; }
qdb cmd_t::toquads ( pobj o ) { jsonld_api a ( opts ); rdf_db r ( a ); auto nodeMap = o; std::map<string, pnode> lists; for ( auto g : *nodeMap->MAP() ) { if ( is_rel_iri ( g.first ) ) continue; if ( !g.second || !g.second->MAP() ) throw wruntime_error ( L"Expected map in nodemap." ); r.graph_to_rdf ( g.first, *g.second->MAP() ); } return r; }
pobj cmd_t::load_json ( string fname, bool print ) { json_spirit::wmValue v; if ( fname == L"" ) json_spirit::read_stream ( std::wcin, v ); else { std::wifstream is ( ws(fname) ); if (!is.is_open()) throw std::runtime_error("couldnt open file"); if (!json_spirit::read_stream ( is, v )) throw std::runtime_error("couldnt load json"); } pobj r = ::convert ( v ); if ( !r ) throw wruntime_error ( L"Couldn't read input." ); if ( print ) dout << r->toString() << std::endl; return r; }
void arrayToString(string& str, const uint8_t*& parr) { uint32_t size = *((uint32_t*)parr); parr += 4; char* name_c_str = (char*)(parr); if (name_c_str[size] != '\0') { throw wruntime_error("arrayToString() - ERROR: " "string from array is not null terminated!"); } if (size > 0) { str = name_c_str; } else { } parr += size + 1; }
void Texture::loadImFromFile(const std::string& filename, uint8_t*& im, uint32_t& width, uint32_t& height, uint32_t& n_chan) { freeimage_init_lock_.lock(); if (!freeimage_init_) { freeimage_init_lock_.unlock(); throw std::wruntime_error("Texture::Texture() - ERROR: Please call " "initTextureSystem() before loading textures from file!"); } freeimage_init_lock_.unlock(); // Check if the file has any backslashes (these dont load on Mac OS X) std::string file = filename; size_t ind = file.find_first_of('\\'); while (ind != std::string::npos) { file[ind] = '/'; ind = file.find_first_of('\\'); } // NEW CODE USING THE FREEIMAGE LIBRARY FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //image format FIBITMAP* dib = NULL; //pointer to the image, once loaded BYTE* fi_bits = NULL; //pointer to the image data // check the file signature and deduce its format fif = FreeImage_GetFileType(file.c_str(), 0); // if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(file.c_str()); } // if still unkown, return failure if (fif == FIF_UNKNOWN) { throw wruntime_error(wstring(L"Texture() - ERROR: Cannot deduce format" L" of the file: ") + string_util::ToWideString(file)); } // check that FreeImage has reading capabilities and if so load the file if (FreeImage_FIFSupportsReading(fif)) { dib = FreeImage_Load(fif, file.c_str()); } //if the image failed to load, return failure if (!dib) { throw wruntime_error(wstring(L"Texture() - ERROR: FreeImage couldn't " L"load the file: ") + string_util::ToWideString(file)); } n_chan = FreeImage_GetBPP(dib) / 8; FreeImage_FlipVertical(dib); //retrieve the image data fi_bits = FreeImage_GetBits(dib); //get the image width and height width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); // if this somehow one of these failed (they shouldn't), return failure if ((fi_bits == 0) || (width == 0) || (height == 0)) { throw wruntime_error(wstring(L"Texture() - ERROR: FreeImage couldn't " L"load the file: ") + string_util::ToWideString(filename)); } // Copy it into memory and leave it there in case we need it later. im = new uint8_t[width * height * n_chan]; memcpy(im, fi_bits, sizeof(im[0]) * width * height * n_chan); // Unfortunately the R and B bits get flipped: // http://sourceforge.net/p/freeimage/bugs/172/ if (n_chan > 1) { uint8_t* tmp = new uint8_t[n_chan]; for (uint32_t v = 0; v < height; v++) { for (uint32_t u = 0; u < width; u++) { for (uint32_t i = 0; i < n_chan; i++) { tmp[n_chan - i - 1] = im[(v * width + u) * n_chan + i]; } for (uint32_t i = 0; i < n_chan; i++) { im[(v * width + u) * n_chan + i] = tmp[i]; } } } delete[] tmp; } }
// Load a texture from disk Texture::Texture(const std::string& filename, const TextureWrapMode wrap, const TextureFilterMode filter, const bool mip_map) { freeimage_init_lock_.lock(); if (!freeimage_init_) { freeimage_init_lock_.unlock(); throw std::wruntime_error("Texture::Texture() - ERROR: Please call " "initTextureSystem() before loading textures from file!"); } freeimage_init_lock_.unlock(); mip_map_ = mip_map; wrap_ = wrap; filter_ = filter; filename_ = filename; // Check if the file has any backslashes (these dont load on Mac OS X) size_t ind = filename_.find_first_of('\\'); while (ind != std::string::npos) { filename_[ind] = '/'; ind = filename_.find_first_of('\\'); } managed_ = false; from_file_ = true; generateTextureID(); // NEW CODE USING THE FREEIMAGE LIBRARY FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //image format FIBITMAP* dib = NULL; //pointer to the image, once loaded BYTE* fi_bits = NULL; //pointer to the image data // check the file signature and deduce its format fif = FreeImage_GetFileType(filename_.c_str(), 0); // if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(filename_.c_str()); } // if still unkown, return failure if (fif == FIF_UNKNOWN) { throw wruntime_error(wstring(L"Texture() - ERROR: Cannot deduce format" L" of the file: ") + string_util::ToWideString(filename_)); } // check that FreeImage has reading capabilities and if so load the file if (FreeImage_FIFSupportsReading(fif)) { dib = FreeImage_Load(fif, filename_.c_str()); } //if the image failed to load, return failure if (!dib) { throw wruntime_error(wstring(L"Texture() - ERROR: FreeImage couldn't " L"load the file: ") + string_util::ToWideString(filename_)); } // Convert everything to RGBA: unsigned int nbits = FreeImage_GetBPP(dib); if (nbits != 32) { FIBITMAP* old_dib = dib; dib = FreeImage_ConvertTo32Bits(old_dib); FreeImage_Unload(old_dib); } FreeImage_FlipVertical(dib); //retrieve the image data fi_bits = FreeImage_GetBits(dib); //get the image width and height w_ = FreeImage_GetWidth(dib); h_ = FreeImage_GetHeight(dib); // if this somehow one of these failed (they shouldn't), return failure if ((fi_bits == 0) || (w_ == 0) || (h_ == 0)) { throw wruntime_error(wstring(L"Texture() - ERROR: FreeImage couldn't " L"load the file: ") + string_util::ToWideString(filename)); } // Copy it into memory and leave it there in case we need it later. bits_ = new unsigned char[w_ * h_ * 4]; memcpy(bits_, fi_bits, sizeof(bits_[0]) * w_ * h_ * 4); format_internal_ = GL_RGBA; format_ = GL_BGRA; // FreeImage has bits flipped! type_ = GL_UNSIGNED_BYTE; dirty_data_ = true; sync(); // Sync anyway on startup (fixes some shutdown bugs) // Free FreeImage's copy of the data FreeImage_Unload(dib); // Unbind the texture so no one accidently makes changes to it GLState::glsBindTexture(GL_TEXTURE_2D, 0); }
bool Texture::saveImToFile(const std::string& filename, const uint8_t* im, const uint32_t width, const uint32_t height, const bool save_flipped, const uint32_t num_channels) { freeimage_init_lock_.lock(); if (!freeimage_init_) { freeimage_init_lock_.unlock(); throw std::wruntime_error("Texture::Texture() - ERROR: Please call " "initTextureSystem() before loading textures from file!"); } freeimage_init_lock_.unlock(); // NEW CODE USING THE FREEIMAGE LIBRARY FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; //image format // if still unknown, try to guess the file format from the file extension if (fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(filename.c_str()); } // if still unkown, return failure if (fif == FIF_UNKNOWN) { throw wruntime_error(wstring(L"saveRGBToFile() - ERROR: Cannot deduce " L"format of the filename: ") + string_util::ToWideString(filename)); } if (!FreeImage_FIFSupportsWriting(fif)) { throw std::wruntime_error("saveRGBToFile() - ERROR: Freeimage does not " "support writing to this image format!"); return false; } BYTE* fi_bits = NULL; uint8_t* im_rev = NULL; // Unfortunately, Freeimage has a bug: // http://sourceforge.net/p/freeimage/bugs/172/ // It ignores the mask properties and so the red and blue channels (24bpp) // get flipped. Unfortunately, we have to swap them around. // As of 4/26/2013 this issue still isn't fixed. switch (num_channels) { case 0: throw std::wruntime_error("saveImToFile() - ERROR: num_channels == 0"); case 1: fi_bits = (BYTE*)im; break; case 2: case 3: im_rev = new uint8_t[width * height * num_channels]; for (uint32_t i = 0; i < width * height * num_channels; i+=num_channels) { for (uint32_t j = 0; j < num_channels; j++) { im_rev[i + j] = im[i + (num_channels - 1 - j)]; } } fi_bits = (BYTE*)im_rev; break; default: throw std::wruntime_error("saveImToFile() - ERROR: num_channels > 0." " Saving images with alpha not yet supported."); } uint32_t pitch = num_channels * width; uint32_t bpp = 8 * num_channels; uint32_t red_mask = 0x0000FF; // Free image likes the mask backwards? uint32_t green_mask = 0x00FF00; uint32_t blue_mask = 0xFF0000; FIBITMAP* fi_bit_map = FreeImage_ConvertFromRawBits(fi_bits, width, height, pitch, bpp, red_mask, blue_mask, green_mask, save_flipped); bool ret = false; if (fi_bit_map) { ret = (bool)FreeImage_Save(fif, fi_bit_map, filename.c_str(), JPEG_QUALITYSUPERB); } if (fi_bit_map) { FreeImage_Unload(fi_bit_map); } SAFE_DELETE_ARR(im_rev); return ret; }
// Load a texture from disk TextureRenderable::TextureRenderable(const GLint internal_format, const int w, const int h, const GLint format, const GLint type, const uint32_t num_textures, const bool create_depth_texture, const TextureFilterMode filter) { filter_ = filter; internal_format_ = internal_format; screen_size_.set((float)w, (float)h); format_ = format; type_ = type; num_textures_ = num_textures; shared_depth_texture_ = false; #if defined(_DEBUG) || defined(DEBUG) if (num_textures == 0) { throw wruntime_error(L"TextureRenderable::TextureRenderable()" L" - ERROR: num_textures should not be zero"); } #endif // Create the FBO GLState::glsGenFramebuffers(1, &fbo_); GLState::glsBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_); // Create the openGL texture IDs textures_ = new GLuint[num_textures_]; GLState::glsGenTextures(num_textures, textures_); GLint filter_enum; switch (filter_) { case TEXTURE_NEAREST: filter_enum = GL_NEAREST; break; case TEXTURE_LINEAR: filter_enum = GL_LINEAR; break; default: throw std::wruntime_error("TextureRenderable::TextureRenderable() - " "ERROR: filter enum is not recognized!"); } // Create the openGL textures for (uint32_t i = 0; i < num_textures_; i ++) { GLState::glsBindTexture(GL_TEXTURE_2D, textures_[i]); GLState::glsTexImage2D(GL_TEXTURE_2D, 0, internal_format_ , w, h, 0, format_, type_, NULL); GLState::glsFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures_[i], 0); // No filtering for the render textures GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_enum); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_enum); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } if (!create_depth_texture) { depth_texture_ = 0; } else { GLState::glsGenTextures(1, &depth_texture_); GLState::glsBindTexture(GL_TEXTURE_2D, depth_texture_); GLState::glsTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_enum); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_enum); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); GLState::glsTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); GLState::glsFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture_, 0); } draw_buffers_ = new GLenum[num_textures_]; for (uint32_t i = 0; i < num_textures_; i ++) { draw_buffers_[i] = GL_COLOR_ATTACHMENT0 + i; } GLState::glsDrawBuffers(num_textures_, draw_buffers_); GLState::glsCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); // restore default FBO so no one accidently makes changes to ours GLState::glsBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); }