vertex_engine/bin/layers/opengl_panel/opengl_panel.cpp

115 lines
2.8 KiB
C++
Raw Normal View History

2024-05-15 09:09:36 +03:00
#include "opengl_panel.hpp"
#include "utils.hpp"
namespace sandbox
{
cube::cube()
{
hack::log()("create cube");
2025-01-14 01:05:07 +03:00
const std::filesystem::path vsp { "/mnt/raid/projects/vertex_engine/vertex_engine/bin/layers/opengl_panel/shaders/vertes.shader" };
const std::filesystem::path fsp { "/mnt/raid/projects/vertex_engine/vertex_engine/bin/layers/opengl_panel/shaders/frag.shader" };
2024-05-15 09:09:36 +03:00
add_shader(GL_VERTEX_SHADER, vsp);
add_shader(GL_FRAGMENT_SHADER, fsp);
shader_program::link();
m_vertices =
{
-0.1f, 0.0f, 0.7f,
0.1f, 0.0f, 0.7f,
0.1f, 0.0f, -0.7f,
-0.1f, 0.0f, -0.7f,
0.0f, 0.3f, 0.0f
};
m_indices =
{
0, 1, 1, 4, 4, 0,
0, 3, 3, 4, 4, 2,
2, 1, 3, 2
};
buffer::link();
}
void cube::use() { shader_program::use(); }
void cube::set_scale(float val) { set("scale", val); }
void cube::set_position(glm::vec3 val) { set("position", val); }
void cube::set_color(glm::vec4 val) { set("color", val); }
void cube::render() { buffer::render(); }
void opengl_panel::on_attach()
{
hack::log()("on_attach");
}
void opengl_panel::on_detach()
{
hack::log()("on_attach");
}
void opengl_panel::render()
{
2024-05-15 10:57:21 +03:00
m_cb_1.use();
m_cam.update(m_cb_1);
2024-05-15 09:09:36 +03:00
float r = std::sin(glfwGetTime());
float g = std::cos(glfwGetTime());
2024-05-15 10:57:21 +03:00
m_cb_1.set_color(glm::vec4{ 0.85f, 0.45f, 0.95f, 0.f }); // розовый
m_cb_1.set_scale(1.0f + std::sin(glfwGetTime()) * 0.2f);
m_cb_1.set_position(glm::vec3{ 0.f, 0.5, 0.f });
m_cb_1.render();
2024-05-15 09:09:36 +03:00
r = std::cos(glfwGetTime());
g = std::sin(glfwGetTime());
2024-05-15 10:57:21 +03:00
m_cb_2.use();
2024-05-15 09:09:36 +03:00
2024-05-15 10:57:21 +03:00
m_cam.update(m_cb_2);
2024-05-15 09:09:36 +03:00
2024-05-15 10:57:21 +03:00
m_cb_2.set_color(glm::vec4{ 0.66f, 0.66f, 0.66f, 0.f }); // серый/белый
m_cb_2.set_scale(1.0f - std::cos(glfwGetTime()) * 0.2f);
m_cb_2.set_position(glm::vec3{ r, g, 0.f });
m_cb_2.render();
2024-05-15 09:09:36 +03:00
}
void opengl_panel::on_event(VE::event e)
{
// для событий от перефирии
2024-05-15 10:57:21 +03:00
if (e.m_type.type() == typeid(VE::event_type))
{
auto t = std::any_cast<VE::event_type>(e.m_type);
if (t == VE::event_type::MOUSE_CURSOR_POSITION)
{
auto [pos_x, pos_y] = std::any_cast<std::pair<float, float>>(e.m_data);
m_cam.mouse_callback(pos_x, pos_y);
}
if (t == VE::event_type::KEY_REPEATE || t == VE::event_type::KEY_PRESSED)
{
auto key = std::any_cast<int>(e.m_data);
if (key == VE::key::W)
m_cam.up();
if (key == VE::key::S)
m_cam.down();
if (key == VE::key::A)
m_cam.left();
if (key == VE::key::D)
m_cam.right();
}
}
2024-05-15 09:09:36 +03:00
if (e.m_type.type() == typeid(test_event))
{
auto t = std::any_cast<test_event>(e.m_type);
if (t == test_event::TEST_EVEN)
hack::log()(std::any_cast<std::string>(e.m_data));
}
}
}