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.
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.
It seems like the issue is that the Mesh class is missing setter functions for vertices and indices. Add the following methods to Mesh.h and Mesh.cpp to fix the problem:
Mesh.h:
cpp
// Add these lines in Mesh class definition
void SetVertices(const std::vector<Vertex>& vertices);
void SetIndices(const std::vector<uint32_t>& indices);
Now, you should no longer receive errors at the mentioned lines in LoadNode and LoadMesh methods. Please make sure you call mesh.Inititalize() after loading the mesh in your LoadMesh method. You can modify your LoadMesh method like this:
User
This solved on of the problems. However, there is still an issue in calling the Mesh class Initialize method using the code in the LoadMesh method. It seems the method arguments don't match. Does a separate method need to be created to initialize the mesh using those arguments or is there another solution?
Assistant
My apologies for the confusion. You're right, the signatures of the Mesh class Initialize method and the call from the LoadMesh method in the Model class don't match.
You can create an overloaded version of the Initialize method for the Mesh class that accepts the necessary arguments. Add the following lines to Mesh.h and Mesh.cpp:
Mesh.h:
cpp
// Add this line in the Mesh class definition
void Initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue);
User
I am getting errors in the new Intiialize method. It seems the stagingBuffer and bufferSize variables haven't been declared. Where should they be declared and do they need to be initialized?
private:
static void FramebufferResizeCallback(GLFWwindow* window, int width, int height);
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
code
int width;
int height;
const char* title;
GLFWwindow* window;
double lastFrameTime;
};
I want to add functionality to load 3d models using the ASSIMP library and render them using the engine. How would I got about doing this?
To load 3D models using ASSIMP and render them using your engine, you can follow these steps:
Add ASSIMP dependency to your project. You can download the library from their GitHub repo, build it, and add its include and library directories to your build configuration.
Create a Model class to handle loading and managing 3D models.
#include "Model.h"
Model::Model()
{
}
Model::~Model()
{
}
void Model::LoadModel(const std::string& filepath, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filepath, aiProcess_Triangulate | aiProcess_JoinIdenticalVertices);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
throw std::runtime_error("Failed to load model: " + std::string(importer.GetErrorString()));
}
LoadNode(scene->mRootNode, scene, device, physicalDevice, commandPool, graphicsQueue);
}
void Model::Cleanup()
{
for(auto& mesh : meshes)
{
mesh.Cleanup();
}
}
const std::vector<Mesh>& Model::GetMeshes() const
{
return meshes;
}
void Model::LoadNode(aiNode* node, const aiScene* scene, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue)
{
for(unsigned int i = 0; i < node->mNumMeshes; i++)
{
meshes.push_back(LoadMesh(scene->mMeshes[node->mMeshes[i]], scene));
meshes.back().Initialize(device, physicalDevice, commandPool, graphicsQueue);
}
for(unsigned int i = 0; i < node->mNumChildren; i++)
{
LoadNode(node->mChildren[i], scene, device, physicalDevice, commandPool, graphicsQueue);
}
}
Mesh Model::LoadMesh(aiMesh* mesh, const aiScene* scene)
{
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
for(unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
vertex.position.x = mesh->mVertices[i].x;
vertex.position.y = mesh->mVertices[i].y;
vertex.position.z = mesh->mVertices[i].z;
// Load vertex color, texture coordinates, normals, etc. if needed.
vertices.push_back(vertex);
}
for(unsigned int i = 0; i < mesh->mNumFaces; i++)
{
if(mesh->mFaces[i].mNumIndices != 3)
{
throw std::runtime_error("Mesh face with not exactly 3 indices found. This engine only supports triangle meshes.");
}
indices.push_back(mesh->mFaces[i].mIndices[0]);
indices.push_back(mesh->mFaces[i].mIndices[1]);
indices.push_back(mesh->mFaces[i].mIndices[2]);
}
Mesh result;
result.SetVertices(vertices);
result.SetIndices(indices);
return result;
}
Load and render the 3D model in your main loop. In your Scene or GameObject class, you can create a Model instance, load a model file, and render the individual meshes using your existing rendering code.
Example of loading the model in Scene or GameObject:
cpp
Model model;
model.LoadModel("path/to/model.obj", device, physicalDevice, commandPool, graphicsQueue);
Example of rendering the model in Scene or GameObject:
cpp
for(const Mesh& mesh : model.GetMeshes())
{
// Render the mesh with your existing rendering code.
}
Make sure to call the Cleanup() method for the Model instance when you no longer need the model data.
code
// Create vertex buffer and index buffer
// (assuming you have helper functions CreateBuffer and CopyBuffer)
// …
Now, the Mesh.Initialize call from the LoadMesh method in Model class should work with the new overloaded Initialize method. Remember to update your Cleanup method in Mesh.cpp to also handle the cases when the method was called without initializing the mesh first, by wrapping the cleanup code within an if block:
cpp
void Mesh::Cleanup()
{
if (device != VK_NULL_HANDLE)
{
// Same cleanup code as before
}
}
I apologize for the oversight. It seems I provided incomplete code for the new Initialize method before. Here's the corrected version with the stagingBuffer and bufferSize initialized.
In this updated Initialize method, we declare and initialize the stagingBuffer and bufferSize variables locally, as they are not needed as member variables of the Mesh class. They are only used during the buffer creation process, so there is no need to store them in the class permanently.