Added the texture and the model to the CMAKE
This commit is contained in:
parent
d733b641b8
commit
8f4ff25734
@ -189,6 +189,8 @@ endfunction ()
|
||||
add_chapter (main
|
||||
SHADER shaders/main
|
||||
TEXTURES texture.jpg
|
||||
TEXTURES viking_room.png
|
||||
MODELS viking_room.obj
|
||||
)
|
||||
|
||||
#add_chapter (38_ray_tracing
|
||||
|
||||
37
src/main.cpp
37
src/main.cpp
@ -10,6 +10,7 @@
|
||||
#include <ios>
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <fstream> // to load shaders
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@ -35,11 +36,16 @@ import vulkan_hpp;
|
||||
// Images
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
// OBJ loader
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include <tiny_obj_loader.h>
|
||||
|
||||
// Consts
|
||||
constexpr uint32_t WIDTH = 800;
|
||||
constexpr uint32_t HEIGHT = 600;
|
||||
constexpr int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
const std::string MODEL_PATH = "models/viking_room.obj";
|
||||
const std::string TEXTURE_PATH = "textures/viking_room.png";
|
||||
|
||||
const std::vector<char const*> validationLayers = {
|
||||
"VK_LAYER_KHRONOS_validation"
|
||||
@ -70,6 +76,7 @@ struct Vertex {
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
const std::vector<Vertex> vertices = {
|
||||
{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||
@ -86,6 +93,8 @@ const std::vector<uint16_t> indices = {
|
||||
0, 1, 2, 2, 3, 0,
|
||||
4, 5, 6, 6, 7, 4
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
|
||||
//Uniform buffer
|
||||
@ -142,7 +151,7 @@ class HelloTriangleApplication {
|
||||
vk::raii::DeviceMemory vertexBufferMemory = nullptr;
|
||||
// Index - Buffers
|
||||
vk::raii::Buffer indexBuffer = nullptr;
|
||||
vk::raii::DeviceMemory indexBufferMemory = nullptr;
|
||||
vk::raii::DeviceMemory indexBufferMemory = nullptr;s
|
||||
// Uniform Buffers - Buffers
|
||||
std::vector<vk::raii::Buffer> uniformBuffers;
|
||||
std::vector<vk::raii::DeviceMemory> uniformBuffersMemory;
|
||||
@ -163,6 +172,9 @@ class HelloTriangleApplication {
|
||||
// Indexes
|
||||
uint32_t queueIndex = ~0;
|
||||
uint32_t frameIndex = 0;
|
||||
// Vertices
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
// Bools
|
||||
bool framebufferResized = false;
|
||||
|
||||
@ -679,7 +691,7 @@ class HelloTriangleApplication {
|
||||
std::cout << "Creating a texture image...\n";
|
||||
#endif
|
||||
int texWidth, texHeight, texChannels;
|
||||
stbi_uc* pixels = stbi_load("textures/texture.jpg", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
||||
stbi_uc* pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
||||
vk::DeviceSize imageSize = texWidth * texHeight * 4;
|
||||
|
||||
if (!pixels) {
|
||||
@ -805,6 +817,25 @@ class HelloTriangleApplication {
|
||||
#endif
|
||||
}
|
||||
|
||||
void loadModel() {
|
||||
#ifdef DEBUG
|
||||
std::cout << "Loading the 3D model...\n";
|
||||
#endif
|
||||
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string warn, err;
|
||||
|
||||
if (!tinyobj::LoadObj(&attrib, &matrials, &warn, &err, MODEL_PATH.c_str())) {
|
||||
throw std::runtime_error(warn + err);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "3D model loaded !\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void createVertexBuffer() {
|
||||
#ifdef DEBUG
|
||||
std::cout << "Creating a vertex buffer...\n";
|
||||
@ -1075,7 +1106,7 @@ class HelloTriangleApplication {
|
||||
commandBuffer.beginRendering(renderingInfo);
|
||||
commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, graphicsPipeline );
|
||||
commandBuffer.bindVertexBuffers(0, *vertexBuffer, {0});
|
||||
commandBuffer.bindIndexBuffer( *indexBuffer, 0, vk::IndexType::eUint16 );
|
||||
commandBuffer.bindIndexBuffer( *indexBuffer, 0, vk::IndexType::eUint32 );
|
||||
// Dynamic ViewPort
|
||||
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
|
||||
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0,0), swapChainExtent));
|
||||
|
||||
BIN
textures/viking_room.png
Normal file
BIN
textures/viking_room.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 940 KiB |
Loading…
x
Reference in New Issue
Block a user