diff --git a/include/components/text_ui_renderer.hpp b/include/components/text_ui_renderer.hpp index 333649b..420f9d2 100644 --- a/include/components/text_ui_renderer.hpp +++ b/include/components/text_ui_renderer.hpp @@ -6,6 +6,7 @@ #include "resources/font.hpp" #include "resources/mesh.hpp" +#include "resources/shader.hpp" #include diff --git a/include/resources/mesh.hpp b/include/resources/mesh.hpp index b270cb0..fc48973 100644 --- a/include/resources/mesh.hpp +++ b/include/resources/mesh.hpp @@ -4,9 +4,7 @@ #include "resource.hpp" -#include "resources/shader.hpp" - -#include +#include "gfx.hpp" #include #include @@ -20,34 +18,30 @@ struct Vertex { glm::vec2 uv; }; +namespace engine { + class GFXDevice; +} + namespace engine::resources { class ENGINE_API Mesh : public Resource { public: - Mesh(const std::vector& vertices); - Mesh(const std::vector& vertices, const std::vector& indices); - Mesh(const std::filesystem::path& resPath); + Mesh(GFXDevice* gfx, const std::vector& vertices); + Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices); + Mesh(GFXDevice* gfx, const std::filesystem::path& resPath); ~Mesh() override; - void drawMesh(const Shader& shader); - - static void invalidate() - { - s_active_vao = -1; - } + void drawMesh(const gfx::Pipeline* pipeline); std::vector m_vertices; - std::vector m_indices; + std::vector m_indices; private: - static int s_active_vao; + const gfx::Buffer* vb; + const gfx::Buffer* ib; - GLuint m_vao; - GLuint m_vbo; - GLuint m_ebo; - - void bindVAO() const; + GFXDevice* gfx; void initMesh(); diff --git a/src/components/mesh_renderer.cpp b/src/components/mesh_renderer.cpp index 8c98701..b0c061b 100644 --- a/src/components/mesh_renderer.cpp +++ b/src/components/mesh_renderer.cpp @@ -33,13 +33,13 @@ void Renderer::render(glm::mat4 transform) m_shader->setUniform_v3("baseColor", m_color ); m_shader->setUniform_v3("emission", m_emission ); - if (m_mesh) - m_mesh->drawMesh(*m_shader); +// if (m_mesh) +// m_mesh->drawMesh(*m_shader); } void Renderer::setMesh(const std::string& name) { - m_mesh = parent.res.get(name); +// m_mesh = parent.res.get(name); } void Renderer::setTexture(const std::string& name) diff --git a/src/components/text_ui_renderer.cpp b/src/components/text_ui_renderer.cpp index a7011bb..2d9effe 100644 --- a/src/components/text_ui_renderer.cpp +++ b/src/components/text_ui_renderer.cpp @@ -57,6 +57,7 @@ void UI::render(glm::mat4 transform) float w = glyph.size.x * scale; float h = glyph.size.y * scale; + /* resources::Mesh mesh({ {{xpos, ypos + h, 0.0f}, {}, {0.0f, 0.0f}}, {{xpos, ypos , 0.0f}, {}, {0.0f, 1.0f}}, @@ -64,11 +65,11 @@ void UI::render(glm::mat4 transform) {{xpos, ypos + h, 0.0f}, {}, {0.0f, 0.0f}}, {{xpos + w, ypos, 0.0f}, {}, {1.0f, 1.0f}}, {{xpos + w, ypos + h, 0.0f}, {}, {1.0f, 0.0f}}, - }); + });*/ glBindTexture(GL_TEXTURE_2D, glyph.textureID); - mesh.drawMesh(*m_shader); +// mesh.drawMesh(*m_shader); x += (glyph.advance >> 6) * scale; diff --git a/src/engine.cpp b/src/engine.cpp index 47797a7..197d8f5 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -37,10 +37,10 @@ namespace engine { { {-0.5f, 0.5f}, {0.0f, 1.0f, 1.0f} }, }; vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, sizeof(Vertex) * vertices.size(), vertices.data()); - const std::vector indices = { + const std::vector indices = { 0, 1, 2, 2, 1, 3, }; - ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint16_t) * indices.size(), indices.data()); + ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint32_t) * indices.size(), indices.data()); } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 19c51cb..167f136 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -1134,7 +1134,7 @@ namespace engine { // do a simple draw call vkCmdDraw(pimpl->commandBuffer, call.count, 1, 0, 0); } else { - vkCmdBindIndexBuffer(pimpl->commandBuffer, call.indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT16); + vkCmdBindIndexBuffer(pimpl->commandBuffer, call.indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT32); vkCmdDrawIndexed(pimpl->commandBuffer, call.count, 1, 0, 0, 0); } queue.pop(); diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp index b6092f3..9633493 100644 --- a/src/resources/mesh.cpp +++ b/src/resources/mesh.cpp @@ -1,5 +1,7 @@ #include "resources/mesh.hpp" +#include "gfx_device.hpp" + namespace engine::resources { struct MeshFileHeader { @@ -8,7 +10,7 @@ struct MeshFileHeader { int material; }; -static void loadMeshFromFile(const std::filesystem::path& path, std::vector* vertices, std::vector* indices) +static void loadMeshFromFile(const std::filesystem::path& path, std::vector* vertices, std::vector* indices) { // TODO @@ -26,73 +28,34 @@ static void loadMeshFromFile(const std::filesystem::path& path, std::vectorresize(header.index_count); vertices->resize(header.vertex_count); - fread(&(*indices)[0], sizeof(unsigned int) * header.index_count, 1, fp); - fread(&((*vertices)[0].pos[0]), sizeof(float) * 8 * header.vertex_count, 1, fp); - + fread(indices->data(), sizeof(unsigned int) * header.index_count, 1, fp); + fread(vertices->data(), sizeof(float) * 8 * header.vertex_count, 1, fp); fclose(fp); } -static void loadObjFromFile(const std::filesystem::path& path, std::vector* vertices, std::vector* indices) -{ - -} - -// -1 means invalidated -int Mesh::s_active_vao = -1; - -void Mesh::bindVAO() const -{ - if (s_active_vao != m_vao) { - glBindVertexArray(m_vao); - s_active_vao = m_vao; - } -} - void Mesh::initMesh() { - glGenVertexArrays(1, &m_vao); - bindVAO(); - glGenBuffers(1, &m_vbo); - glGenBuffers(1, &m_ebo); - - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - glBufferData(GL_ARRAY_BUFFER, m_vertices.size()*sizeof(Vertex), &m_vertices[0], GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), &(m_indices[0]), GL_STATIC_DRAW); - - // position - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, pos)); - // normal - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, norm)); - // uv - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, uv)); - + vb = gfx->createBuffer(gfx::BufferType::VERTEX, m_vertices.size() * sizeof(Vertex), m_vertices.data()); + ib = gfx->createBuffer(gfx::BufferType::INDEX, m_indices.size() * sizeof(uint32_t), m_indices.data()); } -void Mesh::drawMesh(const Shader& shader) +void Mesh::drawMesh(const gfx::Pipeline* pipeline) { - bindVAO(); - shader.makeActive(); -#ifndef SDLTEST_NOGFX - glDrawElements(GL_TRIANGLES, static_cast(m_indices.size()), GL_UNSIGNED_INT, 0); -#endif + gfx->drawIndexed(pipeline, vb, ib, m_indices.size()); } -Mesh::Mesh(const std::vector& vertices) : Resource("", "mesh") +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) : Resource("", "mesh"), gfx(gfx) { // constructor for custom meshes without an index array m_vertices = vertices; // COPY over vertices - for (int i = 0; i < m_vertices.size(); i++) { + for (uint32_t i = 0; i < m_vertices.size(); i++) { m_indices.push_back(i); } initMesh(); } -Mesh::Mesh(const std::vector& vertices, const std::vector& indices) : Resource("", "mesh") +Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : Resource("", "mesh"), gfx(gfx) { m_vertices = vertices; // COPY over vertices m_indices = indices; // COPY over indices; @@ -100,7 +63,7 @@ Mesh::Mesh(const std::vector& vertices, const std::vector& } // To be used with the resource manager -Mesh::Mesh(const std::filesystem::path& resPath) : Resource(resPath, "mesh") +Mesh::Mesh(GFXDevice* gfx, const std::filesystem::path& resPath) : Resource(resPath, "mesh"), gfx(gfx) { loadMeshFromFile(resPath, &m_vertices, &m_indices); initMesh(); @@ -108,12 +71,8 @@ Mesh::Mesh(const std::filesystem::path& resPath) : Resource(resPath, "mesh") Mesh::~Mesh() { - glDeleteVertexArrays(1, &m_vao); - glDeleteBuffers(1, &m_vbo); - glDeleteBuffers(1, &m_ebo); - if (s_active_vao == m_vao) { - s_active_vao = -1; - } + gfx->destroyBuffer(ib); + gfx->destroyBuffer(vb); } }