.OBJ Loading
This commit is contained in:
parent
ac402dbef8
commit
43fd780e0b
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
CPPFLAGS=-g
|
||||
LDFLAGS=-lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||
LDFLAGS=-lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi -ltinyobjloader
|
||||
DEBUGFLAGS=-DDEBUG -fsanitize=address
|
||||
GDBFLAGS=
|
||||
SRC = $(shell find . -name "*.cpp")
|
||||
|
BIN
assets/textures/viking_room.png
Normal file
BIN
assets/textures/viking_room.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 940 KiB |
2029
lib/tiny_obj_loader.h
Normal file
2029
lib/tiny_obj_loader.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,9 @@ Graphics::graphicspipeline graphicsPipeline;
|
||||
RenderPresent::render renderPresentation;
|
||||
BuffersLibraries::buffers buffers;
|
||||
TextureLibraries::texture texture;
|
||||
ModelLib::model model;
|
||||
VkInstance vulkaninstance;
|
||||
|
||||
//TODO: add global instances?
|
||||
|
||||
// Getters and Setters!
|
||||
@ -67,6 +69,7 @@ void initVulkan() {
|
||||
texture.createTextureImage();
|
||||
texture.createTextureImageView();
|
||||
texture.createTextureSampler();
|
||||
model.loadModel();
|
||||
buffers.createVertexBuffer();
|
||||
buffers.createIndexBuffer();
|
||||
buffers.createUniformBuffers();
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "graphics/render.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "global.h"
|
||||
#include "graphics/model.h"
|
||||
class EntryApp {
|
||||
public:
|
||||
static EntryApp& getInstance();
|
||||
|
@ -30,6 +30,9 @@ namespace Global {
|
||||
VkImage depthImage;
|
||||
VkDeviceMemory depthImageMemory;
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
// Index buffer definition, showing which points to reuse.
|
||||
std::vector<uint32_t> indices;
|
||||
Global::QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
|
||||
// First we feed in a integer we want to use to hold the number of queued items, that fills it, then we create that amount of default constructed *VkQueueFamilyProperties* structs.
|
||||
// These store the flags, the amount of queued items in the family, and timestamp data. Queue families are simply group collections of tasks we want to get done.
|
||||
|
10
src/global.h
10
src/global.h
@ -6,6 +6,7 @@
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
@ -40,6 +41,10 @@ namespace Global {
|
||||
extern VkImageView depthImageView;
|
||||
extern VkImage depthImage;
|
||||
extern VkDeviceMemory depthImageMemory;
|
||||
const std::string MODEL_PATH = "assets/models/viking_room.obj";
|
||||
const std::string TEXTURE_PATH = "assets/textures/viking_room.png";
|
||||
|
||||
|
||||
|
||||
struct UniformBufferObject {
|
||||
float time;
|
||||
@ -82,9 +87,12 @@ namespace Global {
|
||||
return attributeDescriptions;
|
||||
}
|
||||
};
|
||||
|
||||
const uint32_t WIDTH = 800;
|
||||
const uint32_t HEIGHT = 800;
|
||||
|
||||
extern std::vector<Vertex> vertices;
|
||||
// Index buffer definition, showing which points to reuse.
|
||||
extern std::vector<uint32_t> indices;
|
||||
struct QueueFamilyIndices {
|
||||
// We need to check that the Queue families support graphics operations and window presentation, sometimes they can support one or the other,
|
||||
// therefore, we take into account both for completion.
|
||||
|
@ -18,24 +18,6 @@ std::vector<void*> uniformBuffersMapped;
|
||||
namespace BuffersLibraries {
|
||||
|
||||
|
||||
const std::vector<Global::Vertex> vertices = {
|
||||
// X Y Z | R G B | W Q
|
||||
{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.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}, {0.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
|
||||
|
||||
|
||||
{{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}
|
||||
};
|
||||
// Index buffer definition, showing which points to reuse.
|
||||
const std::vector<uint16_t> indices = {
|
||||
0, 1, 2, 2, 3, 0,
|
||||
4, 5, 6, 6, 7, 4
|
||||
};
|
||||
|
||||
uint32_t buffers::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
|
||||
// Graphics cards offer different types of memory to allocate from, here we query to find the right type of memory for our needs.
|
||||
@ -111,7 +93,7 @@ namespace BuffersLibraries {
|
||||
}
|
||||
|
||||
void buffers::createIndexBuffer() {
|
||||
VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();
|
||||
VkDeviceSize bufferSize = sizeof(Global::indices[0]) * Global::indices.size();
|
||||
|
||||
VkBuffer stagingBuffer;
|
||||
VkDeviceMemory stagingBufferMemory;
|
||||
@ -120,7 +102,7 @@ namespace BuffersLibraries {
|
||||
|
||||
void* data;
|
||||
vkMapMemory(Global::device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
||||
memcpy(data, indices.data(), (size_t) bufferSize);
|
||||
memcpy(data, Global::indices.data(), (size_t) bufferSize);
|
||||
|
||||
vkUnmapMemory(Global::device, stagingBufferMemory);
|
||||
|
||||
@ -135,7 +117,7 @@ namespace BuffersLibraries {
|
||||
// Create a Vertex Buffer to hold the vertex information in memory so it doesn't have to be hardcoded!
|
||||
// Size denotes the size of the buffer in bytes, usage in this case is the buffer behaviour, using a bitwise OR.
|
||||
// Sharing mode denostes the same as the images in the swap chain! in this case, only the graphics queue uses this buffer, so we make it exclusive.
|
||||
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
|
||||
VkDeviceSize bufferSize = sizeof(Global::vertices[0]) * Global::vertices.size();
|
||||
|
||||
VkBuffer stagingBuffer;
|
||||
VkDeviceMemory stagingBufferMemory;
|
||||
@ -143,7 +125,7 @@ namespace BuffersLibraries {
|
||||
|
||||
void* data;
|
||||
vkMapMemory(Global::device, stagingBufferMemory, 0, bufferSize, 0, &data);
|
||||
memcpy(data, vertices.data(), (size_t) bufferSize);
|
||||
memcpy(data, Global::vertices.data(), (size_t) bufferSize);
|
||||
vkUnmapMemory(Global::device, stagingBufferMemory);
|
||||
|
||||
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
|
||||
@ -167,12 +149,7 @@ namespace BuffersLibraries {
|
||||
VkBuffer buffers::getIndexBuffer() {
|
||||
return indexBuffer;
|
||||
}
|
||||
std::vector<Global::Vertex> buffers::getVertices() {
|
||||
return vertices;
|
||||
}
|
||||
std::vector<uint16_t> buffers::getIndices() {
|
||||
return indices;
|
||||
}
|
||||
|
||||
// ------------------------------ Uniform Buffer Setup -------------------------------- //
|
||||
void buffers::createDescriptorSetLayout() {
|
||||
// Create a table of pointers to data, a Descriptor Set!
|
||||
@ -231,10 +208,10 @@ namespace BuffersLibraries {
|
||||
Global::UniformBufferObject ubo{};
|
||||
ubo.time = time;
|
||||
// Modify the model projection transformation to rotate around the Z over time.
|
||||
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(20.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
// Modify the view transformation to look at the object from above at a 45 degree angle.
|
||||
// This takes the eye position, center position, and the up direction.
|
||||
ubo.view = glm::lookAt(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
// 45 degree field of view, set aspect ratio, and near and far clipping range.
|
||||
ubo.proj = glm::perspective(glm::radians(45.0f), deviceLibrary.getSwapChainExtent().width / (float) deviceLibrary.getSwapChainExtent().height, 0.1f, 10.0f);
|
||||
|
||||
|
@ -11,8 +11,6 @@ namespace BuffersLibraries {
|
||||
void destroyBuffers();
|
||||
VkBuffer getVertexBuffer();
|
||||
VkBuffer getIndexBuffer();
|
||||
std::vector<Global::Vertex> getVertices();
|
||||
std::vector<uint16_t> getIndices();
|
||||
void createDescriptorSetLayout();
|
||||
void createUniformBuffers();
|
||||
void updateUniformBuffer(uint32_t currentImage);
|
||||
|
@ -354,11 +354,11 @@ namespace Graphics {
|
||||
VkBuffer vertexBuffers[] = {buffers.getVertexBuffer()};
|
||||
VkDeviceSize offsets[] = {0};
|
||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
||||
vkCmdBindIndexBuffer(commandBuffer, buffers.getIndexBuffer(), 0, VK_INDEX_TYPE_UINT16);
|
||||
vkCmdBindIndexBuffer(commandBuffer, buffers.getIndexBuffer(), 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &Global::descriptorSets[Global::currentFrame], 0, nullptr);
|
||||
|
||||
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(buffers.getIndices().size()), 1, 0, 0, 0);
|
||||
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(Global::indices.size()), 1, 0, 0, 0);
|
||||
vkCmdEndRenderPass(commandBuffer);
|
||||
|
||||
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
|
||||
|
47
src/graphics/model.cpp
Normal file
47
src/graphics/model.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "buffers.h"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#define TINY_OBJ_IMPLEMENTATION
|
||||
#include <tiny_obj_loader.h>
|
||||
#include "model.h"
|
||||
namespace ModelLib {
|
||||
BuffersLibraries::buffers buf;
|
||||
|
||||
void model::loadModel() {
|
||||
tinyobj::ObjReaderConfig readerConfig;
|
||||
|
||||
tinyobj::ObjReader reader;
|
||||
|
||||
if(!reader.ParseFromFile(Global::MODEL_PATH, readerConfig)) {
|
||||
if(!reader.Error().empty()) {
|
||||
throw std::runtime_error(reader.Error());
|
||||
}
|
||||
}
|
||||
|
||||
auto& attrib = reader.GetAttrib();
|
||||
auto& shapes = reader.GetShapes();
|
||||
auto& materials = reader.GetMaterials();
|
||||
|
||||
for (const auto& shape : shapes) {
|
||||
for (const auto& index : shape.mesh.indices) {
|
||||
Global::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};
|
||||
|
||||
Global::vertices.push_back(vertex);
|
||||
Global::indices.push_back(Global::indices.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
src/graphics/model.h
Normal file
8
src/graphics/model.h
Normal file
@ -0,0 +1,8 @@
|
||||
#include "../global.h"
|
||||
|
||||
namespace ModelLib {
|
||||
class model {
|
||||
public:
|
||||
void loadModel();
|
||||
};
|
||||
}
|
@ -190,7 +190,7 @@ void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t
|
||||
// Import pixels from image with data on color channels, width and height, and colorspace!
|
||||
// Its a lot of kind of complicated memory calls to bring it from a file -> to a buffer -> to a image object.
|
||||
int textureWidth, textureHeight, textureChannels;
|
||||
stbi_uc* pixels = stbi_load("assets/textures/test.png", &textureWidth, &textureHeight, &textureChannels, STBI_rgb_alpha);
|
||||
stbi_uc* pixels = stbi_load(Global::TEXTURE_PATH.c_str(), &textureWidth, &textureHeight, &textureChannels, STBI_rgb_alpha);
|
||||
|
||||
VkDeviceSize imageSize = textureWidth * textureHeight * 4;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user