Now Models can be loaded

This commit is contained in:
Ano-sys
2025-04-06 00:36:45 +02:00
parent edc4931b67
commit a4a1847f95
6 changed files with 16146 additions and 8 deletions
+55 -3
View File
@@ -7,6 +7,10 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
// same as stb
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
namespace vapp{
const std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
@@ -870,7 +874,7 @@ namespace vapp{
imageInfo.flags = 0; // something tells me this has another value when using voxels
if(vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS){
throw std::runtime_error("Func: createTextureImage\nError: Failed to create Image!\n");
throw std::runtime_error("Func: createImage\nError: Failed to create Image!\n");
}
VkMemoryRequirements memRequirements;
@@ -890,7 +894,13 @@ namespace vapp{
void Vulkan::createTextureImage(){
int texWidth, texHeight, texChannels;
stbi_uc *pixels = stbi_load("textures/texture.jpeg", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
if(MODEL_PATH == "" || TEXTURE_PATH == ""){
throw std::runtime_error("Func: createTextureImage\nError: No file was given in MODEL_PATH or TEXTURE_PATH!\n");
}
stbi_uc *pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
VkDeviceSize imageSize = texWidth * texHeight * 4; // 4 bytes per pixel
if(!pixels) throw std::runtime_error("Func: createTextureImage\nError: Failed to load Texture Image!\n");
@@ -1043,6 +1053,47 @@ namespace vapp{
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
}
void Vulkan::loadModel(){
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn, err;
if(!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, MODEL_PATH.c_str())){
throw std::runtime_error("Func: loadModel\nError: " + warn + err + "\n");
}
std::unordered_map<Vertex, uint32_t> uniqueVertices{};
for(const auto &shape : shapes){
for(const auto &index : shape.mesh.indices){
Vertex vertex{};
vertex.pos = {
attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2],
};
vertex.texCoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.0f - attrib.texcoords[2 * index.texcoord_index + 1],
};
vertex.color = { 1.0f, 1.0f, 1.0f };
if(!uniqueVertices.contains(vertex)){
uniqueVertices[vertex] = static_cast<uint32_t>(vertices.size());
vertices.push_back(vertex);
}
indices.push_back(uniqueVertices[vertex]);
}
}
}
void Vulkan::createVertexBuffer(){
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
@@ -1372,7 +1423,7 @@ namespace vapp{
void Vulkan::initWindow(const char *windowName){
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // tell glfw to not use opengl
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // disable window resize
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // disable window resize
this->window = glfwCreateWindow(static_cast<int>(_width), static_cast<int>(_height), windowName, nullptr, nullptr);
glfwSetWindowUserPointer(window, this);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
@@ -1395,6 +1446,7 @@ namespace vapp{
createTextureImage();
createTextureImageView();
createTextureSampler();
loadModel();
createVertexBuffer();
createIndexBuffer();
createUniformBuffers();