Texture.readData
This commit is contained in:
parent
6430121cf2
commit
714a49685f
@ -1,52 +1,61 @@
|
|||||||
#include "Texture.h"
|
#include "Texture.h"
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "ImageData.h"
|
#include "ImageData.h"
|
||||||
|
|
||||||
Texture::Texture(uint id, int width, int height)
|
Texture::Texture(uint id, int width, int height)
|
||||||
: id(id), width(width), height(height) {
|
: id(id), width(width), height(height) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::Texture(ubyte* data, int width, int height, uint format)
|
Texture::Texture(ubyte* data, int width, int height, uint format)
|
||||||
: width(width), height(height) {
|
: width(width), height(height) {
|
||||||
glGenTextures(1, &id);
|
glGenTextures(1, &id);
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||||
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::~Texture() {
|
Texture::~Texture() {
|
||||||
glDeleteTextures(1, &id);
|
glDeleteTextures(1, &id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::bind(){
|
void Texture::bind(){
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::reload(ubyte* data){
|
void Texture::reload(ubyte* data){
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageData* Texture::readData() {
|
||||||
|
std::unique_ptr<ubyte[]> data (new ubyte[width * height * 4]);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
return new ImageData(ImageFormat::rgba8888, width, height, data.release());
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture* Texture::from(const ImageData* image) {
|
Texture* Texture::from(const ImageData* image) {
|
||||||
uint width = image->getWidth();
|
uint width = image->getWidth();
|
||||||
uint height = image->getHeight();
|
uint height = image->getHeight();
|
||||||
uint format;
|
uint format;
|
||||||
const void* data = image->getData();
|
const void* data = image->getData();
|
||||||
switch (image->getFormat()) {
|
switch (image->getFormat()) {
|
||||||
case ImageFormat::rgb888: format = GL_RGB; break;
|
case ImageFormat::rgb888: format = GL_RGB; break;
|
||||||
case ImageFormat::rgba8888: format = GL_RGBA; break;
|
case ImageFormat::rgba8888: format = GL_RGBA; break;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("unsupported image data format");
|
throw std::runtime_error("unsupported image data format");
|
||||||
}
|
}
|
||||||
return new Texture((ubyte*)data, width, height, format);
|
return new Texture((ubyte*)data, width, height, format);
|
||||||
}
|
}
|
||||||
@ -18,6 +18,8 @@ public:
|
|||||||
void bind();
|
void bind();
|
||||||
void reload(ubyte* data);
|
void reload(ubyte* data);
|
||||||
|
|
||||||
|
ImageData* readData();
|
||||||
|
|
||||||
static Texture* from(const ImageData* image);
|
static Texture* from(const ImageData* image);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user