void GlTexture:: reset(unsigned short width, unsigned short height, unsigned int pixelFormat, unsigned int internalFormat, unsigned type, void* data) { if (0==textureId) { GlException_SAFE_CALL( glGenTextures(1, &ownTextureId) ); textureId = ownTextureId; } this->width = width; this->height = height; if (0==width) return; bindTexture2D(); GlException_SAFE_CALL( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); GlException_SAFE_CALL( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); //GlException_SAFE_CALL( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) ); GlException_SAFE_CALL( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); GlException_SAFE_CALL( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); int gl_max_texture_size = 0; glGetIntegerv (GL_MAX_TEXTURE_SIZE, &gl_max_texture_size); EXCEPTION_ASSERT_LESS(width, gl_max_texture_size); EXCEPTION_ASSERT_LESS(height, gl_max_texture_size); GlException_SAFE_CALL( glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, pixelFormat, type, data) ); unbindTexture2D(); }
GLuint loadTextureFromPNGData(png_byte* image_data, int &width, int &height) { if (image_data == NULL) LOGE("loadTextureFromPNGData:Image data == NULL"); GLuint texture = genTexture(); bindTexture2D(texture); texImage2D(width, height, static_cast<GLvoid*>(image_data)); texParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); texParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST); return texture; }
GLuint loadTextureFromPNG(const ResourcePtr& pngData, int &width, int &height) { //Now generate the OpenGL texture object GLuint texture = genTexture(); bindTexture2D(texture); png_byte* image_data = loadImageFromPNG(pngData, width, height); // got this logic from android_source/frameworks/base/cmds/bootanimation/BootAnimation.cpp, // but it doesn't seem to work right. The odd-sized texture loads, but it is squished. I could probably fix // that, but I'm not going to worry about it for now. // int tw = 1 << (31 - __builtin_clz(width)); // int th = 1 << (31 - __builtin_clz(height)); // if (tw < width) tw <<= 1; // if (th < height) th <<= 1; // // if (tw != width || th != height) // { // GLint crop[4] = { 0, height, width, -height }; // texImage2D(tw, th, 0); // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(image_data)); // checkGLError("glTexSubImage2D"); // glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); // checkGLError("glTexParameteriv"); // } // else // { texImage2D(width, height, static_cast<GLvoid*>(image_data)); // } texParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); texParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST); // I thought these might help with random black pixels on the edge of a rotated texture, but they didn't. // texParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // texParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //clean up memory and close stuff //png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); //delete[] image_data; //delete[] row_pointers; delete[] image_data; return texture; }
GLuint loadTextureFromPKM(const ResourcePtr& pkmData, int &width, int &height) { #ifdef GL_ETC1_RGB8_OES const unsigned ETC_PKM_HEADER_SIZE = 16; if (pkmData->data().size() <= ETC_PKM_HEADER_SIZE) { LOGE("pkmData is not the right size. expected > %u, is: %zu", ETC_PKM_HEADER_SIZE, pkmData->data().size()); return TEXTURE_LOAD_ERROR; } etc1_byte* header = &pkmData->data()[0]; if (!etc1_pkm_is_valid(header)) return TEXTURE_LOAD_ERROR; width = etc1_pkm_get_width(header); height = etc1_pkm_get_height(header); size_t encodedDataSize = etc1_get_encoded_data_size(width, height); if (pkmData->data().size() != ETC_PKM_HEADER_SIZE + encodedDataSize) { LOGE("pkmData is not the right size. expected: %zu, is: %zu", ETC_PKM_HEADER_SIZE + encodedDataSize, pkmData->data().size()); return TEXTURE_LOAD_ERROR; } //Now generate the OpenGL texture object GLuint texture = genTexture(); bindTexture2D(texture); compressedTexImage2D(GL_ETC1_RGB8_OES, width, height, encodedDataSize, static_cast<GLvoid*> (&pkmData->data()[ETC_PKM_HEADER_SIZE])); texParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); texParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST); return texture; #else LOGE("PKM texture format not supported by this platform"); return TEXTURE_LOAD_ERROR; #endif }
GlTexture::ScopeBinding GlTexture::getScopeBinding() { bindTexture2D(); return ScopeBinding(*this, &GlTexture::unbindTexture2Dwrap); }