Skip to content
Snippets Groups Projects
Commit d1f0d5c7 authored by BATON Theau's avatar BATON Theau
Browse files

Add tile

parent a0a157f9
Branches
No related tags found
No related merge requests found
Showing
with 205 additions and 268 deletions
......@@ -11,9 +11,11 @@
*.exe
*.git
*.ttf
build/*
include/*
library/*
vcpkg_installed/*
old/*
old2/*
\ No newline at end of file
#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
#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
assets/textures/Tile_Test.png

1.17 KiB | W: | H:

assets/textures/Tile_Test.png

460 B | W: | H:

assets/textures/Tile_Test.png
assets/textures/Tile_Test.png
assets/textures/Tile_Test.png
assets/textures/Tile_Test.png
  • 2-up
  • Swipe
  • Onion skin
assets/textures/Tile_Test_4.png

246 B

#pragma once
#include <glm/glm.hpp>
namespace megu {
struct Vertex {
glm::vec2 _position;
glm::vec2 _texture;
};
}
\ No newline at end of file
......@@ -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
......@@ -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
#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
#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
#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
#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
#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
#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
......@@ -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;
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment