1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : m_path(path) { X_PROFILE_FUNCTION(); int width, height, channels; stbi_set_flip_vertically_on_load(true); stbi_uc* data{nullptr}; { X_PROFILE_SCOPE("stbi_load - OpenGLTexture2D::OpenGLTexture2D(const std::string&)"); data = stbi_load(path.c_str(), &width, &height, &channels, 0); } X_CORE_ASSERT(data, "Failed to load image"); m_isLoaded = true; m_width = width; m_height = height;
GLenum internalFormat = 0, dataFormat = 0; if (channels == 4) { internalFormat = GL_RGBA8; dataFormat = GL_RGBA; } else if (channels == 3) { internalFormat = GL_RGB8; dataFormat = GL_RGB; } glGenTextures(1, &m_rendererId); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_rendererId);
m_internalFormat = internalFormat; m_dataFormat = dataFormat; X_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!"); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_width, m_height, 0, dataFormat, GL_UNSIGNED_BYTE, data ); stbi_image_free(data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0); }
|