vertex_engine/bin/layers/opengl_panel/opengl_panel.hpp
2025-01-14 01:05:07 +03:00

97 lines
2.5 KiB
C++
Executable File

#pragma once
#include "VE.hpp" // IWYU pragma: keep
namespace sandbox
{
class cube : public VE::opengl
{
public:
cube();
~cube() = default;
public:
void set_scale(float val);
void set_position(glm::vec3 val);
void set_color(glm::vec4 val);
void use();
void render();
};
struct camera
{
// Начальное положение камеры
mt::vec3 m_pos = mt::vec3{ 0.0f, 0.0f, 3.0f };
mt::vec3 m_front = mt::vec3{ 0.0f, 0.0f, -1.0f };
mt::vec3 m_up = mt::vec3{ 0.0f, 1.0f, 0.0f };
float m_speed = 0.05f;
glm::mat4 m_view, m_projection;
void up() { m_pos += m_speed * m_front; }
void down() { m_pos -= m_speed * m_front; }
void left() { m_pos -= mt::normalize(mt::cross(m_front, m_up)) * m_speed; }
void right() { m_pos += mt::normalize(mt::cross(m_front, m_up)) * m_speed; }
template<typename T>
void update(const T& obj)
{
m_view = glm::lookAt(m_pos, m_pos + m_front, m_up);
m_projection = glm::perspective(glm::radians(45.0f),
static_cast<float>(VE::application::get()->get_glfw()->width()) / VE::application::get()->get_glfw()->height(),
0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(obj.get_id(), "view"), 1, GL_FALSE, mt::value_ptr(m_view));
glUniformMatrix4fv(glGetUniformLocation(obj.get_id(), "projection"), 1, GL_FALSE, glm::value_ptr(m_projection));
}
// Управление камерой через мышь
bool m_first = true;
float m_yaw = -90.0f;
float m_pitch = 0.0f;
float m_last_x = 400, m_last_y = 300;
void mouse_callback(double xpos, double ypos)
{
if (m_first)
{
m_last_x = xpos;
m_last_y = ypos;
m_first = false;
}
float x_offset = xpos - m_last_x;
float y_offset = m_last_y - ypos;
m_last_x = xpos;
m_last_y = ypos;
float sensitivity = 0.1f;
x_offset *= sensitivity;
y_offset *= sensitivity;
m_yaw += x_offset;
m_pitch += y_offset;
if (m_pitch > 89.0f) m_pitch = 89.0f;
if (m_pitch < -89.0f) m_pitch = -89.0f;
mt::vec3 front;
front.x = std::cos(mt::radians(m_yaw)) * std::cos(mt::radians(m_pitch));
front.y = std::sin(mt::radians(m_pitch));
front.z = std::sin(mt::radians(m_yaw)) * std::cos(mt::radians(m_pitch));
m_front = mt::normalize(front);
}
};
class opengl_panel : public VE::layer
{
VE_FN_OVERIDE();
cube m_cb_1;
cube m_cb_2;
camera m_cam;
};
}