summaryrefslogtreecommitdiff
path: root/src/camera.cpp
blob: 27805ac3dcc0de71acd0d9869e777d8ec7ef3dab (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * 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<std::shared_ptr<VK::Camera>*>(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<VK::Camera> *ptr;

  mrb_get_args(
    mrb, "ffffff",
    &position_x, &position_y, &position_z,
    &rotation_x, &rotation_y, &rotation_z);
  ptr = (std::shared_ptr<VK::Camera>*)DATA_PTR(self);
  if(ptr) mrb_free(mrb, ptr);
  ptr = (std::shared_ptr<VK::Camera>*)mrb_malloc(
    mrb, sizeof(std::shared_ptr<VK::Camera>));
  
  new(ptr)std::shared_ptr<VK::Camera>(std::make_shared<VK::Camera>());
  (*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<VK::Camera> *ptr;

  ptr = (std::shared_ptr<VK::Camera>*)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<VK::Camera> *ptr;

  mrb_get_args(mrb, "fff", &x, &y, &z);
  ptr = (std::shared_ptr<VK::Camera>*)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<VK::Camera> *ptr;

  mrb_get_args(mrb, "fff", &x, &y, &z);
  ptr = (std::shared_ptr<VK::Camera>*)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<VK::Camera> *ptr;

  mrb_get_args(mrb, "fff", &x, &y, &z);
  ptr = (std::shared_ptr<VK::Camera>*)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));
}