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..498dcf3b
--- /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 srcCanvas = this._srcCanvas;
73 if (!srcCanvas)
74 srcCanvas = this.findCanvas( this._texMapName, root );
75 if (srcCanvas)
76 {
77 this._srcCanvas = srcCanvas;
78 var srcWorld
79 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
80 srcWorld = srcCanvas.elementModel.shapeModel.GLWorld;
81 if (!srcWorld) srcWorld = srcCanvas.__GLWorld;
82 if (srcWorld)
83 {
84 this._srcWorld = srcWorld;
85
86 // add a notifier to the world
87 srcWorld.addListener( this, this.worldCallback, { srcWorld: this._srcWorld } );
88
89 // check if the source is animated
90 this._isAnimated = srcWorld._hasAnimatedMaterials;
91 }
92
93 this.loadFromCanvas();
94 }
95 else
96 {
97 this.loadFromFile();
98 }
99 }
100
101 this.worldCallback = function( type, callbackObj, calleeData, callerData )
102 {
103 console.log( "texture callback, type: " + type );
104 if (calleeData.srcWorld)
105 {
106 var srcWorld = callbackObj.getSrcWorld();
107 var dstWorld = callbackObj.getDstWorld();
108 var notifier = srcWorld._notifier;
109 var texture = this.callbackObj;
110 if (texture)
111 {
112 switch (type)
113 {
114 case notifier.OBJECT_DELETE:
115 texture.rebuildSrcLocally();
116 break;
117
118 case notifier.OBJECT_REINSTANTIATE:
119 break;
120
121 case notifier.OBJECT_CHANGE:
122 break;
123
124 case notifier.FIRST_RENDER:
125 texture._isAnimated = srcWorld.hasAnimatedMaterials();
126 break;
127
128 default:
129 throw new Exception( "unrecognized texture callback type: " + type );
130 break;
131 }
132 }
133 }
134 }
135
136 this.rebuildSrcLocally = function()
137 {
138 var srcWorld = this._srcWorld;
139 if (srcWorld)
140 {
141 // get the data from the old world
142 var jStr = srcWorld.exportJSON();
143 var index = jStr.indexOf( ';' );
144 if ((jStr[0] === 'v') && (index < 24))
145 jStr = jStr.substr( index+1 );
146 var jObj = JSON.parse( jStr );
147 var oldCanvas = srcWorld.getCanvas();
148
149 // create a new canvas
150 var NJUtils = require("js/lib/NJUtils").NJUtils;
151 this._srcCanvas = NJUtils.makeNJElement("canvas", "texture_internal_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
152 srcCanvas = this._srcCanvas;
153 srcCanvas.width = oldCanvas.width;
154 srcCanvas.height = oldCanvas.height;
155
156 // rebuild the world
157 var GLWorld = require("js/lib/drawing/world").World;
158 this._srcWorld = new GLWorld( this._srcCanvas, true, true );
159 this._srcWorld.importJSON( jObj );
160
161 this._isLocal = true;
162 }
163 }
164
165 this.loadFromFile = function()
166 {
167 var tex = this._texture;
168 this._srcCanvas = null;
169
170 // only load if something has changed
171 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
172 {
173 var texMapName = this._texMapName;
174 var wrap = this._wrap;
175 var mips = this._mips;
176
177 var dstWorld = this.getDstWorld();
178 if (dstWorld)
179 {
180 var renderer = dstWorld.getRenderer();
181 tex = renderer.getTextureByName(texMapName, wrap, mips );
182 this._texture = tex;
183 dstWorld.textureToLoad( tex );
184 }
185 }
186
187 return tex;
188 }
189
190 var __texCounter = 0;
191 this.loadFromCanvas = function()
192 {
193 var NJUtils = require("js/lib/NJUtils").NJUtils;
194
195 var srcCanvas = this._srcCanvas;
196 var wrap = this._wrap;
197 var mips = this._mips;
198
199 this._texMapName = "GLTexture_" + __texCounter;
200 __texCounter++;
201
202 // create the texture
203 var world = this.getDstWorld();
204 tex = world.getGLContext().createTexture();
205 this._texture = tex;
206 tex.texparams = new _texparams(wrap, mips); // defined in renderer.js
207 tex.image = new Image;
208
209 // create the canvas and context to render into
210 var doc = srcCanvas.ownerDocument;
211 //this._renderCanvas = doc.createElement("texture_canvas");
212 this._renderCanvas = NJUtils.makeNJElement("canvas", "texture_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
213
214 this.render();
215
216 return tex;
217 }
218
219 this.render = function()
220 {
221 if (!this._srcCanvas)
222 {
223 console.log( " no source canvas in GLTexture.render" );
224 return;
225 }
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 renderer = world.getRenderer();