Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/src/main.cpp

95 lines
2.5 KiB
C++
Raw Normal View History

2018-09-26 17:51:22 -04:00
#include <SDL.h>
2018-09-29 21:03:06 -04:00
#include <SDL_vulkan.h>
2018-10-01 21:02:27 -04:00
#include <iostream>
2018-10-16 08:49:25 -04:00
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "renderer.h"
2018-09-29 21:03:06 -04:00
#include "platform.h"
2018-10-16 08:49:25 -04:00
#include "world.h"
#include "mesh.h"
2018-09-29 21:03:06 -04:00
SDL_Window* window = nullptr;
std::vector<const char*> platform::getRequiredExtensions() {
uint32_t count = 0;
SDL_Vulkan_GetInstanceExtensions(window, &count, nullptr);
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
std::vector<const char*> names(count);
SDL_Vulkan_GetInstanceExtensions(window, &count, names.data());
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
return names;
}
2018-09-26 17:51:22 -04:00
uint32_t platform::getTime() {
return SDL_GetTicks();
}
int main(int, char*[]) {
2018-10-01 21:02:27 -04:00
window = SDL_CreateWindow("Graph", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
if(!window)
return -1;
2018-10-16 08:49:25 -04:00
Renderer* renderer = new Renderer();
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
VkSurfaceKHR surface = nullptr;
SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface);
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
RenderTarget* target = renderer->createSurfaceRenderTarget(surface);
2018-10-16 08:49:25 -04:00
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("suzanne.obj", aiProcess_Triangulate);
aiMesh* m = scene->mMeshes[0];
Mesh* mesh = new Mesh();
for(unsigned int i = 0; i < m->mNumVertices; i++) {
Vertex vertex;
vertex.position = glm::vec3(m->mVertices[i].x, m->mVertices[i].y, m->mVertices[i].z);
2018-10-16 13:03:26 -04:00
vertex.normal = glm::vec3(m->mNormals[i].x, m->mNormals[i].y, m->mNormals[i].z);
2018-10-16 08:49:25 -04:00
mesh->vertices.push_back(vertex);
}
for(unsigned int i = 0; i < m->mNumFaces; i++) {
aiFace face = m->mFaces[i];
for(unsigned int j = 0; j < face.mNumIndices; j++)
mesh->indices.push_back(face.mIndices[j]);
}
renderer->fillMeshBuffers(mesh);
World world;
world.meshes.push_back(mesh);
2018-09-26 17:51:22 -04:00
bool running = true;
while(running) {
SDL_Event event = {};
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
running = false;
2018-10-16 08:49:25 -04:00
2018-10-01 21:02:27 -04:00
if(event.type == SDL_WINDOWEVENT) {
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
target = renderer->createSurfaceRenderTarget(surface, target);
}
2018-09-26 17:51:22 -04:00
}
2018-10-16 08:49:25 -04:00
renderer->render(world, target);
2018-09-26 17:51:22 -04:00
}
2018-10-16 08:49:25 -04:00
2018-10-16 09:01:14 -04:00
renderer->destroyMeshBuffers(mesh);
2018-09-29 21:03:06 -04:00
renderer->destroyRenderTarget(target);
2018-10-16 08:49:25 -04:00
2018-10-01 21:02:27 -04:00
vkDestroySurfaceKHR(renderer->getInstance(), surface, nullptr);
2018-10-16 08:49:25 -04:00
delete renderer;
2018-10-16 08:49:25 -04:00
SDL_DestroyWindow(window);
2018-10-16 08:49:25 -04:00
2018-09-26 17:51:22 -04:00
return 0;
}