I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
The basic class structure is as follows:
- Core:
- class Engine
- This is the main class that will initialize, run, and shut down other subsystems of the engine (e.g., Window, Renderer, Input, etc.)
- Window:
- class Window
- This class will handle window creation, resizing, and destruction using GLFW. It should also handle keyboard and mouse input events.
- Renderer:
- class Renderer
- This class will manage Vulkan objects (e.g., instance, device, swapchain, command buffers) and rendering pipelines.
- class Shader
- To handle shader modules creation and destruction.
- class Texture
- To handle texture loading and destruction.
- class Pipeline
- To encapsulate the description and creation of a Vulkan graphics or compute pipeline.
- Scene:
- class Scene
- This class will manage the list of objects in the vector, update their transforms, and send them to the Renderer for rendering.
- class GameObject
- To represent individual objects in the scene, store their transformation matrices, and bind the relevant data like vertices and indices.
- class Camera
- To store and manage camera properties such as position, rotation, view, and projection matrices.
- class Mesh
- To handle mesh data (e.g., vertices, indices, etc.) for individual objects.
- class Material
- To store and manage material properties such as colors, shaders, and textures.
- Math:
- Utilize GLM library to handle vector and matrix math operations.
Here are the header files for context:
BufferUtils.h:
#pragma once
#include <vulkan/vulkan.h> #include <stdint.h>
namespace BufferUtils { void CreateBuffer( VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
uint32_t FindMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties); void CopyBuffer( VkDevice device, VkCommandPool commandPool, VkQueue graphicsQueue, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size); - Assistant
- User
Should the terrain generation be handled in a separate class (e.g. a Terrain or World class), or should I add the methods you've described to the Scene class? Note that I also want to expand the generation capability to work with chunks, biomes, texture blending etc.
- Assistant
Considering that you plan to expand the terrain generation capability with features like chunks, biomes, and texture blending, it's a good idea to create a separate class (e.g.,
TerrainorWorld) to handle terrain generation. This approach will help keep the code organized and modular, and it will be easier to maintain and add new features in the future.Here's a suggestion for a basic
Terrainclass:class Terrain { public: Terrain(/*... constructor arguments such as seed, world size, scale, etc. ...*/); ~Terrain(); void GenerateTerrain(/*... arguments for terrain generation parameters ...*/); GameObject* GetTerrainObject(); private: void GenerateHeightMap(); void ConvertHeightMapToMeshData(); int seed; int worldSize; float scale; SimplexNoise noise; std::vector<std::vector<float>> heightMap; std::vector<Vertex> vertices; std::vector<uint32_t> indices; Mesh terrainMesh; Material terrainMaterial; GameObject terrainObject; }; - User
What would the .cpp file look like for the Terrain class?
- Assistant
Here's an example implementation of the
Terrainclass in a .cpp file:#include "Terrain.h" Terrain::Terrain(int seed, int worldSize, float scale) : seed(seed), worldSize(worldSize), scale(scale), noise(seed) { } Terrain::~Terrain() { // Cleanup any resources associated with the Terrain class terrainMesh.Cleanup(); terrainMaterial.Cleanup(); } void Terrain::GenerateHeightMap() { heightMap = std::vector<std::vector<float>>(worldSize, std::vector<float>(worldSize, 0.0f)); for (int x = 0; x < worldSize; ++x) { for (int z = 0; z < worldSize; ++z) { float noiseValue = noise.noise(x * scale, z * scale); heightMap[x][z] = noiseValue; } } } void Terrain::ConvertHeightMapToMeshData() { // ... Convert height map to vertices and indices (code from the previous example) } void Terrain::GenerateTerrain(/*... arguments for terrain generation parameters, if needed ...*/) { GenerateHeightMap(); ConvertHeightMapToMeshData(); terrainMesh.Initialize(vertices, indices, device, physicalDevice, commandPool, graphicsQueue); // Initialize terrainMaterial (shaders, textures, etc.) as needed terrainObject.Initialize(terrainMesh, terrainMaterial); } GameObject* Terrain::GetTerrainObject() { return &terrainObject; } - User
What would the Terrain header file look like?
- Assistant
Here's an example implementation of the
Terrainheader file (Terrain.h):#pragma once #include "Simplex.h" #include "Mesh.h" #include "Material.h" #include "GameObject.h" class Terrain { public: Terrain(int seed, int worldSize, float scale); ~Terrain(); void GenerateTerrain(/*... arguments for terrain generation parameters, if needed ...*/); GameObject* GetTerrainObject(); private: void GenerateHeightMap(); void ConvertHeightMapToMeshData(); int seed; int worldSize; float scale; SimplexNoise noise; std::vector<std::vector<float>> heightMap; std::vector<Vertex> vertices; std::vector<uint32_t> indices; Mesh terrainMesh; Material terrainMaterial; GameObject terrainObject; };This
Terrain.hheader file includes the necessary classes likeSimplexNoise,Mesh,Material, andGameObject. It also contains public methods for generating terrain and accessing the resulting GameObject.Additionally, it has private methods for generating the height map and converting it to mesh data, as well as member variables for storing the seed, worldSize, scale, heightMap, vertices, indices, terrainMesh, terrainMaterial, and terrainObject created during terrain generation.
- User
The GenerateTerrain method is currently missing the following variables: device, physicalDevice, commandPool, graphicsQueue. Should these be passed to the GenerateTerrain method as variables or is there another way to link these variables to the appropriate Vulkan objects?
- Assistant
You can pass these variables to the
Terrainclass' constructor and store them as member variables to be used in theGenerateTerrainmethod. This way, you only need to pass them once when creating aTerrainobject, and they would be available for the methods within the class.Here's how you can modify your
Terrainheader file and constructor to accept these variables:In the
Terrain.hheader file, add the following member variables:private: //... VkDevice device; VkPhysicalDevice physicalDevice; VkCommandPool commandPool; VkQueue graphicsQueue;Modify the
Terrainconstructor in the header file to accept these new variables: - User
Should the variables for the Vulkan objects be pointers? I'm concerned about duplicating instances and causing memory issues.
- Assistant
Using pointers or references for Vulkan objects can indeed help avoid unintentional duplication of instances, leading to better memory management. In such a case, you can modify the
Terrainclass and its methods to expect and store pointers or references to Vulkan objects instead of the objects themselves.Here's an update to your
Terrainclass using pointers for Vulkan objects:In
Terrain.h, change member variables to pointers: