This commit is contained in:
Bailey Harrison 2023-11-28 12:50:55 +00:00
parent 33ee5aa321
commit 7aca66ff06
13 changed files with 35396 additions and 86 deletions

View File

@ -28,7 +28,10 @@ set(SRC_FILES
"src/imgui/imgui_widgets.cpp" "src/imgui/imgui_widgets.cpp"
"src/imgui/imstb_textedit.h" "src/imgui/imstb_textedit.h"
"src/input_manager.cpp" "src/input_manager.cpp"
"src/libs/json.hpp"
"src/libs/stb_impl.cpp" "src/libs/stb_impl.cpp"
"src/libs/tiny_gltf.h"
"src/libs/tiny_gltf_impl.cpp"
"src/renderer.cpp" "src/renderer.cpp"
"src/resources/font.cpp" "src/resources/font.cpp"
"src/resources/material.cpp" "src/resources/material.cpp"

View File

@ -15,8 +15,8 @@ layout(location = 0) out vec4 outColor;
void main() { void main() {
// constants // constants
vec3 lightColor = vec3(1.0, 1.0, 1.0); vec3 lightColor = vec3(1.0, 0.9, 0.9);
vec3 ambientColor = vec3(1.0, 1.0, 1.0); vec3 ambientColor = vec3(1.0, 0.0, 0.0);
float ambientStrength = 0.05; float ambientStrength = 0.05;
vec3 emission = vec3(0.0, 0.0, 0.0); vec3 emission = vec3(0.0, 0.0, 0.0);

View File

Before

Width:  |  Height:  |  Size: 140 B

After

Width:  |  Height:  |  Size: 140 B

View File

@ -167,9 +167,13 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
/* default textures */ /* default textures */
{ {
auto whiteTexture = LoadTextureFromFile(GetResourcePath("engine/textures/white.png"), Texture::Filtering::kOff, renderer()); auto whiteTexture = LoadTextureFromFile(GetResourcePath("engine/textures/white.png"), Texture::Filtering::kOff, renderer(), true);
GetResourceManager<Texture>()->AddPersistent("builtin.white", std::move(whiteTexture)); GetResourceManager<Texture>()->AddPersistent("builtin.white", std::move(whiteTexture));
} }
{
auto normalTexture = LoadTextureFromFile(GetResourcePath("engine/textures/normal.png"), Texture::Filtering::kOff, renderer(), false);
GetResourceManager<Texture>()->AddPersistent("builtin.normal", std::move(normalTexture));
}
} }
Application::~Application() Application::~Application()

View File

@ -727,7 +727,7 @@ gfx::DrawBuffer* GFXDevice::BeginRender()
std::array<VkClearValue, 2> clearValues{}; // Using same value for all components enables std::array<VkClearValue, 2> clearValues{}; // Using same value for all components enables
// compression according to NVIDIA Best Practices // compression according to NVIDIA Best Practices
clearValues[0].color.float32[0] = 0.0f; clearValues[0].color.float32[0] = 0.02f;
clearValues[0].color.float32[1] = 0.0f; clearValues[0].color.float32[1] = 0.0f;
clearValues[0].color.float32[2] = 0.0f; clearValues[0].color.float32[2] = 0.0f;
clearValues[0].color.float32[3] = 1.0f; clearValues[0].color.float32[3] = 1.0f;

26753
src/libs/json.hpp Normal file

File diff suppressed because it is too large Load Diff

8485
src/libs/tiny_gltf.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
#define TINYGLTF_IMPLEMENTATION
#include "libs/tiny_gltf.h"

View File

@ -3,14 +3,71 @@
#include "log.h" #include "log.h"
#include "util/files.h" #include "util/files.h"
#include "libs/tiny_gltf.h"
#include "components/mesh_renderable.h"
namespace tg = tinygltf;
namespace engine::util { namespace engine::util {
engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic) engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
{ {
auto file = ReadTextFile(path); tg::TinyGLTF loader;
tg::Model model;
std::string err, warn;
loader.SetParseStrictness(tg::ParseStrictness::Strict);
const bool success = loader.LoadBinaryFromFile(&model, &err, &warn, path);
if (!warn.empty()) {
LOG_WARN("glTF Loader: {}", warn);
}
if (!err.empty()) {
LOG_ERROR("glTF Loader: {}", err);
}
if (!success) {
throw std::runtime_error("Failed to load glTF file!");
}
LOG_INFO("Loaded glTF model, contains {} scenes", model.scenes.size());
// test model loading
if (model.scenes.size() < 1) {
throw std::runtime_error("Need at least 1 scene");
}
int scene_index = 0;
if (model.defaultScene != -1) scene_index = model.defaultScene;
tg::Scene& s = model.scenes.at(scene_index);
if (s.nodes.size() < 1) {
throw std::runtime_error("Need at least 1 node in the scene");
}
const tg::Node& node = model.nodes.at(s.nodes[0]);
const Entity e = scene.CreateEntity("test_node", 0);
//const tg::Mesh& gt_mesh = model.meshes.at(0);
//std::vector<uint32_t> indices;
//model.buffers[0].
//auto mesh = std::make_unique<Mesh>(scene.app()->renderer()->GetDevice(), vertices, indices);
//auto mr = scene.AddComponent<MeshRenderableComponent>(e);
// if (node.mesh)
return static_cast<Entity>(0u); return static_cast<Entity>(0u);
} }

View File

@ -83,13 +83,14 @@ static void buildGraph(const std::map<int, std::shared_ptr<Texture>>& textures,
scene->GetComponent<TransformComponent>(child)->is_static = is_static; scene->GetComponent<TransformComponent>(child)->is_static = is_static;
auto childRenderer = scene->AddComponent<MeshRenderableComponent>(child); auto childRenderer = scene->AddComponent<MeshRenderableComponent>(child);
childRenderer->mesh = meshes[parentNode->mMeshes[i]]; childRenderer->mesh = meshes[parentNode->mMeshes[i]];
childRenderer->material = std::make_shared<Material>(scene->app()->renderer(), scene->app()->GetResource<Shader>("builtin.standard")); childRenderer->material = std::make_shared<Material>(scene->app()->renderer(), scene->app()->GetResource<Shader>("builtin.fancy"));
if (textures.contains(meshTextureIndices[parentNode->mMeshes[i]])) { if (textures.contains(meshTextureIndices[parentNode->mMeshes[i]])) {
childRenderer->material->SetAlbedoTexture(textures.at(meshTextureIndices[parentNode->mMeshes[i]])); childRenderer->material->SetAlbedoTexture(textures.at(meshTextureIndices[parentNode->mMeshes[i]]));
} }
else { else {
childRenderer->material->SetAlbedoTexture(scene->app()->GetResource<Texture>("builtin.white")); childRenderer->material->SetAlbedoTexture(scene->app()->GetResource<Texture>("builtin.white"));
} }
childRenderer->material->SetNormalTexture(scene->app()->GetResource<Texture>("builtin.normal"));
} }
for (uint32_t i = 0; i < parentNode->mNumChildren; i++) { for (uint32_t i = 0; i < parentNode->mNumChildren; i++) {

BIN
test/res/models/teapot.glb Normal file

Binary file not shown.

View File

@ -15,97 +15,97 @@
#include "window.h" #include "window.h"
CameraControllerSystem::CameraControllerSystem(engine::Scene* scene) CameraControllerSystem::CameraControllerSystem(engine::Scene* scene)
: System(scene, {typeid(engine::TransformComponent).hash_code(), : System(scene, {typeid(engine::TransformComponent).hash_code(), typeid(CameraControllerComponent).hash_code()})
typeid(CameraControllerComponent).hash_code()}) {} {
}
void CameraControllerSystem::OnUpdate(float ts) { void CameraControllerSystem::OnUpdate(float ts)
if (t == nullptr || c == nullptr) { {
for (engine::Entity entity : entities_) { if (t == nullptr || c == nullptr) {
t = scene_->GetComponent<engine::TransformComponent>(entity); for (engine::Entity entity : entities_) {
c = scene_->GetComponent<CameraControllerComponent>(entity); t = scene_->GetComponent<engine::TransformComponent>(entity);
break; c = scene_->GetComponent<CameraControllerComponent>(entity);
break;
}
if (t == nullptr) return;
if (c == nullptr) return;
} }
if (t == nullptr) return;
if (c == nullptr) return;
}
const float dt = ts; const float dt = ts;
// in metres per second // in metres per second
float speed = c->kWalkSpeed; float speed = c->kWalkSpeed;
if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f; if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f;
float dx = scene_->app()->input_manager()->GetAxis("movex"); float dx = scene_->app()->input_manager()->GetAxis("movex");
float dy = (-scene_->app()->input_manager()->GetAxis("movey")); float dy = (-scene_->app()->input_manager()->GetAxis("movey"));
// calculate new pitch and yaw // calculate new pitch and yaw
constexpr float kMaxPitch = glm::pi<float>(); constexpr float kMaxPitch = glm::pi<float>();
constexpr float kMinPitch = 0.0f; constexpr float kMinPitch = 0.0f;
float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * c->kCameraSensitivity;
c->kCameraSensitivity; c->pitch += d_pitch;
c->pitch += d_pitch; if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) {
if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) { c->pitch -= d_pitch;
c->pitch -= d_pitch; }
} c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * c->kCameraSensitivity;
c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f *
c->kCameraSensitivity;
// update position relative to camera direction in xz plane // update position relative to camera direction in xz plane
const glm::vec3 d2x_rotated = glm::rotateZ(glm::vec3{dx, 0.0f, 0.0f}, c->yaw); const glm::vec3 d2x_rotated = glm::rotateZ(glm::vec3{dx, 0.0f, 0.0f}, c->yaw);
const glm::vec3 d2y_rotated = const glm::vec3 d2y_rotated = glm::rotateZ(glm::rotateX(glm::vec3{0.0f, 0.0f, dy}, c->pitch), c->yaw);
glm::rotateZ(glm::rotateX(glm::vec3{0.0f, 0.0f, dy}, c->pitch), c->yaw); glm::vec3 h_vel = (d2x_rotated + d2y_rotated);
glm::vec3 h_vel = (d2x_rotated + d2y_rotated); h_vel *= speed;
h_vel *= speed; t->position += h_vel * dt;
t->position += h_vel * dt;
constexpr float kMaxDistanceFromOrigin = 10000.0f; constexpr float kMaxDistanceFromOrigin = 10000.0f;
if (glm::length(t->position) > kMaxDistanceFromOrigin) { if (glm::length(t->position) > kMaxDistanceFromOrigin) {
t->position = {0.0f, 5.0f, 0.0f}; t->position = {0.0f, 5.0f, 0.0f};
} }
/* ROTATION STUFF */ /* ROTATION STUFF */
// pitch quaternion // pitch quaternion
const float half_pitch = c->pitch / 2.0f; const float half_pitch = c->pitch / 2.0f;
glm::quat pitch_quat{}; glm::quat pitch_quat{};
pitch_quat.x = glm::sin(half_pitch); pitch_quat.x = glm::sin(half_pitch);
pitch_quat.y = 0.0f; pitch_quat.y = 0.0f;
pitch_quat.z = 0.0f; pitch_quat.z = 0.0f;
pitch_quat.w = glm::cos(half_pitch); pitch_quat.w = glm::cos(half_pitch);
// yaw quaternion // yaw quaternion
const float half_yaw = c->yaw / 2.0f; const float half_yaw = c->yaw / 2.0f;
glm::quat yaw_quat{}; glm::quat yaw_quat{};
yaw_quat.x = 0.0f; yaw_quat.x = 0.0f;
yaw_quat.y = 0.0f; yaw_quat.y = 0.0f;
yaw_quat.z = glm::sin(half_yaw); yaw_quat.z = glm::sin(half_yaw);
yaw_quat.w = glm::cos(half_yaw); yaw_quat.w = glm::cos(half_yaw);
// update rotation // update rotation
t->rotation = yaw_quat * pitch_quat; t->rotation = yaw_quat * pitch_quat;
/* user interface inputs */ /* user interface inputs */
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) { if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) {
std::string pos_string{"x: " + std::to_string(t->position.x) + std::string pos_string{"x: " + std::to_string(t->position.x) + " y: " + std::to_string(t->position.y) + " z: " + std::to_string(t->position.z)};
" y: " + std::to_string(t->position.y) + LOG_INFO("position {}", pos_string);
" z: " + std::to_string(t->position.z)}; }
LOG_INFO("position {}", pos_string);
}
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) { if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) {
t->position = {0.0f, 5.0f, 0.0f}; t->position = {0.0f, 5.0f, 0.0f};
} }
if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) { if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) {
scene_->app()->window()->ToggleFullscreen(); scene_->app()->window()->ToggleFullscreen();
} }
if (scene_->app()->input_manager()->GetButtonPress("exit")) { if (scene_->app()->input_manager()->GetButtonPress("exit")) {
//scene_->app()->window()->SetCloseFlag(); scene_->app()->window()->SetCloseFlag();
scene_->app()->scene_manager()->SetActiveScene(next_scene_); }
}
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_F)) {
scene_->app()->scene_manager()->SetActiveScene(next_scene_);
}
} }

View File

@ -168,16 +168,19 @@ void PlayGame(GameSettings settings)
engine::util::LoadMeshFromFile(scene2, app.GetResourcePath("models/MY_AXES.dae"), true); engine::util::LoadMeshFromFile(scene2, app.GetResourcePath("models/MY_AXES.dae"), true);
} }
{ /* a cube with a normal map */ { /* floor */
engine::Entity wall2 = scene2->CreateEntity("wall2", 0, glm::vec3{ 60.0f, 0.0f, 0.0f });
engine::Entity pivot = scene2->CreateEntity("pivot", 0, glm::vec3{ 0.0f, 0.0f, 0.0f });
engine::Entity wall2 = scene2->CreateEntity("wall2", pivot, glm::vec3{-50.0f, -50.0f, 0.0f});
auto wall_renderable = scene2->AddComponent<engine::MeshRenderableComponent>(wall2); auto wall_renderable = scene2->AddComponent<engine::MeshRenderableComponent>(wall2);
wall_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 8.0f, 8.0f, 8.0f); wall_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 100.0f, 100.0f, 0.1f, 100.0f);
wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.fancy")); wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.fancy"));
std::shared_ptr<engine::Texture> albedo_texture = std::shared_ptr<engine::Texture> albedo_texture =
engine::LoadTextureFromFile(app.GetResourcePath("textures/brickwall_albedo.jpg"), engine::Texture::Filtering::kBilinear, app.renderer()); engine::LoadTextureFromFile(app.GetResourcePath("textures/brickwall_albedo.jpg"), engine::Texture::Filtering::kAnisotropic, app.renderer());
std::shared_ptr<engine::Texture> normal_texture = std::shared_ptr<engine::Texture> normal_texture =
engine::LoadTextureFromFile(app.GetResourcePath("textures/brickwall_normal.jpg"), engine::Texture::Filtering::kBilinear, app.renderer(), false); engine::LoadTextureFromFile(app.GetResourcePath("textures/brickwall_normal.jpg"), engine::Texture::Filtering::kAnisotropic, app.renderer(), false);
wall_renderable->material->SetAlbedoTexture(albedo_texture); wall_renderable->material->SetAlbedoTexture(albedo_texture);
wall_renderable->material->SetNormalTexture(normal_texture); wall_renderable->material->SetNormalTexture(normal_texture);
@ -185,8 +188,8 @@ void PlayGame(GameSettings settings)
auto custom = scene2->AddComponent<engine::CustomComponent>(wall2); auto custom = scene2->AddComponent<engine::CustomComponent>(wall2);
custom->onInit = []() {}; custom->onInit = []() {};
custom->onUpdate = [&](float dt) { custom->onUpdate = [&](float dt) {
scene2->GetComponent<engine::TransformComponent>(wall2)->rotation *= glm::angleAxis(dt * 0.2f, glm::normalize(glm::vec3{ 0.3f, 2.1f, 1.0f })); scene2->GetComponent<engine::TransformComponent>(pivot)->rotation *= glm::angleAxis(dt * 0.03f, glm::normalize(glm::vec3{0.0f, 0.0f, 1.0f}));
}; };
} }
{ /* light */ { /* light */
@ -196,6 +199,8 @@ void PlayGame(GameSettings settings)
wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.standard")); wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.standard"));
wall_renderable->material->SetAlbedoTexture(app.GetResource<engine::Texture>("builtin.white")); wall_renderable->material->SetAlbedoTexture(app.GetResource<engine::Texture>("builtin.white"));
} }
engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb"));
} }
my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2; my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2;