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

View File

@ -136,7 +136,7 @@ bool assetload::font(
}
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);
return true;
}
@ -270,13 +270,19 @@ static bool animation(
Frame frame;
UVRegion region = dstAtlas->get(name);
frame.dstPos = glm::ivec2(region.u1 * dstTex->width, region.v1 * dstTex->height);
frame.size = glm::ivec2(region.u2 * dstTex->width, region.v2 * dstTex->height) - frame.dstPos;
uint dstWidth = dstTex->getWidth();
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()) {
for (const auto& elem : builder.getNames()) {
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);
}
}
@ -288,7 +294,7 @@ static bool animation(
}
region = srcAtlas->get(elem.first);
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);
}
}

View File

@ -95,7 +95,7 @@ ImageData* BlocksPreview::draw(
break;
}
}
return fbo->texture->readData();
return fbo->getTexture()->readData();
}
std::unique_ptr<Atlas> BlocksPreview::build(
@ -113,8 +113,8 @@ std::unique_ptr<Atlas> BlocksPreview::build(
Viewport viewport(iconSize, iconSize);
GfxContext pctx(nullptr, viewport, nullptr);
GfxContext ctx = pctx.sub();
ctx.cullFace(true);
ctx.depthTest(true);
ctx.setCullFace(true);
ctx.setDepthTest(true);
Framebuffer fbo(iconSize, iconSize, true);
Batch3D batch(1024);

View File

@ -159,8 +159,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
Shader* linesShader = assets->getShader("lines");
{
GfxContext ctx = pctx.sub();
ctx.depthTest(true);
ctx.cullFace(true);
ctx.setDepthTest(true);
ctx.setCullFace(true);
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) {
GfxContext ctx = pctx.sub();
ctx.depthTest(true);
ctx.setDepthTest(true);
linesShader->use();
@ -248,14 +248,14 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
0.f, (float)displayHeight,
-length, length) * model * glm::inverse(camera->rotation));
ctx.depthTest(false);
ctx.setDepthTest(false);
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, 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->render();
ctx.depthTest(true);
ctx.setDepthTest(true);
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, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f);

View File

@ -8,6 +8,8 @@
#include "../../graphics/Shader.h"
#include "../../graphics/Mesh.h"
#include "../../graphics/Batch3D.h"
#include "../../graphics/Texture.h"
#include "../../graphics/Framebuffer.h"
#include "../../window/Window.h"
#include "../../window/Camera.h"
@ -19,9 +21,9 @@ const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader)
: size(size),
shader(shader),
batch3d(new Batch3D(4096))
: size(size),
shader(shader),
batch3d(std::make_unique<Batch3D>(4096))
{
glGenTextures(1, &cubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
@ -33,7 +35,10 @@ Skybox::Skybox(uint size, Shader* shader)
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);
}
glGenFramebuffers(1, &fbo);
uint fboid;
glGenFramebuffers(1, &fboid);
fbo = std::make_unique<Framebuffer>(fboid, 0, (std::unique_ptr<Texture>)nullptr);
float vertices[] {
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
@ -59,7 +64,6 @@ Skybox::Skybox(uint size, Shader* shader)
Skybox::~Skybox() {
glDeleteTextures(1, &cubemap);
glDeleteFramebuffers(1, &fbo);
}
void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) {
@ -109,7 +113,7 @@ void Skybox::draw(
drawBackground(camera, assets, width, height);
GfxContext ctx = pctx.sub();
ctx.blendMode(blendmode::addition);
ctx.setBlendMode(blendmode::addition);
Shader* shader = assets->getShader("ui3d");
shader->use();
@ -141,15 +145,15 @@ void Skybox::draw(
void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) {
GfxContext ctx = pctx.sub();
ctx.depthMask(false);
ctx.depthTest(false);
ctx.setDepthMask(false);
ctx.setDepthTest(false);
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
ready = true;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
shader->use();
Window::viewport(0,0, size, size);
const glm::vec3 xaxs[] = {
{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);
glActiveTexture(GL_TEXTURE0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Window::viewport(0, 0, Window::width, Window::height);
}
void Skybox::bind() const {
@ -208,4 +210,4 @@ void Skybox::unbind() const {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
glActiveTexture(GL_TEXTURE0);
}
}

View File

@ -13,6 +13,7 @@ class Shader;
class Assets;
class Camera;
class Batch3D;
class Framebuffer;
struct skysprite {
std::string texture;
@ -22,7 +23,7 @@ struct skysprite {
};
class Skybox {
uint fbo;
std::unique_ptr<Framebuffer> fbo;
uint cubemap;
uint size;
Shader* shader;
@ -44,7 +45,8 @@ public:
Camera* camera,
Assets* assets,
float daytime,
float fog);
float fog
);
void refresh(const GfxContext& pctx, float t, float mie, uint quality);
void bind() const;
@ -54,4 +56,4 @@ public:
}
};
#endif // FRONTEND_GRAPHICS_SKYBOX_H_
#endif // FRONTEND_GRAPHICS_SKYBOX_H_

View File

@ -88,12 +88,11 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
batch->flush();
{
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) {
if (node->isVisible())
node->draw(pctx, assets);
}
batch->flush();
}
}

View File

@ -204,7 +204,7 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
auto texture = assets->getTexture(this->texture);
if (texture && autoresize) {
setSize(glm::vec2(texture->width, texture->height));
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
}
batch->texture(texture);
batch->setColor(color);
@ -383,7 +383,7 @@ void TextBox::draw(const GfxContext* pctx, Assets* assets) {
glm::vec2 size = getSize();
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();
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->flush();
}
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {

View File

@ -481,18 +481,16 @@ void Hud::draw(const GfxContext& ctx){
// Crosshair
if (!pause && !inventoryOpen && !player->debug) {
GfxContext chctx = ctx.sub();
chctx.blendMode(blendmode::inversion);
chctx.setBlendMode(blendmode::inversion);
auto texture = assets->getTexture("gui/crosshair");
batch->texture(texture);
int chsizex = texture != nullptr ? texture->width : 16;
int chsizey = texture != nullptr ? texture->height : 16;
int chsizex = texture != nullptr ? texture->getWidth() : 16;
int chsizey = texture != nullptr ? texture->getHeight() : 16;
batch->rect(
(width-chsizex)/2, (height-chsizey)/2,
chsizex, chsizey, 0,0, 1,1, 1,1,1,1
);
batch->flush();
}
batch->flush();
}
void Hud::updateElementsPosition(const Viewport& viewport) {

View File

@ -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;
}

View File

@ -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_ */

View File

@ -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++;
}

View File

@ -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_

View File

@ -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();

View File

@ -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;
}

View File

@ -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);
};

View File

@ -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;