VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
VulkanMesh.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include "context.hpp"
9#include "components/Mesh.hpp"
10#include "components/ErrorUtils.hpp"
11#include "Resources.hpp"
12
14
15#include <sys/types.h>
16
17namespace vex {
19
21 struct DrawIndexedIndirectCommand;
22
24 class VulkanMesh {
25 public:
28 VulkanMesh(VulkanContext& context);
30
34 void upload(const MeshData& meshData);
35
36 // @brief Function to extract transparent triangles
37 // @details Transforms triangle centers to world space, calculates distance to camera, and populates `outTriangles` with render data.
38 // @param const glm::mat4& modelMatrix - Model matrix for the mesh.
39 // @param const glm::vec3& cameraPos - Position of the camera.
40 // @param uint32_t modelIndex - Index of the current model.
41 // @param uint32_t frameIndex - Index of the current frame.
42 // @param vex::Entity entity - Entity associated with the mesh.
43 // @param std::vector<TransparentTriangle>& outTriangles - Vector to store extracted transparent triangles.
44 void extractTransparentTriangles(
45 const glm::mat4& modelMatrix,
46 const glm::vec3& cameraPos,
47 uint32_t modelIndex,
48 uint32_t frameIndex,
49 vex::Entity entity,
50 std::vector<TransparentTriangle>& outTriangles
51 );
52
63 void draw(VkCommandBuffer cmd, VkPipelineLayout pipelineLayout,
64 VulkanResources& resources, uint32_t frameIndex, uint32_t modelIndex, glm::mat4 modelMatrix, const MeshComponent& mc) const;
65
79 VkCommandBuffer cmd,
80 VkPipelineLayout pipelineLayout,
81 VulkanResources& resources,
82 uint32_t frameIndex,
83 uint32_t modelIndex,
84 uint32_t submeshIndex,
85 glm::mat4 modelMatrix,
86 bool modelChanged,
87 bool submeshChanged,
88 const MeshComponent& mc
89 ) const;
90
93 int getNumOfInstances() const { return numOfInstances; }
94
96 void addInstance() { numOfInstances++; }
97
99 void removeInstance() { numOfInstances--; }
100
101 private:
104 VkBuffer vertexBuffer;
105 VmaAllocation vertexAlloc;
106 VkBuffer indexBuffer;
107 VmaAllocation indexAlloc;
108 uint32_t indexCount;
109 };
110
116 void StreamToGPU(void* dst, const void* src, size_t sizeBytes);
117
118 VulkanContext& m_r_context;
119 std::vector<SubmeshBuffers> m_submeshBuffers;
120 std::vector<std::string> m_submeshTextures;
121 int numOfInstances = 0;
122
123 std::vector<Submesh> m_cpuSubmeshData;
124 };
125
128 // @brief Distance from the camera to the triangle's center (for sorting)
129 float distanceToCamera;
130
131 // @brief Data needed to render the triangle after sorting
132 uint32_t modelIndex;
133 uint32_t frameIndex;
134 uint32_t firstIndex;
135 uint32_t submeshIndex;
136 VulkanMesh* mesh;
137 vex::Entity entity;
138
139 //glm::mat4 modelMatrix;
140 };
141}
Contains basic components like transform, camera, name components..
This file defines MeshData struct and all other structs needed for it.
This file defines VulkanResources Class.
Class defining mesh object with vulkan specific data and methods.
int getNumOfInstances() const
Helper function to get number of mesh components using this VulkanMesh instance, needed for mesh mana...
void StreamToGPU(void *dst, const void *src, size_t sizeBytes)
Helper function to stream data to GPU memory.
void removeInstance()
decrements number of mesh components using this VulkanMesh instance.
VulkanMesh(VulkanContext &context)
Constructor for VulkanMesh class.
void draw(VkCommandBuffer cmd, VkPipelineLayout pipelineLayout, VulkanResources &resources, uint32_t frameIndex, uint32_t modelIndex, glm::mat4 modelMatrix, const MeshComponent &mc) const
Draws the mesh to the screen.
void upload(const MeshData &meshData)
Uploads mesh data to the GPU.
void bindAndDrawBatched(VkCommandBuffer cmd, VkPipelineLayout pipelineLayout, VulkanResources &resources, uint32_t frameIndex, uint32_t modelIndex, uint32_t submeshIndex, glm::mat4 modelMatrix, bool modelChanged, bool submeshChanged, const MeshComponent &mc) const
Batch-optimized draw call for sorted transparent triangles.
void addInstance()
increments number of mesh components using this VulkanMesh instance.
This class manages resources like textures, descriptor sets, and uniform buffers.
Definition Resources.hpp:23
This file defines struct holding all vulkan data, like device, surface, swapchain,...
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11
Struct containing raw meshData, mesh id, texture paths and material properties. It just template and ...
Mesh data structure for loading and managing mesh data.
Definition Mesh.hpp:41
struct used to held transparent triangles data for sorting and special rendering
Struct holding all vulkan data, like device, surface, swapchain, images, views, and more.
Definition context.hpp:26
struct used to hold buffers and allocations for each submesh in single object.