-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.cpp
More file actions
547 lines (471 loc) · 20.2 KB
/
editor.cpp
File metadata and controls
547 lines (471 loc) · 20.2 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include "editor.h"
#include "scene.h"
#include "obj_model.h"
#include "res_man.h"
#include "renderer.h"
#include "render_utils.h"
#include "engine/gameos.hpp"
#include "engine/utils/camera.h"
#include "utils/logging.h"
#include "utils/vec.h"
#include "utils/quaternion.h"
#include "utils/logging.h"
#include "utils/math_utils.h"
#include "utils/camera_utils.h"
#include <algorithm>
#include <vector>
#include <cassert>
#include <cfloat> // FLT_MAX
enum class EditorOpMode {
kMove,
kRotate,
kScale
};
enum class GizmoMode {
kMove,
kRotate,
kScale
};
class Gizmo {
static const float kAxisLength;
static const float kAxisWidth;
static const float kRotSphereRadius;
static const float kScaleCubesScale;
static const float kScreenPercentage;
GizmoMode mode_ = GizmoMode::kMove;
vec3 pos_ = vec3(0,0,0);
mat4 rot_ = mat4::identity();
quaternion rot_q_ = quaternion::identity();
bool bWorldSpace = false;
public:
// controlled object position
void set_position(const vec3 &pos) { pos_ = pos; }
vec3 get_position() const { return pos_; }
// controlled object rotation
void set_rotation(const quaternion& q) { rot_q_ = q; rot_ = quat_to_mat4(q); }
const mat4& get_rotation() const { return rot_; }
const quaternion& get_rotation_q() const { return rot_q_; }
void set_world_space(bool ws) { bWorldSpace = ws; }
bool get_world_space() { return bWorldSpace; }
void update_mode(const EditorOpMode ed_mode) {
if (EditorOpMode::kMove == ed_mode)
mode_ = GizmoMode::kMove;
else if (EditorOpMode::kRotate == ed_mode)
mode_ = GizmoMode::kRotate;
else if (EditorOpMode::kScale == ed_mode)
mode_ = GizmoMode::kScale;
else
mode_ = GizmoMode::kMove;
}
float get_gizmo_scale(const struct RenderFrameContext* rfc) const {
return get_scale(pos_, rfc->view_, rfc->proj_, rfc->fov_, rfc->b_is_perspective_);
}
static float get_scale(const vec3& pos, const mat4& view, const mat4& proj, const float fov, bool b_is_perspective) {
if(b_is_perspective) {
// get how many world units will be wisible horizontally at given pos_.z;
const float view_z = (view * vec4(pos, 1.0f)).z;
const float max_x_at_z = 2.0f*(1.0f/proj.elem[0][0])*view_z;
return max_x_at_z * kScreenPercentage;
} else {
const float max_x = 2.0f / proj.elem[0][0];
return max_x * kScreenPercentage;
}
}
float get_rotation_sphere_radius(const camera* cam) const {
return kRotSphereRadius * get_scale(pos_, cam->get_view(), cam->get_projection(),
cam->get_fov(), cam->get_is_perspective());
}
void draw(struct RenderFrameContext* rfc) {
const vec3 pos = pos_;
GizmoMode mode = mode_;
const mat4 rot = bWorldSpace ? mat4::identity() : rot_;
RenderMesh *cube = res_man_load_mesh("cube");
const float scaler = get_scale(pos_, rfc->view_, rfc->proj_, rfc->fov_, rfc->b_is_perspective_);
const float al = kAxisLength;
const float aw = kAxisWidth;
const mat4 tr_x = mat4::translation(pos) * rot * mat4::translation(vec3(al * scaler, 0.0f, 0.0f)) *
mat4::scale(vec3(al, aw, aw) * scaler);
const mat4 tr_y = mat4::translation(pos) * rot * mat4::translation(vec3(0.0f, al *scaler, 0.0f)) *
mat4::scale(vec3(aw, al, aw) * scaler);
const mat4 tr_z = mat4::translation(pos) * rot * mat4::translation(vec3(0.0f, 0.0f, al * scaler)) *
mat4::scale(vec3(aw, aw, al) * scaler);
uint32_t axis_x_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveX : 0;
uint32_t axis_y_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveY : 0;
uint32_t axis_z_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveZ : 0;
add_debug_mesh(rfc, cube, tr_x, vec4(1.0f, 0.15f, 0.15f, 1.0f), axis_x_id);
add_debug_mesh(rfc, cube, tr_y, vec4(0.15f, 1.0f, 0.15f, 1.0f), axis_y_id);
add_debug_mesh(rfc, cube, tr_z, vec4(0.15f, 0.15f, 1.0f, 1.0f), axis_z_id);
if (GizmoMode::kScale == mode) {
const float cl = aw*kScaleCubesScale;
const mat4 scale_cube_scale = mat4::scale(vec3(cl, cl, cl) * scaler);
const mat4 tr_sx =
mat4::translation(pos) * rot * mat4::translation(vec3(2.0f * al * scaler, 0.0f, 0.0f)) * scale_cube_scale ;
const mat4 tr_sy =
mat4::translation(pos) * rot * mat4::translation(vec3(0.0f, 2.0f * al * scaler, 0.0f)) * scale_cube_scale ;
const mat4 tr_sz =
mat4::translation(pos) * rot * mat4::translation(vec3(0.0f, 0.0f, 2.0f * al * scaler)) * scale_cube_scale ;
add_debug_mesh(rfc, cube, tr_sx, vec4(1.0f, 0.15f, 0.15f, 1.0f),
ReservedObjIds::kGizmoScaleX);
add_debug_mesh(rfc, cube, tr_sy, vec4(0.15f, 1.0f, 0.15f, 1.0f),
ReservedObjIds::kGizmoScaleY);
add_debug_mesh(rfc, cube, tr_sz, vec4(0.15f, 0.15f, 1.0f, 1.0f),
ReservedObjIds::kGizmoScaleZ);
const mat4 tr_sxyz = mat4::translation(pos) * scale_cube_scale;
add_debug_mesh(rfc, cube, tr_sxyz, vec4(0.15f, 0.15f, 1.0f, 1.0f),
ReservedObjIds::kGizmoScaleXYZ);
}
const float sl = al*0.25f*scaler;
const float st = al*scaler;
const mat4 tr_xz = mat4::translation(pos) * rot * mat4::translation(vec3(st, 0.0f, st)) * mat4::scale(vec3(sl, sl*0.01f, sl));
const mat4 tr_yx = mat4::translation(pos) * rot * mat4::translation(vec3(st, st, 0.0f)) * mat4::scale(vec3(sl, sl, sl*0.01f));
const mat4 tr_yz = mat4::translation(pos) * rot * mat4::translation(vec3(0.0f, st, st)) * mat4::scale(vec3(sl*0.01f, sl, sl));
if (GizmoMode::kRotate != mode) {
uint32_t plane_xz_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveXZ
: ReservedObjIds::kGizmoScaleXZ;
uint32_t plane_yx_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveYX
: ReservedObjIds::kGizmoScaleYX;
uint32_t plane_yz_id = GizmoMode::kMove == mode ? ReservedObjIds::kGizmoMoveYZ
: ReservedObjIds::kGizmoScaleYZ;
add_debug_mesh(rfc, cube, tr_xz, vec4(1.0f, 0.0f, 1.0f, .25f), plane_xz_id);
add_debug_mesh(rfc, cube, tr_yx, vec4(1.0f, 1.0f, 0.0f, .25f), plane_yx_id);
add_debug_mesh(rfc, cube, tr_yz, vec4(0.0f, 1.0f, 1.0f, .25f), plane_yz_id);
} else {
RenderMesh *torus = res_man_load_mesh("torus");
RenderMesh *sphere = res_man_load_mesh("sphere");
const float sr = kRotSphereRadius;
const mat4 tr_rx = mat4::translation(pos) * rot * mat4::rotationY(90 * M_PI / 180.0f) *
mat4::scale(vec3(sr, sr, 0.01f) * scaler);
const mat4 tr_ry = mat4::translation(pos) * rot * mat4::rotationX(90 * M_PI / 180.0f) *
mat4::scale(vec3(sr, sr, 0.01f) * scaler);
const mat4 tr_rz = mat4::translation(pos) * rot * mat4::scale(vec3(sr, sr, 0.01f) * scaler);
const mat4 tr_s = mat4::translation(pos) * mat4::scale(vec3(sr, sr, sr) * scaler);
add_debug_mesh(rfc, sphere, tr_s, vec4(.5f, 0.5f, .5f, .4f),
ReservedObjIds::kGizmoRotateXYZ);
add_debug_mesh(rfc, torus, tr_rx, vec4(1.0f, 0.f, 0.f, 1.0f),
ReservedObjIds::kGizmoRotateX);
add_debug_mesh(rfc, torus, tr_ry, vec4(0.f, 1.0f, 0.f, 1.0f),
ReservedObjIds::kGizmoRotateY);
add_debug_mesh(rfc, torus, tr_rz, vec4(0.f, 0.f, 1.0f, 1.0f),
ReservedObjIds::kGizmoRotateZ);
}
}
};
const float Gizmo::kAxisLength = 1.0f;
const float Gizmo::kAxisWidth = .05f;
const float Gizmo::kRotSphereRadius = 1.5f;
const float Gizmo::kScaleCubesScale = 1.2f;
const float Gizmo::kScreenPercentage = .05f;
Gizmo g_gizmo;
// TODO: move all variables in a single Editor state
static EditorOpMode s_editor_mode = EditorOpMode::kMove;
static GameObject* g_sel_obj = nullptr;
static std::vector<UserEditorInterface> registered_editors;
static int g_active_user_editor = -1;
void initialize_editor()
{
}
void finalize_editor()
{
}
int editor_register_user_editor(UserEditorInterface ue_interface) {
registered_editors.push_back(ue_interface);
return (int)registered_editors.size()-1;
}
void editor_unregister_user_editor(int id) {
assert(id>=0 && id < (int)registered_editors.size());
registered_editors.erase(registered_editors.begin() + id);
}
// x,y = [-1,1] screen space range
GameObject* select_object_under_cursor(const camera *cam, float x, float y) {
const mat4& inv_view = cam->get_inv_view();
const vec3 ws_cursor_pos =
get_cursor_pos(&inv_view, &cam->get_inv_projection(), x, y, 10.0f);
const vec3 ws_cam_pos = (inv_view * vec4(0, 0, 0, 1)).xyz();
const vec3 ws_dir = normalize(ws_cursor_pos - ws_cam_pos);
using el_t = std::pair<float, GameObject *>;
std::vector<el_t> int_obj;
scene_get_intersected_objects(ws_cam_pos, ws_dir, int_obj);
std::sort(int_obj.begin(), int_obj.end(),
[](const el_t &a, const el_t &b) -> bool { return a.first < b.first; });
if (int_obj.empty())
return nullptr;
GameObject *closest_obj = int_obj[0].second;
printf("int name: %s\n", closest_obj->GetName());
for (auto &obj : int_obj) {
printf("%.3f : %s\n", obj.first, obj.second->GetName());
}
return closest_obj;
}
// drag related variables
static bool drag_started = false;
static vec3 drag_start_mouse_world_pos;
static vec2 drag_start_mouse_proj_pos;
static vec3 drag_start_obj_pos;
static quaternion drag_start_obj_rot;
static vec3 drag_start_obj_scale;
static vec3 drag_start_obj_world_scale;
static vec3 drag_cur_mouse_world_pos;
static float drag_obj_view_dist;
static int drag_type;
static vec3 drag_rotation_gizmo_helper_pos;
static EditorOpMode update_input_mode(const EditorOpMode ed_mode, bool mouse_buttons_pressed) {
if(!mouse_buttons_pressed) {
if(gos_GetKeyStatus(KEY_Q) == KEY_PRESSED)
return EditorOpMode::kMove;
if(gos_GetKeyStatus(KEY_W) == KEY_PRESSED)
return EditorOpMode::kRotate;
if(gos_GetKeyStatus(KEY_E) == KEY_PRESSED)
return EditorOpMode::kScale;
}
return ed_mode;
}
void editor_update(camera *cam, const float dt) {
int XDelta, YDelta, WheelDelta;
float XPos, YPos;
DWORD buttonsPressed;
gos_GetMouseInfo(&XPos, &YPos, &XDelta, &YDelta, &WheelDelta, &buttonsPressed);
vec2 cur_mouse_proj_pos = 2 * vec2(XPos, 1 - YPos) - vec2(1);
const float screen_width = (float)Environment.drawableWidth;
const float screen_height = (float)Environment.drawableHeight;
if (gos_GetKeyStatus(KEY_ESCAPE) == KEY_RELEASED) {
if(g_active_user_editor == -1) {
gos_TerminateApplication();
} else {
g_active_user_editor = -1;
log_info("Switched to main editor\n");
}
}
if (gos_GetKeyStatus(KEY_1) == KEY_RELEASED)
g_gizmo.set_world_space(!g_gizmo.get_world_space());
s_editor_mode = update_input_mode(s_editor_mode, !!buttonsPressed);
g_gizmo.update_mode(s_editor_mode);
const uint32_t sel_id = scene_get_object_id_under_cursor();
GameObject *go_under_cursor = nullptr;
if (sel_id >= scene::kFirstGameObjectId) {
go_under_cursor = scene_get_object_by_id(sel_id);
}
if (gos_GetKeyStatus(KEY_LMOUSE) == KEY_PRESSED) {
if (go_under_cursor) {
g_sel_obj = go_under_cursor;
const auto *tc = g_sel_obj->GetComponent<TransformComponent>();
if (tc) {
g_gizmo.set_position(tc->GetPosition());
g_gizmo.set_rotation(tc->GetRotation());
}
}
else if (sel_id >= ReservedObjIds::kGizmoFirst && sel_id < ReservedObjIds::kGizmoLast) {
gosASSERT(g_sel_obj);
drag_type = sel_id;
const auto *tc = g_sel_obj->GetComponent<TransformComponent>();
if (tc) {
drag_start_obj_pos = tc->GetPosition();
drag_start_obj_rot = tc->GetRotation();
drag_start_obj_scale = tc->GetScale();
drag_start_obj_world_scale = tc->GetWorldSpaceScale();
mat4 vm = cam->get_view();
vec3 vp = (vm * vec4(drag_start_obj_pos, 1.0f)).getXYZ();
drag_obj_view_dist = vp.z;
drag_start_mouse_proj_pos = cur_mouse_proj_pos;
drag_start_mouse_world_pos =
screen2world(cam, cur_mouse_proj_pos, drag_obj_view_dist);
drag_started = true;
}
}
}
const bool lmouse_down = gos_GetKeyStatus(KEY_LMOUSE) == KEY_PRESSED || gos_GetKeyStatus(KEY_LMOUSE) == KEY_HELD;
if (drag_started && lmouse_down && drag_start_mouse_proj_pos!=cur_mouse_proj_pos) {
gosASSERT(g_sel_obj);
drag_cur_mouse_world_pos = screen2world(cam, cur_mouse_proj_pos, drag_obj_view_dist);
auto *tc = g_sel_obj->GetComponent<TransformComponent>();
g_gizmo.set_position(tc->GetPosition());
vec3 ray_origin;
vec3 ray_dir;
if(cam->get_is_perspective()) {
ray_origin = (cam->get_inv_view() * vec4(0, 0, 0, 1)).xyz();
ray_dir = normalize(drag_cur_mouse_world_pos - ray_origin);
} else {
ray_origin = (cam->get_inv_projection() * vec4(cur_mouse_proj_pos, 0, 1)).xyz();
ray_origin = (cam->get_inv_view() * vec4(ray_origin, 1)).xyz();
ray_dir = cam->get_view().getForwardVec();
}
vec3 ray_dir_start = normalize(drag_start_mouse_world_pos - ray_origin);
switch (drag_type) {
case ReservedObjIds::kGizmoMoveX:
case ReservedObjIds::kGizmoMoveY:
case ReservedObjIds::kGizmoMoveZ:
{
const int axis_idx = drag_type - ReservedObjIds::kGizmoMoveX;
static const vec3 axes[3] = {vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f)};
vec3 axis = axes[axis_idx];
if(!g_gizmo.get_world_space())
axis = quat_get_axis(g_gizmo.get_rotation_q(), axis_idx);
vec3 pr_start = project_on_vector(ray_dir, drag_start_mouse_world_pos, axis);
vec3 pr_end = project_on_vector(ray_dir, drag_cur_mouse_world_pos, axis);
vec3 upd_pos = drag_start_obj_pos + (pr_end - pr_start);
tc->SetPosition(upd_pos);
break;
}
case ReservedObjIds::kGizmoMoveXZ:
case ReservedObjIds::kGizmoMoveYX:
case ReservedObjIds::kGizmoMoveYZ:
{
const int plane_idx = drag_type - ReservedObjIds::kGizmoMoveXZ;
vec3 cur_pos = tc->GetPosition(); // maybe start pos?
const vec4 planes[3] = {vec4(0.0f, 1.0f, 0.0f, cur_pos.y),
vec4(0.0f, 0.0f, 1.0f, cur_pos.z),
vec4(1.0f, 0.0f, 0.0f, cur_pos.x)};
vec4 plane = planes[plane_idx];
if (!g_gizmo.get_world_space()) {
plane = transform_plane_around_point(g_gizmo.get_rotation(), plane, cur_pos);
}
vec3 start_pos = ray_plane_intersect(ray_dir_start, ray_origin, plane);
vec3 offset = start_pos - drag_start_obj_pos;
vec3 upd_pos = ray_plane_intersect(ray_dir, ray_origin, plane);
drag_rotation_gizmo_helper_pos = upd_pos;
tc->SetPosition(upd_pos - offset);
break;
}
case ReservedObjIds::kGizmoRotateX:
case ReservedObjIds::kGizmoRotateY:
case ReservedObjIds::kGizmoRotateZ:
{
vec3 cur_pos = tc->GetPosition();
int plane_idx = drag_type - ReservedObjIds::kGizmoRotateX;
const vec4 wplanes[3] = {vec4(1.0f, 0.0f, 0.0f, cur_pos.x),
vec4(0.0f, 1.0f, 0.0f, cur_pos.y),
vec4(0.0f, 0.0f, 1.0f, cur_pos.z)};
const vec4 oplanes[3] = {make_plane(drag_start_obj_rot.axis0(), cur_pos),
make_plane(drag_start_obj_rot.axis1(), cur_pos),
make_plane(drag_start_obj_rot.axis2(), cur_pos)};
const vec4 plane =
g_gizmo.get_world_space() ? wplanes[plane_idx] : oplanes[plane_idx];
vec3 start_pos = ray_plane_intersect(ray_dir_start, ray_origin, plane);
vec3 upd_pos = ray_plane_intersect(ray_dir, ray_origin, plane);
float r = g_gizmo.get_rotation_sphere_radius(cam);
drag_rotation_gizmo_helper_pos = normalize(upd_pos - cur_pos) * r + cur_pos;
// calculate angle between 2 vectors
vec3 v0 = normalize(start_pos - drag_start_obj_pos);
vec3 v1 = normalize(upd_pos - drag_start_obj_pos);
float dp = clamp(dot(v0, v1), -1.0f, 1.0f);
float angle = acosf(dp);
vec3 perp = cross(v0, v1);
vec3 rot_axis = plane.xyz();
float k = dot(perp, rot_axis) < 0.0f ? -1.0f : 1.0f;
quaternion add_rot = quaternion(rot_axis, angle * k);
quaternion upd_rot = add_rot * drag_start_obj_rot;
tc->SetRotation(upd_rot);
g_gizmo.set_rotation(upd_rot);
break;
}
case ReservedObjIds::kGizmoRotateXYZ:
{
float r = g_gizmo.get_rotation_sphere_radius(cam);
vec4 sphere = vec4(drag_start_obj_pos, r);
vec3 start_pos = ray_sphere_intersect(ray_dir_start, ray_origin, sphere);
vec3 upd_pos = ray_sphere_intersect(ray_dir, ray_origin, sphere);
if (upd_pos.x == FLT_MAX) { // no intersection
break;
}
vec3 v0 = normalize(start_pos - drag_start_obj_pos);
vec3 v1 = normalize(upd_pos - drag_start_obj_pos);
gosASSERT(v0 != v1);
vec3 axis = normalize(cross(v0, v1));
float angle = acosf(clamp(dot(v0, v1), -1.0f ,1.0f));
quaternion add_rot = quaternion(axis, angle);
quaternion upd_rot = add_rot * drag_start_obj_rot;
tc->SetRotation(upd_rot);
g_gizmo.set_rotation(upd_rot);
drag_rotation_gizmo_helper_pos = upd_pos;
break;
}
case ReservedObjIds::kGizmoScaleX:
case ReservedObjIds::kGizmoScaleY:
case ReservedObjIds::kGizmoScaleZ:
case ReservedObjIds::kGizmoScaleXZ:
case ReservedObjIds::kGizmoScaleYX:
case ReservedObjIds::kGizmoScaleYZ:
case ReservedObjIds::kGizmoScaleXYZ:
{
const int axis_idx = drag_type - ReservedObjIds::kGizmoScaleX;
const vec3 axes[7] = { vec3(1,0,0), vec3(0,1,0), vec3(0,0,1), vec3(1,0,1), vec3(1,1,0), vec3(0,1,1), vec3(1,1,1) };
const vec2 screen_delta =
proj2screen(cur_mouse_proj_pos, screen_width, screen_height) -
proj2screen(drag_start_mouse_proj_pos, screen_width, screen_height);
// TODO: use distance to the object as an additional multiplier for better UX?
const float scale = screen_delta.x * 0.025f;
const vec3 scale_axis = scale * axes[axis_idx] + vec3(1,1,1);
// for scaling in world space we would need to support skew, that means we will not have pure scale, rotate transform anymore
// could be handled by adding 4th matrix Sw so the wole stack will be: T * Sw * R * Sl,
// but I think it is a bit of an overkill, so no support for World Space scale at the moment
if (g_gizmo.get_world_space()) {
tc->SetWorldSpaceScale(drag_start_obj_world_scale * scale_axis);
}
else {
tc->SetScale(drag_start_obj_scale * scale_axis);
}
break;
}
}
}
if (gos_GetKeyStatus(KEY_LMOUSE) == KEY_RELEASED) {
drag_started = false;
log_debug("drag stop\n");
}
for(auto i=0; i<(int)registered_editors.size(); ++i) {
if(registered_editors[i].wants_activate()) {
g_active_user_editor = i;
log_info("Switched to editor: %s\n", registered_editors[i].name());
}
}
if(g_active_user_editor != -1) {
g_sel_obj = registered_editors[g_active_user_editor].update(cam, dt, g_sel_obj);
}
}
void editor_render_update(struct RenderFrameContext *rfc)
{
if(g_sel_obj) {
auto* tc = g_sel_obj->GetComponent<TransformComponent>();
if(tc) {
if(!drag_started) {
// object may be updating its position
g_gizmo.set_position(tc->GetPosition());
g_gizmo.set_rotation(tc->GetRotation());
} else {
if ((uint32_t)drag_type >= ReservedObjIds::kGizmoMoveXZ &&
(uint32_t)drag_type <= ReservedObjIds ::kGizmoRotateXYZ) {
// a) only makes sence when we drag as its position is in absolute coord,
// and will not take into accout gizmo scaling
// b) when mouse if not moving will draw last known position, minor, fix it later
float s = g_gizmo.get_gizmo_scale(rfc);
const mat4 tr = mat4::translation(drag_rotation_gizmo_helper_pos) *
mat4::scale(vec3(s * 0.05f));
add_debug_mesh(rfc, res_man_load_mesh("sphere"), tr,
vec4(1, 1, 1, 1));
}
}
const vec3 gp = g_gizmo.get_position();
const float s = g_gizmo.get_gizmo_scale(rfc);
const quaternion& q = g_gizmo.get_world_space() ? quaternion::identity()
: g_gizmo.get_rotation_q();
rfc->rl_->addDebugLine(gp, gp + 2.5f*s*q.axis0(), vec4(1, 0, 0, 1));
rfc->rl_->addDebugLine(gp, gp + 2.5f*s*q.axis1(), vec4(0, 1, 0, 1));
rfc->rl_->addDebugLine(gp, gp + 2.5f*s*q.axis2(), vec4(0, 0, 1, 1));
g_gizmo.draw(rfc);
}
}
RenderMesh *sphere = res_man_load_mesh("sphere");
assert(sphere);
{
auto& light_list = scene_get_light_list();
rfc->rl_->ReservePackets(light_list.size());
// add lights to debug render pass
for (auto &l : light_list) {
vec4 c(l.color_.getXYZ(), 0.5f);
add_debug_mesh_constant_size(rfc, sphere, c, l.transform_, 1000.0f);
}
}
if(g_active_user_editor != -1) {
registered_editors[g_active_user_editor].render_update(rfc);
}
}