Now Models can be loaded
This commit is contained in:
@@ -20,6 +20,14 @@ foreach(TEXTURE ${TEXTURE_FILES})
|
|||||||
file(COPY ${TEXTURE_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/textures/")
|
file(COPY ${TEXTURE_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/textures/")
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
# Copy models into cmake output
|
||||||
|
file(GLOB MODEL_FILES
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/models/*"
|
||||||
|
)
|
||||||
|
foreach(MODEL ${MODEL_FILES})
|
||||||
|
file(COPY ${MODEL_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/models/")
|
||||||
|
endforeach()
|
||||||
|
|
||||||
# Copy shaders into cmake output
|
# Copy shaders into cmake output
|
||||||
file(GLOB SHADER_FILES
|
file(GLOB SHADER_FILES
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert"
|
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
* - glfw
|
* - glfw
|
||||||
* - glm
|
* - glm
|
||||||
* - stb
|
* - stb
|
||||||
|
* - tinyobjloader
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define DEBUG 1
|
#define DEBUG 1
|
||||||
@@ -14,7 +15,11 @@ int main(int argc, char **argv){
|
|||||||
std::cout << "Program path: " << argv[0] << std::endl;
|
std::cout << "Program path: " << argv[0] << std::endl;
|
||||||
try{
|
try{
|
||||||
vapp::Vulkan app;
|
vapp::Vulkan app;
|
||||||
app.run("Vulkan", 1200, 900);
|
|
||||||
|
app.MODEL_PATH = "models/viking_room.obj";
|
||||||
|
app.TEXTURE_PATH = "textures/viking_room.png";
|
||||||
|
|
||||||
|
app.run("Vulkan", 800, 800);
|
||||||
}
|
}
|
||||||
catch(const std::exception& e){
|
catch(const std::exception& e){
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
|
|||||||
+16053
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 940 KiB |
+55
-3
@@ -7,6 +7,10 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include <stb/stb_image.h>
|
#include <stb/stb_image.h>
|
||||||
|
|
||||||
|
// same as stb
|
||||||
|
#define TINYOBJLOADER_IMPLEMENTATION
|
||||||
|
#include <tiny_obj_loader.h>
|
||||||
|
|
||||||
namespace vapp{
|
namespace vapp{
|
||||||
const std::vector<const char*> validationLayers = {
|
const std::vector<const char*> validationLayers = {
|
||||||
"VK_LAYER_KHRONOS_validation"
|
"VK_LAYER_KHRONOS_validation"
|
||||||
@@ -870,7 +874,7 @@ namespace vapp{
|
|||||||
imageInfo.flags = 0; // something tells me this has another value when using voxels
|
imageInfo.flags = 0; // something tells me this has another value when using voxels
|
||||||
|
|
||||||
if(vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS){
|
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;
|
VkMemoryRequirements memRequirements;
|
||||||
@@ -890,7 +894,13 @@ namespace vapp{
|
|||||||
|
|
||||||
void Vulkan::createTextureImage(){
|
void Vulkan::createTextureImage(){
|
||||||
int texWidth, texHeight, texChannels;
|
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
|
VkDeviceSize imageSize = texWidth * texHeight * 4; // 4 bytes per pixel
|
||||||
|
|
||||||
if(!pixels) throw std::runtime_error("Func: createTextureImage\nError: Failed to load Texture Image!\n");
|
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);
|
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(){
|
void Vulkan::createVertexBuffer(){
|
||||||
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
|
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
|
||||||
|
|
||||||
@@ -1372,7 +1423,7 @@ namespace vapp{
|
|||||||
void Vulkan::initWindow(const char *windowName){
|
void Vulkan::initWindow(const char *windowName){
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // tell glfw to not use opengl
|
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);
|
this->window = glfwCreateWindow(static_cast<int>(_width), static_cast<int>(_height), windowName, nullptr, nullptr);
|
||||||
glfwSetWindowUserPointer(window, this);
|
glfwSetWindowUserPointer(window, this);
|
||||||
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
|
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
|
||||||
@@ -1395,6 +1446,7 @@ namespace vapp{
|
|||||||
createTextureImage();
|
createTextureImage();
|
||||||
createTextureImageView();
|
createTextureImageView();
|
||||||
createTextureSampler();
|
createTextureSampler();
|
||||||
|
loadModel();
|
||||||
createVertexBuffer();
|
createVertexBuffer();
|
||||||
createIndexBuffer();
|
createIndexBuffer();
|
||||||
createUniformBuffers();
|
createUniformBuffers();
|
||||||
|
|||||||
+24
-4
@@ -13,20 +13,19 @@
|
|||||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
|
#include <glm/gtx/hash.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <limits>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace vapp{
|
namespace vapp{
|
||||||
// Change here for other presentMode
|
// Change here for other presentMode
|
||||||
@@ -67,8 +66,13 @@ namespace vapp{
|
|||||||
|
|
||||||
return attributeDescriptions;
|
return attributeDescriptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const Vertex &other) const{
|
||||||
|
return pos == other.pos && color == other.color && texCoord == other.texCoord;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
const std::vector<Vertex> vertices = {
|
const std::vector<Vertex> vertices = {
|
||||||
{{-0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
|
{{-0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
|
||||||
{{0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
{{0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
|
||||||
@@ -86,6 +90,7 @@ namespace vapp{
|
|||||||
0, 1, 2, 2, 3, 0,
|
0, 1, 2, 2, 3, 0,
|
||||||
4, 5, 6, 6, 7, 4
|
4, 5, 6, 6, 7, 4
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
// END Remove
|
// END Remove
|
||||||
|
|
||||||
struct UniformBufferObject{
|
struct UniformBufferObject{
|
||||||
@@ -154,6 +159,8 @@ namespace vapp{
|
|||||||
const int MAX_FRAMES_IN_FLIGHT = 2;
|
const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||||
uint32_t currentFrame = 0;
|
uint32_t currentFrame = 0;
|
||||||
|
|
||||||
|
std::vector<Vertex> vertices;
|
||||||
|
std::vector<uint32_t> indices;
|
||||||
VkBuffer vertexBuffer;
|
VkBuffer vertexBuffer;
|
||||||
VkDeviceMemory vertexBufferMemory;
|
VkDeviceMemory vertexBufferMemory;
|
||||||
VkBuffer indexBuffer;
|
VkBuffer indexBuffer;
|
||||||
@@ -216,6 +223,7 @@ namespace vapp{
|
|||||||
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
|
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
|
||||||
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
||||||
void copyBufferOld(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
void copyBufferOld(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
|
||||||
|
void loadModel();
|
||||||
void createVertexBuffer();
|
void createVertexBuffer();
|
||||||
void createIndexBuffer();
|
void createIndexBuffer();
|
||||||
void createUniformBuffers();
|
void createUniformBuffers();
|
||||||
@@ -241,9 +249,21 @@ namespace vapp{
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool framebufferResized = false;
|
bool framebufferResized = false;
|
||||||
|
std::string MODEL_PATH;
|
||||||
|
std::string TEXTURE_PATH;
|
||||||
|
|
||||||
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
|
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
|
||||||
};
|
};
|
||||||
} // vapp
|
} // vapp
|
||||||
|
|
||||||
|
namespace std{
|
||||||
|
template<> struct hash<vapp::Vertex>{
|
||||||
|
size_t operator()(vapp::Vertex const &vertex) const{
|
||||||
|
return ((hash<glm::vec3>()(vertex.pos) ^
|
||||||
|
(hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^
|
||||||
|
(hash<glm::vec2>()(vertex.texCoord) << 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif //VULKAN_APP_H
|
#endif //VULKAN_APP_H
|
||||||
|
|||||||
Reference in New Issue
Block a user