12 #if defined(__ANDROID__) 13 #include <android/asset_manager.h> 14 #elif defined(SMALL3D_IOS) 17 #define GLFW_INCLUDE_VULKAN 18 #include <GLFW/glfw3.h> 26 #include <unordered_map> 29 #define GLM_FORCE_RADIANS 30 #include <glm/glm.hpp> 32 #include FT_FREETYPE_H 41 VkImageView imageView = VK_NULL_HANDLE;
42 VkImage image = VK_NULL_HANDLE;
43 VkDeviceMemory imageMemory = VK_NULL_HANDLE;
44 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
45 VkDescriptorSet orthoDescriptorSet = VK_NULL_HANDLE;
52 struct UboOrientation {
53 glm::mat4x4 xRotationMatrix;
54 glm::mat4x4 yRotationMatrix;
55 glm::mat4x4 zRotationMatrix;
78 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS) 82 std::string windowTitle =
"";
84 static int realScreenWidth, realScreenHeight;
86 std::string shadersPath =
"";
88 uint32_t maxObjectsPerPass;
90 uint32_t perspectivePipelineIndex = 100;
91 uint32_t orthographicPipelineIndex = 100;
93 uint32_t currentSwapchainImageIndex = 0;
95 std::vector<VkBuffer> renderOrientationBuffersDynamic;
96 std::vector<VkDeviceMemory> renderOrientationBuffersDynamicMemory;
98 std::vector<VkBuffer> cameraOrientationBuffers;
99 std::vector<VkDeviceMemory> cameraOrientationBufferMemories;
101 std::vector<VkBuffer> worldDetailsBuffers;
102 std::vector<VkDeviceMemory> worldDetailsBufferMemories;
104 std::vector<VkBuffer> lightIntensityBuffers;
105 std::vector<VkDeviceMemory> lightIntensityBufferMemories;
107 std::vector<VkBuffer> colourBuffersDynamic;
108 std::vector<VkDeviceMemory> colourBuffersDynamicMemory;
110 VkSampler textureSampler;
112 std::vector<VkImageView> boundTextureViews;
114 std::allocator<char> alloc;
116 float frustumScale = 0.0f;
119 float zOffsetFromCamera = 0.0f;
121 std::unordered_map<std::string, VulkanImage> textures;
123 FT_Library library = 0;
124 std::vector<float> textMemory;
125 std::unordered_map<std::string, FT_Face> fontFaces;
128 std::vector<AAsset*> fontAssets;
131 static std::vector<Model> nextModelsToDraw;
133 static VkVertexInputBindingDescription bd[3];
134 static VkVertexInputAttributeDescription ad[3];
136 static VkDescriptorSetLayout descriptorSetLayout;
137 static VkDescriptorSet descriptorSet;
138 static VkDescriptorSetLayout textureDescriptorSetLayout;
139 static VkDescriptorSetLayout perspectiveLayouts[2];
141 static VkVertexInputBindingDescription orthobd[2];
142 static VkVertexInputAttributeDescription orthoad[2];
144 static VkDescriptorSetLayout orthoDescriptorSetLayout;
145 static VkDescriptorSet orthoDescriptorSet;
146 static VkDescriptorSetLayout textureOrthoDescriptorSetLayout;
147 static VkDescriptorSetLayout orthographicLayouts[2];
149 size_t dynamicOrientationAlignment = 0;
150 UboOrientation* uboOrientationDynamic =
nullptr;
151 uint32_t orientationMemIndex = 0;
152 size_t uboOrientationDynamicSize = 0;
154 size_t dynamicColourAlignment = 0;
155 UboColour* uboColourDynamic =
nullptr;
156 uint32_t colourMemIndex = 0;
157 size_t uboColourDynamicSize = 0;
160 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS) 161 static void framebufferSizeCallback(GLFWwindow* window,
int width,
165 static int setInputStateCallback(VkPipelineVertexInputStateCreateInfo*
166 inputStateCreateInfo);
167 static int setPipelineLayoutCallback(VkPipelineLayoutCreateInfo*
168 pipelineLayoutCreateInfo);
170 int bindBuffers(VkCommandBuffer commandBuffer,
const Model& model);
171 void recordDrawCommand(VkCommandBuffer commandBuffer,
172 VkPipelineLayout pipelineLayout,
const Model& model,
173 uint32_t swapchainImageIndex);
175 static int setOrthoInputStateCallback(VkPipelineVertexInputStateCreateInfo*
176 inputStateCreateInfo);
177 static int setOrthoPipelineLayoutCallback(VkPipelineLayoutCreateInfo*
178 pipelineLayoutCreateInfo);
180 int bindOrthoBuffers(VkCommandBuffer commandBuffer,
182 void recordOrthoDrawCommand(VkCommandBuffer commandBuffer,
183 VkPipelineLayout pipelineLayout,
const Model& model,
184 uint32_t swapchainImageIndex);
188 VkDescriptorPool descriptorPool;
189 bool descriptorPoolCreated =
false;
190 void createDescriptorPool();
192 VkDescriptorPool orthoDescriptorPool;
193 bool orthoDescriptorPoolCreated =
false;
194 void createOrthoDescriptorPool();
196 VkCommandBuffer nextCommandBuffer;
198 std::vector<Model> garbageModels;
200 void allocateDescriptorSets();
201 void updateDescriptorSets();
203 void allocateOrthoDescriptorSets();
204 void updateOrthoDescriptorSets();
206 void setColourBuffer(glm::vec4 colour, uint32_t memIndex);
208 void positionNextObject(
const glm::vec3 offset,
209 const glm::vec3 rotation,
211 void positionCamera();
212 VulkanImage getTextureHandle(
const std::string name)
const;
213 VulkanImage generateTexture(
const std::string name,
215 const unsigned long width,
216 const unsigned long height,
219 void init(
const int width,
const int height,
220 const std::string shadersPath);
221 void initWindow(
int& width,
int& height);
223 void setPerspectiveAndLight();
233 #if defined(__ANDROID__) || defined(SMALL3D_IOS) 235 Renderer(
const std::string &windowTitle =
"",
237 const int height = 0,
238 const float frustumScale = 1.0f,
239 const float zNear = 1.0f,
240 const float zFar = 24.0f,
241 const float zOffsetFromCamera = -1.0f,
242 const std::string &shadersPath =
244 "resources/shaders/",
246 "resources1/shaders/",
248 const uint32_t maxObjectsPerPass = 20);
250 Renderer(
const std::string &windowTitle,
const int width,
251 const int height,
const float frustumScale,
const float zNear,
252 const float zFar,
const float zOffsetFromCamera,
253 const std::string &shadersPath,
const uint32_t maxObjectsPerPass);
260 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS) 334 const int height = 0,
335 const float frustumScale = 1.0f,
336 const float zNear = 1.0f,
337 const float zFar = 24.0f,
338 const float zOffsetFromCamera = -1.0f,
339 const std::string &shadersPath =
340 "resources/shaders/",
341 const uint32_t maxObjectsPerPass = 20);
348 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS) 360 void generateTexture(
const std::string &name,
const Image &image);
373 void generateTexture(
const std::string &name,
const std::string &text,
374 const glm::vec3 &colour,
375 const int fontSize = 48,
376 const std::string &fontPath =
378 "resources/fonts/CrusoeText/CrusoeText-Regular.ttf",
380 "resources1/fonts/CrusoeText/CrusoeText-Regular.ttf",
382 const bool replace =
true);
399 const glm::vec3& topLeft,
400 const glm::vec3& bottomRight);
417 void render(
Model& model,
const glm::vec3 &offset,
const glm::vec3 &rotation,
418 const glm::vec4 &colour,
const std::string &textureName =
"",
419 const bool perspective =
true);
429 void render(
Model& model,
const glm::vec3 &offset,
const glm::vec3 &rotation,
430 const std::string &textureName);
439 void render(
Model& model,
const std::string& textureName,
440 const bool perspective =
true);
449 const bool perspective =
true);
505 void operator=(
Renderer const&) =
delete;
507 void operator=(
Renderer&&) =
delete;
void deleteTexture(const std::string &name)
Deletes the texture indicated by the given name.
Definition: Renderer.cpp:1502
A 3D model, loaded from a Wavefront file.
Definition: Model.hpp:25
void clearScreen() const
This function does not do anything in this edition (Vulkan) of small3d, since the screen is automatic...
Definition: Renderer.cpp:1808
int getScreenHeight()
Get the real screen height.
Definition: Renderer.cpp:1251
Header of the Image class.
void clearBuffers(Model &model) const
Clear a Model from the GPU buffers (the object itself remains intact).
Definition: Renderer.cpp:1792
Header of the SceneObject class.
void swapBuffers()
Swap the buffers.
Definition: Renderer.cpp:1827
An image, loaded from a png file, which can be used for generating textures.
Definition: Image.hpp:29
glm::vec3 cameraPosition
The camera position in world space.
Definition: Renderer.hpp:272
glm::vec3 lightDirection
Vector, indicating the direction of the light in the scene.
Definition: Renderer.hpp:267
An object that appears on the 3D scene. It is made up of one or more models (the latter for animation...
Definition: SceneObject.hpp:35
void render(Model &model, const glm::vec3 &offset, const glm::vec3 &rotation, const glm::vec4 &colour, const std::string &textureName="", const bool perspective=true)
Render a Model.
Definition: Renderer.cpp:1548
~Renderer()
Destructor.
Definition: Renderer.cpp:1269
The renderer (Vulkan)
Definition: Renderer.hpp:73
void createRectangle(Model &rect, const glm::vec3 &topLeft, const glm::vec3 &bottomRight)
Populates a Model object with a rectangle stretching between the given coordinates.
Definition: Renderer.cpp:1516
glm::vec3 cameraRotation
The camera rotation (around the x, y and z axes)
Definition: Renderer.hpp:277
Header of the Model class.
Header of the Logger class.
static Renderer & getInstance(const std::string &windowTitle="", const int width=0, const int height=0, const float frustumScale=1.0f, const float zNear=1.0f, const float zFar=24.0f, const float zOffsetFromCamera=-1.0f, const std::string &shadersPath="resources/shaders/", const uint32_t maxObjectsPerPass=20)
Get the instance of the Renderer (the Renderer is a singleton).
Definition: Renderer.cpp:1255
GLFWwindow * getWindow() const
Get the GLFW window object, associated with the Renderer.
Definition: Renderer.cpp:1379
int getScreenWidth()
Get the real screen width.
Definition: Renderer.cpp:1247
float lightIntensity
The light intensity (set to -1.0f if no lighting is to be used).
Definition: Renderer.hpp:294