mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 09:59:44 +00:00
Merge branch 'MihailRis:main' into main
This commit is contained in:
commit
7536ac2736
130 changed files with 2932 additions and 1028 deletions
|
|
@ -4,7 +4,7 @@
|
|||
#include <GL/glew.h>
|
||||
|
||||
Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
|
||||
: Texture(0, width, height)
|
||||
: Texture(0U, width, height, imageFormat)
|
||||
{
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
|
||||
|
|
@ -14,7 +14,6 @@ Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
|
|||
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
uint format = gl::to_glenum(imageFormat);
|
||||
for (uint face = 0; face < 6; face++) {
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
|
||||
|
|
@ -37,3 +36,23 @@ void Cubemap::bind() const {
|
|||
void Cubemap::unbind() const {
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
||||
void Cubemap::reload(const ubyte* data, uint width, uint height) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
|
||||
for (uint face = 0; face < 6; face++) {
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
|
||||
0,
|
||||
format,
|
||||
width,
|
||||
height,
|
||||
0,
|
||||
format,
|
||||
GL_UNSIGNED_BYTE,
|
||||
data
|
||||
);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,4 +9,5 @@ public:
|
|||
|
||||
virtual void bind() const override;
|
||||
virtual void unbind() const override;
|
||||
virtual void reload(const ubyte* data, uint w, uint h) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -74,6 +74,18 @@ DrawContext::~DrawContext() {
|
|||
if (lineWidth != parent->lineWidth) {
|
||||
glLineWidth(parent->lineWidth);
|
||||
}
|
||||
|
||||
for (int i = 0; i < textures.size(); i++) {
|
||||
auto texture = parent->textures[i];
|
||||
if (textures[i] != texture) {
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
if (texture == nullptr) {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
} else {
|
||||
texture->bind();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const glm::uvec2& DrawContext::getViewport() const {
|
||||
|
|
@ -154,3 +166,13 @@ void DrawContext::setLineWidth(float width) {
|
|||
lineWidth = width;
|
||||
glLineWidth(width);
|
||||
}
|
||||
|
||||
void DrawContext::useTexture(int target, const Bindable* texture) {
|
||||
glActiveTexture(GL_TEXTURE0 + target);
|
||||
if (texture == nullptr) {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
} else {
|
||||
texture->bind();
|
||||
}
|
||||
textures.at(target) = texture;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ class DrawContext {
|
|||
BlendMode blendMode = BlendMode::normal;
|
||||
int scissorsCount = 0;
|
||||
float lineWidth = 1.0f;
|
||||
std::array<const Bindable*, advanced_pipeline::TARGETS_COUNT> textures {};
|
||||
public:
|
||||
DrawContext(
|
||||
const DrawContext* parent,
|
||||
|
|
@ -44,4 +46,5 @@ public:
|
|||
void setBlendMode(BlendMode mode);
|
||||
void setScissors(const glm::vec4& area);
|
||||
void setLineWidth(float width);
|
||||
void useTexture(int target, const Bindable* texture);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -26,6 +26,11 @@ Font::Font(
|
|||
pages(std::move(pages)),
|
||||
glyphs(std::move(glyphs)),
|
||||
fontFile(std::move(fontFile)) {
|
||||
if (this->fontFile.has_value()) {
|
||||
if (auto fontPtr = this->fontFile->lock()) {
|
||||
monospace = fontPtr->isMonospace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Font::~Font() = default;
|
||||
|
|
@ -34,6 +39,10 @@ int Font::getYOffset() const {
|
|||
return yoffset;
|
||||
}
|
||||
|
||||
bool Font::isMonospace() const {
|
||||
return monospace;
|
||||
}
|
||||
|
||||
int Font::getLineHeight() const {
|
||||
return lineHeight;
|
||||
}
|
||||
|
|
@ -53,7 +62,7 @@ bool Font::isPrintableChar(uint codepoint) const {
|
|||
|
||||
int FontMetrics::calcWidth(std::wstring_view text, size_t offset, size_t length) const {
|
||||
auto font = this->font.has_value() ? this->font->lock() : nullptr;
|
||||
if (font == nullptr) {
|
||||
if (font == nullptr || font->isMonospace()) {
|
||||
return std::min(text.length() - offset, length) * _glyphInterval;
|
||||
}
|
||||
int totalWidth = 0;
|
||||
|
|
@ -153,6 +162,8 @@ static inline void draw_text(
|
|||
const FontStylesScheme* styles,
|
||||
size_t styleMapOffset
|
||||
) {
|
||||
bool is3d = std::is_same<Batch, Batch3D>();
|
||||
|
||||
static FontStylesScheme defStyles {{{}}, {0}};
|
||||
|
||||
if (styles == nullptr) {
|
||||
|
|
@ -185,13 +196,26 @@ static inline void draw_text(
|
|||
float advance = baseAdvance;
|
||||
if (auto glyph = font.getGlyph(c)) {
|
||||
yOffset = glyph->yOffset;
|
||||
advance = glyph->xAdvance;
|
||||
advance = glyph->xAdvance /
|
||||
static_cast<float>(font.getLineHeight()) * 2.0f *
|
||||
baseAdvance;
|
||||
}
|
||||
uint charpage = c >> 8;
|
||||
if (charpage == page){
|
||||
batch.texture(font.getPage(charpage));
|
||||
draw_glyph(
|
||||
batch, pos, glm::vec2(x, y - yOffset / static_cast<float>(font.getLineHeight())), c, right, up, interval, style
|
||||
batch,
|
||||
pos,
|
||||
glm::vec2(
|
||||
x,
|
||||
y - yOffset * (is3d ? -1 : 1) /
|
||||
static_cast<float>(font.getLineHeight())
|
||||
),
|
||||
c,
|
||||
right,
|
||||
up,
|
||||
interval,
|
||||
style
|
||||
);
|
||||
}
|
||||
else if (charpage > page && charpage < next){
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ public:
|
|||
);
|
||||
~Font();
|
||||
|
||||
bool isMonospace() const;
|
||||
int getLineHeight() const;
|
||||
int getYOffset() const;
|
||||
|
||||
|
|
@ -94,6 +95,7 @@ private:
|
|||
int lineHeight;
|
||||
int yoffset;
|
||||
int glyphInterval;
|
||||
bool monospace = true;
|
||||
std::vector<std::unique_ptr<Texture>> pages;
|
||||
std::vector<Glyph> glyphs;
|
||||
std::optional<std::weak_ptr<vector_fonts::FontFile>> fontFile;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
#include "Framebuffer.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "Texture.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
static debug::Logger logger("gl-framebuffer");
|
||||
|
||||
Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
|
||||
: fbo(fbo), depth(depth), texture(std::move(texture))
|
||||
{
|
||||
: fbo(fbo), depth(depth), texture(std::move(texture)) {
|
||||
if (this->texture) {
|
||||
width = this->texture->getWidth();
|
||||
height = this->texture->getHeight();
|
||||
|
|
@ -18,35 +18,55 @@ Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
|
|||
}
|
||||
}
|
||||
|
||||
static std::unique_ptr<Texture> create_texture(int width, int height, int format) {
|
||||
GLuint tex;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
static std::unique_ptr<Texture> create_texture(
|
||||
int width, int height, bool alpha
|
||||
) {
|
||||
GLenum glformat = alpha ? GL_RGBA : GL_RGB;
|
||||
|
||||
GLuint textureid;
|
||||
glGenTextures(1, &textureid);
|
||||
glBindTexture(GL_TEXTURE_2D, textureid);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
glformat,
|
||||
width,
|
||||
height,
|
||||
0,
|
||||
glformat,
|
||||
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);
|
||||
return std::make_unique<Texture>(tex, width, height);
|
||||
glFramebufferTexture2D(
|
||||
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureid, 0
|
||||
);
|
||||
return std::make_unique<Texture>(
|
||||
textureid,
|
||||
width,
|
||||
height,
|
||||
alpha ? ImageFormat::RGBA8888 : ImageFormat::RGB888
|
||||
);
|
||||
}
|
||||
|
||||
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
||||
: width(width), height(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);
|
||||
texture = create_texture(width, height, alpha);
|
||||
|
||||
// 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);
|
||||
glFramebufferRenderbuffer(
|
||||
GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth
|
||||
);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
logger.error() << "framebuffer is not complete!";
|
||||
|
|
@ -60,11 +80,15 @@ Framebuffer::~Framebuffer() {
|
|||
glDeleteRenderbuffers(1, &depth);
|
||||
}
|
||||
|
||||
void Framebuffer::bind() {
|
||||
void Framebuffer::setTexture(std::unique_ptr<Texture> texture) {
|
||||
this->texture = std::move(texture);
|
||||
}
|
||||
|
||||
void Framebuffer::bind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
}
|
||||
|
||||
void Framebuffer::unbind() {
|
||||
void Framebuffer::unbind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +105,7 @@ void Framebuffer::resize(uint width, uint height) {
|
|||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
|
||||
texture = create_texture(width, height, format);
|
||||
texture->resize(width, height);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
|
|
@ -102,5 +126,5 @@ uint Framebuffer::getHeight() const {
|
|||
}
|
||||
|
||||
uint Framebuffer::getFBO() const {
|
||||
return fbo;
|
||||
return fbo;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,18 +12,19 @@ class Framebuffer : public Bindable {
|
|||
uint depth;
|
||||
uint width;
|
||||
uint height;
|
||||
uint format;
|
||||
std::shared_ptr<Texture> texture;
|
||||
public:
|
||||
Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
|
||||
void setTexture(std::unique_ptr<Texture> texture);
|
||||
|
||||
/// @brief Use framebuffer
|
||||
void bind() override;
|
||||
void bind() const override;
|
||||
|
||||
/// @brief Stop using framebuffer
|
||||
void unbind() override;
|
||||
void unbind() const override;
|
||||
|
||||
/// @brief Update framebuffer texture size
|
||||
/// @param width new width
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ GBuffer::~GBuffer() {
|
|||
glDeleteFramebuffers(1, &ssaoFbo);
|
||||
}
|
||||
|
||||
void GBuffer::bind() {
|
||||
void GBuffer::bind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
|
@ -117,7 +117,7 @@ void GBuffer::bindSSAO() const {
|
|||
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFbo);
|
||||
}
|
||||
|
||||
void GBuffer::unbind() {
|
||||
void GBuffer::unbind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ public:
|
|||
GBuffer(uint width, uint height);
|
||||
~GBuffer() override;
|
||||
|
||||
void bind() override;
|
||||
void bind() const override;
|
||||
void bindSSAO() const;
|
||||
void unbind() override;
|
||||
void unbind() const override;
|
||||
|
||||
void bindBuffers() const;
|
||||
void bindSSAOBuffer() const;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
|
||||
uint Texture::MAX_RESOLUTION = 1024; // Window.initialize overrides it
|
||||
|
||||
Texture::Texture(uint id, uint width, uint height)
|
||||
: id(id), width(width), height(height) {
|
||||
Texture::Texture(uint id, uint width, uint height, ImageFormat imageFormat)
|
||||
: id(id), width(width), height(height), format(gl::to_glenum(imageFormat)) {
|
||||
}
|
||||
|
||||
Texture::Texture(const ubyte* data, uint width, uint height, ImageFormat imageFormat)
|
||||
|
|
@ -17,7 +17,7 @@ Texture::Texture(const ubyte* data, uint width, uint height, ImageFormat imageFo
|
|||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
GLenum format = gl::to_glenum(imageFormat);
|
||||
format = gl::to_glenum(imageFormat);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data)
|
||||
|
|
@ -41,23 +41,27 @@ void Texture::unbind() const {
|
|||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::resize(uint width, uint height) {
|
||||
reload(nullptr, width, height);
|
||||
}
|
||||
|
||||
void Texture::reload(const ImageData& image) {
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
reload(image.getData(), width, height);
|
||||
}
|
||||
|
||||
void Texture::reload(const ubyte* data, uint width, uint height) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data));
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data));
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h) {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.getData());
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, format, GL_UNSIGNED_BYTE, image.getData());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,32 @@
|
|||
#pragma once
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "ImageData.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Texture {
|
||||
#include "ImageData.hpp"
|
||||
#include "commons.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "typedefs.hpp"
|
||||
|
||||
class Texture : public Bindable {
|
||||
protected:
|
||||
uint id;
|
||||
uint width;
|
||||
uint height;
|
||||
uint format;
|
||||
public:
|
||||
Texture(uint id, uint width, uint height);
|
||||
Texture(uint id, uint width, uint height, ImageFormat imageFormat);
|
||||
Texture(const ubyte* data, uint width, uint height, ImageFormat format);
|
||||
virtual ~Texture();
|
||||
|
||||
virtual void bind() const;
|
||||
virtual void unbind() const;
|
||||
void reload(const ubyte* data, uint w, uint h);
|
||||
virtual void bind() const override;
|
||||
virtual void unbind() const override;
|
||||
virtual void reload(const ubyte* data, uint w, uint h);
|
||||
void reloadPartial(const ImageData& image, uint x, uint y, uint w, uint h);
|
||||
|
||||
void setNearestFilter();
|
||||
|
||||
void reload(const ImageData& image);
|
||||
virtual void resize(uint w, uint h);
|
||||
|
||||
void setMipMapping(bool flag, bool pixelated);
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ namespace advanced_pipeline {
|
|||
inline constexpr int TARGET_SSAO = 5;
|
||||
inline constexpr int TARGET_SHADOWS0 = 6;
|
||||
inline constexpr int TARGET_SHADOWS1 = 7;
|
||||
|
||||
inline constexpr int TARGETS_COUNT = 8;
|
||||
}
|
||||
|
||||
VC_ENUM_METADATA(CursorShape)
|
||||
|
|
@ -85,6 +87,6 @@ class Bindable {
|
|||
public:
|
||||
virtual ~Bindable() = default;
|
||||
|
||||
virtual void bind() = 0;
|
||||
virtual void unbind() = 0;
|
||||
virtual void bind() const = 0;
|
||||
virtual void unbind() const = 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue