summaryrefslogtreecommitdiff
path: root/src/model/instance.cpp
blob: 83a93c22089b19372a5e8b13b7cd96e043ad8c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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<VK::ModelInstance*>(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<VK::Model> *model_ptr;

  model_v = mrb_iv_get(mrb, self, id_at_model);
  ptr = (VK::ModelInstance*)DATA_PTR(self);
  model_ptr = (std::shared_ptr<VK::Model>*) 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());
}