mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
pbr work
This commit is contained in:
parent
33ee5aa321
commit
7aca66ff06
@ -28,7 +28,10 @@ set(SRC_FILES
|
||||
"src/imgui/imgui_widgets.cpp"
|
||||
"src/imgui/imstb_textedit.h"
|
||||
"src/input_manager.cpp"
|
||||
"src/libs/json.hpp"
|
||||
"src/libs/stb_impl.cpp"
|
||||
"src/libs/tiny_gltf.h"
|
||||
"src/libs/tiny_gltf_impl.cpp"
|
||||
"src/renderer.cpp"
|
||||
"src/resources/font.cpp"
|
||||
"src/resources/material.cpp"
|
||||
|
@ -15,8 +15,8 @@ layout(location = 0) out vec4 outColor;
|
||||
void main() {
|
||||
|
||||
// constants
|
||||
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
||||
vec3 ambientColor = vec3(1.0, 1.0, 1.0);
|
||||
vec3 lightColor = vec3(1.0, 0.9, 0.9);
|
||||
vec3 ambientColor = vec3(1.0, 0.0, 0.0);
|
||||
float ambientStrength = 0.05;
|
||||
vec3 emission = vec3(0.0, 0.0, 0.0);
|
||||
|
||||
|
Before Width: | Height: | Size: 140 B After Width: | Height: | Size: 140 B |
@ -167,9 +167,13 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
|
||||
|
||||
/* 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));
|
||||
}
|
||||
{
|
||||
auto normalTexture = LoadTextureFromFile(GetResourcePath("engine/textures/normal.png"), Texture::Filtering::kOff, renderer(), false);
|
||||
GetResourceManager<Texture>()->AddPersistent("builtin.normal", std::move(normalTexture));
|
||||
}
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
|
@ -727,7 +727,7 @@ gfx::DrawBuffer* GFXDevice::BeginRender()
|
||||
|
||||
std::array<VkClearValue, 2> clearValues{}; // Using same value for all components enables
|
||||
// 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[2] = 0.0f;
|
||||
clearValues[0].color.float32[3] = 1.0f;
|
||||
|
26753
src/libs/json.hpp
Normal file
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
8485
src/libs/tiny_gltf.h
Normal file
File diff suppressed because it is too large
Load Diff
2
src/libs/tiny_gltf_impl.cpp
Normal file
2
src/libs/tiny_gltf_impl.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#define TINYGLTF_IMPLEMENTATION
|
||||
#include "libs/tiny_gltf.h"
|
@ -3,15 +3,72 @@
|
||||
#include "log.h"
|
||||
#include "util/files.h"
|
||||
|
||||
#include "libs/tiny_gltf.h"
|
||||
|
||||
#include "components/mesh_renderable.h"
|
||||
|
||||
namespace tg = tinygltf;
|
||||
|
||||
namespace engine::util {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -83,13 +83,14 @@ static void buildGraph(const std::map<int, std::shared_ptr<Texture>>& textures,
|
||||
scene->GetComponent<TransformComponent>(child)->is_static = is_static;
|
||||
auto childRenderer = scene->AddComponent<MeshRenderableComponent>(child);
|
||||
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]])) {
|
||||
childRenderer->material->SetAlbedoTexture(textures.at(meshTextureIndices[parentNode->mMeshes[i]]));
|
||||
}
|
||||
else {
|
||||
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++) {
|
||||
|
BIN
test/res/models/teapot.glb
Normal file
BIN
test/res/models/teapot.glb
Normal file
Binary file not shown.
@ -15,97 +15,97 @@
|
||||
#include "window.h"
|
||||
|
||||
CameraControllerSystem::CameraControllerSystem(engine::Scene* scene)
|
||||
: System(scene, {typeid(engine::TransformComponent).hash_code(),
|
||||
typeid(CameraControllerComponent).hash_code()}) {}
|
||||
|
||||
void CameraControllerSystem::OnUpdate(float ts) {
|
||||
if (t == nullptr || c == nullptr) {
|
||||
for (engine::Entity entity : entities_) {
|
||||
t = scene_->GetComponent<engine::TransformComponent>(entity);
|
||||
c = scene_->GetComponent<CameraControllerComponent>(entity);
|
||||
break;
|
||||
}
|
||||
if (t == nullptr) return;
|
||||
if (c == nullptr) return;
|
||||
}
|
||||
|
||||
const float dt = ts;
|
||||
|
||||
// in metres per second
|
||||
float speed = c->kWalkSpeed;
|
||||
if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f;
|
||||
|
||||
float dx = scene_->app()->input_manager()->GetAxis("movex");
|
||||
float dy = (-scene_->app()->input_manager()->GetAxis("movey"));
|
||||
|
||||
// calculate new pitch and yaw
|
||||
|
||||
constexpr float kMaxPitch = glm::pi<float>();
|
||||
constexpr float kMinPitch = 0.0f;
|
||||
|
||||
float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f *
|
||||
c->kCameraSensitivity;
|
||||
c->pitch += d_pitch;
|
||||
if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) {
|
||||
c->pitch -= d_pitch;
|
||||
}
|
||||
c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f *
|
||||
c->kCameraSensitivity;
|
||||
|
||||
// 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 d2y_rotated =
|
||||
glm::rotateZ(glm::rotateX(glm::vec3{0.0f, 0.0f, dy}, c->pitch), c->yaw);
|
||||
glm::vec3 h_vel = (d2x_rotated + d2y_rotated);
|
||||
h_vel *= speed;
|
||||
t->position += h_vel * dt;
|
||||
|
||||
constexpr float kMaxDistanceFromOrigin = 10000.0f;
|
||||
|
||||
if (glm::length(t->position) > kMaxDistanceFromOrigin) {
|
||||
t->position = {0.0f, 5.0f, 0.0f};
|
||||
}
|
||||
|
||||
/* ROTATION STUFF */
|
||||
|
||||
// pitch quaternion
|
||||
const float half_pitch = c->pitch / 2.0f;
|
||||
glm::quat pitch_quat{};
|
||||
pitch_quat.x = glm::sin(half_pitch);
|
||||
pitch_quat.y = 0.0f;
|
||||
pitch_quat.z = 0.0f;
|
||||
pitch_quat.w = glm::cos(half_pitch);
|
||||
|
||||
// yaw quaternion
|
||||
const float half_yaw = c->yaw / 2.0f;
|
||||
glm::quat yaw_quat{};
|
||||
yaw_quat.x = 0.0f;
|
||||
yaw_quat.y = 0.0f;
|
||||
yaw_quat.z = glm::sin(half_yaw);
|
||||
yaw_quat.w = glm::cos(half_yaw);
|
||||
|
||||
// update rotation
|
||||
t->rotation = yaw_quat * pitch_quat;
|
||||
|
||||
/* user interface inputs */
|
||||
|
||||
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) {
|
||||
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)};
|
||||
LOG_INFO("position {}", pos_string);
|
||||
}
|
||||
|
||||
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) {
|
||||
t->position = {0.0f, 5.0f, 0.0f};
|
||||
}
|
||||
|
||||
if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) {
|
||||
scene_->app()->window()->ToggleFullscreen();
|
||||
}
|
||||
|
||||
if (scene_->app()->input_manager()->GetButtonPress("exit")) {
|
||||
//scene_->app()->window()->SetCloseFlag();
|
||||
scene_->app()->scene_manager()->SetActiveScene(next_scene_);
|
||||
}
|
||||
: System(scene, {typeid(engine::TransformComponent).hash_code(), typeid(CameraControllerComponent).hash_code()})
|
||||
{
|
||||
}
|
||||
|
||||
void CameraControllerSystem::OnUpdate(float ts)
|
||||
{
|
||||
if (t == nullptr || c == nullptr) {
|
||||
for (engine::Entity entity : entities_) {
|
||||
t = scene_->GetComponent<engine::TransformComponent>(entity);
|
||||
c = scene_->GetComponent<CameraControllerComponent>(entity);
|
||||
break;
|
||||
}
|
||||
if (t == nullptr) return;
|
||||
if (c == nullptr) return;
|
||||
}
|
||||
|
||||
const float dt = ts;
|
||||
|
||||
// in metres per second
|
||||
float speed = c->kWalkSpeed;
|
||||
if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f;
|
||||
|
||||
float dx = scene_->app()->input_manager()->GetAxis("movex");
|
||||
float dy = (-scene_->app()->input_manager()->GetAxis("movey"));
|
||||
|
||||
// calculate new pitch and yaw
|
||||
|
||||
constexpr float kMaxPitch = glm::pi<float>();
|
||||
constexpr float kMinPitch = 0.0f;
|
||||
|
||||
float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * c->kCameraSensitivity;
|
||||
c->pitch += d_pitch;
|
||||
if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) {
|
||||
c->pitch -= d_pitch;
|
||||
}
|
||||
c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * c->kCameraSensitivity;
|
||||
|
||||
// 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 d2y_rotated = glm::rotateZ(glm::rotateX(glm::vec3{0.0f, 0.0f, dy}, c->pitch), c->yaw);
|
||||
glm::vec3 h_vel = (d2x_rotated + d2y_rotated);
|
||||
h_vel *= speed;
|
||||
t->position += h_vel * dt;
|
||||
|
||||
constexpr float kMaxDistanceFromOrigin = 10000.0f;
|
||||
|
||||
if (glm::length(t->position) > kMaxDistanceFromOrigin) {
|
||||
t->position = {0.0f, 5.0f, 0.0f};
|
||||
}
|
||||
|
||||
/* ROTATION STUFF */
|
||||
|
||||
// pitch quaternion
|
||||
const float half_pitch = c->pitch / 2.0f;
|
||||
glm::quat pitch_quat{};
|
||||
pitch_quat.x = glm::sin(half_pitch);
|
||||
pitch_quat.y = 0.0f;
|
||||
pitch_quat.z = 0.0f;
|
||||
pitch_quat.w = glm::cos(half_pitch);
|
||||
|
||||
// yaw quaternion
|
||||
const float half_yaw = c->yaw / 2.0f;
|
||||
glm::quat yaw_quat{};
|
||||
yaw_quat.x = 0.0f;
|
||||
yaw_quat.y = 0.0f;
|
||||
yaw_quat.z = glm::sin(half_yaw);
|
||||
yaw_quat.w = glm::cos(half_yaw);
|
||||
|
||||
// update rotation
|
||||
t->rotation = yaw_quat * pitch_quat;
|
||||
|
||||
/* user interface inputs */
|
||||
|
||||
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) {
|
||||
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)};
|
||||
LOG_INFO("position {}", pos_string);
|
||||
}
|
||||
|
||||
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) {
|
||||
t->position = {0.0f, 5.0f, 0.0f};
|
||||
}
|
||||
|
||||
if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) {
|
||||
scene_->app()->window()->ToggleFullscreen();
|
||||
}
|
||||
|
||||
if (scene_->app()->input_manager()->GetButtonPress("exit")) {
|
||||
scene_->app()->window()->SetCloseFlag();
|
||||
}
|
||||
|
||||
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_F)) {
|
||||
scene_->app()->scene_manager()->SetActiveScene(next_scene_);
|
||||
}
|
||||
}
|
@ -168,16 +168,19 @@ void PlayGame(GameSettings settings)
|
||||
engine::util::LoadMeshFromFile(scene2, app.GetResourcePath("models/MY_AXES.dae"), true);
|
||||
}
|
||||
|
||||
{ /* a cube with a normal map */
|
||||
engine::Entity wall2 = scene2->CreateEntity("wall2", 0, glm::vec3{ 60.0f, 0.0f, 0.0f });
|
||||
{ /* floor */
|
||||
|
||||
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);
|
||||
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"));
|
||||
|
||||
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 =
|
||||
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->SetNormalTexture(normal_texture);
|
||||
@ -185,8 +188,8 @@ void PlayGame(GameSettings settings)
|
||||
auto custom = scene2->AddComponent<engine::CustomComponent>(wall2);
|
||||
custom->onInit = []() {};
|
||||
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 */
|
||||
@ -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->SetAlbedoTexture(app.GetResource<engine::Texture>("builtin.white"));
|
||||
}
|
||||
|
||||
engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb"));
|
||||
}
|
||||
|
||||
my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2;
|
||||
|
Loading…
Reference in New Issue
Block a user