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
+3 -3
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);
+5 -5
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);
+15 -13
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);
}
}
+5 -3
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_
+1 -2
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();
}
}
+2 -3
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) {
+3 -5
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) {