38 if (!LoadFromPNG(file_path)) {
39 LOGE(
"Texture initialing error");
44 bool Texture::LoadFromPNG(
const char* file_path) {
45 FILE* file = fopen(file_path,
"rb");
48 LOGE(
"fp not loaded: %s", strerror(errno));
52 fseek(file, 8, SEEK_CUR);
55 png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
56 png_infop info_ptr = png_create_info_struct(png_ptr);
58 png_init_io(png_ptr, file);
59 png_set_sig_bytes(png_ptr, 8);
60 png_read_info(png_ptr, info_ptr);
69 png_bytep* row_pointers =
new png_bytep[height_];
70 for (
uint i = 0; i < height_; ++i) {
71 row_pointers[i] = (png_bytep)(byte_data_ + i * row);
73 png_read_image(png_ptr, row_pointers);
74 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
83 if (color_type_ == PNG_COLOR_TYPE_RGBA) {
84 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA,
85 GL_UNSIGNED_BYTE, byte_data_);
87 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB,
88 GL_UNSIGNED_BYTE, byte_data_);
91 glBindTexture(GL_TEXTURE_2D, 0);
94 delete[] row_pointers;
GLM_FUNC_DECL genType::row_type row(genType const &m, length_t const &index)
Texture(const char *file_path)
void CheckGlError(const char *operation)
static int RoundUpPowerOfTwo(int w)
GLuint GetTextureID() const
static const int kMaxExponentiation