From 900120ca393fd2eb49ca8c63bcd9b4e0c0aab1a2 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Thu, 8 Sep 2022 16:41:40 -0300 Subject: styl Improve legibility of rotation_3d and vector_3d --- src/candy_gear.cpp | 8 +-- src/main.cpp | 8 +-- src/model.cpp | 6 +- src/rotation3d.cpp | 113 --------------------------------- src/rotation3d.hpp | 27 -------- src/rotation_3d.cpp | 113 +++++++++++++++++++++++++++++++++ src/rotation_3d.hpp | 27 ++++++++ src/vector3d.cpp | 176 --------------------------------------------------- src/vector3d.hpp | 34 ---------- src/vector_3d.cpp | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vector_3d.hpp | 34 ++++++++++ 11 files changed, 362 insertions(+), 361 deletions(-) delete mode 100644 src/rotation3d.cpp delete mode 100644 src/rotation3d.hpp create mode 100644 src/rotation_3d.cpp create mode 100644 src/rotation_3d.hpp delete mode 100644 src/vector3d.cpp delete mode 100644 src/vector3d.hpp create mode 100644 src/vector_3d.cpp create mode 100644 src/vector_3d.hpp diff --git a/src/candy_gear.cpp b/src/candy_gear.cpp index 45d105e..9460b51 100644 --- a/src/candy_gear.cpp +++ b/src/candy_gear.cpp @@ -22,8 +22,8 @@ #include -#include "rotation3d.hpp" -#include "vector3d.hpp" +#include "rotation_3d.hpp" +#include "vector_3d.hpp" static mrb_value parse_node(mrb_state *mrb, const YAML::Node &node) @@ -64,7 +64,7 @@ cg_mCandyGear_set_camera_position(mrb_state *mrb, mrb_value self) { std::shared_ptr *camera_position; - mrb_get_args(mrb, "d", &camera_position, &cg_vector3d_type); + mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); cg_core.vk_graphics_pipeline_3d->camera_position = (*camera_position); return self; @@ -75,7 +75,7 @@ cg_mCandyGear_set_camera_rotation(mrb_state *mrb, mrb_value self) { std::shared_ptr *camera_rotation; - mrb_get_args(mrb, "d", &camera_rotation, &cg_rotation3d_type); + mrb_get_args(mrb, "d", &camera_rotation, &cg_rotation_3d_type); cg_core.vk_graphics_pipeline_3d->camera_rotation = (*camera_rotation); return self; diff --git a/src/main.cpp b/src/main.cpp index d82ba1b..120a7ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,11 +21,11 @@ #include "core.hpp" #include "key.hpp" #include "model.hpp" -#include "rotation3d.hpp" +#include "rotation_3d.hpp" #include "sound.hpp" #include "sprite.hpp" #include "texture.hpp" -#include "vector3d.hpp" +#include "vector_3d.hpp" cg_sCore cg_core; @@ -62,11 +62,11 @@ int main(int argc, char *argv[]) cg_candy_gear_init(cg_core.mrb); cg_key_init(cg_core.mrb); cg_model_init(cg_core.mrb); - cg_rotation3d_init(cg_core.mrb); + cg_rotation_3d_init(cg_core.mrb); cg_sound_init(cg_core.mrb); cg_sprite_init(cg_core.mrb); cg_texture_init(cg_core.mrb); - cg_vector3d_init(cg_core.mrb); + cg_vector_3d_init(cg_core.mrb); main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self); sym_init = mrb_intern_cstr(cg_core.mrb, "init"); diff --git a/src/model.cpp b/src/model.cpp index c0a35d8..45744f6 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -16,9 +16,9 @@ #include "model.hpp" -#include "rotation3d.hpp" +#include "rotation_3d.hpp" #include "texture.hpp" -#include "vector3d.hpp" +#include "vector_3d.hpp" #include "vk/model_instance.hpp" void @@ -66,7 +66,7 @@ cg_cModel_draw(mrb_state *mrb, mrb_value self) std::shared_ptr *rotation; mrb_get_args( - mrb, "dd", &position, &cg_vector3d_type, &rotation, &cg_rotation3d_type); + mrb, "dd", &position, &cg_vector_3d_type, &rotation, &cg_rotation_3d_type); instance.position = **position; instance.rotation = **rotation; diff --git a/src/rotation3d.cpp b/src/rotation3d.cpp deleted file mode 100644 index d95947b..0000000 --- a/src/rotation3d.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 "rotation3d.hpp" - -#include "vector3d.hpp" - -namespace -{ -inline float -pitch_limit(float pitch) -{ - if(pitch > M_PI/2) pitch = M_PI/2; - else if (pitch < -(M_PI/2)) pitch = -(M_PI/2); - - return pitch; -} - -} - -void -cg_free_rotation3d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_rotation3d_type = { - "CG_Rotation3D", cg_free_rotation3d}; - -static mrb_value -cg_cRotation3D_initialize(mrb_state *mrb, mrb_value self) -{ - mrb_float pitch, yaw, roll; - std::shared_ptr *ptr; - - mrb_get_args(mrb, "fff", &pitch, &yaw, &roll); - 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(pitch_limit(pitch), yaw, roll)); - - mrb_data_init(self, ptr, &cg_rotation3d_type); - return self; -} - -// Rotation when there is a up direction (ex.: Earth). -static mrb_value -cg_cRotation3D_rotate(mrb_state *mrb, mrb_value self) -{ - mrb_float pitch, yaw; - auto *ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "ff", &pitch, &yaw); - - (*ptr)->x = pitch_limit((*ptr)->x + pitch); - (*ptr)->y += yaw; - - return self; -} - -// Rotation when there is a up direction (ex.: space without gravity). -static mrb_value -cg_cRotation3D_baseless_rotation(mrb_state *mrb, mrb_value self) -{ - mrb_float x, y; - - // TODO - - return self; -} - -void -cg_rotation3d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cRotation3D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cRotation3D = mrb_define_class_under( - mrb, cg_m, "Rotation3D", mrb->object_class); - MRB_SET_INSTANCE_TT(cg_cRotation3D, MRB_TT_DATA); - mrb_define_method( - mrb, cg_cRotation3D, "initialize", cg_cRotation3D_initialize, - MRB_ARGS_REQ(3)); - - mrb_define_method( - mrb, cg_cRotation3D, "pitch", cg_cVector3D_get_x, MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cRotation3D, "yaw", cg_cVector3D_get_y, MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cRotation3D, "roll", cg_cVector3D_get_z, MRB_ARGS_NONE()); - - mrb_define_method( - mrb, cg_cRotation3D, "rotate", cg_cRotation3D_rotate, MRB_ARGS_REQ(2)); -} diff --git a/src/rotation3d.hpp b/src/rotation3d.hpp deleted file mode 100644 index 6d3980d..0000000 --- a/src/rotation3d.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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. - */ - -#ifndef CANDY_GEAR_ROTATION_3D_H -#define CANDY_GEAR_ROTATION_3D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_rotation3d_type; - -void -cg_rotation3d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_ROTATION_3D_H */ diff --git a/src/rotation_3d.cpp b/src/rotation_3d.cpp new file mode 100644 index 0000000..db0ce5a --- /dev/null +++ b/src/rotation_3d.cpp @@ -0,0 +1,113 @@ +/* + * 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 "rotation_3d.hpp" + +#include "vector_3d.hpp" + +namespace +{ +inline float +pitch_limit(float pitch) +{ + if(pitch > M_PI/2) pitch = M_PI/2; + else if (pitch < -(M_PI/2)) pitch = -(M_PI/2); + + return pitch; +} + +} + +void +cg_free_rotation_3d(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast*>(obj); + + ptr->~shared_ptr(); + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_rotation_3d_type = { + "CG_Rotation3D", cg_free_rotation_3d}; + +static mrb_value +cg_cRotation3D_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_float pitch, yaw, roll; + std::shared_ptr *ptr; + + mrb_get_args(mrb, "fff", &pitch, &yaw, &roll); + 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(pitch_limit(pitch), yaw, roll)); + + mrb_data_init(self, ptr, &cg_rotation_3d_type); + return self; +} + +// Rotation when there is a up direction (ex.: Earth). +static mrb_value +cg_cRotation3D_rotate(mrb_state *mrb, mrb_value self) +{ + mrb_float pitch, yaw; + auto *ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "ff", &pitch, &yaw); + + (*ptr)->x = pitch_limit((*ptr)->x + pitch); + (*ptr)->y += yaw; + + return self; +} + +// Rotation when there is a up direction (ex.: space without gravity). +static mrb_value +cg_cRotation3D_baseless_rotation(mrb_state *mrb, mrb_value self) +{ + mrb_float x, y; + + // TODO + + return self; +} + +void +cg_rotation_3d_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cRotation3D; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cRotation3D = mrb_define_class_under( + mrb, cg_m, "Rotation3D", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cRotation3D, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cRotation3D, "initialize", cg_cRotation3D_initialize, + MRB_ARGS_REQ(3)); + + mrb_define_method( + mrb, cg_cRotation3D, "pitch", cg_cVector3D_get_x, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRotation3D, "yaw", cg_cVector3D_get_y, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRotation3D, "roll", cg_cVector3D_get_z, MRB_ARGS_NONE()); + + mrb_define_method( + mrb, cg_cRotation3D, "rotate", cg_cRotation3D_rotate, MRB_ARGS_REQ(2)); +} diff --git a/src/rotation_3d.hpp b/src/rotation_3d.hpp new file mode 100644 index 0000000..bba4bec --- /dev/null +++ b/src/rotation_3d.hpp @@ -0,0 +1,27 @@ +/* + * 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. + */ + +#ifndef CANDY_GEAR_ROTATION_3D_H +#define CANDY_GEAR_ROTATION_3D_H 1 + +#include "core.hpp" + +extern const struct mrb_data_type cg_rotation_3d_type; + +void +cg_rotation_3d_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_ROTATION_3D_H */ diff --git a/src/vector3d.cpp b/src/vector3d.cpp deleted file mode 100644 index a2e0e4c..0000000 --- a/src/vector3d.cpp +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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 "vector3d.hpp" - -#include - -#include - -#include "rotation3d.hpp" - -void -cg_free_vector3d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_vector3d_type = { - "CG_Vector3D", cg_free_vector3d}; - -static mrb_value -cg_cVector3D_initialize(mrb_state *mrb, mrb_value self) -{ - mrb_float x = 0.0f; - mrb_float y = 0.0f; - mrb_float z = 0.0f; - std::shared_ptr *ptr; - - mrb_get_args(mrb, "|fff", &x, &y, &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(x, y, z)); - - mrb_data_init(self, ptr, &cg_vector3d_type); - return self; -} - -mrb_value -cg_cVector3D_get_x(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - return mrb_float_value(mrb, (*ptr)->x); -} - -mrb_value -cg_cVector3D_get_y(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - return mrb_float_value(mrb, (*ptr)->y); -} - -mrb_value -cg_cVector3D_get_z(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - return mrb_float_value(mrb, (*ptr)->z); -} - -static mrb_value -cg_cVector3D_set_x(mrb_state *mrb, mrb_value self) -{ - mrb_float x; - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "f", &x); - (*ptr)->x = x; - - return self; -} - -static mrb_value -cg_cVector3D_set_y(mrb_state *mrb, mrb_value self) -{ - mrb_float y; - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "f", &y); - (*ptr)->y = y; - - return self; -} - -static mrb_value -cg_cVector3D_set_z(mrb_state *mrb, mrb_value self) -{ - mrb_float z; - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "f", &z); - (*ptr)->z = z; - - return self; -} - -static mrb_value -cg_cVector3D_translate(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *ptr = - (std::shared_ptr*)DATA_PTR(self); - std::shared_ptr *direction, *rotation; - - mrb_get_args( - mrb, "dd", &direction, &cg_vector3d_type, &rotation, &cg_rotation3d_type); - - glm::mat4 matrix{1.0f}; - glm::vec4 vec{(**direction), 1.0f}; - matrix = glm::rotate(matrix, (*rotation)->y, glm::vec3{0.0f, 1.0f, 0.0f}); - matrix = glm::rotate(matrix, (*rotation)->x, glm::vec3{1.0f, 0.0f, 0.0f}); - matrix = glm::rotate(matrix, (*rotation)->z, glm::vec3{0.0f, 0.0f, 1.0f}); - vec = matrix * vec; - - (*ptr)->x += vec.x; - (*ptr)->y += vec.y; - (*ptr)->z += vec.z; - - return self; -} - -void -cg_vector3d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cVector3D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cVector3D = mrb_define_class_under( - mrb, cg_m, "Vector3D", mrb->object_class); - MRB_SET_INSTANCE_TT(cg_cVector3D, MRB_TT_DATA); - mrb_define_method( - mrb, cg_cVector3D, "initialize", cg_cVector3D_initialize, - MRB_ARGS_NONE()|MRB_ARGS_OPT(3)); - - mrb_define_method( - mrb, cg_cVector3D, "x", cg_cVector3D_get_x, MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cVector3D, "y", cg_cVector3D_get_y, MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cVector3D, "z", cg_cVector3D_get_z, MRB_ARGS_NONE()); - - mrb_define_method( - mrb, cg_cVector3D, "x=", cg_cVector3D_set_x, MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cVector3D, "y=", cg_cVector3D_set_y, MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cVector3D, "z=", cg_cVector3D_set_z, MRB_ARGS_REQ(1)); - - mrb_define_method( - mrb, cg_cVector3D, "translate", cg_cVector3D_translate, MRB_ARGS_REQ(2)); -} diff --git a/src/vector3d.hpp b/src/vector3d.hpp deleted file mode 100644 index fab6f61..0000000 --- a/src/vector3d.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ - -#ifndef CANDY_GEAR_VECTOR_3D_H -#define CANDY_GEAR_VECTOR_3D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_vector3d_type; - -mrb_value -cg_cVector3D_get_x(mrb_state *mrb, mrb_value self); -mrb_value -cg_cVector3D_get_y(mrb_state *mrb, mrb_value self); -mrb_value -cg_cVector3D_get_z(mrb_state *mrb, mrb_value self); - -void -cg_vector3d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_VECTOR_3D_H */ diff --git a/src/vector_3d.cpp b/src/vector_3d.cpp new file mode 100644 index 0000000..6417a18 --- /dev/null +++ b/src/vector_3d.cpp @@ -0,0 +1,177 @@ +/* + * 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 "vector_3d.hpp" + +#include + +#include + +#include "rotation_3d.hpp" + +void +cg_free_vector_3d(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast*>(obj); + + ptr->~shared_ptr(); + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_vector_3d_type = { + "CG_Vector3D", cg_free_vector_3d}; + +static mrb_value +cg_cVector3D_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_float x = 0.0f; + mrb_float y = 0.0f; + mrb_float z = 0.0f; + std::shared_ptr *ptr; + + mrb_get_args(mrb, "|fff", &x, &y, &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(x, y, z)); + + mrb_data_init(self, ptr, &cg_vector_3d_type); + return self; +} + +mrb_value +cg_cVector3D_get_x(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + return mrb_float_value(mrb, (*ptr)->x); +} + +mrb_value +cg_cVector3D_get_y(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + return mrb_float_value(mrb, (*ptr)->y); +} + +mrb_value +cg_cVector3D_get_z(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + return mrb_float_value(mrb, (*ptr)->z); +} + +static mrb_value +cg_cVector3D_set_x(mrb_state *mrb, mrb_value self) +{ + mrb_float x; + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "f", &x); + (*ptr)->x = x; + + return self; +} + +static mrb_value +cg_cVector3D_set_y(mrb_state *mrb, mrb_value self) +{ + mrb_float y; + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "f", &y); + (*ptr)->y = y; + + return self; +} + +static mrb_value +cg_cVector3D_set_z(mrb_state *mrb, mrb_value self) +{ + mrb_float z; + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "f", &z); + (*ptr)->z = z; + + return self; +} + +static mrb_value +cg_cVector3D_translate(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *ptr = + (std::shared_ptr*)DATA_PTR(self); + std::shared_ptr *direction, *rotation; + + mrb_get_args( + mrb, "dd", &direction, &cg_vector_3d_type, + &rotation, &cg_rotation_3d_type); + + glm::mat4 matrix{1.0f}; + glm::vec4 vec{(**direction), 1.0f}; + matrix = glm::rotate(matrix, (*rotation)->y, glm::vec3{0.0f, 1.0f, 0.0f}); + matrix = glm::rotate(matrix, (*rotation)->x, glm::vec3{1.0f, 0.0f, 0.0f}); + matrix = glm::rotate(matrix, (*rotation)->z, glm::vec3{0.0f, 0.0f, 1.0f}); + vec = matrix * vec; + + (*ptr)->x += vec.x; + (*ptr)->y += vec.y; + (*ptr)->z += vec.z; + + return self; +} + +void +cg_vector_3d_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cVector3D; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cVector3D = mrb_define_class_under( + mrb, cg_m, "Vector3D", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cVector3D, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cVector3D, "initialize", cg_cVector3D_initialize, + MRB_ARGS_NONE()|MRB_ARGS_OPT(3)); + + mrb_define_method( + mrb, cg_cVector3D, "x", cg_cVector3D_get_x, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cVector3D, "y", cg_cVector3D_get_y, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cVector3D, "z", cg_cVector3D_get_z, MRB_ARGS_NONE()); + + mrb_define_method( + mrb, cg_cVector3D, "x=", cg_cVector3D_set_x, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cVector3D, "y=", cg_cVector3D_set_y, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cVector3D, "z=", cg_cVector3D_set_z, MRB_ARGS_REQ(1)); + + mrb_define_method( + mrb, cg_cVector3D, "translate", cg_cVector3D_translate, MRB_ARGS_REQ(2)); +} diff --git a/src/vector_3d.hpp b/src/vector_3d.hpp new file mode 100644 index 0000000..2827813 --- /dev/null +++ b/src/vector_3d.hpp @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef CANDY_GEAR_VECTOR_3D_H +#define CANDY_GEAR_VECTOR_3D_H 1 + +#include "core.hpp" + +extern const struct mrb_data_type cg_vector_3d_type; + +mrb_value +cg_cVector3D_get_x(mrb_state *mrb, mrb_value self); +mrb_value +cg_cVector3D_get_y(mrb_state *mrb, mrb_value self); +mrb_value +cg_cVector3D_get_z(mrb_state *mrb, mrb_value self); + +void +cg_vector_3d_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_VECTOR_3D_H */ -- cgit v1.2.3