GL-related refactor

This commit is contained in:
MihailRis
2024-03-07 13:09:58 +03:00
parent f8e02e1b3b
commit 87edb1d45e
16 changed files with 224 additions and 111 deletions
+11 -5
View File
@@ -136,7 +136,7 @@ bool assetload::font(
} }
pages.push_back(std::move(texture)); pages.push_back(std::move(texture));
} }
int res = pages[0]->height / 16; int res = pages[0]->getHeight() / 16;
assets->store(new Font(std::move(pages), res, 4), name); assets->store(new Font(std::move(pages), res, 4), name);
return true; return true;
} }
@@ -270,13 +270,19 @@ static bool animation(
Frame frame; Frame frame;
UVRegion region = dstAtlas->get(name); UVRegion region = dstAtlas->get(name);
frame.dstPos = glm::ivec2(region.u1 * dstTex->width, region.v1 * dstTex->height); uint dstWidth = dstTex->getWidth();
frame.size = glm::ivec2(region.u2 * dstTex->width, region.v2 * dstTex->height) - frame.dstPos; uint dstHeight = dstTex->getHeight();
uint srcWidth = srcTex->getWidth();
uint srcHeight = srcTex->getHeight();
frame.dstPos = glm::ivec2(region.u1 * dstWidth, region.v1 * dstHeight);
frame.size = glm::ivec2(region.u2 * dstWidth, region.v2 * dstHeight) - frame.dstPos;
if (frameList.empty()) { if (frameList.empty()) {
for (const auto& elem : builder.getNames()) { for (const auto& elem : builder.getNames()) {
region = srcAtlas->get(elem); region = srcAtlas->get(elem);
frame.srcPos = glm::ivec2(region.u1 * srcTex->width, srcTex->height - region.v2 * srcTex->height); frame.srcPos = glm::ivec2(region.u1 * srcWidth, srcHeight - region.v2 * srcHeight);
animation.addFrame(frame); animation.addFrame(frame);
} }
} }
@@ -288,7 +294,7 @@ static bool animation(
} }
region = srcAtlas->get(elem.first); region = srcAtlas->get(elem.first);
frame.duration = elem.second; frame.duration = elem.second;
frame.srcPos = glm::ivec2(region.u1 * srcTex->width, srcTex->height - region.v2 * srcTex->height); frame.srcPos = glm::ivec2(region.u1 * srcWidth, srcHeight - region.v2 * srcHeight);
animation.addFrame(frame); animation.addFrame(frame);
} }
} }
+3 -3
View File
@@ -95,7 +95,7 @@ ImageData* BlocksPreview::draw(
break; break;
} }
} }
return fbo->texture->readData(); return fbo->getTexture()->readData();
} }
std::unique_ptr<Atlas> BlocksPreview::build( std::unique_ptr<Atlas> BlocksPreview::build(
@@ -113,8 +113,8 @@ std::unique_ptr<Atlas> BlocksPreview::build(
Viewport viewport(iconSize, iconSize); Viewport viewport(iconSize, iconSize);
GfxContext pctx(nullptr, viewport, nullptr); GfxContext pctx(nullptr, viewport, nullptr);
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
ctx.cullFace(true); ctx.setCullFace(true);
ctx.depthTest(true); ctx.setDepthTest(true);
Framebuffer fbo(iconSize, iconSize, true); Framebuffer fbo(iconSize, iconSize, true);
Batch3D batch(1024); Batch3D batch(1024);
+5 -5
View File
@@ -159,8 +159,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
Shader* linesShader = assets->getShader("lines"); Shader* linesShader = assets->getShader("lines");
{ {
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
ctx.depthTest(true); ctx.setDepthTest(true);
ctx.cullFace(true); ctx.setCullFace(true);
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance-2); float fogFactor = 15.0f / ((float)settings.chunks.loadDistance-2);
@@ -224,7 +224,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
if (hudVisible && player->debug) { if (hudVisible && player->debug) {
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
ctx.depthTest(true); ctx.setDepthTest(true);
linesShader->use(); linesShader->use();
@@ -248,14 +248,14 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
0.f, (float)displayHeight, 0.f, (float)displayHeight,
-length, length) * model * glm::inverse(camera->rotation)); -length, length) * model * glm::inverse(camera->rotation));
ctx.depthTest(false); ctx.setDepthTest(false);
lineBatch->lineWidth(4.0f); lineBatch->lineWidth(4.0f);
lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 1.f);
lineBatch->render(); lineBatch->render();
ctx.depthTest(true); ctx.setDepthTest(true);
lineBatch->lineWidth(2.0f); lineBatch->lineWidth(2.0f);
lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f);
+14 -12
View File
@@ -8,6 +8,8 @@
#include "../../graphics/Shader.h" #include "../../graphics/Shader.h"
#include "../../graphics/Mesh.h" #include "../../graphics/Mesh.h"
#include "../../graphics/Batch3D.h" #include "../../graphics/Batch3D.h"
#include "../../graphics/Texture.h"
#include "../../graphics/Framebuffer.h"
#include "../../window/Window.h" #include "../../window/Window.h"
#include "../../window/Camera.h" #include "../../window/Camera.h"
@@ -19,9 +21,9 @@ const int STARS_COUNT = 3000;
const int STARS_SEED = 632; const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader) Skybox::Skybox(uint size, Shader* shader)
: size(size), : size(size),
shader(shader), shader(shader),
batch3d(new Batch3D(4096)) batch3d(std::make_unique<Batch3D>(4096))
{ {
glGenTextures(1, &cubemap); glGenTextures(1, &cubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
@@ -33,7 +35,10 @@ Skybox::Skybox(uint size, Shader* shader)
for (uint face = 0; face < 6; face++) { for (uint face = 0; face < 6; face++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
} }
glGenFramebuffers(1, &fbo);
uint fboid;
glGenFramebuffers(1, &fboid);
fbo = std::make_unique<Framebuffer>(fboid, 0, (std::unique_ptr<Texture>)nullptr);
float vertices[] { float vertices[] {
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
@@ -59,7 +64,6 @@ Skybox::Skybox(uint size, Shader* shader)
Skybox::~Skybox() { Skybox::~Skybox() {
glDeleteTextures(1, &cubemap); glDeleteTextures(1, &cubemap);
glDeleteFramebuffers(1, &fbo);
} }
void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) { void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) {
@@ -109,7 +113,7 @@ void Skybox::draw(
drawBackground(camera, assets, width, height); drawBackground(camera, assets, width, height);
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
ctx.blendMode(blendmode::addition); ctx.setBlendMode(blendmode::addition);
Shader* shader = assets->getShader("ui3d"); Shader* shader = assets->getShader("ui3d");
shader->use(); shader->use();
@@ -141,15 +145,15 @@ void Skybox::draw(
void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) { void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) {
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
ctx.depthMask(false); ctx.setDepthMask(false);
ctx.depthTest(false); ctx.setDepthTest(false);
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
ready = true; ready = true;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
shader->use(); shader->use();
Window::viewport(0,0, size, size);
const glm::vec3 xaxs[] = { const glm::vec3 xaxs[] = {
{0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f},
@@ -194,8 +198,6 @@ void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) {
} }
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Window::viewport(0, 0, Window::width, Window::height);
} }
void Skybox::bind() const { void Skybox::bind() const {
+4 -2
View File
@@ -13,6 +13,7 @@ class Shader;
class Assets; class Assets;
class Camera; class Camera;
class Batch3D; class Batch3D;
class Framebuffer;
struct skysprite { struct skysprite {
std::string texture; std::string texture;
@@ -22,7 +23,7 @@ struct skysprite {
}; };
class Skybox { class Skybox {
uint fbo; std::unique_ptr<Framebuffer> fbo;
uint cubemap; uint cubemap;
uint size; uint size;
Shader* shader; Shader* shader;
@@ -44,7 +45,8 @@ public:
Camera* camera, Camera* camera,
Assets* assets, Assets* assets,
float daytime, float daytime,
float fog); float fog
);
void refresh(const GfxContext& pctx, float t, float mie, uint quality); void refresh(const GfxContext& pctx, float t, float mie, uint quality);
void bind() const; void bind() const;
+1 -2
View File
@@ -88,12 +88,11 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
batch->flush(); batch->flush();
{ {
GfxContext ctx = pctx->sub(); GfxContext ctx = pctx->sub();
ctx.scissors(glm::vec4(pos.x, pos.y, size.x, size.y)); ctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
for (auto node : nodes) { for (auto node : nodes) {
if (node->isVisible()) if (node->isVisible())
node->draw(pctx, assets); node->draw(pctx, assets);
} }
batch->flush();
} }
} }
+2 -3
View File
@@ -204,7 +204,7 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
auto texture = assets->getTexture(this->texture); auto texture = assets->getTexture(this->texture);
if (texture && autoresize) { if (texture && autoresize) {
setSize(glm::vec2(texture->width, texture->height)); setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
} }
batch->texture(texture); batch->texture(texture);
batch->setColor(color); batch->setColor(color);
@@ -383,7 +383,7 @@ void TextBox::draw(const GfxContext* pctx, Assets* assets) {
glm::vec2 size = getSize(); glm::vec2 size = getSize();
auto subctx = pctx->sub(); auto subctx = pctx->sub();
subctx.scissors(glm::vec4(pos.x, pos.y, size.x, size.y)); subctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
const int lineHeight = font->getLineHeight() * label->getLineInterval(); const int lineHeight = font->getLineHeight() * label->getLineInterval();
glm::vec2 lcoord = label->calcPos(); glm::vec2 lcoord = label->calcPos();
@@ -418,7 +418,6 @@ void TextBox::draw(const GfxContext* pctx, Assets* assets) {
batch->rect(lcoord.x, lcoord.y+label->getLineYOffset(endLine), end, lineHeight); batch->rect(lcoord.x, lcoord.y+label->getLineYOffset(endLine), end, lineHeight);
} }
} }
batch->flush();
} }
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) { void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
+3 -5
View File
@@ -481,18 +481,16 @@ void Hud::draw(const GfxContext& ctx){
// Crosshair // Crosshair
if (!pause && !inventoryOpen && !player->debug) { if (!pause && !inventoryOpen && !player->debug) {
GfxContext chctx = ctx.sub(); GfxContext chctx = ctx.sub();
chctx.blendMode(blendmode::inversion); chctx.setBlendMode(blendmode::inversion);
auto texture = assets->getTexture("gui/crosshair"); auto texture = assets->getTexture("gui/crosshair");
batch->texture(texture); batch->texture(texture);
int chsizex = texture != nullptr ? texture->width : 16; int chsizex = texture != nullptr ? texture->getWidth() : 16;
int chsizey = texture != nullptr ? texture->height : 16; int chsizey = texture != nullptr ? texture->getHeight() : 16;
batch->rect( batch->rect(
(width-chsizex)/2, (height-chsizey)/2, (width-chsizex)/2, (height-chsizey)/2,
chsizex, chsizey, 0,0, 1,1, 1,1,1,1 chsizex, chsizey, 0,0, 1,1, 1,1,1,1
); );
batch->flush();
} }
batch->flush();
} }
void Hud::updateElementsPosition(const Viewport& viewport) { void Hud::updateElementsPosition(const Viewport& viewport) {
+41 -13
View File
@@ -3,38 +3,66 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "Texture.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) Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height) { : width(width), height(height)
glGenFramebuffers(1, &fbo); {
bind(); glGenFramebuffers(1, &fbo);
GLuint tex; glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex); // Setup color attachment (texture)
GLuint tex;
GLuint format = alpha ? GL_RGBA : GL_RGB; 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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 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); 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); glGenRenderbuffers(1, &depth);
glBindRenderbuffer(GL_RENDERBUFFER, depth); glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
unbind(); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
Framebuffer::~Framebuffer() { Framebuffer::~Framebuffer() {
delete texture; glDeleteFramebuffers(1, &fbo);
glDeleteFramebuffers(1, &fbo);
glDeleteRenderbuffers(1, &depth); glDeleteRenderbuffers(1, &depth);
} }
void Framebuffer::bind() { void Framebuffer::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo);
} }
void Framebuffer::unbind() { 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;
} }
+9 -2
View File
@@ -3,20 +3,27 @@
#include "../typedefs.h" #include "../typedefs.h"
#include <memory>
class Texture; class Texture;
class Framebuffer { class Framebuffer {
uint fbo; uint fbo;
uint depth; uint depth;
public:
uint width; uint width;
uint height; 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(uint width, uint height, bool alpha=false);
~Framebuffer(); ~Framebuffer();
void bind(); void bind();
void unbind(); void unbind();
Texture* getTexture() const;
uint getWidth() const;
uint getHeight() const;
}; };
#endif /* SRC_GRAPHICS_FRAMEBUFFER_H_ */ #endif /* SRC_GRAPHICS_FRAMEBUFFER_H_ */
+69 -26
View File
@@ -3,10 +3,16 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "Batch2D.h" #include "Batch2D.h"
#include "Framebuffer.h"
GfxContext::GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d) GfxContext::GfxContext(
: parent(parent), viewport(viewport), g2d(g2d) { const GfxContext* parent,
} const Viewport& viewport,
Batch2D* g2d
) : parent(parent),
viewport(viewport),
g2d(g2d)
{}
GfxContext::~GfxContext() { GfxContext::~GfxContext() {
while (scissorsCount--) { while (scissorsCount--) {
@@ -16,19 +22,38 @@ GfxContext::~GfxContext() {
if (parent == nullptr) if (parent == nullptr)
return; return;
if (depthMask_ != parent->depthMask_) { if (g2d) {
glDepthMask(parent->depthMask_); 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); else glEnable(GL_DEPTH_TEST);
} }
if (cullFace_ != parent->cullFace_) { if (cullFace != parent->cullFace) {
if (cullFace_) glDisable(GL_CULL_FACE); if (cullFace) glDisable(GL_CULL_FACE);
else glEnable(GL_CULL_FACE); else glEnable(GL_CULL_FACE);
} }
if (blendMode_ != parent->blendMode_) { if (blendMode != parent->blendMode) {
Window::setBlendMode(parent->blendMode_); Window::setBlendMode(parent->blendMode);
} }
} }
@@ -42,22 +67,40 @@ Batch2D* GfxContext::getBatch2D() const {
GfxContext GfxContext::sub() const { GfxContext GfxContext::sub() const {
auto ctx = GfxContext(this, viewport, g2d); auto ctx = GfxContext(this, viewport, g2d);
ctx.depthTest_ = depthTest_; ctx.depthTest = depthTest;
ctx.cullFace_ = cullFace_; ctx.cullFace = cullFace;
return ctx; return ctx;
} }
void GfxContext::depthMask(bool flag) { void GfxContext::setViewport(const Viewport& viewport) {
if (depthMask_ == flag) this->viewport = viewport;
Window::viewport(
0, 0,
viewport.getWidth(),
viewport.getHeight()
);
}
void GfxContext::setFramebuffer(Framebuffer* fbo) {
if (this->fbo == fbo)
return; 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); glDepthMask(GL_FALSE + flag);
} }
void GfxContext::depthTest(bool flag) { void GfxContext::setDepthTest(bool flag) {
if (depthTest_ == flag) if (depthTest == flag)
return; return;
depthTest_ = flag; depthTest = flag;
if (flag) { if (flag) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
} else { } else {
@@ -65,10 +108,10 @@ void GfxContext::depthTest(bool flag) {
} }
} }
void GfxContext::cullFace(bool flag) { void GfxContext::setCullFace(bool flag) {
if (cullFace_ == flag) if (cullFace == flag)
return; return;
cullFace_ = flag; cullFace = flag;
if (flag) { if (flag) {
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
} else { } else {
@@ -76,14 +119,14 @@ void GfxContext::cullFace(bool flag) {
} }
} }
void GfxContext::blendMode(blendmode mode) { void GfxContext::setBlendMode(blendmode mode) {
if (blendMode_ == mode) if (blendMode == mode)
return; return;
blendMode_ = mode; blendMode = mode;
Window::setBlendMode(mode); Window::setBlendMode(mode);
} }
void GfxContext::scissors(glm::vec4 area) { void GfxContext::setScissors(glm::vec4 area) {
Window::pushScissor(area); Window::pushScissor(area);
scissorsCount++; scissorsCount++;
} }
+15 -11
View File
@@ -6,29 +6,33 @@
#include "../window/Window.h" #include "../window/Window.h"
class Batch2D; class Batch2D;
class Framebuffer;
class GfxContext { class GfxContext {
const GfxContext* parent; const GfxContext* parent;
Viewport& viewport; Viewport viewport;
Batch2D* const g2d; Batch2D* const g2d;
bool depthMask_ = true; Framebuffer* fbo = nullptr;
bool depthTest_ = false; bool depthMask = true;
bool cullFace_ = false; bool depthTest = false;
blendmode blendMode_ = blendmode::normal; bool cullFace = false;
blendmode blendMode = blendmode::normal;
int scissorsCount = 0; int scissorsCount = 0;
public: public:
GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d); GfxContext(const GfxContext* parent, const Viewport& viewport, Batch2D* g2d);
~GfxContext(); ~GfxContext();
Batch2D* getBatch2D() const; Batch2D* getBatch2D() const;
const Viewport& getViewport() const; const Viewport& getViewport() const;
GfxContext sub() const; GfxContext sub() const;
void depthMask(bool flag); void setViewport(const Viewport& viewport);
void depthTest(bool flag); void setFramebuffer(Framebuffer* fbo);
void cullFace(bool flag); void setDepthMask(bool flag);
void blendMode(blendmode mode); void setDepthTest(bool flag);
void scissors(glm::vec4 area); void setCullFace(bool flag);
void setBlendMode(blendmode mode);
void setScissors(glm::vec4 area);
}; };
#endif // GRAPHICS_GFX_CONTEXT_H_ #endif // GRAPHICS_GFX_CONTEXT_H_
+2 -1
View File
@@ -3,13 +3,14 @@
#include <string> #include <string>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "../typedefs.h"
class GLSLExtension; class GLSLExtension;
class Shader { class Shader {
uint id;
public: public:
static GLSLExtension* preprocessor; static GLSLExtension* preprocessor;
unsigned int id;
Shader(unsigned int id); Shader(unsigned int id);
~Shader(); ~Shader();
+18 -4
View File
@@ -5,17 +5,19 @@
#include "ImageData.h" #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) { : 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) { : 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(
format, GL_UNSIGNED_BYTE, (GLvoid *) data); 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_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);
@@ -66,3 +68,15 @@ Texture* Texture::from(const ImageData* image) {
} }
return new Texture((ubyte*)data, width, height, format); 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
View File
@@ -7,20 +7,26 @@
class ImageData; class ImageData;
class Texture { class Texture {
public: protected:
uint id; uint id;
int width; uint width;
int height; uint height;
Texture(uint id, int width, int height); public:
Texture(ubyte* data, int width, int height, uint format); Texture(uint id, uint width, uint height);
~Texture(); Texture(ubyte* data, uint width, uint height, uint format);
virtual ~Texture();
void bind(); virtual void bind();
void reload(ubyte* data); virtual void reload(ubyte* data);
void setNearestFilter(); 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); static Texture* from(const ImageData* image);
}; };
+8 -4
View File
@@ -35,19 +35,23 @@ void TextureAnimator::update(float delta) {
frame = elem.frames[elem.currentFrame]; frame = elem.frames[elem.currentFrame];
} }
if (frameNum != 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); 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); 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_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboD); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboD);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboR); 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 // Extensions
const int ext = 2; const int ext = 2;