lv2 tweaks, add set dft transition, zoom fix, fs test_filter fix
[goodguy/history.git] / cinelerra-5.1 / cinelerra / scenegraph.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21
22 #include "affine.h"
23 #include "clip.h"
24 #include <math.h>
25 #include "overlayframe.h"
26 #include "scenegraph.h"
27 #include <string.h>
28
29
30 #define PRINT_INDENT for(int i = 0; i < indent; i++) printf(" ");
31
32
33 SceneNode::SceneNode()
34 {
35         reset();
36 }
37
38 SceneNode::SceneNode(const char *title)
39 {
40         reset();
41         strcpy(this->title, title);
42 }
43
44 SceneNode::SceneNode(VFrame *image, int private_image, float x, float y)
45 {
46         reset();
47         this->image = image;
48         if(this->image) this->private_image = private_image;
49         this->x = x;
50         this->y = y;
51 }
52
53 SceneNode::~SceneNode()
54 {
55         nodes.remove_all_objects();
56         if(private_image) delete image;
57 }
58
59 void SceneNode::reset()
60 {
61         parent = 0;
62         scene = 0;
63         x = y = z = rx = ry = rz = 0;
64         sx = sy = sz = 1;
65         hidden = 0;
66         image = 0;
67         private_image = 0;
68         flip = 0;
69         title[0] = 0;
70 }
71
72 void SceneNode::append(SceneNode *node)
73 {
74         node->parent = this;
75         node->scene = this->scene;
76         nodes.append(node);
77 }
78
79 void SceneNode::copy_ref(SceneNode *node)
80 {
81         if(private_image) delete image;
82         private_image = 0;
83         x = node->x;
84         y = node->y;
85         z = node->z;
86         rx = node->rx;
87         ry = node->ry;
88         rz = node->rz;
89         sx = node->sx;
90         sy = node->sy;
91         sz = node->sz;
92         hidden = node->hidden;
93         image = node->image;
94         flip = node->flip;
95 }
96
97 SceneNode* SceneNode::get_node(int number)
98 {
99         return nodes.get(number);
100 }
101
102 int SceneNode::get_memory_usage()
103 {
104         if(image)
105                 return image->get_memory_usage();
106         else
107                 return 0;
108 }
109
110 void SceneNode::render(VFrame *frame, int do_camera)
111 {
112         const int debug = 0;
113         if(debug) printf("SceneNode::render %d this=%p title=%s image=%p x=%f y=%f frame=%p do_camera=%d\n",
114                 __LINE__,
115                 this,
116                 title,
117                 image,
118                 this->x,
119                 this->y,
120                 frame,
121                 do_camera);
122
123         VFrame *temp = 0;
124         VFrame *temp2 = 0;
125         if(image)
126         {
127                 float x = this->x;
128                 float y = this->y;
129                 float sx = this->sx;
130                 float sy = this->sy;
131                 float ry = this->ry;
132                 if(parent)
133                         parent->transform_coord(&x, &y, &sx, &sy, &ry);
134
135                 if(do_camera) scene->transform_camera(frame,
136                         &x,
137                         &y,
138                         &sx,
139                         &sy,
140                         get_flip());
141
142                 if(debug) printf("SceneNode::render %d at_y=%f\n",
143                         __LINE__,
144                         ((SceneCamera*)scene->cameras.get(0))->at_y);
145
146 // Render everything into a temporary, then overlay the temporary
147 //              if(!EQUIV(ry, 0) || get_flip())
148 //              {
149                 temp = new VFrame(image->get_w(), image->get_h(), image->get_color_model(), 0);
150 //              }
151                 if(debug) printf("SceneNode::render %d\n", __LINE__);
152
153
154 // 1st comes our image
155                 temp->copy_from(image);
156
157 // Then come the subnodes without camera transforms
158                 if(debug) printf("SceneNode::render %d this=%p nodes=%d\n", __LINE__, this, nodes.size());
159                 for(int i = 0; i < nodes.size(); i++)
160                         nodes.get(i)->render(temp, 0);
161
162
163
164                 if(debug) printf("SceneNode::render %d\n", __LINE__);
165
166 // Then comes rotation into temp2
167                 VFrame *src = temp;
168                 if(!EQUIV(ry, 0))
169                 {
170                         src = temp2 = new VFrame(image->get_w(), image->get_h(), image->get_color_model(), 0);
171                         if(!scene->affine) scene->affine = new AffineEngine(scene->cpus, scene->cpus);
172                         scene->affine->rotate(temp2, temp, ry);
173                         if(debug) printf("SceneNode::render %d ry=%f\n", __LINE__, ry);
174                 }
175
176 // Then comes flipping
177                 if(get_flip())
178                         src->flip_horiz();
179
180                 if(debug) printf("SceneNode::render %d src=%p x=%f y=%f sx=%f sy=%f\n",
181                         __LINE__, src, x, y, sx, sy); 
182 // Overlay on the output frame
183                 if(!scene->overlayer) scene->overlayer = new OverlayFrame(scene->cpus);
184
185                 if(get_flip())
186                 {
187                         scene->overlayer->overlay(frame,
188                                 src,
189                                 0,
190                                 0,
191                                 image->get_w(),
192                                 image->get_h(),
193                                 frame->get_w() - x - image->get_w() * sx,
194                                 y,
195                                 frame->get_w() - x,
196                                 y + image->get_h() * sy,
197                                 1,
198                                 TRANSFER_NORMAL,
199                                 NEAREST_NEIGHBOR);
200                 }
201                 else
202                 {
203                         if(debug) printf("SceneNode::render %d image=%p src=%p frame=%p\n",
204                                 __LINE__,
205                                 image,
206                                 src,
207                                 frame);
208                         scene->overlayer->overlay(frame,
209                                 src,
210                                 0,
211                                 0,
212                                 image->get_w(),
213                                 image->get_h(),
214                                 x,
215                                 y,
216                                 x + image->get_w() * sx,
217                                 y + image->get_h() * sy,
218                                 1,
219                                 TRANSFER_NORMAL,
220                                 NEAREST_NEIGHBOR);
221                 }
222
223                 if(debug) printf("SceneNode::render %d\n", __LINE__);
224
225         }
226         else
227         {
228                 for(int i = 0; i < nodes.size(); i++)
229                         nodes.get(i)->render(frame, 1);
230         }
231
232         if(debug) printf("SceneNode::render %d this=%p title=%s\n", __LINE__, this, title);
233
234         if(temp) delete temp;
235         if(temp2) delete temp2;
236 }
237
238 int SceneNode::get_flip()
239 {
240         if(flip) return 1;
241         if(parent && !parent->image) return parent->get_flip();
242         return 0;
243 }
244
245
246 void SceneNode::transform_coord(
247         float *x,
248         float *y,
249         float *sx,
250         float *sy,
251         float *ry)
252 {
253 // Rotate it
254 //      if(!EQUIV(this->ry, 0))
255 //      {
256 //              float pivot_x = 0;
257 //              float pivot_y = 0;
258 //              if(image)
259 //              {
260 //                      pivot_x = image->get_w() / 2;
261 //                      pivot_y = image->get_h() / 2;
262 //              }
263 //
264 //              float rel_x = *x - pivot_x;
265 //              float rel_y = *y - pivot_y;
266 //              float angle = atan2(rel_y, rel_x);
267 //              float mag = sqrt(rel_x * rel_x + rel_y * rel_y);
268 //              angle += this->ry * 2 * M_PI / 360;
269 //              *x = mag * cos(angle) + pivot_x;
270 //              *y = mag * sin(angle) + pivot_y;
271 //      }
272
273 // Nodes with images reset the transformation
274         if(!image)
275         {
276 // Scale it
277                 *x *= this->sx;
278                 *y *= this->sy;
279                 *sx *= this->sx;
280                 *sy *= this->sy;
281
282 // Translate it
283                 *x += this->x;
284                 *y += this->y;
285
286
287 // Accumulate rotation
288                 *ry += this->ry;
289
290                 if(parent)
291                 {
292                         parent->transform_coord(x, y, sx, sy, ry);
293                 }
294         }
295 }
296
297 void SceneNode::dump(int indent)
298 {
299         PRINT_INDENT
300         printf("SceneNode::dump %d this=%p title=%s\n", __LINE__, this, title);
301         PRINT_INDENT
302         printf("  image=%p private_image=%d hidden=%d flip=%d\n",
303                 image,
304                 private_image,
305                 hidden,
306                 flip);
307         PRINT_INDENT
308         printf("  x=%f y=%f z=%f sz=%f sy=%f sz=%f rx=%f ry=%f rz=%f\n",
309                 x, y, z, sz, sy, sz, rx, ry, rz);
310         PRINT_INDENT
311         printf("  nodes=%d\n", nodes.size());
312         for(int i = 0; i < nodes.size(); i++)
313         {
314                 nodes.get(i)->dump(indent + 4);
315         }
316 }
317
318
319
320
321
322 SceneGraph::SceneGraph() : VFrameScene()
323 {
324         current_camera = 0;
325         affine = 0;
326         overlayer = 0;
327 }
328
329 SceneGraph::~SceneGraph()
330 {
331         nodes.remove_all_objects();
332         cameras.remove_all_objects();
333         delete affine;
334         delete overlayer;
335 }
336
337
338 SceneNode* SceneGraph::get_node(int number)
339 {
340         return nodes.get(number);
341 }
342
343 void SceneGraph::append(SceneNode *node)
344 {
345         node->parent = 0;
346         node->scene = this;
347         nodes.append(node);
348 }
349
350 void SceneGraph::append_camera(SceneNode *node)
351 {
352         node->scene = this;
353         cameras.append(node);
354 }
355
356 void SceneGraph::render(VFrame *frame, int cpus)
357 {
358         const int debug = 0;
359         if(debug) printf("SceneGraph::render %d\n", __LINE__);
360         if(debug) dump();
361
362         this->cpus = cpus;
363
364         for(int i = 0; i < nodes.size(); i++)
365         {
366                 nodes.get(i)->render(frame, 1);
367         }
368 }
369
370 void SceneGraph::transform_camera(VFrame *frame,
371         float *x,
372         float *y,
373         float *sx,
374         float *sy,
375         int flip)
376 {
377         if(cameras.size())
378         {
379                 SceneCamera *camera = (SceneCamera*)cameras.get(current_camera);
380
381                 if(flip)
382                 {
383                         *x = frame->get_w() - *x;
384                 }
385
386                 *x -= camera->at_x;
387                 *y -= camera->at_y;
388                 *x *= camera->scale;
389                 *y *= camera->scale;
390                 *sx *= camera->scale;
391                 *sy *= camera->scale;
392
393                 if(flip)
394                 {
395                         *x = frame->get_w() - *x;
396                 }
397         }
398 }
399
400 void SceneGraph::dump()
401 {
402         int indent = 2;
403         printf("SceneGraph::dump %d cameras=%d\n", __LINE__, cameras.size());
404         for(int i = 0; i < cameras.size(); i++)
405                 cameras.get(i)->dump(indent);
406         printf("SceneGraph::dump %d nodes=%d\n", __LINE__, nodes.size());
407         for(int i = 0; i < nodes.size(); i++)
408                 nodes.get(i)->dump(indent);
409 }
410
411
412
413
414
415
416
417 SceneTransform::SceneTransform() : SceneNode()
418 {
419 }
420
421 SceneTransform::~SceneTransform()
422 {
423 }
424
425
426
427
428
429 SceneLight::SceneLight() : SceneNode()
430 {
431 }
432
433 SceneLight::~SceneLight()
434 {
435 }
436
437
438
439
440
441
442
443 SceneCamera::SceneCamera() : SceneNode()
444 {
445         at_x = at_y = at_z = 0;
446         scale = 1;
447 }
448
449 SceneCamera::~SceneCamera()
450 {
451 }
452
453 void SceneCamera::dump(int indent)
454 {
455         PRINT_INDENT
456         printf("SceneCamera::dump %d this=%p\n", __LINE__, this);
457         PRINT_INDENT
458         printf("  at_x=%f at_y=%f at_z=%f scale=%f\n",
459                 at_x,
460                 at_y,
461                 at_z,
462                 scale);
463 }
464
465
466
467
468 SceneMaterial::SceneMaterial() : SceneNode()
469 {
470         r = g = b = a = 0;
471         texture = 0;
472         s = t = 0;
473 }
474
475 SceneMaterial::~SceneMaterial()
476 {
477         delete [] texture;
478 }
479
480
481
482
483
484
485
486 SceneShape::SceneShape() : SceneNode()
487 {
488         pivot_x = pivot_y = pivot_z = 0;
489 }
490
491 SceneShape::~SceneShape()
492 {
493 }
494
495
496
497
498
499
500
501
502 SceneCylinder::SceneCylinder() : SceneShape()
503 {
504 }
505
506 SceneCylinder::~SceneCylinder()
507 {
508 }
509
510
511
512
513 SceneSphere::SceneSphere() : SceneShape()
514 {
515 }
516
517
518 SceneSphere::~SceneSphere()
519 {
520 }
521
522
523
524
525 SceneBox::SceneBox() : SceneShape()
526 {
527 }
528
529
530 SceneBox::~SceneBox()
531 {
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545