GL-related refactor
This commit is contained in:
@@ -3,38 +3,66 @@
|
||||
#include <GL/glew.h>
|
||||
#include "Texture.h"
|
||||
|
||||
Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
|
||||
: fbo(fbo), depth(depth), texture(std::move(texture))
|
||||
{
|
||||
if (texture) {
|
||||
width = texture->getWidth();
|
||||
height = texture->getHeight();
|
||||
} else {
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
: width(width), height(height)
|
||||
{
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
// Setup color attachment (texture)
|
||||
GLuint tex;
|
||||
GLuint format = alpha ? GL_RGBA : GL_RGB;
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
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);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
|
||||
texture = new Texture(tex, width, height);
|
||||
texture = std::make_unique<Texture>(tex, width, height);
|
||||
|
||||
// Setup depth attachment
|
||||
glGenRenderbuffers(1, &depth);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, depth);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
|
||||
unbind();
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
Framebuffer::~Framebuffer() {
|
||||
delete texture;
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteRenderbuffers(1, &depth);
|
||||
}
|
||||
|
||||
void Framebuffer::bind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
}
|
||||
|
||||
void Framebuffer::unbind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
Texture* Framebuffer::getTexture() const {
|
||||
return texture.get();
|
||||
}
|
||||
|
||||
uint Framebuffer::getWidth() const {
|
||||
return width;
|
||||
}
|
||||
|
||||
uint Framebuffer::getHeight() const {
|
||||
return height;
|
||||
}
|
||||
|
||||
@@ -3,20 +3,27 @@
|
||||
|
||||
#include "../typedefs.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Texture;
|
||||
|
||||
class Framebuffer {
|
||||
uint fbo;
|
||||
uint depth;
|
||||
public:
|
||||
uint width;
|
||||
uint height;
|
||||
Texture* texture;
|
||||
std::unique_ptr<Texture> texture;
|
||||
public:
|
||||
Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
|
||||
Texture* getTexture() const;
|
||||
uint getWidth() const;
|
||||
uint getHeight() const;
|
||||
};
|
||||
|
||||
#endif /* SRC_GRAPHICS_FRAMEBUFFER_H_ */
|
||||
|
||||
+70
-27
@@ -3,10 +3,16 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "Batch2D.h"
|
||||
#include "Framebuffer.h"
|
||||
|
||||
GfxContext::GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d)
|
||||
: parent(parent), viewport(viewport), g2d(g2d) {
|
||||
}
|
||||
GfxContext::GfxContext(
|
||||
const GfxContext* parent,
|
||||
const Viewport& viewport,
|
||||
Batch2D* g2d
|
||||
) : parent(parent),
|
||||
viewport(viewport),
|
||||
g2d(g2d)
|
||||
{}
|
||||
|
||||
GfxContext::~GfxContext() {
|
||||
while (scissorsCount--) {
|
||||
@@ -15,20 +21,39 @@ GfxContext::~GfxContext() {
|
||||
|
||||
if (parent == nullptr)
|
||||
return;
|
||||
|
||||
if (depthMask_ != parent->depthMask_) {
|
||||
glDepthMask(parent->depthMask_);
|
||||
|
||||
if (g2d) {
|
||||
g2d->flush();
|
||||
}
|
||||
if (depthTest_ != parent->depthTest_) {
|
||||
if (depthTest_) glDisable(GL_DEPTH_TEST);
|
||||
|
||||
if (fbo != parent->fbo) {
|
||||
if (fbo) {
|
||||
fbo->unbind();
|
||||
}
|
||||
if (parent->fbo) {
|
||||
fbo->bind();
|
||||
}
|
||||
}
|
||||
|
||||
Window::viewport(
|
||||
0, 0,
|
||||
parent->viewport.getWidth(),
|
||||
parent->viewport.getHeight()
|
||||
);
|
||||
|
||||
if (depthMask != parent->depthMask) {
|
||||
glDepthMask(parent->depthMask);
|
||||
}
|
||||
if (depthTest != parent->depthTest) {
|
||||
if (depthTest) glDisable(GL_DEPTH_TEST);
|
||||
else glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
if (cullFace_ != parent->cullFace_) {
|
||||
if (cullFace_) glDisable(GL_CULL_FACE);
|
||||
if (cullFace != parent->cullFace) {
|
||||
if (cullFace) glDisable(GL_CULL_FACE);
|
||||
else glEnable(GL_CULL_FACE);
|
||||
}
|
||||
if (blendMode_ != parent->blendMode_) {
|
||||
Window::setBlendMode(parent->blendMode_);
|
||||
if (blendMode != parent->blendMode) {
|
||||
Window::setBlendMode(parent->blendMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,22 +67,40 @@ Batch2D* GfxContext::getBatch2D() const {
|
||||
|
||||
GfxContext GfxContext::sub() const {
|
||||
auto ctx = GfxContext(this, viewport, g2d);
|
||||
ctx.depthTest_ = depthTest_;
|
||||
ctx.cullFace_ = cullFace_;
|
||||
ctx.depthTest = depthTest;
|
||||
ctx.cullFace = cullFace;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void GfxContext::depthMask(bool flag) {
|
||||
if (depthMask_ == flag)
|
||||
void GfxContext::setViewport(const Viewport& viewport) {
|
||||
this->viewport = viewport;
|
||||
Window::viewport(
|
||||
0, 0,
|
||||
viewport.getWidth(),
|
||||
viewport.getHeight()
|
||||
);
|
||||
}
|
||||
|
||||
void GfxContext::setFramebuffer(Framebuffer* fbo) {
|
||||
if (this->fbo == fbo)
|
||||
return;
|
||||
depthMask_ = flag;
|
||||
this->fbo = fbo;
|
||||
if (fbo) {
|
||||
fbo->bind();
|
||||
}
|
||||
}
|
||||
|
||||
void GfxContext::setDepthMask(bool flag) {
|
||||
if (depthMask == flag)
|
||||
return;
|
||||
depthMask = flag;
|
||||
glDepthMask(GL_FALSE + flag);
|
||||
}
|
||||
|
||||
void GfxContext::depthTest(bool flag) {
|
||||
if (depthTest_ == flag)
|
||||
void GfxContext::setDepthTest(bool flag) {
|
||||
if (depthTest == flag)
|
||||
return;
|
||||
depthTest_ = flag;
|
||||
depthTest = flag;
|
||||
if (flag) {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
} else {
|
||||
@@ -65,10 +108,10 @@ void GfxContext::depthTest(bool flag) {
|
||||
}
|
||||
}
|
||||
|
||||
void GfxContext::cullFace(bool flag) {
|
||||
if (cullFace_ == flag)
|
||||
void GfxContext::setCullFace(bool flag) {
|
||||
if (cullFace == flag)
|
||||
return;
|
||||
cullFace_ = flag;
|
||||
cullFace = flag;
|
||||
if (flag) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
} else {
|
||||
@@ -76,14 +119,14 @@ void GfxContext::cullFace(bool flag) {
|
||||
}
|
||||
}
|
||||
|
||||
void GfxContext::blendMode(blendmode mode) {
|
||||
if (blendMode_ == mode)
|
||||
void GfxContext::setBlendMode(blendmode mode) {
|
||||
if (blendMode == mode)
|
||||
return;
|
||||
blendMode_ = mode;
|
||||
blendMode = mode;
|
||||
Window::setBlendMode(mode);
|
||||
}
|
||||
|
||||
void GfxContext::scissors(glm::vec4 area) {
|
||||
void GfxContext::setScissors(glm::vec4 area) {
|
||||
Window::pushScissor(area);
|
||||
scissorsCount++;
|
||||
}
|
||||
|
||||
+15
-11
@@ -6,29 +6,33 @@
|
||||
#include "../window/Window.h"
|
||||
|
||||
class Batch2D;
|
||||
class Framebuffer;
|
||||
|
||||
class GfxContext {
|
||||
const GfxContext* parent;
|
||||
Viewport& viewport;
|
||||
Viewport viewport;
|
||||
Batch2D* const g2d;
|
||||
bool depthMask_ = true;
|
||||
bool depthTest_ = false;
|
||||
bool cullFace_ = false;
|
||||
blendmode blendMode_ = blendmode::normal;
|
||||
Framebuffer* fbo = nullptr;
|
||||
bool depthMask = true;
|
||||
bool depthTest = false;
|
||||
bool cullFace = false;
|
||||
blendmode blendMode = blendmode::normal;
|
||||
int scissorsCount = 0;
|
||||
public:
|
||||
GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d);
|
||||
GfxContext(const GfxContext* parent, const Viewport& viewport, Batch2D* g2d);
|
||||
~GfxContext();
|
||||
|
||||
Batch2D* getBatch2D() const;
|
||||
const Viewport& getViewport() const;
|
||||
GfxContext sub() const;
|
||||
|
||||
void depthMask(bool flag);
|
||||
void depthTest(bool flag);
|
||||
void cullFace(bool flag);
|
||||
void blendMode(blendmode mode);
|
||||
void scissors(glm::vec4 area);
|
||||
void setViewport(const Viewport& viewport);
|
||||
void setFramebuffer(Framebuffer* fbo);
|
||||
void setDepthMask(bool flag);
|
||||
void setDepthTest(bool flag);
|
||||
void setCullFace(bool flag);
|
||||
void setBlendMode(blendmode mode);
|
||||
void setScissors(glm::vec4 area);
|
||||
};
|
||||
|
||||
#endif // GRAPHICS_GFX_CONTEXT_H_
|
||||
@@ -3,13 +3,14 @@
|
||||
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
#include "../typedefs.h"
|
||||
|
||||
class GLSLExtension;
|
||||
|
||||
class Shader {
|
||||
uint id;
|
||||
public:
|
||||
static GLSLExtension* preprocessor;
|
||||
unsigned int id;
|
||||
|
||||
Shader(unsigned int id);
|
||||
~Shader();
|
||||
|
||||
@@ -5,17 +5,19 @@
|
||||
|
||||
#include "ImageData.h"
|
||||
|
||||
Texture::Texture(uint id, int width, int height)
|
||||
Texture::Texture(uint id, uint width, uint height)
|
||||
: id(id), width(width), height(height) {
|
||||
}
|
||||
|
||||
Texture::Texture(ubyte* data, int width, int height, uint format)
|
||||
Texture::Texture(ubyte* data, uint width, uint 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);
|
||||
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);
|
||||
@@ -66,3 +68,15 @@ Texture* Texture::from(const ImageData* image) {
|
||||
}
|
||||
return new Texture((ubyte*)data, width, height, format);
|
||||
}
|
||||
|
||||
uint Texture::getWidth() const {
|
||||
return width;
|
||||
}
|
||||
|
||||
uint Texture::getHeight() const {
|
||||
return height;
|
||||
}
|
||||
|
||||
uint Texture::getId() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
+15
-9
@@ -7,20 +7,26 @@
|
||||
class ImageData;
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
protected:
|
||||
uint id;
|
||||
int width;
|
||||
int height;
|
||||
Texture(uint id, int width, int height);
|
||||
Texture(ubyte* data, int width, int height, uint format);
|
||||
~Texture();
|
||||
uint width;
|
||||
uint height;
|
||||
public:
|
||||
Texture(uint id, uint width, uint height);
|
||||
Texture(ubyte* data, uint width, uint height, uint format);
|
||||
virtual ~Texture();
|
||||
|
||||
void bind();
|
||||
void reload(ubyte* data);
|
||||
virtual void bind();
|
||||
virtual void reload(ubyte* data);
|
||||
|
||||
void setNearestFilter();
|
||||
|
||||
ImageData* readData();
|
||||
virtual ImageData* readData();
|
||||
|
||||
virtual uint getWidth() const;
|
||||
virtual uint getHeight() const;
|
||||
|
||||
virtual uint getId() const;
|
||||
|
||||
static Texture* from(const ImageData* image);
|
||||
};
|
||||
|
||||
@@ -35,19 +35,23 @@ void TextureAnimator::update(float delta) {
|
||||
frame = elem.frames[elem.currentFrame];
|
||||
}
|
||||
if (frameNum != elem.currentFrame){
|
||||
if (changedTextures.find(elem.dstTexture->id) == changedTextures.end()) changedTextures.insert(elem.dstTexture->id);
|
||||
uint elemDstId = elem.dstTexture->getId();
|
||||
uint elemSrcId = elem.srcTexture->getId();
|
||||
|
||||
if (changedTextures.find(elemDstId) == changedTextures.end())
|
||||
changedTextures.insert(elemDstId);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboD);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.dstTexture->id, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elemDstId, 0);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fboR);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.srcTexture->id, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elemSrcId, 0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboD);
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboR);
|
||||
|
||||
float srcPosY = elem.srcTexture->height - frame.size.y - frame.srcPos.y; // vertical flip
|
||||
float srcPosY = elem.srcTexture->getHeight() - frame.size.y - frame.srcPos.y; // vertical flip
|
||||
|
||||
// Extensions
|
||||
const int ext = 2;
|
||||
|
||||
Reference in New Issue
Block a user