-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySceneGraph.js
1688 lines (1380 loc) · 58.8 KB
/
MySceneGraph.js
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
const DEGREE_TO_RAD = Math.PI / 180;
// Order of the groups in the XML document.
var INITIALS_INDEX = 0;
var VIEWS_INDEX = 1;
var ILLUMINATION_INDEX = 2;
var LIGHTS_INDEX = 3;
var TEXTURES_INDEX = 4;
var SPRITESHEETS_INDEX = 5;
var MATERIALS_INDEX = 6;
var ANIMATIONS_INDEX = 7;
var NODES_INDEX = 7;
var NODES_INDEX_ANIMATIONS = 8;
/**
* MySceneGraph class, representing the scene graph.
*/
class MySceneGraph {
/**
* Constructor for MySceneGraph class.
* Initializes necessary variables and starts the XML file reading process.
* @param {string} filename - File that defines the 3D scene
* @param {XMLScene} scene
*/
constructor(filename, scene) {
this.loadedOk = null;
// Establish bidirectional references between scene and graph.
this.scene = scene;
scene.graph = this;
this.nodes = [];
this.idRoot = null; // The id of the root element.
this.axisCoords = [];
this.axisCoords["x"] = [1, 0, 0];
this.axisCoords["y"] = [0, 1, 0];
this.axisCoords["z"] = [0, 0, 1];
this.defaultCamera;
// File reading
this.reader = new CGFXMLreader();
/*
* Read the contents of the xml file, and refer to this class for loading and error handlers.
* After the file is read, the reader calls onXMLReady on this object.
* If any error occurs, the reader calls onXMLError on this object, with an error message
*/
this.reader.open("scenes/" + filename, this);
}
/*
* Callback to be executed after successful reading
*/
onXMLReady() {
this.log("XML Loading finished.");
var rootElement = this.reader.xmlDoc.documentElement;
// Here should go the calls for different functions to parse the various blocks
var error = this.parseXMLFile(rootElement);
if (error != null) {
this.onXMLError(error);
return;
}
this.loadedOk = true;
// As the graph loaded ok, signal the scene so that any additional initialization depending on the graph can take place
this.scene.onGraphLoaded();
}
/*
* Callback to be executed on any read error, showing an error on the console.
* @param {string} message
*/
onXMLError(message) {
console.error("XML Loading Error: " + message);
this.loadedOk = false;
}
/**
* Callback to be executed on any minor error, showing a warning on the console.
* @param {string} message
*/
onXMLMinorError(message) {
console.warn("Warning: " + message);
}
/**
* Callback to be executed on any message.
* @param {string} message
*/
log(message) {
console.log(" " + message);
}
/**
* Parses the XML file, processing each block.
* @param {XML root element} rootElement
*/
parseXMLFile(rootElement) {
if (rootElement.nodeName != "lsf") return "root tag <lsf> missing";
var nodes = rootElement.children;
// Reads the names of the nodes to an auxiliary buffer.
var nodeNames = [];
for (var i = 0; i < nodes.length; i++) {
nodeNames.push(nodes[i].nodeName);
}
var error;
// Processes each node, verifying errors.
// <initials>
var index;
if ((index = nodeNames.indexOf("initials")) == -1)
return "tag <initials> missing";
else {
if (index != INITIALS_INDEX)
this.onXMLMinorError("tag <initials> out of order " + index);
//Parse initials block
if ((error = this.parseInitials(nodes[index])) != null) return error;
}
// <views>
if ((index = nodeNames.indexOf("views")) == -1)
return "tag <views> missing";
else {
if (index != VIEWS_INDEX)
this.onXMLMinorError("tag <views> out of order");
//Parse views block
if ((error = this.parseViews(nodes[index])) != null) return error;
}
// <illumination>
if ((index = nodeNames.indexOf("illumination")) == -1)
return "tag <illumination> missing";
else {
if (index != ILLUMINATION_INDEX)
this.onXMLMinorError("tag <illumination> out of order");
//Parse illumination block
if ((error = this.parseIllumination(nodes[index])) != null) return error;
}
// <lights>
if ((index = nodeNames.indexOf("lights")) == -1)
return "tag <lights> missing";
else {
if (index != LIGHTS_INDEX)
this.onXMLMinorError("tag <lights> out of order");
//Parse lights block
if ((error = this.parseLights(nodes[index])) != null) return error;
}
// <textures>
if ((index = nodeNames.indexOf("textures")) == -1)
return "tag <textures> missing";
else {
if (index != TEXTURES_INDEX)
this.onXMLMinorError("tag <textures> out of order");
//Parse textures block
if ((error = this.parseTextures(nodes[index])) != null) return error;
}
if ((index = nodeNames.indexOf("spritesheets")) == -1)
return "tag <spritesheets> missing";
else {
if (index != SPRITESHEETS_INDEX)
this.onXMLMinorError("tag <spritesheets> out of order");
//Parse textures block
if ((error = this.parseSpritesheets(nodes[index])) != null) return error;
}
// <materials>
if ((index = nodeNames.indexOf("materials")) == -1)
return "tag <materials> missing";
else {
if (index != MATERIALS_INDEX)
this.onXMLMinorError("tag <materials> out of order");
//Parse materials block
if ((error = this.parseMaterials(nodes[index])) != null) return error;
}
var animations_enabled=0;
//animations
if (nodeNames.indexOf("animations") != -1)
{
index=nodeNames.indexOf("animations");
animations_enabled=1;
if (index != ANIMATIONS_INDEX)
this.onXMLMinorError("tag <animations> out of order");
}
//Parse materials block
if ((error = this.parseAnimations(nodes[index])) != null) return error;
// <nodes>
if ((index = nodeNames.indexOf("nodes")) == -1)
return "tag <nodes> missing";
else {
if (index != NODES_INDEX_ANIMATIONS && animations_enabled)
this.onXMLMinorError("tag <nodes> out of order");
else if(index != NODES_INDEX && !animations_enabled)
this.onXMLMinorError("tag <nodes> out of order");
//Parse nodes block
if ((error = this.parseNodes(nodes[index])) != null) return error;
}
this.log("all parsed");
}
/**
* Parses the <initials> block.
* @param {initials block element} initialsNode
*/
parseInitials(initialsNode) {
var children = initialsNode.children;
var nodeNames = [];
for (var i = 0; i < children.length; i++)
nodeNames.push(children[i].nodeName);
var rootIndex = nodeNames.indexOf("root");
var referenceIndex = nodeNames.indexOf("reference");
// Get root of the scene.
if (rootIndex == -1) return "No root id defined for scene.";
var rootNode = children[rootIndex];
var id = this.reader.getString(rootNode, "id");
if (id == null) return "No root id defined for scene.";
this.idRoot = id;
// Get axis length
if (referenceIndex == -1)
this.onXMLMinorError(
"no axis_length defined for scene; assuming 'length = 1'"
);
var refNode = children[referenceIndex];
var axis_length = this.reader.getFloat(refNode, "length");
if (axis_length == null)
this.onXMLMinorError(
"no axis_length defined for scene; assuming 'length = 1'"
);
this.referenceLength = axis_length || 1;
this.log("Parsed initials");
return null;
}
/**
* Parses the <views> block.
* @param {view block element} viewsNode
*/
parseViews(viewsNode) {
this.cameras=[];
this.camerasName=[];
var camerasNo=0;
var def_set=false;
this.defaultCamera = viewsNode.getAttribute("default");
for (var i = 0; i < viewsNode.children.length; i++) {
if (viewsNode.children[i].nodeName == "perspective") {
var cameraID = viewsNode.children[i].getAttribute("id");
var near = parseFloat(viewsNode.children[i].getAttribute("near"));
var far = parseFloat(viewsNode.children[i].getAttribute("far"));
var angle = parseFloat(viewsNode.children[i].getAttribute("angle"));
if(viewsNode.children[i].children.length!=2)
this.onXMLError("Perspective declaration has a invalid number of children");
for (var j = 0; j < viewsNode.children[i].children.length; j++) {
if (viewsNode.children[i].children[j].nodeName == "from")
{
var from_x = parseFloat(viewsNode.children[i].children[j].getAttribute("x"));
var from_y = parseFloat(viewsNode.children[i].children[j].getAttribute("y"));
var from_z = parseFloat(viewsNode.children[i].children[j].getAttribute("z"));
}
else if (viewsNode.children[i].children[j].nodeName == "to")
{
var to_x = parseFloat(viewsNode.children[i].children[j].getAttribute("x"));
var to_y = parseFloat(viewsNode.children[i].children[j].getAttribute("y"));
var to_z = parseFloat(viewsNode.children[i].children[j].getAttribute("z"));
}
else this.onXMLMinorError("Unknown perspective child "+viewsNode.children[i].children[j].nodeName+".Skipping");
}
var camera = new CGFcamera(angle,near,far,[from_x,from_y,from_z],[to_x,to_y,to_z]);
if(cameraID==this.defaultCamera)
def_set=true;
this.cameras[cameraID] = camera;
this.camerasName[camerasNo]=cameraID;
camerasNo++;
}
if (viewsNode.children[i].nodeName == "ortho") {
var cameraID = viewsNode.children[i].getAttribute("id");
var near = parseFloat(viewsNode.children[i].getAttribute("near"));
var far = parseFloat(viewsNode.children[i].getAttribute("far"));
var left = parseFloat(viewsNode.children[i].getAttribute("left"));
var right = parseFloat(viewsNode.children[i].getAttribute("right"));
var top = parseFloat(viewsNode.children[i].getAttribute("top"));
var bottom = parseFloat(viewsNode.children[i].getAttribute("bottom"));
if(viewsNode.children[i].children.length!=3)
this.onXMLError("Perspective declaration has a invalid number of children");
for (var j = 0; j < viewsNode.children[i].children.length; j++) {
if (viewsNode.children[i].children[j].nodeName == "from")
{
var from_x = parseFloat(viewsNode.children[i].children[j].getAttribute("x"));
var from_y = parseFloat(viewsNode.children[i].children[j].getAttribute("y"));
var from_z = parseFloat(viewsNode.children[i].children[j].getAttribute("z"));
}
else if (viewsNode.children[i].children[j].nodeName == "to")
{
var to_x = parseFloat(viewsNode.children[i].children[j].getAttribute("x"));
var to_y = parseFloat(viewsNode.children[i].children[j].getAttribute("y"));
var to_z = parseFloat(viewsNode.children[i].children[j].getAttribute("z"));
}
else if (viewsNode.children[i].children[j].nodeName == "up")
{
var up_x = parseFloat(viewsNode.children[i].children[j].getAttribute("x"));
var up_y = parseFloat(viewsNode.children[i].children[j].getAttribute("y"));
var up_z = parseFloat(viewsNode.children[i].children[j].getAttribute("z"));
}
else this.onXMLMinorError("Unknown ortho child "+ viewsNode.children[i].children[j].nodeName+".Skipping");
}
var camera = new CGFcameraOrtho(left,right,bottom,top,near,far,[from_x,from_y,from_z],[to_x,to_y,to_z],[up_x,up_y,up_z]);
if(cameraID==this.defaultCamera)
def_set=true;
this.camerasName[camerasNo]=cameraID;
this.cameras[cameraID] = camera;
camerasNo++;
}
}
if(def_set==false)
{
this.onXMLMinorError("Default view not defined. Assuming first instance of camera");
this.defaultCamera=viewsNode.children[0].cameraID;
}
return null;
}
/**
* Parses the <illumination> node.
* @param {illumination block element} illuminationsNode
*/
parseIllumination(illuminationsNode) {
var children = illuminationsNode.children;
this.ambient = [];
this.background = [];
var nodeNames = [];
for (var i = 0; i < children.length; i++)
nodeNames.push(children[i].nodeName);
var ambientIndex = nodeNames.indexOf("ambient");
var backgroundIndex = nodeNames.indexOf("background");
var color = this.parseColor(children[ambientIndex], "ambient");
if (!Array.isArray(color)) return color;
else this.ambient = color;
color = this.parseColor(children[backgroundIndex], "background");
if (!Array.isArray(color)) return color;
else this.background = color;
this.log("Parsed Illumination.");
return null;
}
/**
* Parses the <light> node.
* @param {lights block element} lightsNode
*/
parseLights(lightsNode) {
var children = lightsNode.children;
this.lights = [];
var numLights = 0;
var grandChildren = [];
var nodeNames = [];
// Any number of lights.
for (var i = 0; i < children.length; i++) {
// Storing light information
var global = [];
var attributeNames = [];
var attributeTypes = [];
//Check type of light
if (children[i].nodeName != "light") {
this.onXMLMinorError("unknown tag <" + children[i].nodeName + ">");
continue;
} else {
attributeNames.push(
...["enable", "position", "ambient", "diffuse", "specular"]
);
attributeTypes.push(
...["boolean", "position", "color", "color", "color"]
);
}
// Get id of the current light.
var lightId = this.reader.getString(children[i], "id");
if (lightId == null) return "no ID defined for light";
// Checks for repeated IDs.
if (this.lights[lightId] != null)
return (
"ID must be unique for each light (conflict: ID = " + lightId + ")"
);
grandChildren = children[i].children;
// Specifications for the current light.
nodeNames = [];
for (var j = 0; j < grandChildren.length; j++) {
nodeNames.push(grandChildren[j].nodeName);
}
for (var j = 0; j < attributeNames.length; j++) {
var attributeIndex = nodeNames.indexOf(attributeNames[j]);
if (attributeIndex != -1) {
if (attributeTypes[j] == "boolean")
var aux = this.parseBoolean(
grandChildren[attributeIndex],
"value",
"enabled attribute for light of ID" + lightId
);
else if (attributeTypes[j] == "position")
var aux = this.parseCoordinates4D(
grandChildren[attributeIndex],
"light position for ID" + lightId
);
else
var aux = this.parseColor(
grandChildren[attributeIndex],
attributeNames[j] + " illumination for ID" + lightId
);
if (typeof aux === "string") return aux;
global.push(aux);
} else
return (
"light " + attributeNames[i] + " undefined for ID = " + lightId
);
}
this.lights[lightId] = global;
numLights++;
}
if (numLights == 0) return "at least one light must be defined";
else if (numLights > 8)
this.onXMLMinorError(
"too many lights defined; WebGL imposes a limit of 8 lights"
);
this.log("Parsed lights");
return null;
}
fileExists(url) {
if(url){
var request = new XMLHttpRequest();
request.open('GET', url, false);
request.send();
return request.status==200;
} else {
return false;
}
}
/**
* Parses the <textures> block.
* @param {textures block element} texturesNode
*/
parseTextures(texturesNode) {
var children = texturesNode.children;
this.textures = [];
var texturesNo = 0;
for (var i = 0; i < children.length; i++) {
if (children[i].nodeName != "texture") {
this.onXMLMinorError("unknwon tag <" + children[i].nodeName + ">");
continue;
}
var textureID = this.reader.getString(children[i], "id");
// Get id of the current texture.
if (textureID == null) return "no ID defined for texture";
// Checks for repeated IDs.
if (this.textures[textureID] != null)
return (
"ID must be unique for each texture (conflict: ID = " +
textureID +
")"
);
// Checks path od the current texture.
var path = children[i].getAttribute("path");
if (path == null) return "no Path defined for texture";
// check if directory exists
if(!this.fileExists(path))
this.onXMLMinorError(path + " file doesn't exist");
var texture = new CGFtexture(this.scene, path);
this.textures[textureID] = texture;
texturesNo++;
}
// Checks if there is at least one texture block
if (texturesNo <= 0) return "at least one texture block must be defined";
this.log("Parsed textures");
return null;
}
/**
* Parses the <spritesheets> block.
* @param {spritesheets block element} spritesheetsNode
*/
parseSpritesheets(spritesheetsNode) {
var children = spritesheetsNode.children;
this.spritesheets = [];
var spritesheetsNo = 0;
for (var i = 0; i < children.length; i++) {
if (children[i].nodeName != "spritesheet") {
this.onXMLMinorError("unknwon tag <" + children[i].nodeName + ">");
continue;
}
var spritesheetID = this.reader.getString(children[i], "id");
// Get id of the current spritesheet.
if (spritesheetID == null) return "no ID defined for spritesheet";
// Checks for repeated IDs.
if (this.spritesheets[spritesheetID] != null)
return (
"ID must be unique for each spritesheet (conflict: ID = " +
spritesheetID +
")"
);
// Checks path od the current spritesheet.
var path = children[i].getAttribute("path");
if (path == null) return "no Path defined for spritesheet";
// check if directory exists
if(!this.fileExists(path))
this.onXMLMinorError(path + " file doesn't exist");
var sizeM = parseFloat(this.reader.getString(children[i], "sizeM"));
var sizeN = parseFloat(this.reader.getString(children[i], "sizeN"));
var spritesheet = new MySpritesheet(this.scene, path,sizeM,sizeN);
this.spritesheets[spritesheetID] = spritesheet;
spritesheetsNo++;
}
// Checks if there is at least one texture block
if (spritesheetsNo <= 0) return "at least one texture block must be defined";
this.log("Parsed textures");
return null;
}
/**
* Parses the <materials> node.
* @param {materials block element} materialsNode
*/
parseMaterials(materialsNode) {
var children = materialsNode.children;
this.materials = [];
var grandChildren = [];
var materialsNo = 0;
// Any number of materials.
for (var i = 0; i < children.length; i++) {
if (children[i].nodeName != "material") {
this.onXMLMinorError("unknown tag <" + children[i].nodeName + ">");
continue;
}
// Get id of the current material.
var materialID = this.reader.getString(children[i], "id");
if (materialID == null) return "no ID defined for material";
// Checks for repeated IDs.
if (this.materials[materialID] != null)
return (
"ID must be unique for each material (conflict: ID = " +
materialID +
")"
);
grandChildren = children[i].children;
var shininess = null;
var ambient = null;
var diffuse = null;
var specular = null;
var emissive = null;
for (var j = 0; j < grandChildren.length; j++) {
if (grandChildren[j].nodeName == "shininess")
shininess = parseFloat(grandChildren[j].getAttribute("value"));
if (grandChildren[j].nodeName == "ambient") {
ambient = [
parseFloat(grandChildren[j].getAttribute("r")),
parseFloat(grandChildren[j].getAttribute("g")),
parseFloat(grandChildren[j].getAttribute("b")),
parseFloat(grandChildren[j].getAttribute("a")),
];
}
if (grandChildren[j].nodeName == "diffuse")
diffuse = [
parseFloat(grandChildren[j].getAttribute("r")),
parseFloat(grandChildren[j].getAttribute("g")),
parseFloat(grandChildren[j].getAttribute("b")),
parseFloat(grandChildren[j].getAttribute("a")),
];
if (grandChildren[j].nodeName == "specular")
specular = [
parseFloat(grandChildren[j].getAttribute("r")),
parseFloat(grandChildren[j].getAttribute("g")),
parseFloat(grandChildren[j].getAttribute("b")),
parseFloat(grandChildren[j].getAttribute("a")),
];
if (grandChildren[j].nodeName == "emissive")
emissive = [
parseFloat(grandChildren[j].getAttribute("r")),
parseFloat(grandChildren[j].getAttribute("g")),
parseFloat(grandChildren[j].getAttribute("b")),
parseFloat(grandChildren[j].getAttribute("a")),
];
}
var matError = null;
if (shininess == null)
return (
"shininess must be defined for each material (conflict: ID = " +
materialID +
")"
);
if (ambient == null)
return (
"ambient must be defined for each material (conflict: ID = " +
materialID +
")"
);
matError = this.checkRGBA(ambient[0], ambient[1], ambient[2], ambient[3]);
if (matError != null)
return matError + " (conflict: ID = " + materialID + " -> ambient)";
if (diffuse == null)
return (
"diffuse must be defined for each material (conflict: ID = " +
materialID +
")"
);
matError = this.checkRGBA(diffuse[0], diffuse[1], diffuse[2], diffuse[3]);
if (matError != null)
return matError + " (conflict: ID = " + materialID + " -> diffuse)";
if (specular == null)
return (
"specular must be defined for each material (conflict: ID = " +
materialID +
")"
);
matError = this.checkRGBA(
specular[0],
specular[1],
specular[2],
specular[3]
);
if (matError != null)
return matError + " (conflict: ID = " + materialID + " -> specular)";
if (emissive == null)
return (
"emissive must be defined for each material (conflict: ID = " +
materialID +
")"
);
matError = this.checkRGBA(
emissive[0],
emissive[1],
emissive[2],
emissive[3]
);
if (matError != null)
return matError + " (conflict: ID = " + materialID + " -> emissive)";
// Create material.
var material = new CGFappearance(this.scene);
material.setShininess(shininess);
material.setAmbient(ambient[0], ambient[1], ambient[2], ambient[3]);
material.setDiffuse(diffuse[0], diffuse[1], diffuse[2], diffuse[3]);
material.setSpecular(specular[0], specular[1], specular[2], specular[3]);
material.setEmission(emissive[0], emissive[1], emissive[2], emissive[3]);
this.materials[materialID] = material;
materialsNo++;
}
// Checks if there is at least one material block
if (materialsNo <= 0) return "at least one material block must be defined";
this.log("Parsed materials");
return null;
}
checkRGBA(r, g, b, a) {
if (r < 0 || r > 1 || isNaN(r))
return "r of rgba must be a value between 0 and 1";
if (g < 0 || g > 1 || isNaN(g))
return "g of rgba must be a value between 0 and 1";
if (b < 0 || b > 1 || isNaN(b))
return "b of rgba must be a value between 0 and 1";
if (a < 0 || a > 1 || isNaN(a))
return "a of rgba must be a value between 0 and 1";
return null;
}
/**
* Parses the <animations> block.
* @param {animations block element} nodesNode
*/
parseAnimations(AnimationsNode) {
var children = AnimationsNode.children;
this.animations = [];
var animationsNo = 0;
for (var i = 0; i < children.length; i++) {
var grandChildren = children[i].children;
if (children[i].nodeName != "animation") {
this.onXMLMinorError("unknwon tag <" + children[i].nodeName + ">");
continue;
}
var animationID = this.reader.getString(children[i], "id");
// Get id of the current texture.
if (animationID == null) return "no ID defined for animation";
// Checks for repeated IDs.
if (this.animations[animationID] != null)
return (
"ID must be unique for each animation (conflict: ID = " +
animationID +
")"
);
this.animations[animationID] = new MyAnimation(animationID);
for (var j = 0; j < children[i].children.length; j++) {
if (grandChildren[j].nodeName == "keyframe")
{
//console.log("keyframe");
var instant = parseFloat(grandChildren[j].getAttribute("instant"));
this.animations[animationID].instants.push(instant);
for (var k = 0; k < grandChildren[j].children.length; k++) {
var transformMatrix = mat4.create();
mat4.identity(transformMatrix);
if (grandChildren[j].children[k].nodeName == "translation")
{
var x = parseFloat(grandChildren[j].children[k].getAttribute("x"));
var y = parseFloat(grandChildren[j].children[k].getAttribute("y"));
var z = parseFloat(grandChildren[j].children[k].getAttribute("z"));
//to do: check values
/* mat4.translate(
transformMatrix,
transformMatrix,
[x, y, z]
); */
var vec_trans=[x,y,z];
this.animations[animationID].trans_vec.push(vec_trans);
this.animations[animationID].current_trans_vec.push([0,0,0]);
//console.log("translaction"+transformMatrix);
}
if (grandChildren[j].children[k].nodeName == "rotation")
{
var axis = grandChildren[j].children[k].getAttribute("axis");
var angle = parseFloat(grandChildren[j].children[k].getAttribute("angle"));
//to do: check values
/* mat4.rotate(
transformMatrix,
transformMatrix,
angle * DEGREE_TO_RAD,
[axis=="x"? 1 : 0, axis=="y"? 1 : 0, axis=="z"? 1 : 0]
); */
var vec_rot=[axis=="x"? 1 : 0, axis=="y"? 1 : 0, axis=="z"? 1 : 0];
this.animations[animationID].rot_vec.push(vec_rot);
this.animations[animationID].angles.push(angle* DEGREE_TO_RAD);
this.animations[animationID].current_angles.push(0);
}
if (grandChildren[j].children[k].nodeName == "scale")
{
var sx = parseFloat(grandChildren[j].children[k].getAttribute("sx"));
var sy = parseFloat(grandChildren[j].children[k].getAttribute("sy"));
var sz = parseFloat(grandChildren[j].children[k].getAttribute("sz"));
//to do: check values
var vec_scale=[sx, sy, sz];
this.animations[animationID].scale_vec.push(vec_scale);
this.animations[animationID].current_scale_vec.push([1,1,1]);
//console.log("scale"+transformMatrix);
}
}
//console.log(transformMatrix);
}
}
animationsNo++;
}
// Checks if there is at least one texture block
//if (animationsNo <= 0) return "at least one texture block must be defined";
this.log("Parsed textures");
return null;
}
/**
* Parses the <nodes> block.
* @param {nodes block element} nodesNode
*/
parseNodes(nodesNode) {
var children = nodesNode.children;
this.nodes = [];
var grandChildren = [];
var nodeNames = [];
// Any number of nodes.
for (var i = 0; i < children.length; i++) {
//this.log(nodesNode.children[i].id);
if (children[i].nodeName != "node") {
this.onXMLMinorError("unknown tag <" + children[i].nodeName + ">");
continue;
}
// Get id of the current node.
var nodeID = this.reader.getString(children[i], "id");
if (nodeID == null) return "no ID defined for nodeID";
this.nodes[nodeID] = MyNode(nodeID);
// Checks for repeated IDs.
if (this.nodes[nodeID] != null)
return (
"ID must be unique for each node (conflict: ID = " + nodeID + ")"
);
grandChildren = children[i].children;
this.nodes[nodeID] = new MyNode(nodeID);
nodeNames = [];
for (var j = 0; j < grandChildren.length; j++) {
nodeNames.push(grandChildren[j].nodeName);
}
var transformationsIndex = nodeNames.indexOf("transformations");
var materialIndex = nodeNames.indexOf("material");
var textureIndex = nodeNames.indexOf("texture");
var descendantsIndex = nodeNames.indexOf("descendants");
// Transformations
var transformationsNode = grandChildren[transformationsIndex].children;
for (var j = 0; j < transformationsNode.length; j++) {
if (transformationsNode[j].nodeName == "translation") {
var x = parseFloat(transformationsNode[j].getAttribute("x"));
var y = parseFloat(transformationsNode[j].getAttribute("y"));
var z = parseFloat(transformationsNode[j].getAttribute("z"));
//to do assume default values
if (x == null || y == null || z == null) {
this.onXMLMinorError("No values for translation, skiping");
continue;
} else if (isNaN(x) || isNaN(y) || isNaN(z)) {
this.onXMLMinorError("Non numeric values for trasnlation, skiping");
continue;
}
mat4.translate(
this.nodes[nodeID].transformMatrix,
this.nodes[nodeID].transformMatrix,
[x, y, z]
);
}
if (transformationsNode[j].nodeName == "rotation") {
var axis = transformationsNode[j].getAttribute("axis");
var angle = parseFloat(transformationsNode[j].getAttribute("angle"));
if (angle == null || axis == null) {
this.onXMLMinorError("No values for rotation, skiping");
continue;
} else if (isNaN(angle)) {
this.onXMLMinorError("Non numeric value for angle, skiping");
continue;
}
if (axis != "x" && axis != "y" && axis != "z") {
this.onXMLMinorError("Unknown axis provided for ratation, skiping");
continue;
}
if (axis == "x")
mat4.rotate(
this.nodes[nodeID].transformMatrix,
this.nodes[nodeID].transformMatrix,
angle * DEGREE_TO_RAD,
[1, 0, 0]
);
if (axis == "y")
mat4.rotate(
this.nodes[nodeID].transformMatrix,