aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/texture.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r--js/lib/rdge/texture.js332
1 files changed, 332 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
new file mode 100644
index 00000000..37658580
--- /dev/null
+++ b/js/lib/rdge/texture.js
@@ -0,0 +1,332 @@
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
7var Material = require("js/lib/rdge/materials/material").Material;
8
9var __textureCounter = 0;
10
11///////////////////////////////////////////////////////////////////////
12// Class GLTexture
13// GL representation of a texture.
14///////////////////////////////////////////////////////////////////////
15function Texture( dstWorld, texMapName, wrap, mips )
16{
17 ///////////////////////////////////////////////////////////////////////
18 // Instance variables
19 ///////////////////////////////////////////////////////////////////////
20 this._texture;
21
22 // the canvas generating the texture map (if there is one)
23 this._srcCanvas;
24 this._srcWorld;
25
26 // texture attributes
27 if (typeof texMapName === "string")
28 this._texMapName = texMapName.slice();
29 else
30 this._srcCanvas = texMapName;
31
32
33 // set default values for wrap and mips
34 if (wrap === undefined)
35 wrap = "REPEAT";
36 if (mips === undefined)
37 mips = true;
38 this._wrap = wrap;
39 this._mips = mips;
40
41 // cache whether or not the source is animated
42 this._isAnimated = false;
43
44 // the destination world that will use the texture map
45 this._dstWorld = dstWorld;
46
47 this._texCount = __textureCounter;
48 __textureCounter++;
49
50 ///////////////////////////////////////////////////////////////////////
51 // Property Accessors
52 ///////////////////////////////////////////////////////////////////////
53 this.getTexture = function() { return this._texture; }
54
55 this.setSrcWorld = function(w) { this._srcWorld = w; }
56 this.getSrcWorld = function() { return this._srcWorld; }
57
58 this.setDstWorld = function(w) { this._dstWorld = w; }
59 this.getDstWorld = function() { return this._dstWorld; }
60
61 this.isAnimated = function() { return this._isAnimated; }
62
63 ///////////////////////////////////////////////////////////////////////
64 // Methods
65 ///////////////////////////////////////////////////////////////////////
66
67 this.init = function()
68 {
69 // determine if the source is a canvas or an image file
70 var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
71 //var root = viewUtils.application.ninja.currentDocument.documentRoot;
72 var root;
73 if (viewUtils.application.ninja.currentDocument)
74 root = viewUtils.application.ninja.currentDocument.model.documentRoot;
75 var srcCanvas = this._srcCanvas;
76 if (!srcCanvas && root)
77 srcCanvas = this.findCanvas( this._texMapName, root );
78 if (srcCanvas)
79 {
80 this._srcCanvas = srcCanvas;
81 var srcWorld
82 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
83 srcWorld = srcCanvas.elementModel.shapeModel.GLWorld;
84 if (!srcWorld) srcWorld = srcCanvas.__GLWorld;
85 if (srcWorld)
86 {
87 this._srcWorld = srcWorld;
88
89 // add a notifier to the world
90 srcWorld.addListener( this, this.worldCallback, { srcWorld: this._srcWorld } );
91
92 // check if the source is animated
93 this._isAnimated = srcWorld._hasAnimatedMaterials;
94 }
95
96 this.loadFromCanvas();
97 }
98 else
99 {
100 this.loadFromFile();
101 }
102 }
103
104 this.worldCallback = function( type, callbackObj, calleeData, callerData )
105 {
106 console.log( "texture callback, type: " + type );
107 if (calleeData.srcWorld)
108 {
109 var srcWorld = callbackObj.getSrcWorld();
110 var dstWorld = callbackObj.getDstWorld();
111 var notifier = srcWorld._notifier;
112 var texture = this.callbackObj;
113 if (texture)
114 {
115 switch (type)
116 {
117 case notifier.OBJECT_DELETE:
118 texture.rebuildSrcLocally();
119 break;
120
121 case notifier.OBJECT_REINSTANTIATE:
122 break;
123
124 case notifier.OBJECT_CHANGE:
125 break;
126
127 case notifier.FIRST_RENDER:
128 texture._isAnimated = srcWorld.hasAnimatedMaterials();
129 break;
130
131 default:
132 throw new Exception( "unrecognized texture callback type: " + type );
133 break;
134 }
135 }
136 }
137 }
138
139 this.rebuildSrcLocally = function()
140 {
141 var srcWorld = this._srcWorld;
142 if (srcWorld)
143 {
144 // get the data from the old world
145 var jStr = srcWorld.exportJSON();
146 var index = jStr.indexOf( ';' );
147 if ((jStr[0] === 'v') && (index < 24))
148 jStr = jStr.substr( index+1 );
149 var jObj = JSON.parse( jStr );
150 var oldCanvas = srcWorld.getCanvas();
151
152 // create a new canvas
153 var NJUtils = require("js/lib/NJUtils").NJUtils;
154 this._srcCanvas = NJUtils.makeNJElement("canvas", "texture_internal_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
155 srcCanvas = this._srcCanvas;
156 srcCanvas.width = oldCanvas.width;
157 srcCanvas.height = oldCanvas.height;
158
159 // rebuild the world
160 var GLWorld = require("js/lib/drawing/world").World;
161 this._srcWorld = new GLWorld( this._srcCanvas, true, true );
162 this._srcWorld.importJSON( jObj );
163
164 this._isLocal = true;
165 }
166 }
167
168 this.loadFromFile = function()
169 {
170 var tex = this._texture;
171 this._srcCanvas = null;
172
173 // only load if something has changed
174 //if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
175 {
176 var texMapName = this._texMapName;
177 var wrap = this._wrap;
178 var mips = this._mips;
179
180 var dstWorld = this.getDstWorld();
181 if (dstWorld)
182 {
183 var renderer = dstWorld.getRenderer();
184 tex = renderer.getTextureByName(texMapName, wrap, mips );
185 this._texture = tex;
186 dstWorld.textureToLoad( tex );
187 }
188 }
189
190 return tex;
191 }
192
193 var __texCounter = 0;
194 this.loadFromCanvas = function()
195 {
196 var NJUtils = require("js/lib/NJUtils").NJUtils;
197
198 var srcCanvas = this._srcCanvas;
199 var wrap = this._wrap;
200 var mips = this._mips;
201
202 this._texMapName = "GLTexture_" + __texCounter;
203 __texCounter++;
204
205 // create the texture
206 var world = this.getDstWorld();
207 tex = world.getGLContext().createTexture();
208 this._texture = tex;
209 tex.texparams = new _texparams(wrap, mips); // defined in renderer.js
210 tex.image = new Image;
211
212 // create the canvas and context to render into
213 var doc = srcCanvas.ownerDocument;
214 //this._renderCanvas = doc.createElement("texture_canvas");
215 this._renderCanvas = NJUtils.makeNJElement("canvas", "texture_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
216
217 this.render();
218
219 return tex;
220 }
221
222 this.render = function()
223 {
224 if (!this._srcCanvas)
225 return;
226 var srcCanvas = this._srcCanvas;
227
228 if (this._isLocal)
229 {
230 this._srcWorld.update();
231 this._srcWorld.draw();
232 }
233
234 var world = this.getDstWorld();
235 if (!world)
236 {
237 console.log( "no world in GLTexture.render" );
238 return;
239 }
240 var