Fix bugs, add mesh gen

This commit is contained in:
Bailey Harrison 2023-01-06 16:45:39 +00:00
parent cf99262dc8
commit d7d8720c75
13 changed files with 130 additions and 82 deletions

View File

@ -1,5 +1,7 @@
#pragma once
#include "log.hpp"
#include "gfx.hpp"
#include "gfx_device.hpp"
@ -24,22 +26,16 @@ public:
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices)
: m_gfx(gfx)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
std::vector<uint32_t> indices(m_count);
for (uint32_t i = 0; i < m_count; i++) {
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = indices.size();
initMesh(vertices, indices);
}
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
: m_gfx(gfx)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = indices.size();
initMesh(vertices, indices);
}
~Mesh()
{
@ -60,6 +56,14 @@ private:
const gfx::Buffer* m_ib;
uint32_t m_count;
void initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = indices.size();
INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
};
}

View File

@ -1,5 +1,7 @@
#pragma once
#include "log.hpp"
#include "gfx.hpp"
#include "gfx_device.hpp"
@ -65,6 +67,8 @@ public:
vertFormat.stride = stride;
m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace);
INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size());
}
~Shader()
{

View File

@ -23,6 +23,6 @@ void main() {
fragPos = vec3(constants.view * constants.model * vec4(inPosition, 1.0));
fragNorm = mat3(transpose(inverse(constants.view * constants.model))) * inNorm;
fragUV = inUV;
vec3 lightPos = vec3(-500.0, 500.0, 40.0);
vec3 lightPos = vec3(-10.0, 10.0, 10.0);
fragLightPos = vec3(constants.view * vec4(lightPos, 1.0));
}

View File

@ -81,11 +81,17 @@ namespace engine {
/* logic */
m_sceneManager->updateActiveScene(m_window->dt());
if(m_window->getKeyPress(inputs::Key::K_L)) {
// m_enableFrameLimiter = !m_enableFrameLimiter;
if(m_window->getKeyPress(inputs::Key::K_F)) [[unlikely]] {
m_window->infoBox("fps", std::to_string(m_window->getFPS()) + " fps " + std::to_string(m_window->dt() * 1000.0f) + " ms");
}
uint64_t now = m_window->getNanos();
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
lastTick = now;
INFO("fps: {}", m_window->getAvgFPS());
m_window->resetAvgFPS();
}
/* draw */
m_gfx->renderFrame();
@ -93,12 +99,6 @@ namespace engine {
m_window->getInputAndEvents();
/* fps limiter */
uint64_t now = m_window->getNanos();
if (now - lastTick >= 1000000000LL * 10LL) {
lastTick = now;
m_window->setTitle(std::to_string(m_window->getAvgFPS()));
m_window->resetAvgFPS();
}
if (m_enableFrameLimiter) {
std::this_thread::sleep_until(endFrame);
}

View File

@ -283,7 +283,7 @@ namespace engine {
switch (messageSeverity) {
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
TRACE("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage);
DEBUG("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
INFO("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage);
@ -1233,7 +1233,7 @@ namespace engine {
VkPhysicalDeviceProperties devProps;
vkGetPhysicalDeviceProperties(pimpl->physicalDevice, &devProps);
INFO("Selected physical device: {}", devProps.deviceName);
DEBUG("Selected physical device: {}", devProps.deviceName);
@ -1702,7 +1702,6 @@ namespace engine {
auto vertShaderCode = util::readTextFile(vertShaderPath);
auto fragShaderCode = util::readTextFile(fragShaderPath);
INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string());
VkShaderModule vertShaderModule = compileShader(pimpl->device, shaderc_vertex_shader, vertShaderCode->data(), vertShaderPath);
VkShaderModule fragShaderModule = compileShader(pimpl->device, shaderc_fragment_shader, fragShaderCode->data(), fragShaderPath);

View File

@ -21,7 +21,7 @@ Texture::Texture(GFXDevice* gfxDevice, const std::string& path)
m_gpuTexture = m_gfxDevice->createTexture(texbuf->data(), (uint32_t)width, (uint32_t)height, gfx::TextureFilter::LINEAR, filter);
DEBUG("loaded texture {}, width: {} height: {} size: {}", path, width, height, texbuf->size());
INFO("Loaded texture: {}, width: {} height: {}", path, width, height);
}

View File

@ -24,7 +24,7 @@ namespace engine {
void SceneManager::updateActiveScene(float ts)
{
if (m_activeSceneIndex >= 0) {
if (m_activeSceneIndex >= 0) [[likely]] {
assert((size_t)m_activeSceneIndex < m_scenes.size());
m_scenes[m_activeSceneIndex]->update(ts);
}

View File

@ -48,11 +48,12 @@ namespace engine {
for (uint32_t entity : m_entities) {
auto t = m_scene->getComponent<TransformComponent>(entity);
auto r = m_scene->getComponent<RenderableComponent>(entity);
DEBUG("rendering mesh: {}, parent: {}", t->tag, t->parent);
assert(r->material != nullptr);
assert(r->mesh != nullptr);
assert(r->material->m_texture != nullptr);
gfx->updateUniformBuffer(r->material->getShader()->getPipeline(), &projMatrix, sizeof(projMatrix), 0);

View File

@ -105,7 +105,7 @@ namespace engine::util {
public:
void write(const char* message) override {
(void)message;
DEBUG("ASSIMP: {}", message);
TRACE("ASSIMP: {}", message);
}
};
@ -160,23 +160,23 @@ namespace engine::util {
assert(scene->HasLights() == false);
assert(scene->hasSkeletons() == false);
INFO("material count: {}, mesh count: {}", scene->mNumMaterials, scene->mNumMeshes);
TRACE("material count: {}, mesh count: {}", scene->mNumMaterials, scene->mNumMeshes);
std::map<int, std::shared_ptr<resources::Texture>> textures{};
for (uint32_t i = 0; i < scene->mNumMaterials; i++) {
const aiMaterial* m = scene->mMaterials[i];
INFO("Material {}:", i);
INFO(" Name: {}", m->GetName().C_Str());
TRACE("Material {}:", i);
TRACE(" Name: {}", m->GetName().C_Str());
for (uint32_t j = 0; j < m->mNumProperties; j++) {
const aiMaterialProperty* p = m->mProperties[j];
INFO(" prop {}, key: {}", j, p->mKey.C_Str());
[[maybe_unused]] const aiMaterialProperty* p = m->mProperties[j];
TRACE(" prop {}, key: {}", j, p->mKey.C_Str());
}
if (aiGetMaterialTextureCount(m, aiTextureType_DIFFUSE) >= 1) {
aiString texPath{};
aiGetMaterialTexture(m, aiTextureType_DIFFUSE, 0, &texPath);
INFO(" Diffuse tex: {}", texPath.C_Str());
TRACE(" Diffuse tex: {}", texPath.C_Str());
std::filesystem::path absPath = path;
absPath = absPath.parent_path();
absPath /= texPath.C_Str();
@ -195,8 +195,8 @@ namespace engine::util {
meshMaterialIndices.push_back(m->mMaterialIndex);
std::vector<Vertex> vertices(m->mNumVertices);
std::vector<uint32_t> indices(m->mNumFaces * 3);
INFO("Mesh {}: vertex count {}", i, vertices.size());
INFO("Mesh {}: index count {}", i, indices.size());
TRACE("Mesh {}: vertex count {}", i, vertices.size());
TRACE("Mesh {}: index count {}", i, indices.size());
for (uint32_t j = 0; j < vertices.size(); j++) {
Vertex v{};
@ -228,6 +228,8 @@ namespace engine::util {
buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj);
INFO("Loaded model: {}, meshes: {}, textures: {}", scene->GetShortFilename(path.c_str()), meshes.size(), textures.size());
Assimp::DefaultLogger::kill();
return obj;
}

View File

@ -13,6 +13,8 @@ set(GAME_SOURCES
src/game.hpp
src/camera_controller.cpp
src/camera_controller.hpp
src/meshgen.cpp
src/meshgen.hpp
)

View File

@ -1,6 +1,7 @@
#include "config.h"
#include "camera_controller.hpp"
#include "meshgen.hpp"
#include "application.hpp"
#include "window.hpp"
@ -108,7 +109,27 @@ void playGame()
auto keepTexture = myScene->addResource<engine::resources::Texture>("whiteTexture", std::move(whiteTexture));
auto keepShader = myScene->addResource<engine::resources::Shader>("theShader", std::move(theShader));
engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
// uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
// myScene->addComponent<RotateComponent>(astronaut);
uint32_t sphere = myScene->createEntity("sphere");
auto sphereRenderable = myScene->addComponent<engine::RenderableComponent>(sphere);
sphereRenderable->material = std::make_unique<engine::resources::Material>(keepShader);
sphereRenderable->material->m_texture = keepTexture;
sphereRenderable->mesh = genSphereMesh(app.gfx(), 100.0f, 100, true);
uint32_t light = myScene->createEntity("light");
myScene->getComponent<engine::TransformComponent>(light)->position = glm::vec3{-10.0f, 10.0f, 10.0f};
auto lightRenderable = myScene->addComponent<engine::RenderableComponent>(light);
lightRenderable->material = sphereRenderable->material;
lightRenderable->mesh = genSphereMesh(app.gfx(), 0.5f, 10, false, true);
uint32_t floor = myScene->createEntity("floor");
// myScene->getComponent<engine::TransformComponent>(floor)->position = glm::vec3{-50.0f, -0.5f, -50.0f};
auto floorRenderable = myScene->addComponent<engine::RenderableComponent>(floor);
floorRenderable->material = sphereRenderable->material;
floorRenderable->mesh = genCuboidMesh(app.gfx(), 1.0f, 1.0f, 1.0f);
myScene->addComponent<RotateComponent>(floor);
app.gameLoop();

View File

@ -4,15 +4,13 @@
#include <glm/ext.hpp>
#include <glm/trigonometric.hpp>
#include <iostream>
#include <stdexcept>
#include <thread>
std::unique_ptr<engine::resources::Mesh> genSphereMesh(float r, int detail, bool windInside)
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside, bool flipNormals)
{
using namespace glm;
std::vector<Vertex> vertices{};
std::vector<engine::Vertex> vertices{};
float angleStep = two_pi<float>() / (float)detail;
@ -66,15 +64,18 @@ std::unique_ptr<engine::resources::Mesh> genSphereMesh(float r, int detail, bool
}
glm::vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos;
glm::vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos;
glm::vec3 norm = glm::normalize(glm::cross(vector1, vector2));
vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos;
vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos;
vec3 norm = normalize(cross(vector1, vector2));
// TODO: FIX NORMALS
if (!windInside)
norm = -norm;
if (flipNormals)
norm = -norm;
for (auto it = vertices.end() - 6; it != vertices.end(); it++) {
it->norm = norm;
}
@ -82,54 +83,68 @@ std::unique_ptr<engine::resources::Mesh> genSphereMesh(float r, int detail, bool
}
}
return std::make_unique<engine::resources::Mesh>(vertices);
return std::make_unique<engine::resources::Mesh>(gfx, vertices);
}
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(float x, float y, float z)
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z)
{
// x goes ->
// y goes ^
// z goes into the screen
using glm::vec3;
using namespace glm;
std::vector<Vertex> v{};
std::vector<engine::Vertex> v{};
// 0 top_left_front
v.push_back({{ 0.0f, y, 0.0f }, {}, {}});
// 1 bottom_left_front
v.push_back({{ 0.0f, 0.0f, 0.0f }, {}, {}});
// 2 top_right_front
v.push_back({{ x, y, 0.0f }, {}, {}});
// 3 bottom_right_front
v.push_back({{ x, 0.0f, 0.0f }, {}, {}});
// front
v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
// 4 top_left_back
v.push_back({{ 0.0f, y, z }, {}, {}});
// 5 bottom_left_back
v.push_back({{ 0.0f, 0.0f, z }, {}, {}});
// 6 top_right_back
v.push_back({{ x, y, z }, {}, {}});
// 7 bottom_right_back
v.push_back({{ x, 0.0f, z }, {}, {}});
// back
v.push_back({{0.0f, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
// front quad
std::vector<unsigned int> indices{
// front
0, 1, 3, 2, 0, 3,
// back
4, 5, 7, 6, 4, 7,
// bottom
5, 1, 3, 7, 5, 3,
// top
4, 0, 2, 6, 4, 2,
// left
4, 5, 1, 0, 4, 1,
// right
2, 3, 7, 6, 2, 7
};
// left
v.push_back({{0.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
return std::make_unique<engine::resources::Mesh>(v, indices);
// right
v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
// bottom
v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
// top
v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
return std::make_unique<engine::resources::Mesh>(gfx, v);
}

View File

@ -3,5 +3,5 @@
#include "resources/mesh.hpp"
// generates a UV sphere
std::unique_ptr<engine::resources::Mesh> genSphereMesh(float r, int detail, bool windInside = false);
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(float x, float y, float z);
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false);
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z);