Added graphics/ImageData class

This commit is contained in:
MihailRis
2023-11-12 03:26:06 +03:00
parent a91fbe7166
commit ffd3e3f852
9 changed files with 110 additions and 61 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1
unsigned char pixels[] = {
255, 255, 255, 255,
};
blank = new Texture(pixels, 1, 1);
blank = new Texture(pixels, 1, 1, GL_RGBA);
_texture = nullptr;
}
+1 -1
View File
@@ -19,7 +19,7 @@ Batch3D::Batch3D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1
unsigned char pixels[] = {
255, 255, 255, 255,
};
blank = new Texture(pixels, 1, 1);
blank = new Texture(pixels, 1, 1, GL_RGBA);
_texture = nullptr;
}
+14
View File
@@ -0,0 +1,14 @@
#include "ImageData.h"
ImageData::ImageData(ImageFormat format, uint width, uint height, void* data)
: format(format), width(width), height(height), data(data) {
}
ImageData::~ImageData() {
switch (format) {
case ImageFormat::rgb888:
case ImageFormat::rgba8888:
delete[] data;
break;
}
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef GRAPHICS_IMAGE_DATA_H_
#define GRAPHICS_IMAGE_DATA_H_
#include "../typedefs.h"
enum class ImageFormat {
rgb888,
rgba8888
};
class ImageData {
ImageFormat format;
uint width;
uint height;
void* data;
public:
ImageData(ImageFormat format, uint width, uint height, void* data);
~ImageData();
void* getData() const {
return data;
}
ImageFormat getFormat() const {
return format;
}
uint getWidth() const {
return width;
}
uint getHeight() const {
return height;
}
};
#endif // GRAPHICS_IMAGE_DATA_H_
+18 -4
View File
@@ -1,15 +1,18 @@
#include "Texture.h"
#include <GL/glew.h>
#include <stdexcept>
#include "ImageData.h"
Texture::Texture(unsigned int id, int width, int height) : id(id), width(width), height(height) {
}
Texture::Texture(unsigned char* data, int width, int height) : width(width), height(height) {
Texture::Texture(unsigned char* data, int width, int height, uint format) : width(width), height(height) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
@@ -21,7 +24,6 @@ Texture::~Texture() {
void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id);
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
}
void Texture::reload(unsigned char* data){
@@ -30,3 +32,15 @@ void Texture::reload(unsigned char* data){
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture* Texture::from(const ImageData* image) {
uint format;
const void* data = image->getData();
switch (image->getFormat()) {
case ImageFormat::rgb888: format = GL_RGB; break;
case ImageFormat::rgba8888: format = GL_RGBA; break;
default:
throw std::runtime_error("unsupported image data format");
}
return new Texture((unsigned char*)data, image->getWidth(), image->getHeight(), format);
}
+5 -1
View File
@@ -3,17 +3,21 @@
#include <string>
class ImageData;
class Texture {
public:
unsigned int id;
int width;
int height;
Texture(unsigned int id, int width, int height);
Texture(unsigned char* data, int width, int height);
Texture(unsigned char* data, int width, int height, uint format);
~Texture();
void bind();
void reload(unsigned char* data);
static Texture* from(const ImageData* image);
};
#endif /* GRAPHICS_TEXTURE_H_ */