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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
if(!dojo._hasResource["dojox.gfx3d.object"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.gfx3d.object"] = true;
dojo.provide("dojox.gfx3d.object");
dojo.require("dojox.gfx");
dojo.require("dojox.gfx3d.lighting");
dojo.require("dojox.gfx3d.scheduler");
dojo.require("dojox.gfx3d.vector");
dojo.require("dojox.gfx3d.gradient");
// FIXME: why the global "out" var here?
var out = function(o, x){
if(arguments.length > 1){
// console.debug("debug:", o);
o = x;
}
var e = {};
for(var i in o){
if(i in e){ continue; }
// console.debug("debug:", i, typeof o[i], o[i]);
}
};
dojo.declare("dojox.gfx3d.Object", null, {
constructor: function(){
// summary: a Object object, which knows how to map
// 3D objects to 2D shapes.
// object: Object: an abstract Object object
// (see dojox.gfx3d.defaultEdges,
// dojox.gfx3d.defaultTriangles,
// dojox.gfx3d.defaultQuads
// dojox.gfx3d.defaultOrbit
// dojox.gfx3d.defaultCube
// or dojox.gfx3d.defaultCylinder)
this.object = null;
// matrix: dojox.gfx3d.matrix: world transform
this.matrix = null;
// cache: buffer for intermediate result, used late for draw()
this.cache = null;
// renderer: a reference for the Viewport
this.renderer = null;
// parent: a reference for parent, Scene or Viewport object
this.parent = null;
// strokeStyle: Object: a stroke object
this.strokeStyle = null;
// fillStyle: Object: a fill object or texture object
this.fillStyle = null;
// shape: dojox.gfx.Shape: an underlying 2D shape
this.shape = null;
},
setObject: function(newObject){
// summary: sets a Object object
// object: Object: an abstract Object object
// (see dojox.gfx3d.defaultEdges,
// dojox.gfx3d.defaultTriangles,
// dojox.gfx3d.defaultQuads
// dojox.gfx3d.defaultOrbit
// dojox.gfx3d.defaultCube
// or dojox.gfx3d.defaultCylinder)
this.object = dojox.gfx.makeParameters(this.object, newObject);
return this;
},
setTransform: function(matrix){
// summary: sets a transformation matrix
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx3d.matrix.Matrix
// constructor for a list of acceptable arguments)
this.matrix = dojox.gfx3d.matrix.clone(matrix ? dojox.gfx3d.matrix.normalize(matrix) : dojox.gfx3d.identity, true);
return this; // self
},
// apply left & right transformation
applyRightTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on right side
// (this.matrix * matrix)
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx.matrix.Matrix
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([this.matrix, matrix]) : this; // self
},
applyLeftTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on left side
// (matrix * this.matrix)
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx.matrix.Matrix
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([matrix, this.matrix]) : this; // self
},
applyTransform: function(matrix){
// summary: a shortcut for dojox.gfx.Shape.applyRightTransform
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx.matrix.Matrix
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([this.matrix, matrix]) : this; // self
},
setFill: function(fill){
// summary: sets a fill object
// (the default implementation is to delegate to
// the underlying 2D shape).
// fill: Object: a fill object
// (see dojox.gfx.defaultLinearGradient,
// dojox.gfx.defaultRadialGradient,
// dojox.gfx.defaultPattern,
// dojo.Color
// or dojox.gfx.MODEL)
this.fillStyle = fill;
return this;
},
setStroke: function(stroke){
// summary: sets a stroke object
// (the default implementation simply ignores it)
// stroke: Object: a stroke object
// (see dojox.gfx.defaultStroke)
this.strokeStyle = stroke;
return this;
},
toStdFill: function(lighting, normal){
return (this.fillStyle && typeof this.fillStyle['type'] != "undefined") ? lighting[this.fillStyle.type](normal, this.fillStyle.finish, this.fillStyle.color) : this.fillStyle;
},
invalidate: function(){
this.renderer.addTodo(this);
},
destroy: function(){
if(this.shape){
var p = this.shape.getParent();
if(p){
p.remove(this.shape);
}
this.shape = null;
}
},
// All the 3D objects need to override the following virtual functions:
// render, getZOrder, getOutline, draw, redraw if necessary.
render: function(camera){
throw "Pure virtual function, not implemented";
},
draw: function(lighting){
throw "Pure virtual function, not implemented";
},
getZOrder: function(){
return 0;
},
getOutline: function(){
return null;
}
});
dojo.declare("dojox.gfx3d.Scene", dojox.gfx3d.Object, {
// summary: the Scene is just a containter.
// note: we have the following assumption:
// all objects in the Scene are not overlapped with other objects
// outside of the scene.
constructor: function(){
// summary: a containter of other 3D objects
this.objects= [];
this.todos = [];
this.schedule = dojox.gfx3d.scheduler.zOrder;
this._draw = dojox.gfx3d.drawer.conservative;
},
setFill: function(fill){
this.fillStyle = fill;
dojo.forEach(this.objects, function(item){
item.setFill(fill);
});
return this;
},
setStroke: function(stroke){
this.strokeStyle = stroke;
dojo.forEach(this.objects, function(item){
item.setStroke(stroke);
});
return this;
},
render: function(camera, deep){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
if(deep){
this.todos = this.objects;
}
dojo.forEach(this.todos, function(item){ item.render(m, deep); });
},
draw: function(lighting){
this.objects = this.schedule(this.objects);
this._draw(this.todos, this.objects, this.renderer);
},
addTodo: function(newObject){
// FIXME: use indexOf?
if(dojo.every(this.todos, function(item){ return item != newObject; })){
this.todos.push(newObject);
this.invalidate();
}
},
invalidate: function(){
this.parent.addTodo(this);
},
getZOrder: function(){
var zOrder = 0;
dojo.forEach(this.objects, function(item){ zOrder += item.getZOrder(); });
return (this.objects.length > 1) ? zOrder / this.objects.length : 0;
}
});
dojo.declare("dojox.gfx3d.Edges", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic edge in 3D viewport
this.object = dojo.clone(dojox.gfx3d.defaultEdges);
},
setObject: function(newObject, /* String, optional */ style){
// summary: setup the object
// newObject: Array of points || Object
// style: String, optional
this.object = dojox.gfx.makeParameters(this.object, (newObject instanceof Array) ? { points: newObject, style: style } : newObject);
return this;
},
getZOrder: function(){
var zOrder = 0;
dojo.forEach(this.cache, function(item){ zOrder += item.z;} );
return (this.cache.length > 1) ? zOrder / this.cache.length : 0;
},
render: function(camera){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
this.cache = dojo.map(this.object.points, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
},
draw: function(){
var c = this.cache;
if(this.shape){
this.shape.setShape("")
}else{
this.shape = this.renderer.createPath();
}
var p = this.shape.setAbsoluteMode("absolute");
if(this.object.style == "strip" || this.object.style == "loop"){
p.moveTo(c[0].x, c[0].y);
dojo.forEach(c.slice(1), function(item){
p.lineTo(item.x, item.y);
});
if(this.object.style == "loop"){
p.closePath();
}
}else{
for(var i = 0; i < this.cache.length; ){
p.moveTo(c[i].x, c[i].y);
i ++;
p.lineTo(c[i].x, c[i].y);
i ++;
}
}
// FIXME: doe setFill make sense here?
p.setStroke(this.strokeStyle);
}
});
dojo.declare("dojox.gfx3d.Orbit", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic edge in 3D viewport
this.object = dojo.clone(dojox.gfx3d.defaultOrbit);
},
render: function(camera){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
var angles = [0, Math.PI/4, Math.PI/3];
var center = dojox.gfx3d.matrix.multiplyPoint(m, this.object.center);
var marks = dojo.map(angles, function(item){
return {x: this.center.x + this.radius * Math.cos(item),
y: this.center.y + this.radius * Math.sin(item), z: this.center.z};
}, this.object);
marks = dojo.map(marks, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
var normal = dojox.gfx3d.vector.normalize(marks);
marks = dojo.map(marks, function(item){
return dojox.gfx3d.vector.substract(item, center);
});
// Use the algorithm here:
// http://www.3dsoftware.com/Math/PlaneCurves/EllipseAlgebra/
// After we normalize the marks, the equation is:
// a x^2 + 2b xy + cy^2 + f = 0: let a = 1
// so the final equation is:
// [ xy, y^2, 1] * [2b, c, f]' = [ -x^2 ]'
var A = {
xx: marks[0].x * marks[0].y, xy: marks[0].y * marks[0].y, xz: 1,
yx: marks[1].x * marks[1].y, yy: marks[1].y * marks[1].y, yz: 1,
zx: marks[2].x * marks[2].y, zy: marks[2].y * marks[2].y, zz: 1,
dx: 0, dy: 0, dz: 0
};
var b = dojo.map(marks, function(item){
return -Math.pow(item.x, 2);
});
// X is 2b, c, f
var X = dojox.gfx3d.matrix.multiplyPoint(dojox.gfx3d.matrix.invert(A),b[0], b[1], b[2]);
var theta = Math.atan2(X.x, 1 - X.y) / 2;
// rotate the marks back to the canonical form
var probes = dojo.map(marks, function(item){
return dojox.gfx.matrix.multiplyPoint(dojox.gfx.matrix.rotate(-theta), item.x, item.y);
});
// we are solving the equation: Ax = b
// A = [x^2, y^2] X = [1/a^2, 1/b^2]', b = [1, 1]'
// so rx = Math.sqrt(1/ ( inv(A)[1:] * b ) );
// so ry = Math.sqrt(1/ ( inv(A)[2:] * b ) );
var a = Math.pow(probes[0].x, 2);
var b = Math.pow(probes[0].y, 2);
var c = Math.pow(probes[1].x, 2);
var d = Math.pow(probes[1].y, 2);
// the invert matrix is
// 1/(ad -bc) [ d, -b; -c, a];
var rx = Math.sqrt( (a*d - b*c)/ (d-b) );
var ry = Math.sqrt( (a*d - b*c)/ (a-c) );
this.cache = {cx: center.x, cy: center.y, rx: rx, ry: ry, theta: theta, normal: normal};
},
draw: function(lighting){
if(this.shape){
this.shape.setShape(this.cache);
} else {
this.shape = this.renderer.createEllipse(this.cache);
}
this.shape.applyTransform(dojox.gfx.matrix.rotateAt(this.cache.theta, this.cache.cx, this.cache.cy))
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, this.cache.normal));
}
});
dojo.declare("dojox.gfx3d.Path3d", dojox.gfx3d.Object, {
// This object is still very immature !
constructor: function(){
// summary: a generic line
// (this is a helper object, which is defined for convenience)
this.object = dojo.clone(dojox.gfx3d.defaultPath3d);
this.segments = [];
this.absolute = true;
this.last = {};
this.path = "";
},
_collectArgs: function(array, args){
// summary: converts an array of arguments to plain numeric values
// array: Array: an output argument (array of numbers)
// args: Array: an input argument (can be values of Boolean, Number, dojox.gfx.Point, or an embedded array of them)
for(var i = 0; i < args.length; ++i){
var t = args[i];
if(typeof(t) == "boolean"){
array.push(t ? 1 : 0);
}else if(typeof(t) == "number"){
array.push(t);
}else if(t instanceof Array){
this._collectArgs(array, t);
}else if("x" in t && "y" in t){
array.push(t.x);
array.push(t.y);
}
}
},
// a dictionary, which maps segment type codes to a number of their argemnts
_validSegments: {m: 3, l: 3, z: 0},
_pushSegment: function(action, args){
// summary: adds a segment
// action: String: valid SVG code for a segment's type
// args: Array: a list of parameters for this segment
var group = this._validSegments[action.toLowerCase()];
if(typeof(group) == "number"){
if(group){
if(args.length >= group){
var segment = {action: action, args: args.slice(0, args.length - args.length % group)};
this.segments.push(segment);
}
}else{
var segment = {action: action, args: []};
this.segments.push(segment);
}
}
},
moveTo: function(){
// summary: formes a move segment
var args = [];
this._collectArgs(args, arguments);
this._pushSegment(this.absolute ? "M" : "m", args);
return this; // self
},
lineTo: function(){
// summary: formes a line segment
var args = [];
this._collectArgs(args, arguments);
this._pushSegment(this.absolute ? "L" : "l", args);
return this; // self
},
closePath: function(){
// summary: closes a path
this._pushSegment("Z", []);
return this; // self
},
render: function(camera){
// TODO: we need to get the ancestors' matrix
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
// iterate all the segments and convert them to 2D canvas
// TODO consider the relative mode
var path = ""
var _validSegments = this._validSegments;
dojo.forEach(this.segments, function(item){
path += item.action;
for(var i = 0; i < item.args.length; i+= _validSegments[item.action.toLowerCase()] ){
var pt = dojox.gfx3d.matrix.multiplyPoint(m, item.args[i], item.args[i+1], item.args[i+2])
path += " " + pt.x + " " + pt.y;
}
});
this.cache = path;
},
_draw: function(){
return this.parent.createPath(this.cache);
}
});
dojo.declare("dojox.gfx3d.Triangles", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic triangle
// (this is a helper object, which is defined for convenience)
this.object = dojo.clone(dojox.gfx3d.defaultTriangles);
},
setObject: function(newObject, /* String, optional */ style){
// summary: setup the object
// newObject: Array of points || Object
// style: String, optional
if(newObject instanceof Array){
this.object = dojox.gfx.makeParameters(this.object, { points: newObject, style: style } );
} else {
this.object = dojox.gfx.makeParameters(this.object, newObject);
}
return this;
},
render: function(camera){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
var c = dojo.map(this.object.points, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
this.cache = [];
var pool = c.slice(0, 2);
var center = c[0];
if(this.object.style == "strip"){
dojo.forEach(c.slice(2), function(item){
pool.push(item);
pool.push(pool[0]);
this.cache.push(pool);
pool = pool.slice(1, 3);
}, this);
} else if(this.object.style == "fan"){
dojo.forEach(c.slice(2), function(item){
pool.push(item);
pool.push(center);
this.cache.push(pool);
pool = [center, item];
}, this);
} else {
for(var i = 0; i < c.length; ){
this.cache.push( [ c[i], c[i+1], c[i+2], c[i] ]);
i += 3;
}
}
},
draw: function(lighting){
// use the BSP to schedule
this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; });
if(this.shape){
this.shape.clear();
} else {
this.shape = this.renderer.createGroup();
}
dojo.forEach(this.cache, function(item){
this.shape.createPolyline(item)
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item)));
}, this);
},
getZOrder: function(){
var zOrder = 0;
dojo.forEach(this.cache, function(item){
zOrder += (item[0].z + item[1].z + item[2].z) / 3; });
return (this.cache.length > 1) ? zOrder / this.cache.length : 0;
}
});
dojo.declare("dojox.gfx3d.Quads", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic triangle
// (this is a helper object, which is defined for convenience)
this.object = dojo.clone(dojox.gfx3d.defaultQuads);
},
setObject: function(newObject, /* String, optional */ style){
// summary: setup the object
// newObject: Array of points || Object
// style: String, optional
this.object = dojox.gfx.makeParameters(this.object, (newObject instanceof Array) ? { points: newObject, style: style } : newObject );
return this;
},
render: function(camera){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
var c = dojo.map(this.object.points, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
this.cache = [];
if(this.object.style == "strip"){
var pool = c.slice(0, 2);
for(var i = 2; i < c.length; ){
pool = pool.concat( [ c[i], c[i+1], pool[0] ] );
this.cache.push(pool);
pool = pool.slice(2,4);
i += 2;
}
}else{
for(var i = 0; i < c.length; ){
this.cache.push( [c[i], c[i+1], c[i+2], c[i+3], c[i] ] );
i += 4;
}
}
},
draw: function(lighting){
// use the BSP to schedule
this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; });
if(this.shape){
this.shape.clear();
}else{
this.shape = this.renderer.createGroup();
}
// using naive iteration to speed things up a bit by avoiding function call overhead
for(var x=0; x<this.cache.length; x++){
this.shape.createPolyline(this.cache[x])
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(this.cache[x])));
}
/*
dojo.forEach(this.cache, function(item){
this.shape.createPolyline(item)
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item)));
}, this);
*/
},
getZOrder: function(){
var zOrder = 0;
// using naive iteration to speed things up a bit by avoiding function call overhead
for(var x=0; x<this.cache.length; x++){
var i = this.cache[x];
zOrder += (i[0].z + i[1].z + i[2].z + i[3].z) / 4;
}
/*
dojo.forEach(this.cache, function(item){
zOrder += (item[0].z + item[1].z + item[2].z + item[3].z) / 4; });
*/
return (this.cache.length > 1) ? zOrder / this.cache.length : 0;
}
});
dojo.declare("dojox.gfx3d.Polygon", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic triangle
// (this is a helper object, which is defined for convenience)
this.object = dojo.clone(dojox.gfx3d.defaultPolygon);
},
setObject: function(newObject){
// summary: setup the object
// newObject: Array of points || Object
this.object = dojox.gfx.makeParameters(this.object, (newObject instanceof Array) ? {path: newObject} : newObject)
return this;
},
render: function(camera){
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
this.cache = dojo.map(this.object.path, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
// add the first point to close the polyline
this.cache.push(this.cache[0]);
},
draw: function(lighting){
if(this.shape){
this.shape.setShape({points: this.cache});
}else{
this.shape = this.renderer.createPolyline({points: this.cache});
}
this.shape.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.matrix.normalize(this.cache)));
},
getZOrder: function(){
var zOrder = 0;
// using naive iteration to speed things up a bit by avoiding function call overhead
for(var x=0; x<this.cache.length; x++){
zOrder += this.cache[x].z;
}
return (this.cache.length > 1) ? zOrder / this.cache.length : 0;
},
getOutline: function(){
return this.cache.slice(0, 3);
}
});
dojo.declare("dojox.gfx3d.Cube", dojox.gfx3d.Object, {
constructor: function(){
// summary: a generic triangle
// (this is a helper object, which is defined for convenience)
this.object = dojo.clone(dojox.gfx3d.defaultCube);
this.polygons = [];
},
setObject: function(newObject){
// summary: setup the object
// newObject: Array of points || Object
this.object = dojox.gfx.makeParameters(this.object, newObject);
},
render: function(camera){
// parse the top, bottom to get 6 polygons:
var a = this.object.top;
var g = this.object.bottom;
var b = {x: g.x, y: a.y, z: a.z};
var c = {x: g.x, y: g.y, z: a.z};
var d = {x: a.x, y: g.y, z: a.z};
var e = {x: a.x, y: a.y, z: g.z};
var f = {x: g.x, y: a.y, z: g.z};
var h = {x: a.x, y: g.y, z: g.z};
var polygons = [a, b, c, d, e, f, g, h];
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
var p = dojo.map(polygons, function(item){
return dojox.gfx3d.matrix.multiplyPoint(m, item);
});
a = p[0]; b = p[1]; c = p[2]; d = p[3]; e = p[4]; f = p[5]; g = p[6]; h = p[7];
this.cache = [[a, b, c, d, a], [e, f, g, h, e], [a, d, h, e, a], [d, c, g, h, d], [c, b, f, g, c], [b, a, e, f, b]];
},
draw: function(lighting){
// use bsp to sort.
this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; });
// only the last 3 polys are visible.
var cache = this.cache.slice(3);
if(this.shape){
this.shape.clear();
}else{
this.shape = this.renderer.createGroup();
}
for(var x=0; x<cache.length; x++){
this.shape.createPolyline(cache[x])
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(cache[x])));
}
/*
dojo.forEach(cache, function(item){
this.shape.createPolyline(item)
.setStroke(this.strokeStyle)
.setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item)));
}, this);
*/
},
getZOrder: function(){
var top = this.cache[0][0];
var bottom = this.cache[1][2];
return (top.z + bottom.z) / 2;
}
});
dojo.declare("dojox.gfx3d.Cylinder", dojox.gfx3d.Object, {
constructor: function(){
this.object = dojo.clone(dojox.gfx3d.defaultCylinder);
},
render: function(camera){
// get the bottom surface first
var m = dojox.gfx3d.matrix.multiply(camera, this.matrix);
var angles = [0, Math.PI/4, Math.PI/3];
var center = dojox.gfx3d.matrix.multiplyPoint(m, this.object.center);
var marks = dojo.map(angles, function(item){
return {x: this.center.x + this.radius * Math.cos(item),
y: this.center.y + this.radius * Math.sin(item), z: this.center.z};
}, this.object);
marks = dojo.map(marks, function(item){
return dojox.gfx3d.vector.substract(dojox.gfx3d.matrix.multiplyPoint(m, item), center);
});
// Use the algorithm here:
// http://www.3dsoftware.com/Math/PlaneCurves/EllipseAlgebra/
// After we normalize the marks, the equation is:
// a x^2 + 2b xy + cy^2 + f = 0: let a = 1
// so the final equation is:
// [ xy, y^2, 1] * [2b, c, f]' = [ -x^2 ]'
var A = {
xx: marks[0].x * marks[0].y, xy: marks[0].y * marks[0].y, xz: 1,
yx: marks[1].x * marks[1].y, yy: marks[1].y * marks[1].y, yz: 1,
zx: marks[2].x * marks[2].y, zy: marks[2].y * marks[2].y, zz: 1,
dx: 0, dy: 0, dz: 0
};
var b = dojo.map(marks, function(item){
return -Math.pow(item.x, 2);
});
// X is 2b, c, f
var X = dojox.gfx3d.matrix.multiplyPoint(dojox.gfx3d.matrix.invert(A), b[0], b[1], b[2]);
var theta = Math.atan2(X.x, 1 - X.y) / 2;
// rotate the marks back to the canonical form
var probes = dojo.map(marks, function(item){
return dojox.gfx.matrix.multiplyPoint(dojox.gfx.matrix.rotate(-theta), item.x, item.y);
});
// we are solving the equation: Ax = b
// A = [x^2, y^2] X = [1/a^2, 1/b^2]', b = [1, 1]'
// so rx = Math.sqrt(1/ ( inv(A)[1:] * b ) );
// so ry = Math.sqrt(1/ ( inv(A)[2:] * b ) );
var a = Math.pow(probes[0].x, 2);
var b = Math.pow(probes[0].y, 2);
var c = Math.pow(probes[1].x, 2);
var d = Math.pow(probes[1].y, 2);
// the invert matrix is
// 1/(ad - bc) [ d, -b; -c, a];
var rx = Math.sqrt((a * d - b * c) / (d - b));
var ry = Math.sqrt((a * d - b * c) / (a - c));
if(rx < ry){
var t = rx;
rx = ry;
ry = t;
theta -= Math.PI/2;
}
var top = dojox.gfx3d.matrix.multiplyPoint(m,
dojox.gfx3d.vector.sum(this.object.center, {x: 0, y:0, z: this.object.height}));
var gradient = this.fillStyle.type == "constant" ? this.fillStyle.color
: dojox.gfx3d.gradient(this.renderer.lighting, this.fillStyle, this.object.center, this.object.radius, Math.PI, 2 * Math.PI, m);
if(isNaN(rx) || isNaN(ry) || isNaN(theta)){
// in case the cap is invisible (parallel to the incident vector)
rx = this.object.radius, ry = 0, theta = 0;
}
this.cache = {center: center, top: top, rx: rx, ry: ry, theta: theta, gradient: gradient};
},
draw: function(){
var c = this.cache, v = dojox.gfx3d.vector, m = dojox.gfx.matrix,
centers = [c.center, c.top], normal = v.substract(c.top, c.center);
if(v.dotProduct(normal, this.renderer.lighting.incident) > 0){
centers = [c.top, c.center];
normal = v.substract(c.center, c.top);
}
var color = this.renderer.lighting[this.fillStyle.type](normal, this.fillStyle.finish, this.fillStyle.color),
d = Math.sqrt( Math.pow(c.center.x - c.top.x, 2) + Math.pow(c.center.y - c.top.y, 2) );
if(this.shape){
this.shape.clear();
}else{
this.shape = this.renderer.createGroup();
}
this.shape.createPath("")
.moveTo(0, -c.rx)
.lineTo(d, -c.rx)
.lineTo(d, c.rx)
.lineTo(0, c.rx)
.arcTo(c.ry, c.rx, 0, true, true, 0, -c.rx)
.setFill(c.gradient).setStroke(this.strokeStyle)
.setTransform([m.translate(centers[0]),
m.rotate(Math.atan2(centers[1].y - centers[0].y, centers[1].x - centers[0].x))]);
if(c.rx > 0 && c.ry > 0){
this.shape.createEllipse({cx: centers[1].x, cy: centers[1].y, rx: c.rx, ry: c.ry})
.setFill(color).setStroke(this.strokeStyle)
.applyTransform(m.rotateAt(c.theta, centers[1]));
}
}
});
// the ultimate container of 3D world
dojo.declare("dojox.gfx3d.Viewport", dojox.gfx.Group, {
constructor: function(){
// summary: a viewport/container for 3D objects, which knows
// the camera and lightings
// matrix: dojox.gfx3d.matrix: world transform
// dimension: Object: the dimension of the canvas
this.dimension = null;
// objects: Array: all 3d Objects
this.objects = [];
// todos: Array: all 3d Objects that needs to redraw
this.todos = [];
// FIXME: memory leak?
this.renderer = this;
// Using zOrder as the default scheduler
this.schedule = dojox.gfx3d.scheduler.zOrder;
this.draw = dojox.gfx3d.drawer.conservative;
// deep: boolean, true means the whole viewport needs to re-render, redraw
this.deep = false;
// lights: Array: an array of light objects
this.lights = [];
this.lighting = null;
},
setCameraTransform: function(matrix){
// summary: sets a transformation matrix
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx.matrix.Matrix
// constructor for a list of acceptable arguments)
this.camera = dojox.gfx3d.matrix.clone(matrix ? dojox.gfx3d.matrix.normalize(matrix) : dojox.gfx3d.identity, true);
this.invalidate();
return this; // self
},
applyCameraRightTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on right side
// (this.matrix * matrix)
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx3d.matrix.Matrix
// constructor for a list of acceptable arguments)
return matrix ? this.setCameraTransform([this.camera, matrix]) : this; // self
},
applyCameraLeftTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on left side
// (matrix * this.matrix)
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx3d.matrix.Matrix
// constructor for a list of acceptable arguments)
return matrix ? this.setCameraTransform([matrix, this.camera]) : this; // self
},
applyCameraTransform: function(matrix){
// summary: a shortcut for dojox.gfx3d.Object.applyRightTransform
// matrix: dojox.gfx3d.matrix.Matrix: a matrix or a matrix-like object
// (see an argument of dojox.gfx3d.matrix.Matrix
// constructor for a list of acceptable arguments)
return this.applyCameraRightTransform(matrix); // self
},
setLights: function(/* Array || Object */lights, /* Color, optional */ ambient,
/* Color, optional */ specular){
// summary: set the lights
// lights: Array: an array of light object
// or lights object
// ambient: Color: an ambient object
// specular: Color: an specular object
this.lights = (lights instanceof Array) ? {sources: lights, ambient: ambient, specular: specular} : lights;
var view = {x: 0, y: 0, z: 1};
this.lighting = new dojox.gfx3d.lighting.Model(view, this.lights.sources,
this.lights.ambient, this.lights.specular);
this.invalidate();
return this;
},
addLights: function(lights){
// summary: add new light/lights to the viewport.
// lights: Array || light object: light object(s)
return this.setLights(this.lights.sources.concat(lights));
},
addTodo: function(newObject){
// NOTE: Viewport implements almost the same addTodo,
// except calling invalidate, since invalidate is used as
// any modification needs to redraw the object itself, call invalidate.
// then call render.
if(dojo.every(this.todos,
function(item){
return item != newObject;
}
)){
this.todos.push(newObject);
}
},
invalidate: function(){
this.deep = true;
this.todos = this.objects;
},
setDimensions: function(dim){
if(dim){
this.dimension = {
width: dojo.isString(dim.width) ? parseInt(dim.width) : dim.width,
height: dojo.isString(dim.height) ? parseInt(dim.height) : dim.height
};
}else{
this.dimension = null;
}
},
render: function(){
// summary: iterate all children and call their render callback function.
if(!this.todos.length){ return; }
// console.debug("Viewport::render");
var m = dojox.gfx3d.matrix;
// Iterate the todos and call render to prepare the rendering:
for(var x=0; x<this.todos.length; x++){
this.todos[x].render(dojox.gfx3d.matrix.normalize([
m.cameraRotateXg(180),
m.cameraTranslate(0, this.dimension.height, 0),
this.camera,
]), this.deep);
}
this.objects = this.schedule(this.objects);
this.draw(this.todos, this.objects, this);
this.todos = [];
this.deep = false;
}
});
//FIXME: Viewport cannot masquerade as a Group
dojox.gfx3d.Viewport.nodeType = dojox.gfx.Group.nodeType;
dojox.gfx3d._creators = {
// summary: object creators
createEdges: function(edges, style){
// summary: creates an edge object
// line: Object: a edge object (see dojox.gfx3d.defaultPath)
return this.create3DObject(dojox.gfx3d.Edges, edges, style); // dojox.gfx3d.Edge
},
createTriangles: function(tris, style){
// summary: creates an edge object
// line: Object: a edge object (see dojox.gfx3d.defaultPath)
return this.create3DObject(dojox.gfx3d.Triangles, tris, style); // dojox.gfx3d.Edge
},
createQuads: function(quads, style){
// summary: creates an edge object
// line: Object: a edge object (see dojox.gfx3d.defaultPath)
return this.create3DObject(dojox.gfx3d.Quads, quads, style); // dojox.gfx3d.Edge
},
createPolygon: function(points){
// summary: creates an triangle object
// points: Array of points || Object
return this.create3DObject(dojox.gfx3d.Polygon, points); // dojox.gfx3d.Polygon
},
createOrbit: function(orbit){
// summary: creates an triangle object
// points: Array of points || Object
return this.create3DObject(dojox.gfx3d.Orbit, orbit); // dojox.gfx3d.Cube
},
createCube: function(cube){
// summary: creates an triangle object
// points: Array of points || Object
return this.create3DObject(dojox.gfx3d.Cube, cube); // dojox.gfx3d.Cube
},
createCylinder: function(cylinder){
// summary: creates an triangle object
// points: Array of points || Object
return this.create3DObject(dojox.gfx3d.Cylinder, cylinder); // dojox.gfx3d.Cube
},
createPath3d: function(path){
// summary: creates an edge object
// line: Object: a edge object (see dojox.gfx3d.defaultPath)
return this.create3DObject(dojox.gfx3d.Path3d, path); // dojox.gfx3d.Edge
},
createScene: function(){
// summary: creates an triangle object
// line: Object: a triangle object (see dojox.gfx3d.defaultPath)
return this.create3DObject(dojox.gfx3d.Scene); // dojox.gfx3d.Scene
},
create3DObject: function(objectType, rawObject, style){
// summary: creates an instance of the passed shapeType class
// shapeType: Function: a class constructor to create an instance of
// rawShape: Object: properties to be passed in to the classes "setShape" method
var obj = new objectType();
this.adopt(obj);
if(rawObject){ obj.setObject(rawObject, style); }
return obj; // dojox.gfx3d.Object
},
// todo : override the add/remove if necessary
adopt: function(obj){
// summary: adds a shape to the list
// shape: dojox.gfx.Shape: a shape
obj.renderer = this.renderer; // obj._setParent(this, null); more TODOs HERER?
obj.parent = this;
this.objects.push(obj);
this.addTodo(obj);
return this;
},
abandon: function(obj, silently){
// summary: removes a shape from the list
// silently: Boolean?: if true, do not redraw a picture yet
for(var i = 0; i < this.objects.length; ++i){
if(this.objects[i] == obj){
this.objects.splice(i, 1);
}
}
// if(this.rawNode == shape.rawNode.parentNode){
// this.rawNode.removeChild(shape.rawNode);
// }
// obj._setParent(null, null);
obj.parent = null;
return this; // self
},
setScheduler: function(scheduler){
this.schedule = scheduler;
},
setDrawer: function(drawer){
this.draw = drawer;
}
};
dojo.extend(dojox.gfx3d.Viewport, dojox.gfx3d._creators);
dojo.extend(dojox.gfx3d.Scene, dojox.gfx3d._creators);
delete dojox.gfx3d._creators;
//FIXME: extending dojox.gfx.Surface and masquerading Viewport as Group is hacky!
// Add createViewport to dojox.gfx.Surface
dojo.extend(dojox.gfx.Surface, {
createViewport: function(){
//FIXME: createObject is non-public method!
var viewport = this.createObject(dojox.gfx3d.Viewport, null, true);
//FIXME: this may not work with dojox.gfx.Group !!
viewport.setDimensions(this.getDimensions());
return viewport;
}
});
}
|