void ParticleTexture::loadTexture(const std::string& path){ tTGA tga; if (!load_TGA(&tga, path.c_str())){ throw std::runtime_error("ERROR: Could not load particle texture!"); } glGetError(); glGenTextures(1, &_textureID); glBindTexture(GL_TEXTURE_2D, _textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D( GL_TEXTURE_2D, 0, (tga.alpha) ? (GL_RGB) : (GL_RGB8), tga.width, tga.height, 0, (tga.alpha) ? (GL_RGBA) : (GL_RGB), GL_UNSIGNED_BYTE, tga.data ); if(glGetError() != GL_NO_ERROR){ glDeleteTextures(1, &_textureID); free_TGA(&tga); throw std::runtime_error("ERROR: Could not initialize particle texture!"); } }
Image* load_image(char *source, char(*get_byte)(char *source)) { Image *image=new(Image); load_TGA(source, get_byte, image); if(image->data) return image; free(image); return 0; }
void Texture::init_TGA(GLuint *tex, std::vector<unsigned char>& data, const char* filename) { data = load_TGA(filename); glDeleteTextures(1, tex); glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]); }
// loads texture data from specified file // TODO: wrap it in a workitem class, for the job system deferred loading void opengl_texture::load() { if( type == "make:" ) { // for generated texture we delay data creation until texture is bound // this ensures the script will receive all simulation data it might potentially want // as any binding will happen after simulation is loaded, initialized and running // until then we supply data for a tiny 2x2 grey stub make_stub(); } else { WriteLog( "Loading texture data from \"" + name + type + "\"", logtype::texture ); data_state = resource_state::loading; if( type == ".dds" ) { load_DDS(); } else if( type == ".tga" ) { load_TGA(); } else if( type == ".png" ) { load_PNG(); } else if( type == ".bmp" ) { load_BMP(); } else if( type == ".tex" ) { load_TEX(); } else { goto fail; } } // data state will be set by called loader, so we're all done here if( data_state == resource_state::good ) { has_alpha = ( data_components == GL_RGBA ? true : false ); size = data.size() / 1024; return; } fail: data_state = resource_state::failed; ErrorLog( "Bad texture: failed to load texture \"" + name + type + "\"" ); // NOTE: temporary workaround for texture assignment errors id = 0; return; }