diff --git a/.gitignore b/.gitignore index e2a14a608ced625ac1a1bcd11c3e85b904f63ba7..a3b8f73e6f65bf6fb18a7afbacdc8c38420a38d7 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,11 @@ *.exe *.git +*.ttf build/* include/* library/* vcpkg_installed/* -old/* \ No newline at end of file +old/* +old2/* \ No newline at end of file diff --git a/assets/shaders/TileArray.frag b/assets/shaders/TileArray.frag new file mode 100644 index 0000000000000000000000000000000000000000..7b71e440beb473547c927c7647ff2a0668fa6aa3 --- /dev/null +++ b/assets/shaders/TileArray.frag @@ -0,0 +1,18 @@ +#version 450 core +out vec4 FragColor; + +uniform sampler2D uSlot; + +uniform vec4 uUvs[128]; +uniform vec2 uSize; + +in vec2 Texture; +flat in int Id; + +void main() { + vec2 coord = vec2( + (uUvs[Id].x / uSize.x) + (uUvs[Id].z / uSize.x) * Texture.x, + (uUvs[Id].y / uSize.y) + (uUvs[Id].w / uSize.y) * Texture.y + ); + FragColor = texture(uSlot, coord); +} \ No newline at end of file diff --git a/assets/shaders/TileArray.vert b/assets/shaders/TileArray.vert new file mode 100644 index 0000000000000000000000000000000000000000..a0e01e2c12099bde0854e1adbbafee85472d654b --- /dev/null +++ b/assets/shaders/TileArray.vert @@ -0,0 +1,21 @@ +#version 450 core +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec2 aTex; + +uniform mat4 uModel; +uniform mat4 uView; +uniform mat4 uProj; + +uniform mat4 uOffsets[128]; + +out vec2 Texture; +flat out int Id; + +void main() { + Texture = aTex; + Id = gl_InstanceID; + vec2 pos = vec2(aPos.x, aPos.y); + + + gl_Position = uProj * uView * uModel * uOffsets[gl_InstanceID] * vec4(pos, 0.0, 1.0); +} \ No newline at end of file diff --git a/assets/textures/Tile_Test.png b/assets/textures/Tile_Test.png index d25e9e0324403cbccc67e1a0eed3fdbb9b63e694..c113642b3f04ce076c806252f8a3f93109b54b5a 100644 Binary files a/assets/textures/Tile_Test.png and b/assets/textures/Tile_Test.png differ diff --git a/assets/textures/Tile_Test_4.png b/assets/textures/Tile_Test_4.png new file mode 100644 index 0000000000000000000000000000000000000000..b5db7d7c8c3da0f2ec9178caf6403d59193535d8 Binary files /dev/null and b/assets/textures/Tile_Test_4.png differ diff --git a/source/engine/graphics/front/geometry/Vertex.hpp b/source/engine/graphics/front/geometry/Vertex.hpp deleted file mode 100644 index 4c3eed9b6badb0cc0441f3094d7700fd439fd20d..0000000000000000000000000000000000000000 --- a/source/engine/graphics/front/geometry/Vertex.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include <glm/glm.hpp> - -namespace megu { - struct Vertex { - glm::vec2 _position; - glm::vec2 _texture; - }; -} \ No newline at end of file diff --git a/source/engine/graphics/front/module/Quad_Module.hpp b/source/engine/graphics/front/module/Quad_Module.hpp index cb8f305deb2fcfbe3130971b4c8d753572396170..5822de04afe111811c6a51352138eddcb991ce92 100644 --- a/source/engine/graphics/front/module/Quad_Module.hpp +++ b/source/engine/graphics/front/module/Quad_Module.hpp @@ -3,9 +3,10 @@ #include "Image_Module.hpp" #include "Sprite_Module.hpp" #include "Text_Module.hpp" +#include "TileArray_Module.hpp" namespace megu { - class Quad_Module : public Image_Module, public Sprite_Module, public Text_Module { + class Quad_Module : public Image_Module, public Sprite_Module, public Text_Module, public TileArray_Module { // ... }; } \ No newline at end of file diff --git a/source/engine/graphics/front/module/Sprite_Module.cpp b/source/engine/graphics/front/module/Sprite_Module.cpp index 2975d5ceeed5ee05b801722fa3c50c6a8208fb83..691983f0b5357b7e93309ecbefd061640bc57827 100644 --- a/source/engine/graphics/front/module/Sprite_Module.cpp +++ b/source/engine/graphics/front/module/Sprite_Module.cpp @@ -74,7 +74,7 @@ namespace megu { this->_program.setUniform("uFrames", uFrames); this->_program.setUniform("uSizes", uSizes); - glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); + glDrawArraysInstanced(Sprite::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); } } } \ No newline at end of file diff --git a/source/engine/graphics/front/module/TileArray_Module.cpp b/source/engine/graphics/front/module/TileArray_Module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b88a30dc6e86bc7c7c8574e9b59e4f3c54eb7ca4 --- /dev/null +++ b/source/engine/graphics/front/module/TileArray_Module.cpp @@ -0,0 +1,63 @@ +#include "TileArray_Module.hpp" + +#include <engine/graphics/back/cameras/Camera.hpp> +#include <glm/gtc/matrix_transform.hpp> + +namespace megu { + TileArray_Module::TileArray_Module() + : _vbo(this->_vao, TileArray::Layout(), TileArray::Vertices(), EditMode::STATIC) { + Source vert_source("assets/shaders/TileArray.vert", Source::Categorie::VERTEX); + this->_program << vert_source; + + Source frag_source("assets/shaders/TileArray.frag", Source::Categorie::FRAGMENT); + this->_program << frag_source; + + this->_program.link(); + vert_source.release(); + frag_source.release(); + } + + void TileArray_Module::draw(const TileArray & vertexArray, const Camera & camera, const Window &) const { + this->_vao.bind(); + this->_program.use(); + + this->_program.setUniform("uProj", camera.projection()); + this->_program.setUniform("uView", camera.view()); + this->_program.setUniform("uModel", vertexArray.transformation().model()); + + vertexArray.texture().bind(); + this->_program.setUniform("uSlot", 0); + this->_program.setUniform("uSize", glm::vec2{vertexArray.texture().width(), vertexArray.texture().height()}); + + std::vector<glm::mat4> uOffsets = {}; + std::vector<glm::vec4> uUvs = {}; + + size_t count = 0; + + for(size_t y = 0; y < vertexArray.height(); ++y) { + for(size_t x = 0; x < vertexArray.width(); ++x) { + uOffsets.push_back(glm::translate(glm::mat4(1.f), {x, y, 0.f})); + uUvs.push_back(vertexArray.uvs()[y][x]); + + if(count > 128) { + this->_program.setUniform("uOffsets", uOffsets); + this->_program.setUniform("uUvs", uUvs); + glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.width() * vertexArray.height())); + + uOffsets.clear(); + uUvs.clear(); + count = 0; + } + ++count; + } + } + + if(count != 0) { + this->_program.setUniform("uOffsets", uOffsets); + this->_program.setUniform("uUvs", uUvs); + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.width() * vertexArray.height())); + //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + } +} \ No newline at end of file diff --git a/source/engine/graphics/front/module/TileArray_Module.hpp b/source/engine/graphics/front/module/TileArray_Module.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d85204be59714ebdb1ea5caa8d290d25a5bbc6ba --- /dev/null +++ b/source/engine/graphics/front/module/TileArray_Module.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "Module.hpp" + +#include <engine/graphics/back/buffers/VertexArray.hpp> +#include <engine/graphics/back/buffers/VerticeBuffer.hpp> +#include <engine/graphics/back/shaders/Program.hpp> + +#include <engine/graphics/front/object/TileArray.hpp> + +namespace megu { + class TileArray_Module : public Module<TileArray> { + public: + TileArray_Module(); + + virtual void draw(const TileArray &, const Camera &, const Window &) const override; + + private: + VertexArray _vao; + VerticeBuffer _vbo; + Program _program; + }; +} \ No newline at end of file diff --git a/source/engine/graphics/front/object/TileArray.cpp b/source/engine/graphics/front/object/TileArray.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf10b68084dc9acf00cce43b77f8266d6b215d84 --- /dev/null +++ b/source/engine/graphics/front/object/TileArray.cpp @@ -0,0 +1,27 @@ +#include "TileArray.hpp" + +#include <iostream> + +namespace megu { + TileArray::TileArray(const std::filesystem::path & path, size_t width, size_t height, float size) + : _width(width), _height(height), _size(size) { + megu::TextureBuffer buffer(path); + this->_texture.store(buffer); + + float twidth = static_cast<float>(this->_texture.width()); + float theight = static_cast<float>(this->_texture.height()); + + std::cout << twidth << "/" << theight << std::endl; + + for(size_t y = 0; y < height; ++y) { + std::vector<glm::vec4> rows; + for(size_t x = 0; x < width; ++x) { + rows.push_back({0.f, 0.f, twidth, theight}); + } + this->_uvs.push_back(rows); + } + + std::cout << this->_uvs.size() << std::endl; + this->_transformation.scale({size, size, 1.f}); + } +} \ No newline at end of file diff --git a/source/engine/graphics/front/object/TileArray.hpp b/source/engine/graphics/front/object/TileArray.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f3a8dcb11318cd4d92ad018d517335c70ce77c95 --- /dev/null +++ b/source/engine/graphics/front/object/TileArray.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include <array> + +#include <engine/graphics/back/geometry/Transformable.hpp> +#include <engine/graphics/back/textures/Texture.hpp> +#include <engine/graphics/front/geometry/Quads.hpp> + +#include <engine/graphics/utility/type.hpp> + +namespace megu { + class TileArray : public Quads { + public: + TileArray(const std::filesystem::path &, size_t, size_t, float); + + inline void setPosition(const Vec2 & pos) {this->_transformation.setPosition(pos.x, pos.y);} + inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);} + inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);} + inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);} + inline void setLayer(float l) {this->_transformation.setZ(l);} + + inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});} + inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});} + inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);} + + inline size_t width() const {return this->_width;} + inline size_t height() const {return this->_height;} + inline float size() const {return this->_size;} + + inline const Transformable & transformation() const {return this->_transformation;} + inline const Texture & texture() const {return this->_texture;} + + inline const std::vector<std::vector<glm::vec4>> & uvs() const {return this->_uvs;} + + private: + Transformable _transformation; + Texture _texture; + size_t _width, _height; + float _size; + std::vector<std::vector<glm::vec4>> _uvs; + }; +} \ No newline at end of file diff --git a/source/engine/graphics/front/object/VerticesArray.cpp b/source/engine/graphics/front/object/VerticesArray.cpp deleted file mode 100644 index 3a576ed543f618453bc8939910edfd16e5f4e44b..0000000000000000000000000000000000000000 --- a/source/engine/graphics/front/object/VerticesArray.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "VerticesArray.hpp" - -namespace megu { - VerticesArray::VerticesArray(Primitive_t primitive, size_t size) - : _primitive(primitive), _vertices(size) {} - - VerticesArray::VerticesArray(const VerticesArray & src) - : _transformation(src._transformation), _primitive(src._primitive), _vertices(src._vertices) {} - - VerticesArray VerticesArray::operator=(const VerticesArray & src) { - this->_transformation = src._transformation; - this->_primitive = src._primitive; - this->_vertices = src._vertices; - return *this; - } - - void VerticesArray::append(const Vertex & vertex) { - this->_vertices.push_back(vertex); - } -} \ No newline at end of file diff --git a/source/engine/graphics/front/object/VerticesArray.hpp b/source/engine/graphics/front/object/VerticesArray.hpp deleted file mode 100644 index 550958f214b45a2f75abe04d5930c6b5068f80e3..0000000000000000000000000000000000000000 --- a/source/engine/graphics/front/object/VerticesArray.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include <array> - -#include <engine/graphics/back/geometry/Transformable.hpp> -#include <engine/graphics/back/textures/Texture.hpp> -#include <engine/graphics/front/geometry/Quads.hpp> -#include <engine/graphics/front/geometry/Vertex.hpp> - -namespace megu { - class VerticesArray { - public: - VerticesArray(Primitive_t, size_t); - VerticesArray(const VerticesArray &); - VerticesArray operator=(const VerticesArray &); - ~VerticesArray() = default; - - inline size_t size() const {return this->_vertices.size();} - - void scale(float x, float y) {this->_transformation.scale(x, y);} - - void append(const Vertex &); - - const Transformable & transformation() const {return this->_transformation;} - const Primitive_t & primitive() const {return this->_primitive;} - const std::vector<Vertex> & vertices() const {return this->_vertices;} - - Vertex & operator[](size_t index) {return this->_vertices[index];} - const Vertex & operator[](size_t index) const {return this->_vertices[index];} - - private: - Transformable _transformation; - Primitive_t _primitive; - std::vector<Vertex> _vertices; - }; -} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index a0077912b2927fe2a5985ab3e40109d34ce7c142..ac230b0e39f59eed22de3ba680a14ac9010edf4c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -16,6 +16,7 @@ #include <engine/graphics/front/object/Image.hpp> #include <engine/graphics/front/object/Sprite.hpp> #include <engine/graphics/front/object/Text.hpp> +#include <engine/graphics/front/object/TileArray.hpp> #include <engine/graphics/front/module/Quad_Module.hpp> #include <engine/graphics/front/engine/Renderer.hpp> #include <engine/graphics/front/engine/Engine.hpp> @@ -79,6 +80,8 @@ int main(int argc, const char * argv[]) { text.scale({200, 200}); + megu::TileArray tiles("assets/textures/Tile_Test_4.png", 3, 3, 32.f); + megu::GraphicEngine engine(window); megu::Renderer basic_renderer(128, 128); @@ -92,8 +95,9 @@ int main(int argc, const char * argv[]) { engine.push(0, basic_renderer); engine.push(0, 1, images_set, &modul); - engine.push(0, 0, sprites_set, &modul); + engine.push(0, 3, sprites_set, &modul); engine.push(0, 2, text, &modul); + engine.push(0, 0, tiles, &modul); double previousTime = megu::Window::Time(); int frameCount = 0; diff --git a/source/main.cpp.old b/source/main.cpp.old deleted file mode 100644 index d0207f29da94f815d12c5521c5dee08954b136ac..0000000000000000000000000000000000000000 --- a/source/main.cpp.old +++ /dev/null @@ -1,198 +0,0 @@ -#include <iostream> - -#include <GL/glew.h> -#include <GLFW/glfw3.h> - -#include <engine/graphics/errors.hpp> -#include <engine/graphics/back.hpp> - -#define NORMALIZE(X) X/255.f - -#define WINDOW_WIDTH 1200 -#define WINDOW_HEIGHT 720 - -int main(int argc, const char * argv[]) { - try { - //? GLFW - if(glfwInit() == GLFW_FALSE) { - std::cerr << "GLFW : GLFW Init Error" << std::endl; - return EXIT_FAILURE; - } - - GLFWmonitor * monitore = glfwGetPrimaryMonitor(); - const GLFWvidmode * videoMode = glfwGetVideoMode(monitore); - - glfwWindowHint(GLFW_RED_BITS, videoMode->redBits); - glfwWindowHint(GLFW_GREEN_BITS, videoMode->greenBits); - glfwWindowHint(GLFW_BLUE_BITS, videoMode->blueBits); - glfwWindowHint(GLFW_REFRESH_RATE, videoMode->refreshRate); - - glfwWindowHint(GLFW_RESIZABLE, true); - glfwWindowHint(GLFW_DECORATED, true); - glfwWindowHint(GLFW_VISIBLE, true); - glfwWindowHint(GLFW_DOUBLEBUFFER, true); - - GLFWwindow * window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Scene", NULL, NULL); - if(window == NULL) { - std::cerr << "GLFW : Window Init Error" << std::endl; - return EXIT_FAILURE; - } - - glfwMakeContextCurrent(window); - glfwSwapInterval(0); - std::cout << "GLFW Inited" << std::endl; - - //? Glew - if (glewInit()) { - std::cerr << "Failed to initialize GLAD" << std::endl; - return EXIT_FAILURE; - } - - std::cout << "Glew Inited" << std::endl; - - //? Viewport - glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); - - megu::error::opengl_error::check(); - std::cout << "Viewport" << std::endl; - - //? Buffers - megu::VertexArray vao; - megu::VerticeBuffer vbo(vao, {megu::layout::POSITION, megu::layout::COLOR}, 128, megu::EditMode::STATIC); - - std::vector<float> vertices = { - -1.f, -1.f, 0.f, NORMALIZE(33), NORMALIZE(114), NORMALIZE(255), - 1.f, -1.f, 0.f, NORMALIZE(33), NORMALIZE(114), NORMALIZE(255), - 1.f, 0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114), - - 1.f, 0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114), - -1.f, 0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114), - -1.f, -1.f, 0.f, NORMALIZE(33), NORMALIZE(114), NORMALIZE(255), - - -1.f, -0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114), - 1.f, -0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114), - 1.f, 1.f, 0.f, NORMALIZE(190), NORMALIZE(105), NORMALIZE(170), - - 1.f, 1.f, 0.f, NORMALIZE(190), NORMALIZE(105), NORMALIZE(170), - -1.f, 1.f, 0.f, NORMALIZE(190), NORMALIZE(105), NORMALIZE(170), - -1.f, 0.0f, 0.f, NORMALIZE(217), NORMALIZE(63), NORMALIZE(114) - }; - - vbo << vertices; - - std::cout << "VBO" << std::endl; - - megu::VertexArray vao_2; - megu::VerticeBuffer vbo_2(vao_2, {megu::layout::POSITION, megu::layout::COLOR}, 128, megu::EditMode::STATIC); - - std::vector<float> vertices_2 = { - -0.5f, -0.5f, 0.0, NORMALIZE(255), NORMALIZE(0), NORMALIZE(0), - 0.5f, -0.5f, 0.0, NORMALIZE(0), NORMALIZE(255), NORMALIZE(0), - 0.0f, 0.5f, 0.0, NORMALIZE(0), NORMALIZE(0), NORMALIZE(255), - }; - - vbo_2 << vertices_2; - - megu::VertexArray vao_F; - megu::VerticeBuffer vbo_F(vao_F, {megu::layout::POSITION, megu::layout::TEXTURE}, 64, megu::EditMode::STATIC); - - std::vector<float> vertices_F = { - -1.f, -1.f, 0.f, 0.f, 0.f, - 1.f, -1.f, 0.f, 1.f, 0.f, - 1.f, 1.f, 0.f, 1.f, 1.f, - - -1.f, -1.f, 0.f, 0.f, 0.f, - 1.f, 1.f, 0.f, 1.f, 1.f, - -1.f, 1.f, 0.f, 0.f, 1.f - }; - - vbo_F << vertices_F; - - std::cout << "VBO - FBO" << std::endl; - - //? Material - - megu::TextureBuffer buffer; - buffer.load("assets/textures/All_Might_Armored.png"); - - megu::Texture texture; - texture.store(buffer); - buffer.free(); - - //? FBO - - megu::FrameBuffer fbo(WINDOW_WIDTH, WINDOW_HEIGHT); - megu::FrameBuffer fbo_2(WINDOW_WIDTH, WINDOW_HEIGHT); - - //? Shaders - megu::Program program; - - { - megu::Source vertex("assets/shaders/Basic.vert", megu::Source::Categorie::VERTEX); - megu::Source fragment("assets/shaders/Basic.frag", megu::Source::Categorie::FRAGMENT); - - program << vertex; - program << fragment; - - program.link(); - } - - std::cout << "Shader - FBO" << std::endl; - - - megu::Program program_F; - - { - megu::Source vertex("assets/shaders/Texture.vert", megu::Source::Categorie::VERTEX); - megu::Source fragment("assets/shaders/Texture.frag", megu::Source::Categorie::FRAGMENT); - - program_F << vertex; - program_F << fragment; - - program_F.link(); - } - - std::cout << "Shader" << std::endl; - - - //? Render Loop - glClearColor(0.0f, 0.0f, 0.0f, 0.f); - - std::cout << "Render Loop Begin !" << std::endl; - while(!glfwWindowShouldClose(window)) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glfwPollEvents(); - - /*fbo.bind(); - vao.bind(); - program.use(); - glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo.size())); - - fbo_2.bind(); - vao_2.bind(); - program.use(); - glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo_2.size()));*/ - - megu::FrameBuffer::BindDefaultFrameBuffer(); - vao_F.bind(); - - //fbo.texture().bind(0); - //fbo_2.texture().bind(1); - - texture.bind(0); - - program_F.use(); - program_F.setUniform("uSamp", std::vector<GLint>({0, 1})); - - glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo_F.size())); - - glfwSwapBuffers(window); - } - std::cout << "Render Loop End !" << std::endl; - } - catch(std::exception & error) { - std::cerr << error.what() << std::endl; - } - - return EXIT_SUCCESS; -} \ No newline at end of file