adding texture atlas class

This commit is contained in:
Kbz-8
2023-04-07 17:27:23 +02:00
parent f6c6b5e018
commit 5f16a98d08
9 changed files with 5230 additions and 22643 deletions

View File

@@ -5,8 +5,7 @@ A rewrite of School 42's MiniLibX using SDL2 and Vulkan. The goal of this versio
# Installation
## Linux
1. Necessary packages :
Dependances :
For Ubuntu/Debian
```bash
@@ -19,7 +18,12 @@ For Arch based distros
~ sudo pacman -S sdl2
```
2. Get MacroLibX
### MacOS
Dependances :
* [MoltenVK](https://github.com/KhronosGroup/MoltenVK/)
* SDL2 `brew install SDL2`
# Get MacroLibX
```bash
~ git clone https://github.com/420verfl0w/MacroLibX.git
@@ -27,7 +31,7 @@ For Arch based distros
~ make
```
3. Compile your project
# Compile your project
```bash
clang myApp.c MacroLibX/libmlx.so -lSDL2

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
/* Updated: 2023/04/02 23:50:31 by maldavid ### ########.fr */
/* Updated: 2023/04/07 14:23:01 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -67,7 +67,7 @@ namespace mlx
if(!_cpu_map->isMapped())
_cpu_map->mapMem(&_cpu_map_adress);
if(_cpu_map_adress == nullptr)
core::error::report(e_kind::fatal_error, "Texture : CPU memory mappind failed");
core::error::report(e_kind::fatal_error, "Texture : CPU memory mapping failed");
return _cpu_map_adress;
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* texture.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
/* Updated: 2023/04/02 22:36:52 by maldavid ### ########.fr */
/* Updated: 2023/04/07 16:39:35 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,7 @@
#include <filesystem>
#include <memory>
#include <array>
#include <functional>
#include <renderer/images/vk_image.h>
#include <renderer/descriptors/vk_descriptor_set.h>

75
src/renderer/images/texture_atlas.cpp git.filemode.normal_file
View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_atlas.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
/* Updated: 2023/04/07 17:07:46 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <renderer/images/texture_atlas.h>
namespace mlx
{
void TextureAtlas::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height)
{
Image::create(width, height, format,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
Image::createSampler();
std::vector<Vertex> vertexData = {
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 0.0f}},
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 0.0f}},
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 1.0f}},
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 1.0f}}
};
std::vector<uint16_t> indexData = { 0, 1, 2, 2, 3, 0 };
_vbo.create(sizeof(Vertex) * vertexData.size(), vertexData.data());
_ibo.create(sizeof(uint16_t) * indexData.size(), indexData.data());
if(pixels != nullptr)
{
Buffer staging_buffer;
std::size_t size = width * height * (format == VK_FORMAT_R32G32B32A32_SFLOAT ? 16 : 4);
staging_buffer.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, pixels);
Image::copyFromBuffer(staging_buffer);
staging_buffer.destroy();
}
}
void TextureAtlas::render(Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv)
{
auto cmd = renderer.getActiveCmdBuffer().get();
std::vector<Vertex> vertexData = {
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, uv[0]},
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, uv[1]},
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[2]},
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[3]}
};
_vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data());
_vbo.bind(renderer);
_ibo.bind(renderer);
glm::vec2 translate(x, y);
vkCmdPushConstants(cmd, renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate);
vkCmdDrawIndexed(cmd, static_cast<uint32_t>(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0);
}
void TextureAtlas::destroy() noexcept
{
Image::destroy();
_vbo.destroy();
_ibo.destroy();
}
}

46
src/renderer/images/texture_atlas.h git.filemode.normal_file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_atlas.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
/* Updated: 2023/04/07 16:44:43 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE_ATLAS__
#define __MLX_TEXTURE_ATLAS__
#include <renderer/images/texture.h>
#include <array>
#include <glm/glm.hpp>
namespace mlx
{
class TextureAtlas : public Image
{
public:
TextureAtlas() = default;
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height);
void render(class Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv);
void destroy() noexcept override;
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
~TextureAtlas() = default;
private:
VBO _vbo;
C_IBO _ibo;
DescriptorSet _set;
uint32_t _render_width;
uint32_t _render_height;
};
}
#endif

View File

@@ -6,24 +6,33 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/04/07 00:13:50 by maldavid ### ########.fr */
/* Updated: 2023/04/07 17:24:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <renderer/text_pipeline.h>
#include <fstream>
#define STB_TRUETYPE_IMPLEMENTATION
#include <std_truetype.h>
#include <stb_truetype.h>
namespace mlx
{
void TextPutPipeline::init(Renderer& renderer) noexcept
{
static uint8_t ttf_buffer[1 << 20];
static uint8_t tmp_bitmap[512 * 512];
void TextPutPipeline::init(Renderer* renderer) noexcept
{
_renderer = renderer;
}
void TextPutPipeline::put(int x, int y, int color, std::string str)
{
}
void TextPutPipeline::destroy() noexcept
{
_atlas.destroy();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2023/04/07 00:11:25 by maldavid ### ########.fr */
/* Updated: 2023/04/07 17:11:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
#define __MLX_TEXT_PIPELINE__
#include <renderer/renderer.h>
#include <renderer/images/texture.h>
#include <renderer/images/texture_atlas.h>
#include <string>
#include <stb_truetype.h>
#include <cstdint>
@@ -26,16 +26,16 @@ namespace mlx
public:
TextPutPipeline() = default;
void init(Renderer& renderer) noexcept;
void init(Renderer* renderer) noexcept;
void put(int x, int y, int color, std::string str);
void destroy() noexcept;
~TextPutPipeline() = default;
private:
uint8_t _ttf_buffer[1 << 20];
stbtt_bakedchar _cdata[96];
Texture _atlas;
TextureAtlas _atlas;
Renderer* _renderer = nullptr;
};
}

BIN
src/utils/opensans-regular.ttf git.filemode.normal_file

Binary file not shown.

File diff suppressed because one or more lines are too long