/* * Copyright 2022 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "camera.hpp" #include "vk/camera.hpp" void cg_free_camera(mrb_state *mrb, void* obj) { auto ptr = static_cast*>(obj); ptr->~shared_ptr(); mrb_free(mrb, ptr); } const struct mrb_data_type cg_camera_type = {"CG_Camera", cg_free_camera}; static mrb_value cg_cCamera_initialize(mrb_state *mrb, mrb_value self) { mrb_float position_x, position_y, position_z, rotation_x, rotation_y, rotation_z; std::shared_ptr *ptr; mrb_get_args( mrb, "ffffff", &position_x, &position_y, &position_z, &rotation_x, &rotation_y, &rotation_z); ptr = (std::shared_ptr*)DATA_PTR(self); if(ptr) mrb_free(mrb, ptr); ptr = (std::shared_ptr*)mrb_malloc( mrb, sizeof(std::shared_ptr)); new(ptr)std::shared_ptr(std::make_shared()); (*ptr)->position = glm::vec3(position_x, position_y, position_z); (*ptr)->rotation = glm::vec3(rotation_x, rotation_y, rotation_z); mrb_data_init(self, ptr, &cg_camera_type); return self; } static mrb_value cg_cCamera_use(mrb_state *mrb, mrb_value self) { std::shared_ptr *ptr; ptr = (std::shared_ptr*)DATA_PTR(self); cg_core.vk_graphics_pipeline->camera = (*ptr); return self; } static mrb_value cg_cCamera_rotate(mrb_state *mrb, mrb_value self) { mrb_float x, y, z; std::shared_ptr *ptr; mrb_get_args(mrb, "fff", &x, &y, &z); ptr = (std::shared_ptr*)DATA_PTR(self); (*ptr)->rotation.x += x; (*ptr)->rotation.y += y; (*ptr)->rotation.z += z; return self; } static mrb_value cg_cCamera_translate(mrb_state *mrb, mrb_value self) { mrb_float x, y, z; std::shared_ptr *ptr; mrb_get_args(mrb, "fff", &x, &y, &z); ptr = (std::shared_ptr*)DATA_PTR(self); (*ptr)->position.x += x; (*ptr)->position.y += y; (*ptr)->position.z += z; return self; } static mrb_value cg_cCamera_translate_by_rotation(mrb_state *mrb, mrb_value self) { mrb_float x, y, z; std::shared_ptr *ptr; mrb_get_args(mrb, "fff", &x, &y, &z); ptr = (std::shared_ptr*)DATA_PTR(self); glm::mat4 matrix{1.0f}; matrix = glm::rotate( matrix, (*ptr)->rotation.x, glm::vec3{1.0f, 0.0f, 0.0f}); matrix = glm::rotate( matrix, (*ptr)->rotation.y, glm::vec3{0.0f, 1.0f, 0.0f}); matrix = glm::rotate( matrix, (*ptr)->rotation.z, glm::vec3{0.0f, 0.0f, 1.0f}); glm::vec4 rotated_vector{matrix * glm::vec4{x, y, z, 1.0}}; (*ptr)->position.x += rotated_vector.x; (*ptr)->position.y += rotated_vector.y; (*ptr)->position.z += rotated_vector.z; return self; } void cg_camera_init(mrb_state *mrb) { struct RClass *cg_m, *cg_cCamera; cg_m = mrb_module_get(mrb, "CandyGear"); cg_cCamera = mrb_define_class_under(mrb, cg_m, "Camera", mrb->object_class); MRB_SET_INSTANCE_TT(cg_cCamera, MRB_TT_DATA); mrb_define_method( mrb, cg_cCamera, "initialize", cg_cCamera_initialize, MRB_ARGS_REQ(6)); mrb_define_method(mrb, cg_cCamera, "use", cg_cCamera_use, MRB_ARGS_NONE()); mrb_define_method( mrb, cg_cCamera, "rotate", cg_cCamera_rotate, MRB_ARGS_REQ(3)); mrb_define_method( mrb, cg_cCamera, "translate", cg_cCamera_translate, MRB_ARGS_REQ(3)); mrb_define_method( mrb, cg_cCamera, "translate_by_rotation", cg_cCamera_translate_by_rotation, MRB_ARGS_REQ(3)); }