From f88712a929ee3543f8e1d45c6071f676df339cdb Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Tue, 2 Aug 2022 16:52:33 -0300 Subject: refa Use Vulkan for graphics This is a partial refactory. Some functionalities implemented in SDL were removed and need reimplementation. --- src/model/instance.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/model/instance.hpp | 25 +++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/model/instance.cpp create mode 100644 src/model/instance.hpp (limited to 'src/model') diff --git a/src/model/instance.cpp b/src/model/instance.cpp new file mode 100644 index 0000000..83a93c2 --- /dev/null +++ b/src/model/instance.cpp @@ -0,0 +1,98 @@ +/* + * 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 "instance.hpp" + +#include "../model.hpp" +#include "../vk/model_instance.hpp" + +// This variable works as a constant +static mrb_sym id_at_model; + +void +cg_free_model_instance(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast(obj); + + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_model_instance_type = { + "CG_Model_Instance", cg_free_model_instance }; + +static mrb_value +cg_cModel_cInstance_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_float position_x, position_y, position_z, + rotation_x, rotation_y, rotation_z; + mrb_value model; + VK::ModelInstance *ptr; + + mrb_get_args( + mrb, "offffff", + &model, + &position_x, &position_y, &position_z, + &rotation_x, &rotation_y, &rotation_z); + ptr = (VK::ModelInstance*)DATA_PTR(self); + if(ptr) mrb_free(mrb, ptr); + ptr = (VK::ModelInstance*)mrb_malloc(mrb, sizeof(VK::ModelInstance)); + + mrb_iv_set(mrb, self, id_at_model, model); + 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_model_instance_type); + return self; +} + +static mrb_value +cg_cModel_cInstance_draw(mrb_state *mrb, mrb_value self) +{ + mrb_value model_v; + + VK::ModelInstance *ptr; + std::shared_ptr *model_ptr; + + model_v = mrb_iv_get(mrb, self, id_at_model); + ptr = (VK::ModelInstance*)DATA_PTR(self); + model_ptr = (std::shared_ptr*) DATA_PTR(model_v); + + auto &instances = + cg_core.vk_graphics_pipeline->models_to_draw[ + cg_core.vk_graphics_pipeline->current_frame][*model_ptr]; + instances.push_back(*ptr); + + return self; +} + +void +cg_model_instance_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cModel, *cg_cInstance; + + id_at_model = mrb_intern_cstr(mrb, "@model"); + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cModel = mrb_class_get_under(mrb, cg_m, "Model"); + cg_cInstance = mrb_define_class_under( + mrb, cg_cModel, "Instance", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cInstance, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cInstance, "initialize", cg_cModel_cInstance_initialize, + MRB_ARGS_REQ(7)); + mrb_define_method( + mrb, cg_cInstance, "draw", cg_cModel_cInstance_draw, MRB_ARGS_NONE()); +} diff --git a/src/model/instance.hpp b/src/model/instance.hpp new file mode 100644 index 0000000..f84003c --- /dev/null +++ b/src/model/instance.hpp @@ -0,0 +1,25 @@ +/* + * 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_MODEL_INSTANCE_H +#define CANDY_GEAR_MODEL_INSTANCE_H 1 + +#include "../core.hpp" + +void +cg_model_instance_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_MODEL_INSTANCE_H */ -- cgit v1.2.3