world previews demo

This commit is contained in:
MihailRis
2024-04-23 01:09:30 +03:00
parent 5afa5304ff
commit 6e2bc9ca95
24 changed files with 153 additions and 59 deletions
+2 -2
View File
@@ -41,8 +41,8 @@ ImageData* Atlas::getImage() const {
return image.get();
}
void AtlasBuilder::add(std::string name, ImageData* image) {
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image)});
void AtlasBuilder::add(std::string name, std::unique_ptr<ImageData> image) {
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image.release())});
names.insert(name);
}
+1 -1
View File
@@ -46,7 +46,7 @@ class AtlasBuilder {
std::set<std::string> names;
public:
AtlasBuilder() {}
void add(std::string name, ImageData* image);
void add(std::string name, std::unique_ptr<ImageData> image);
bool has(const std::string& name) const;
const std::set<std::string>& getNames() { return names; };
+19 -14
View File
@@ -13,17 +13,10 @@ Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
width = 0;
height = 0;
}
}
}
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height)
{
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// Setup color attachment (texture)
static std::unique_ptr<Texture> create_texture(int width, int height, int format) {
GLuint tex;
format = alpha ? GL_RGBA : GL_RGB;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
@@ -32,7 +25,19 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
texture = std::make_unique<Texture>(tex, width, height);
return std::make_unique<Texture>(tex, width, height);
}
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height)
{
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
format = alpha ? GL_RGBA : GL_RGB;
// Setup color attachment (texture)
texture = create_texture(width, height, format);
// Setup depth attachment
glGenRenderbuffers(1, &depth);
@@ -64,11 +69,11 @@ void Framebuffer::resize(uint width, uint height) {
glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
texture->bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
texture->unbind();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
texture = create_texture(width, height, format);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Texture* Framebuffer::getTexture() const {
+8
View File
@@ -40,3 +40,11 @@ void PostProcessing::render(const GfxContext& context, Shader* screenShader) {
fbo->getTexture()->bind();
quadMesh->draw();
}
std::unique_ptr<ImageData> PostProcessing::toImage() {
return fbo->getTexture()->readData();
}
Framebuffer* PostProcessing::getFramebuffer() const {
return fbo.get();
}
+5
View File
@@ -32,6 +32,11 @@ public:
/// @param screenShader shader used for fullscreen quad
/// @throws std::runtime_error if use(...) wasn't called before
void render(const GfxContext& context, Shader* screenShader);
/// @brief Make an image from the last rendered frame
std::unique_ptr<ImageData> toImage();
Framebuffer* getFramebuffer() const;
};
#endif // GRAPHICS_CORE_POST_PROCESSING_H_
+2 -2
View File
@@ -49,12 +49,12 @@ void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, 0);
}
ImageData* Texture::readData() {
std::unique_ptr<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());
return std::make_unique<ImageData>(ImageFormat::rgba8888, width, height, data.release());
}
void Texture::setNearestFilter() {
+2 -1
View File
@@ -2,6 +2,7 @@
#define GRAPHICS_CORE_TEXTURE_H_
#include <string>
#include <memory>
#include "../../typedefs.h"
#include "ImageData.h"
@@ -23,7 +24,7 @@ public:
void setNearestFilter();
virtual ImageData* readData();
virtual std::unique_ptr<ImageData> readData();
virtual uint getWidth() const;
virtual uint getHeight() const;