-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.c
484 lines (339 loc) · 11.8 KB
/
object.c
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
#define __STDC_WANT_LIB_EXT1__ 1
#include "object.h"
#include "../math/mat4x4.h"
#include <string.h>
#include <stdio.h>
#ifndef DEBUG_OUTPUT
#define DEBUG_OUTPUT 0
#endif
float lerp(float x0, float x1, float t0, float t1, float t) {
return x0 + ((x1-x0) * (t-t0)) / (t1-t0);
}
void powergl_animation_next(powergl_object *obj, float delta_time){
#if DEBUG_OUTPUT
printf("\n%s\n", __func__);
#endif
powergl_animation *anim = &obj->animation;
for(size_t i = 0; i < anim->n_channel; i++){
float *pctime = &anim->channels[i].current_time;
size_t *pckey = &anim->channels[i].current_key;
const char *ctype = anim->channels[i].types[*pckey];
*pctime += delta_time;
if(strcmp(ctype, "STEP") == 0){
if(*pctime >= anim->channels[i].inputs[*pckey]){
*anim->channels[i].target = anim->channels[i].outputs[*pckey];
*pckey += 1;
obj->transform.matrix_flag = 1;
if(*pckey >= anim->channels[i].count){
*pckey = 0;
*pctime = 0.0f;
}
}
} else if(strcmp(ctype, "LINEAR") == 0){
if(*pctime >= anim->channels[i].inputs[*pckey+1]){
*pckey += 1;
if(*pckey >= anim->channels[i].count){
*pckey = 0;
*pctime = 0.0f;
}
}
*anim->channels[i].target = lerp(anim->channels[i].outputs[*pckey], anim->channels[i].outputs[*pckey+1],
anim->channels[i].inputs[*pckey], anim->channels[i].inputs[*pckey+1], *pctime);
obj->transform.matrix_flag = 1;
} else {
fprintf(stderr, "%s -> cant process next keyframe interpolation type = %s\n",
__func__ ,ctype);
}
}
}
void powergl_light_update(powergl_object *obj){
}
void powergl_camera_update(powergl_object *obj){
#if DEBUG_OUTPUT
printf("\n%s\n", __func__);
#endif
powergl_mat4 rot, rot2;
powergl_mat4 sca, sca2;
powergl_mat4 tra, tra2;
if(obj->transform.matrix_flag == 1){
sca = powergl_mat4_ident();
sca.c[0].x = 1.0f / obj->transform.scale.x;
sca.c[1].y = 1.0f / obj->transform.scale.y;
sca.c[2].z = 1.0f / obj->transform.scale.z;
rot = powergl_mat4_ident();
rot = powergl_mat4_rot(rot, obj->transform.rotation_z.w, obj->transform.rotation_z.xyz);
rot = powergl_mat4_rot(rot, obj->transform.rotation_y.w, obj->transform.rotation_y.xyz);
rot = powergl_mat4_rot(rot, obj->transform.rotation_x.w, obj->transform.rotation_x.xyz);
rot = powergl_mat4_transpose(rot);
tra = powergl_mat4_ident();
tra.c[3].x = -obj->transform.location.x;
tra.c[3].y = -obj->transform.location.y;
tra.c[3].z = -obj->transform.location.z;
obj->camera.view = powergl_mat4_mul(rot, tra);
obj->camera.view = powergl_mat4_mul(sca, obj->camera.view);
obj->camera.view_flag = 1;
}
if(obj->parent !=NULL && obj->parent->transform.matrix_flag == 1){
sca2 = powergl_mat4_ident();
sca2.c[0].x = 1.0f / obj->parent->transform.scale.x;
sca2.c[1].y = 1.0f / obj->parent->transform.scale.y;
sca2.c[2].z = 1.0f / obj->parent->transform.scale.z;
rot2 = powergl_mat4_ident();
rot2 = powergl_mat4_rot(rot2, obj->parent->transform.rotation_z.w, obj->parent->transform.rotation_z.xyz);
rot2 = powergl_mat4_rot(rot2, obj->parent->transform.rotation_y.w, obj->parent->transform.rotation_y.xyz);
rot2 = powergl_mat4_rot(rot2, obj->parent->transform.rotation_x.w, obj->parent->transform.rotation_x.xyz);
rot2 = powergl_mat4_transpose(rot2);
tra2 = powergl_mat4_ident();
tra2.c[3].x = -obj->parent->transform.location.x;
tra2.c[3].y = -obj->parent->transform.location.y;
tra2.c[3].z = -obj->parent->transform.location.z;
powergl_mat4 tmp = powergl_mat4_mul(rot2, tra2);
obj->camera.view = powergl_mat4_mul(obj->camera.view, tmp);
obj->camera.view = powergl_mat4_mul(sca2, obj->camera.view);
obj->camera.view_flag = 1;
}
if(obj->camera.view_flag == 1 || obj->camera.projection_flag == 1) {
obj->camera.vp = powergl_mat4_mul(obj->camera.projection, obj->camera.view);
obj->camera.view_flag = 0;
obj->camera.projection_flag = 0;
obj->camera.vp_flag = 1;
#if DEBUG_OUTPUT
powergl_mat4_print(obj->id, obj->camera.vp);
#endif
}
}
void powergl_object_update_transform(powergl_object *obj, float delta_time){
#if DEBUG_OUTPUT
printf("\n%s\n", __func__);
#endif
if(obj->animated_flag == 1){
powergl_animation_next(obj, delta_time);
}
if(obj->transform.matrix_flag == 1){
obj->transform.local = powergl_mat4_ident();
obj->transform.local.c[0].x = obj->transform.scale.x;
obj->transform.local.c[1].y = obj->transform.scale.y;
obj->transform.local.c[2].z = obj->transform.scale.z;
obj->transform.local = powergl_mat4_rot(obj->transform.local, obj->transform.rotation_z.w, obj->transform.rotation_z.xyz);
obj->transform.local = powergl_mat4_rot(obj->transform.local, obj->transform.rotation_y.w, obj->transform.rotation_y.xyz);
obj->transform.local = powergl_mat4_rot(obj->transform.local, obj->transform.rotation_x.w, obj->transform.rotation_x.xyz);
obj->transform.local.c[3].xyz = obj->transform.location;
#if DEBUG_OUTPUT
powergl_mat4_print(obj->id, obj->transform.local);
#endif
}
if(obj->parent !=NULL){
if(obj->parent->transform.matrix_flag == 1){
obj->transform.world = powergl_mat4_mul(obj->parent->transform.world, obj->transform.local);
obj->transform.matrix_flag = 1;
#if DEBUG_OUTPUT
powergl_mat4_print(obj->id, obj->transform.world);
#endif
} else {
obj->transform.world = obj->transform.local;
}
} else {
obj->transform.world = obj->transform.local;
}
for(size_t i=0; i < obj->n_object; i++){
powergl_object_update_transform(obj->objects[i], delta_time);
}
}
void powergl_object_update_mvp(powergl_object *obj, powergl_object *cam){
#if DEBUG_OUTPUT
printf("\n%s\n", __func__);
#endif
if(obj->transform.matrix_flag == 1 || cam->camera.vp_flag == 1){
obj->transform.mvp = powergl_mat4_mul(cam->camera.vp, obj->transform.world);
obj->transform.matrix_flag = 0;
obj->transform.mvp_flag = 1;
#if DEBUG_OUTPUT
powergl_mat4_print(obj->id, obj->transform.mvp);
#endif
}
for(size_t i=0; i < obj->n_object; i++){
powergl_object_update_mvp(obj->objects[i], cam);
}
}
void powergl_object_fps_controller(powergl_object *obj, float delta_time){
powergl_event e = obj->event;
powergl_vec3 mov;
mov.x = 0;
mov.y = 0;
mov.z = 0;
float displacement = 10.0f * delta_time;
if(e.key_a_pressed == 1){
mov.x += -displacement;
}
if(e.key_d_pressed == 1){
mov.x += displacement;
}
if(e.key_w_pressed == 1){
mov.y += displacement;
}
if(e.key_s_pressed == 1){
mov.y += -displacement;
}
if(e.key_space_pressed == 1){
mov.z += displacement;
}
if(e.key_lctrl_pressed == 1){
mov.z += -displacement;
}
if(mov.x !=0.0f || mov.y !=0.0f || mov.z !=0.0f){
obj->transform.location = powergl_vec3_add(obj->transform.location, mov);
obj->transform.matrix_flag = 1;
}
}
void powergl_event_handle(powergl_object *obj, SDL_Event *event, float delta_time){
if(obj->event_flag == 1){
switch (event->type) {
case SDL_KEYDOWN:
switch (event->key.keysym.sym) {
case SDLK_a:
obj->event.key_a_pressed = 1;
break;
case SDLK_d:
obj->event.key_d_pressed = 1;
break;
case SDLK_w:
obj->event.key_w_pressed = 1;
break;
case SDLK_s:
obj->event.key_s_pressed = 1;
break;
case SDLK_SPACE:
obj->event.key_space_pressed = 1;
break;
case SDLK_LCTRL:
obj->event.key_lctrl_pressed = 1;
break;
}
break;
case SDL_KEYUP:
switch (event->key.keysym.sym) {
case SDLK_a:
obj->event.key_a_pressed = 0;
break;
case SDLK_d:
obj->event.key_d_pressed = 0;
break;
case SDLK_w:
obj->event.key_w_pressed = 0;
break;
case SDLK_s:
obj->event.key_s_pressed = 0;
break;
case SDLK_SPACE:
obj->event.key_space_pressed = 0;
break;
case SDLK_LCTRL:
obj->event.key_lctrl_pressed = 0;
break;
}
break;
}
}
}
void powergl_transform_reset(powergl_transform *trans){
#if DEBUG_OUTPUT
printf("\N%s\n", __func__);
#endif
trans->local = powergl_mat4_ident();
trans->world = powergl_mat4_ident();
trans->mvp = powergl_mat4_ident();
trans->location = powergl_vec3_zeros();
trans->rotation_x = powergl_vec4_zeros();
trans->rotation_y = powergl_vec4_zeros();
trans->rotation_z = powergl_vec4_zeros();
trans->scale = powergl_vec3_ones();
trans->mvp_flag = 1;
trans->matrix_flag = 1;
#if DEBUG_OUTPUT
powergl_mat4_print("trasform has reset", trans->local);
#endif
}
void powergl_object_geometry_append(powergl_geometry *dest, powergl_geometry *src, powergl_vec3 offset, powergl_vec3 color){
// merge vertices
if(src->n_vertex > 0){
assert(src->vertex);
if(dest->n_vertex > 0 ){
assert(dest->vertex);
dest->vertex = powergl_resize(dest->vertex, src->n_vertex + dest->n_vertex , sizeof(powergl_vec3));
for(size_t i = dest->n_vertex, j = 0; j < src->n_vertex; i++, j++){
dest->vertex[i] = powergl_vec3_add(src->vertex[j], offset);
}
dest->n_vertex += src->n_vertex;
dest->vertex_flag = 1;
dest->triangles.count += src->triangles.count;
} else {
// TODO : eger daha once allocate edilmis boyut yeterliyse yeniden allocation yapilmayacak
dest->vertex = powergl_resize(NULL, src->n_vertex, sizeof(powergl_vec3));
#ifdef __STDC_LIB_EXT1__
set_constraint_handler_s(ignore_handler_s);
errno_t res = memcpy_s(dest->vertex, sizeof(powergl_vec3) * src->n_vertex, src->vertex, sizeof(powergl_vec3) * src->n_vertex);
if(res!=0){
#if DEBUG_OUTPUT
fprintf(stderr, "geometry vertex append failed while memcpy ing");
assert(0);
#endif
}
#else
memcpy(dest->vertex, src->vertex, sizeof(powergl_vec3) * src->n_vertex);
#endif
dest->n_vertex = src->n_vertex;
dest->vertex_flag = 1;
dest->visible_flag = 1;
dest->triangles.count = src->triangles.count;
}
// merge colors
if(src->triangles.n_color > 0){
assert(src->triangles.color);
if(dest->triangles.n_color > 0 ){
assert(dest->triangles.color);
dest->triangles.color = powergl_resize(dest->triangles.color, src->triangles.n_color + dest->triangles.n_color , sizeof(powergl_vec3));
for(size_t i = dest->triangles.n_color, j = 0; j < src->triangles.n_color; i++, j++){
dest->triangles.color[i] = color;
}
dest->triangles.n_color += src->triangles.n_color;
dest->triangles.color_flag = 1;
} else {
// TODO : eger daha once allocate edilmis boyut yeterliyse yeniden allocation yapilmayacak
dest->triangles.color = powergl_resize(NULL, src->triangles.n_color, sizeof(powergl_vec3));
for(size_t i=0; i < src->triangles.n_color; i++){
dest->triangles.color[i] = color;
}
dest->triangles.n_color = src->triangles.n_color;
dest->triangles.color_flag = 1;
}
} else {
#if DEBUG_OUTPUT
fprintf(stderr, "geometry append failed, because color source is empty");
assert(0);
#endif
}
} else {
#if DEBUG_OUTPUT
fprintf(stderr, "geometry append failed, because vertex source is empty");
assert(0);
#endif
}
}
void powergl_object_geometry_reset(powergl_geometry *geo){
if(geo->vertex != NULL){
free(geo->vertex);
geo->vertex = NULL;
// TODO : vertex_flag = 0 islemi problemli olabilir
geo->vertex_flag = 0;
geo->n_vertex = 0;
geo->triangles.count = 0;
// TODO : eger diger attributeler de kullanilmislarsa onlar da silinecek
// TODO : bound dizisi ve diger min_? max_? degeleri de resetlenebilir
}
if(geo->triangles.color != NULL){
free(geo->triangles.color);
geo->triangles.color = NULL;
geo->triangles.color_flag = 0;
geo->triangles.n_color = 0;
}
}