2d fps camera
parent
756b6a636e
commit
99715470ee
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
|
||||
set(BLT_GRAPHICS_VERSION 0.11.7)
|
||||
set(BLT_GRAPHICS_VERSION 0.12.0)
|
||||
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
|
||||
|
||||
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})
|
||||
|
|
|
@ -23,7 +23,7 @@ uniform mat4 model;
|
|||
uniform float z_index;
|
||||
|
||||
void main() {
|
||||
gl_Position = ortho * model * vec4(vertex.xy, z_index, 1.0);
|
||||
gl_Position = ovm * model * vec4(vertex.xy, z_index, 1.0);
|
||||
pos = vertex.xy;
|
||||
uv = uv_in;
|
||||
}
|
||||
|
|
|
@ -23,13 +23,46 @@
|
|||
|
||||
namespace blt::gfx
|
||||
{
|
||||
class lockable_camera
|
||||
{
|
||||
public:
|
||||
static void handle_lock();
|
||||
};
|
||||
|
||||
class first_person_camera
|
||||
{
|
||||
private:
|
||||
blt::vec3 position_;
|
||||
blt::vec3 rotation_;
|
||||
blt::vec3 speed_ {25, 25, 25};
|
||||
blt::vec3 speed_{25, 25, 25};
|
||||
public:
|
||||
void update();
|
||||
|
||||
inline void speed(const blt::vec3& speed)
|
||||
{
|
||||
speed_ = speed;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const vec3& speed() const
|
||||
{
|
||||
return speed_;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const vec3& position() const
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
void update_view(matrix_state_manager& manager);
|
||||
};
|
||||
|
||||
class first_person_camera_2d
|
||||
{
|
||||
private:
|
||||
blt::vec3 position_;
|
||||
blt::vec3 rotation_;
|
||||
blt::vec3 speed_{165, 165, 165};
|
||||
blt::vec3 world_scale_{1,1,1};
|
||||
public:
|
||||
void update();
|
||||
|
||||
|
|
|
@ -43,6 +43,8 @@ namespace blt::gfx
|
|||
blt::mat4x4 view;
|
||||
blt::mat4x4 pvm;
|
||||
blt::mat4x4 ovm;
|
||||
blt::mat4x4 view_2d;
|
||||
blt::vec3 scale_2d;
|
||||
std::int32_t last_width = 0, last_height = 0;
|
||||
uniform_buffer* global_matrix_ubo = nullptr;
|
||||
public:
|
||||
|
@ -69,19 +71,26 @@ namespace blt::gfx
|
|||
template<typename U>
|
||||
void setView(U&& mat)
|
||||
{
|
||||
view = mat;
|
||||
view = (mat);
|
||||
}
|
||||
|
||||
template<typename U, typename G>
|
||||
void setView2D(U&& mat, G&& scale)
|
||||
{
|
||||
view_2d = (mat);
|
||||
scale_2d = (scale);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void setPerspective(U&& mat)
|
||||
{
|
||||
perspective = mat;
|
||||
perspective = (mat);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void setOrtho(U&& mat)
|
||||
{
|
||||
ortho = mat;
|
||||
ortho = (mat);
|
||||
}
|
||||
|
||||
void update_perspectives(std::int32_t width, std::int32_t height, float fov = 90, float near = 0.1f, float far = 500.0f,
|
||||
|
|
|
@ -17,28 +17,17 @@
|
|||
*/
|
||||
#include <blt/gfx/renderer/camera.h>
|
||||
#include <blt/gfx/window.h>
|
||||
#include <blt/std/logging.h>
|
||||
|
||||
void blt::gfx::first_person_camera::update()
|
||||
{
|
||||
lockable_camera::handle_lock();
|
||||
|
||||
using namespace blt::gfx;
|
||||
vec3 move_at;
|
||||
|
||||
float speed_multi = 1;
|
||||
|
||||
int locking_key = GLFW_KEY_ESCAPE;
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
locking_key = GLFW_KEY_F1;
|
||||
#endif
|
||||
|
||||
if (isKeyPressed(locking_key) && keyPressedLastFrame())
|
||||
{
|
||||
if (isCursorLocked())
|
||||
unlockCursor();
|
||||
else
|
||||
lockCursor();
|
||||
}
|
||||
|
||||
if (isKeyPressed(GLFW_KEY_LEFT_CONTROL))
|
||||
speed_multi = 10;
|
||||
if (isKeyPressed(GLFW_KEY_LEFT_ALT))
|
||||
|
@ -117,3 +106,82 @@ void blt::gfx::first_person_camera::update_view(blt::gfx::matrix_state_manager&
|
|||
view.translate(-position_);
|
||||
manager.setView(view);
|
||||
}
|
||||
|
||||
float easeInOutCubic(float x)
|
||||
{
|
||||
return static_cast<float>(x < 0.5 ? 4 * x * x * x : (x < 0.7 ? 1 - std::pow(-2 * x + 2, 3) / 2 : x + 0.19));
|
||||
}
|
||||
|
||||
float easeInOutSine(float x)
|
||||
{
|
||||
return static_cast<float>(-(std::cos(blt::PI * x) - 1) / 2);
|
||||
}
|
||||
|
||||
void blt::gfx::first_person_camera_2d::update()
|
||||
{
|
||||
lockable_camera::handle_lock();
|
||||
|
||||
float speed_multi = 1;
|
||||
|
||||
if (isKeyPressed(GLFW_KEY_LEFT_SHIFT))
|
||||
speed_multi = 10;
|
||||
if (isKeyPressed(GLFW_KEY_LEFT_CONTROL))
|
||||
speed_multi = 0.5;
|
||||
|
||||
vec3 direction;
|
||||
|
||||
if (isKeyPressed(GLFW_KEY_W))
|
||||
direction[1] = 1;
|
||||
else if (isKeyPressed(GLFW_KEY_S))
|
||||
direction[1] = -1;
|
||||
else
|
||||
direction[1] = 0;
|
||||
|
||||
if (isKeyPressed(GLFW_KEY_A))
|
||||
direction[0] = -1;
|
||||
else if (isKeyPressed(GLFW_KEY_D))
|
||||
direction[0] = 1;
|
||||
else
|
||||
direction[0] = 0;
|
||||
|
||||
float scaling_const = easeInOutCubic(world_scale_[0]);
|
||||
|
||||
if (isKeyPressed(GLFW_KEY_Q))
|
||||
world_scale_[0] += scaling_const * static_cast<float>(getFrameDeltaSeconds());
|
||||
else if (isKeyPressed(GLFW_KEY_E))
|
||||
world_scale_[0] -= scaling_const * static_cast<float>(getFrameDeltaSeconds());
|
||||
|
||||
if (world_scale_[0] <= 0.1f)
|
||||
world_scale_[0] = 0.1f;
|
||||
|
||||
world_scale_[1] = world_scale_[0];
|
||||
|
||||
position_ = position_ + (direction * speed_ * speed_multi * static_cast<float>(getFrameDeltaSeconds()));
|
||||
}
|
||||
|
||||
void blt::gfx::first_person_camera_2d::update_view(blt::gfx::matrix_state_manager& manager)
|
||||
{
|
||||
blt::mat4x4 view;
|
||||
view.rotateX(toRadians(rotation_.x()));
|
||||
view.rotateY(toRadians(rotation_.y()));
|
||||
view.rotateZ(toRadians(rotation_.z()));
|
||||
view.translate(-position_);
|
||||
manager.setView2D(view, world_scale_);
|
||||
}
|
||||
|
||||
void blt::gfx::lockable_camera::handle_lock()
|
||||
{
|
||||
int locking_key = GLFW_KEY_ESCAPE;
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
locking_key = GLFW_KEY_F1;
|
||||
#endif
|
||||
|
||||
if (isKeyPressed(locking_key) && keyPressedLastFrame())
|
||||
{
|
||||
if (isCursorLocked())
|
||||
unlockCursor();
|
||||
else
|
||||
lockCursor();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,9 @@
|
|||
void blt::gfx::matrix_state_manager::update()
|
||||
{
|
||||
pvm = perspective * view;
|
||||
ovm = ortho * view;
|
||||
blt::mat4x4 scaled;
|
||||
scaled.scale(scale_2d);
|
||||
ovm = scaled * ortho * view_2d;
|
||||
BLT_ASSERT(global_matrix_ubo != nullptr && "You forgot to call create_internals(). Make sure you call cleanup() as well!");
|
||||
global_matrix_ubo->upload((void*)perspective.ptr(), sizeof(blt::mat4x4), 0);
|
||||
global_matrix_ubo->upload((void*)ortho.ptr(), sizeof(blt::mat4x4), sizeof(blt::mat4x4));
|
||||
|
|
Loading…
Reference in New Issue