aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/scenegraphNodes.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/scenegraphNodes.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/scenegraphNodes.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/scenegraphNodes.js649
1 files changed, 649 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/scenegraphNodes.js b/js/helper-classes/RDGE/src/core/script/scenegraphNodes.js
new file mode 100644
index 00000000..48f0aab9
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/scenegraphNodes.js
@@ -0,0 +1,649 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7
8/*
9 * Creates a unique node id
10 */
11 nodeIdGen = {};
12
13 nodeIdGen.counter = 0;
14
15 nodeIdGen.getId = function()
16 {
17 return "gen_" + nodeIdGen.counter++;
18 }
19
20/*
21 * Constructs a new transform node
22 */
23function createTransformNode(nodeName)
24{
25 node = { 'name': nodeName };
26
27 node.transformNodeTemplate = new transformNodeTemplate(node);
28
29 return node;
30}
31
32/*
33 * Constructs a new material node
34 */
35function createMaterialNode(nodeName)
36{
37 node = { 'name': nodeName };
38
39 node.materialNodeTemplate = new materialNodeTemplate(node);
40
41 return node;
42}
43
44/*
45 * Constructs a new mesh node
46 */
47function createMeshNode(nodeName, primitive)
48{
49 meshNode = { 'mesh':{}, 'meshNodeTemplate':{} };
50
51 var renderer = g_Engine.getContext().renderer;
52
53 if(!primitive.built)
54 {
55 renderer.createPrimitive(primitive);
56 }
57
58 var model = g_meshMan.getModelByName(nodeName);
59 if(!model)
60 {
61 meshNode.mesh.meshNodeTemplate = new meshNodeTemplate(meshNode.mesh, primitive, nodeName);
62
63 g_meshMan.modelMap[nodeName] = meshNode.mesh;
64 return meshNode; // --early out--
65 }
66 else if(!renderer.buffers[model.primitive.buffersID])
67 {
68 renderer.createPrimitive(model.primitive);
69 }
70
71 meshNode.mesh.meshNodeTemplate = new meshNodeTemplate(meshNode.mesh, model.primitive, nodeName);
72
73
74 return meshNode;
75}
76
77/*
78 * Construct a light node
79 */
80function createLightNode(nodeName)
81{
82 node = { 'name': nodeName };
83 node.lightNodeTemplate = new lightNodeTemplate(node);
84
85 return node;
86}
87
88/*
89 * creates a specialized mesh node representing a screen aligned quad with identity transform
90 */
91function createScreenQuadNode()
92{
93 var trNode = createTransformNode();
94 trNode.attachMeshNode("screenQuad", createScreenAlignedQuad());
95 return trNode;
96}
97
98function verifyTransformNode( node )
99{
100 if(node.transformNodeTemplate == undefined)
101 {
102 node.transformNodeTemplate = new transformNodeTemplate(node);
103 }
104}
105
106function verifyMaterialNode( node )
107{
108 if(node.materialNodeTemplate == undefined)
109 {
110 node.materialNodeTemplate = new materialNodeTemplate(node);
111 }
112}
113
114function verifyLightNode( node )
115{
116 if(node.lightNodeTemplate == undefined)
117 {
118 node.lightNodeTemplate = new lightNodeTemplate(node);
119 }
120}
121
122
123/*
124 * Takes an object and attachs transform node
125 * functions and fields if they are not defined
126 */
127transformNodeTemplate = function(trNode)
128{
129 // Lots of checking for things that might exist and adding them when they don't
130
131 /* ------------------------------------------- */
132 if(!trNode.children)
133 {
134 trNode.children = [];
135 }
136
137 if(!trNode.local)
138 {
139 trNode.local = mat4.identity();
140 }
141
142 if(!trNode.world)
143 {
144 trNode.world = mat4.identity();
145 }
146
147 if(!trNode.id)
148 {
149 trNode.id = nodeIdGen.getId();
150 }
151
152 if(!trNode.name)
153 {
154 trNode.name = "xfrmNode" + trNode.id;
155 }
156
157 if(!trNode.parent)
158 {
159 trNode.parent = null;
160 }
161
162 if(!trNode.meshes)
163 {
164 trNode.meshes = [];
165 }
166
167 if(!trNode.nodeType)
168 {
169 trNode.nodeType = rdgeConstants.nodeType.TRNODE;
170 }
171
172 /* ------------------------------------------- */
173
174 // Adding functions to the node none of these exist from export process
175 /*
176 * Attaches a material to a node
177 */
178 trNode.attachMaterial = function(matNode)
179 {
180 verifyMaterialNode(matNode);
181
182 this.materialNode = matNode;
183 }
184
185 /*
186 * @param node - the node to attach, can optionally be a node name paired with a primitive that will be built into a meshNode
187 * @param primitive - an optional parameter that must be supplied if the node is a name and not an object
188 */
189 trNode.attachMeshNode = function( node, primitive )
190 {
191 if(typeof node == "string")
192 {
193 node = createMeshNode(node, primitive)
194 }
195 if(trNode.materialNode == undefined)
196 {
197 trNode.materialNode = createMaterialNode(trNode.name + "|defMaterial");
198 }
199
200 trNode.meshes.push( {'mesh':{'name':node.mesh.attribs.name, 'id':node.mesh.attribs.id}});
201 }
202
203 /*
204 * Inserts a node as a child of this node
205 */
206 trNode.insertAsChild = function(transNode)
207 {
208 if(this == transNode)
209 return;
210
211 verifyTransformNode(transNode);
212
213 transNode.parent = this;
214 this.children.push({transformNode:transNode});
215 }