Add test models and try to improve player controller

This commit is contained in:
Bailey Harrison 2024-03-14 19:37:44 +00:00
parent daba5332b9
commit a4d58a1490
9 changed files with 113 additions and 19 deletions

71
cloc Normal file
View File

@ -0,0 +1,71 @@
include/application.h
include/components/collider.h
include/components/custom.h
include/components/mesh_renderable.h
include/components/transform.h
include/components/ui_renderable.h
include/ecs.h
include/engine_api.h
include/event_system.h
include/gfx.h
include/gfx_device.h
include/input_manager.h
include/inputs/keyboard.h
include/inputs/mouse.h
include/log.h
include/logger.h
include/renderer.h
include/resource_manager.h
include/resources/font.h
include/resources/material.h
include/resources/mesh.h
include/resources/shader.h
include/resources/texture.h
include/scene.h
include/scene_manager.h
include/systems/collisions.h
include/systems/custom_behaviour.h
include/systems/mesh_render_system.h
include/systems/transform.h
include/systems/ui_render_system.h
include/util.h
include/util/files.h
include/util/gltf_loader.h
include/util/model_loader.h
include/window.h
src/application.cpp
src/ecs.cpp
src/gfx_device_vulkan.cpp
src/input_manager.cpp
src/renderer.cpp
src/resources/font.cpp
src/resources/material.cpp
src/resources/mesh.cpp
src/resources/shader.cpp
src/resources/texture.cpp
src/scene.cpp
src/scene_manager.cpp
src/systems/collisions.cpp
src/systems/custom_behaviour.cpp
src/systems/mesh_render_system.cpp
src/systems/transform.cpp
src/systems/ui_render_system.cpp
src/util/files.cpp
src/util/gltf_loader.cpp
src/util/model_loader.cpp
src/vulkan/device.cpp
src/vulkan/device.h
src/vulkan/gpu_allocator.cpp
src/vulkan/gpu_allocator.h
src/vulkan/instance.cpp
src/vulkan/instance.h
src/vulkan/swapchain.cpp
src/vulkan/swapchain.h
src/window.cpp
test/src/camera_controller.cpp
test/src/camera_controller.hpp
test/src/game.cpp
test/src/game.hpp
test/src/main.cpp
test/src/meshgen.cpp
test/src/meshgen.hpp

BIN
doc/blender_template.blend (Stored with Git LFS) Normal file

Binary file not shown.

BIN
doc/blender_template.blend1 (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -33,7 +33,7 @@ void main() {
const float roughness = metallic_roughness.b;
const float roughness_2 = roughness * roughness;
const vec3 light_colour = vec3(1.0, 1.0, 1.0) * 5.0;
const vec3 light_colour = vec3(1.0, 1.0, 1.0) * 2.4;
const vec3 emission = vec3(0.0, 0.0, 0.0);
const float ao = texture(materialSetOcclusionSampler, fragUV).r;

View File

@ -34,7 +34,7 @@ void main() {
fragUV = inUV;
fragPosTangentSpace = worldToTangentSpace * vec3(worldPosition);
fragViewPosTangentSpace = worldToTangentSpace * vec3(inverse(frameSetUniformBuffer.view) * vec4(0.0, 0.0, 0.0, 1.0));
fragLightPosTangentSpace = worldToTangentSpace * vec3(59000.0, 0000.0, 10000.0);
fragLightPosTangentSpace = worldToTangentSpace * vec3(10000.0, 0000.0, 59000.0);
gl_Position.y *= -1.0;
}

Binary file not shown.

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

Binary file not shown.

View File

@ -34,10 +34,6 @@ void CameraControllerSystem::OnUpdate(float ts)
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");
@ -57,29 +53,37 @@ void CameraControllerSystem::OnUpdate(float ts)
const glm::vec3 d2x_rotated = glm::rotateZ(glm::vec3{dx, 0.0f, 0.0f}, c->yaw);
const glm::vec3 d2y_rotated = glm::rotateZ(glm::vec3{0.0f, dy, 0.0f}, c->yaw);
glm::vec3 h_vel = (d2x_rotated + d2y_rotated);
h_vel *= speed;
t->position += h_vel * dt;
c->vel.x = h_vel.x;
c->vel.y = h_vel.y;
// keep vel.z as gravity can increase it every frame
// gravity stuff here:
constexpr float g = -9.81f; // constant velocity gravity???
constexpr float player_height = 71.0f * 25.4f / 1000.0f;
c->vel.z += g * dt;
if (scene_->app()->input_manager()->GetButtonPress("jump")) {
c->vel.z += 4.4f; // m/s
}
// update position with velocity:
// check for collision during next frame and push back and remove velocity in the normal of the collision direction if so
engine::Ray ray{};
ray.origin = t->position;
ray.origin.z -= player_height; // check for collision from the player's feet
ray.direction = c->
const engine::Raycast fall_raycast = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(fall_ray);
if (fall_raycast.hit && fall_raycast.distance < player_height + (-c->fall_vel * dt)) {
t->position.z += -(fall_raycast.distance - player_height);
c->fall_vel = 0.0f;
}
else {
t->position.z += c->fall_vel * dt;
const glm::vec3 dX = c->vel * dt; // where player will end up with no collision
ray.direction = glm::normalize(dX);
const engine::Raycast raycast = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(ray);
if (raycast.hit && raycast.distance < glm::length(dX)) {
// will collide
t->position -= raycast.distance * ray.direction; // push out of collision zone
c->vel.z = 0.0f; // remove velocity normal to collision surface
}
t->position += c->vel * dt;
constexpr float kMaxDistanceFromOrigin = 10000.0f;
if (glm::length(t->position) > kMaxDistanceFromOrigin) {

View File

@ -107,7 +107,6 @@ void PlayGame(GameSettings settings)
floor_col->aabb.min = glm::vec3{0.0f, 0.0f, 0.0f};
floor_col->aabb.max = glm::vec3{100.0f, 100.0f, 0.1f };
engine::Entity redcube = engine::util::LoadGLTF(*main_scene, app.GetResourcePath("models/redcube.glb"));
engine::Entity monke = engine::util::LoadGLTF(*main_scene, app.GetResourcePath("models/monke.glb"));
main_scene->GetComponent<engine::TransformComponent>(monke)->position.y += 10.0f;
@ -126,6 +125,20 @@ void PlayGame(GameSettings settings)
skybox_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 10.0f, 10.0f, 10.0f, 1.0f, true);
skybox_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.skybox"));
skybox_renderable->material->SetAlbedoTexture(app.GetResource<engine::Texture>("builtin.black"));
engine::Entity helmet = engine::util::LoadGLTF(*main_scene, app.GetResourcePath("models/DamagedHelmet.glb"));
main_scene->GetPosition(helmet) += glm::vec3{20.0f, 10.0f, 5.0f};
engine::Entity toycar = engine::util::LoadGLTF(*main_scene, app.GetResourcePath("models/ToyCar.glb"));
main_scene->GetScale(toycar) *= 100.0f;
auto car_spin = main_scene->AddComponent<engine::CustomComponent>(toycar);
car_spin->onInit = []() -> void {};
car_spin->onUpdate = [&](float dt) -> void {
static float yaw = 0.0f;
yaw += dt;
main_scene->GetRotation(toycar) = glm::angleAxis(yaw, glm::vec3{0.0f, 0.0f, 1.0f});
main_scene->GetRotation(toycar) *= glm::angleAxis(glm::half_pi<float>(), glm::vec3{1.0f, 0.0f, 0.0f});
};
}
start_scene->GetSystem<CameraControllerSystem>()->next_scene_ = main_scene;