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.js334
1 files changed, 334 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
new file mode 100644
index 00000000..0a7070d3
--- /dev/null
+++ b/js/lib/rdge/texture.js
@@ -0,0 +1,334 @@
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 //dstWorld.refreshTextures();
127 //dstWorld.restartRenderLoop();
128 break;
129
130 default:
131 throw new Exception( "unrecognized texture callback type: " + type );
132 break;
133 }
134 }
135 }
136 }
137
138 this.rebuildSrcLocally = function()
139 {
140 var srcWorld = this._srcWorld;
141 if (srcWorld)
142 {
143 // get the data from the old world
144 var jStr = srcWorld.exportJSON();
145 var index = jStr.indexOf( ';' );
146 if ((jStr[0] === 'v') && (index < 24))
147 jStr = jStr.substr( index+1 );
148 var jObj = JSON.parse( jStr );
149 var oldCanvas = srcWorld.getCanvas();
150
151 // create a new canvas
152 var NJUtils = require("js/lib/NJUtils").NJUtils;
153 this._srcCanvas = NJUtils.makeNJElement("canvas", "texture_internal_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
154 srcCanvas = this._srcCanvas;
155 srcCanvas.width = oldCanvas.width;
156 srcCanvas.height = oldCanvas.height;
157
158 // rebuild the world
159 var GLWorld = require("js/lib/drawing/world").World;
160 this._srcWorld = new GLWorld( this._srcCanvas, true, true );
161 this._srcWorld.importJSON( jObj );
162
163 this._isLocal = true;
164 }
165 }
166
167 this.loadFromFile = function()
168 {
169 var tex = this._texture;
170 this._srcCanvas = null;
171
172 // only load if something has changed
173 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
174 {
175 var texMapName = this._texMapName;
176 var wrap = this._wrap;
177 var mips = this._mips;
178
179 var dstWorld = this.getDstWorld();
180 if (dstWorld)
181 {
182 var renderer = dstWorld.getRenderer();
183 tex = renderer.getTextureByName(texMapName, wrap, mips );
184 this._texture = tex;
185 dstWorld.textureToLoad( tex );
186 }
187 }
188
189 return tex;
190 }
191
192 var __texCounter = 0;
193 this.loadFromCanvas = function()
194 {
195 var NJUtils = require("js/lib/NJUtils").NJUtils;
196
197 var srcCanvas = this._srcCanvas;
198 var wrap = this._wrap;
199 var mips = this._mips;
200
201 this._texMapName = "GLTexture_" + __texCounter;
202 __texCounter++;
203
204 // create the texture
205 var world = this.getDstWorld();
206 tex = world.getGLContext().createTexture();
207 this._texture = tex;
208 tex.texparams = new _texparams(wrap, mips); // defined in renderer.js
209 tex.image = new Image;
210
211 // create the canvas and context to render into
212 var doc = srcCanvas.ownerDocument;
213 //this._renderCanvas = doc.createElement("texture_canvas");
214 this._renderCanvas = NJUtils.makeNJElement("canvas", "texture_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
215
216 this.render();
217
218 return tex;
219 }
220
221 this.render = function()
222 {
223 if (!this._srcCanvas)
224 {
225 console.log( " no source canvas in GLTexture.render" );
226 return;
227 }
228 var srcCanvas = this._srcCanvas;
229
230 if (this._isLocal)
231 {
232 this._srcWorld.update();
233 this._srcWorld.draw();
234 }
235
236 var world = this.getDstWorld();
237 if (!world)
238 {
239 console.log( "no world in GLTexture.render" );
240 return;