Select Git revision
Controller.java
Forked from
COUETOUX Basile / FirefighterStarter
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
TextureBuffer.cpp 2.77 KiB
#include "TextureBuffer.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include <stb/image/stb_image.h>
#include <iostream>
namespace megu {
TextureBuffer::TextureBuffer()
: _width(0), _height(0), _format(GL_RED) {}
TextureBuffer::TextureBuffer(GLuint width, GLuint height, GLubyte * data, GLenum format)
: _data(data, data + (width * height)), _width(width), _height(height), _format(format) {}
TextureBuffer::TextureBuffer(const TextureBuffer & src)
: _data(src._data), _width(src._width), _height(src._height) {}
TextureBuffer & TextureBuffer::operator=(const TextureBuffer & src) {
this->_data = src._data;
this->_width = src._width;
this->_height = src._height;
return *this;
}
void TextureBuffer::flipVerticaly() {
stbi__vertical_flip(this->_data.data(), this->_width, this->_height, TextureBuffer::BytePerPixel(this->_format));
}
void TextureBuffer::load(const GLubyte * data, size_t size) {
this->_data = std::vector<GLubyte>(data, data + size);
}
void TextureBuffer::load(const std::filesystem::path & path, bool flip) {
if(std::filesystem::is_directory(path) || !std::filesystem::exists(path)) {
throw std::runtime_error("Cannot load file : \"" + path.string() + "\"");
}
int width = 0, height = 0, nrChannels = 0;
stbi_set_flip_vertically_on_load(flip);
GLubyte * data = stbi_load(path.string().c_str(), &width, &height, &nrChannels, 0);
if(data == NULL) {
throw std::runtime_error("File is NULL : \"" + path.string() + "\"");
}
GLenum format = GL_RGB;
switch (nrChannels) {
case 4:
format = GL_RGBA;
break;
case 3:
format = GL_RGB;
break;
default:
format = GL_RGBA;
break;
}
this->_format = format;
this->_width = width;
this->_height = height;
this->load(data, width * height * nrChannels);
stbi_image_free(data);
stbi_set_flip_vertically_on_load(false);
}
void TextureBuffer::free() {
this->_width = 0;
this->_height = 0;
this->_format = GL_RED;
this->_data.clear();
}
int TextureBuffer::BytePerPixel(GLenum format) {
switch (format) {
case GL_ZERO:
return 0;
case GL_RED:
return 1;
case GL_ONE:
return 1;
case GL_RG:
return 2;
case GL_RGB:
return 3;
case GL_RGBA:
return 4;
default:
throw std::runtime_error("Unkwon byter per pixel for " + format);
}
}
}