This commit is contained in:
@clasher113
2024-01-22 10:52:28 +02:00
25 changed files with 693 additions and 496 deletions
+4 -2
View File
@@ -3,13 +3,15 @@
#include <GL/glew.h>
#include "Texture.h"
Framebuffer::Framebuffer(uint width, uint height) : width(width), height(height) {
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height) {
glGenFramebuffers(1, &fbo);
bind();
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
GLuint format = alpha ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+1 -1
View File
@@ -12,7 +12,7 @@ public:
uint width;
uint height;
Texture* texture;
Framebuffer(uint width, uint height);
Framebuffer(uint width, uint height, bool alpha=false);
~Framebuffer();
void bind();
+39 -30
View File
@@ -1,52 +1,61 @@
#include "Texture.h"
#include <GL/glew.h>
#include <stdexcept>
#include <memory>
#include "ImageData.h"
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)
: width(width), height(height) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
glBindTexture(GL_TEXTURE_2D, 0);
: width(width), height(height) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture() {
glDeleteTextures(1, &id);
glDeleteTextures(1, &id);
}
void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id);
glBindTexture(GL_TEXTURE_2D, id);
}
void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
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) {
uint width = image->getWidth();
uint height = image->getHeight();
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((ubyte*)data, width, height, format);
}
uint width = image->getWidth();
uint height = image->getHeight();
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((ubyte*)data, width, height, format);
}
+11 -9
View File
@@ -8,17 +8,19 @@ class ImageData;
class Texture {
public:
uint id;
int width;
int height;
Texture(uint id, int width, int height);
Texture(ubyte* data, int width, int height, uint format);
~Texture();
uint id;
int width;
int height;
Texture(uint id, int width, int height);
Texture(ubyte* data, int width, int height, uint format);
~Texture();
void bind();
void reload(ubyte* data);
void bind();
void reload(ubyte* data);
static Texture* from(const ImageData* image);
ImageData* readData();
static Texture* from(const ImageData* image);
};
#endif /* GRAPHICS_TEXTURE_H_ */