bool Texture::initWithData(int32_t format, const uint8_t * buffer, uint32_t width, uint32_t height, bool mipmaps, int pixelFormat) { destroy(); m_target = GL_TEXTURE_2D; m_format = format; m_pixelFormat = pixelFormat < 0 ? findPixelFormat(format) : pixelFormat; m_width = width; m_height = height; glGenTextures(1, &m_texture); glBindTexture(m_target, m_texture); int mipLevels = mipmaps ? getMipLevelsCount(m_width, m_height) : 1; glTexStorage2D(m_target, mipLevels, m_format, m_width, m_height); glTexSubImage2D(m_target, 0, 0, 0, m_width, m_height, m_pixelFormat, GL_UNSIGNED_BYTE, buffer); setSampling(); if (mipmaps) generateMipmaps(); glBindTexture(m_target, 0); if (glCheckError()) { destroy(); return false; } m_isLoaded = true; return m_isLoaded; }
bool Texture1D::loadFromMemory(UI32 width, I32 internalFormat, UI32 pixelFormat, UI32 pixelType, I32 minFilter, I32 magFilter, I32 wrapMode, const void* data, bool mipmap) { m_width = width; // Create a texture render::lib::genTextures(1, &m_texture); if (m_texture) { setFiltering(minFilter, magFilter); setWrapMode(wrapMode); bind(); render::lib::texImage1D(ODIN_TEXTURE_1D, 0, internalFormat, m_width, 0, pixelFormat, pixelType, data); unbind(); if (mipmap) generateMipmaps(); return true; } else return false; }
void Texture::setData(const unsigned char* data) { // Don't work with any compressed or cached textures GP_ASSERT( data ); GP_ASSERT( (!_compressed) ); GP_ASSERT( (!_cached) ); GL_ASSERT( glBindTexture((GLenum)_type, _handle) ); if (_type == Texture::TEXTURE_2D) { GL_ASSERT( glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, _internalFormat, _texelType, data) ); } else { // Get texture size unsigned int textureSize = _width * _height; textureSize *= _bpp; // Texture Cube for (unsigned int i = 0; i < 6; i++) { GL_ASSERT( glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, _width, _height, _internalFormat, _texelType, &data[i * textureSize]) ); } } if (_mipmapped) { generateMipmaps(); } // Restore the texture id GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) ); }
void Texture2D::autoGenerateMipmaps() { if(generateMipmap && image[0]->hasDirtyContents()) { generateMipmaps(); image[0]->markContentsClean(); } }
bool Cubemap::loadFromMemory(UI32 width, UI32 height, const void* dataPX, const void* dataNX, const void* dataPY, const void* dataNY, const void* dataPZ, const void* dataNZ, I32 internalFormat, UI32 pixelFormat, UI32 pixelType, I32 minFilter, I32 magFilter, I32 wrapMode, bool mipmap) { m_width = width; m_height = height; const void* data[6] = { dataPX, dataNX, dataPY, dataNY, dataPZ, dataNZ }; render::lib::genTextures(1, &m_texture); if (m_texture) { setFiltering(minFilter, magFilter); setWrapMode(wrapMode); bind(); for (UI32 i = 0; i < 6; ++i) { render::lib::texImage2D(ODIN_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, m_width, m_height, 0, pixelFormat, pixelType, data[i]); } unbind(); if (mipmap) generateMipmaps(); return true; } else return false; }
bool Texture::initAsArray(const std::vector<std::string> & filenames, bool mipmaps) { destroy(); m_arraySize = (uint32_t)filenames.size(); std::vector<uint8_t *> imageData; imageData.resize(m_arraySize, nullptr); auto cleanFunc = [&]() { for (uint32_t i = 0; i < m_arraySize; i++) if (imageData[i] != 0) stbi_image_free(imageData[i]); }; int width = 0, height = 0, components = 0; for (uint32_t i = 0; i < m_arraySize; i++) { int w, h, c; imageData[i] = stbi_load(filenames[i].c_str(), &w, &h, &c, 0); if (!imageData[i]) { common::Logger::toLogWithFormat("Error: could not load file '%s'.\n", filenames[i].c_str()); cleanFunc(); return false; } if (i > 0 && (width != w || height != h || components != c)) { common::Logger::toLog("Error: could not create a cubemap, files have different properties (width, height, components).\n"); cleanFunc(); return false; } else { width = w; height = h; components = c; } } m_format = findFormat(components); if (m_format == -1) { common::Logger::toLog("Error: texture format is unknown.\n"); cleanFunc(); return false; } m_target = GL_TEXTURE_2D_ARRAY; m_width = width; m_height = height; m_pixelFormat = findPixelFormat(m_format); glGenTextures(1, &m_texture); glBindTexture(m_target, m_texture); int mipLevels = mipmaps ? getMipLevelsCount(m_width, m_height) : 1; glTexStorage3D(m_target, mipLevels, m_format, m_width, m_height, m_arraySize); for (uint32_t i = 0; i < m_arraySize; i++) { glTexSubImage3D(m_target, 0, 0, 0, i, m_width, m_height, 1, m_pixelFormat, GL_UNSIGNED_BYTE, imageData[i]); } setSampling(); if (mipmaps) generateMipmaps(); glBindTexture(m_target, 0); cleanFunc(); if (glCheckError()) { destroy(); return false; } m_isLoaded = true; return m_isLoaded; }
bool Texture::initAsCubemap(const std::string& frontFilename, const std::string& backFilename, const std::string& leftFilename, const std::string& rightFilename, const std::string& topFilename, const std::string& bottomFilename, bool mipmaps) { destroy(); std::string filenames[6] = { rightFilename, leftFilename, topFilename, bottomFilename, frontFilename, backFilename }; uint8_t * imageData[6] = { 0, 0, 0, 0, 0, 0 }; auto cleanFunc = [&]() { for (size_t i = 0; i < 6; i++) if (imageData[i] != 0) stbi_image_free(imageData[i]); }; int width = 0, height = 0, components = 0; for (size_t i = 0; i < 6; i++) { int w, h, c; imageData[i] = stbi_load(filenames[i].c_str(), &w, &h, &c, 0); if (!imageData[i]) { common::Logger::toLogWithFormat("Error: could not load file '%s'.\n", filenames[i].c_str()); cleanFunc(); return false; } if (i > 0 && (width != w || height != h || components != c)) { common::Logger::toLog("Error: could not create a cubemap, files have different properties (width, height, components).\n"); cleanFunc(); return false; } else { width = w; height = h; components = c; } } m_format = findFormat(components); if (m_format == -1) { common::Logger::toLog("Error: texture format is unknown.\n"); cleanFunc(); return false; } m_target = GL_TEXTURE_CUBE_MAP; m_width = width; m_height = height; m_pixelFormat = findPixelFormat(m_format); glGenTextures(1, &m_texture); glBindTexture(m_target, m_texture); int mipLevels = mipmaps ? getMipLevelsCount(m_width, m_height) : 1; glTexStorage2D(m_target, mipLevels, m_format, m_width, m_height); for (uint32_t i = 0; i < 6; i++) { glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, m_width, m_height, m_pixelFormat, GL_UNSIGNED_BYTE, imageData[i]); } setSampling(); if (mipmaps) generateMipmaps(); glBindTexture(m_target, 0); cleanFunc(); if (glCheckError()) { destroy(); return false; } m_isLoaded = true; return m_isLoaded; }
Texture2DData::Texture2DData(int width, int height, const GLvoid* data) : BaseTextureData(GL_TEXTURE_2D, width, height) { setParameters(GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE, 8.0f); load(data, GL_TEXTURE_2D, GL_RGB); generateMipmaps(); }