diff options
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r-- | js/lib/rdge/texture.js | 329 |
1 files changed, 329 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js new file mode 100644 index 00000000..bcbcb4bc --- /dev/null +++ b/js/lib/rdge/texture.js | |||
@@ -0,0 +1,329 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | var Material = require("js/lib/rdge/materials/material").Material; | ||
8 | |||
9 | var __textureCounter = 0; | ||
10 | |||
11 | /////////////////////////////////////////////////////////////////////// | ||
12 | // Class GLTexture | ||
13 | // GL representation of a texture. | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | function 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 jObj = JSON.parse( jStr ); | ||
146 | var oldCanvas = srcWorld.getCanvas(); | ||
147 | |||
148 | // create a new canvas | ||
149 | this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); | ||
150 | srcCanvas = this._srcCanvas; | ||
151 | srcCanvas.width = oldCanvas.width; | ||
152 | srcCanvas.height = oldCanvas.height; | ||
153 | |||
154 | // rebuild the world | ||
155 | this._srcWorld = new GLWorld( this._srcCanvas, true, true ); | ||
156 | this._srcWorld.importSJON( jObj ); | ||
157 | |||
158 | this._isLocal = true; | ||
159 | } | ||
160 | } | ||
161 | |||
162 | this.loadFromFile = function() | ||
163 | { | ||
164 | var tex = this._texture; | ||
165 | this._srcCanvas = null; | ||
166 | |||
167 | // only load if something has changed | ||
168 | if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? | ||
169 | { | ||
170 | var texMapName = this._texMapName; | ||
171 | var wrap = this._wrap; | ||
172 | var mips = this._mips; | ||
173 | |||
174 | var dstWorld = this.getDstWorld(); | ||
175 | if (dstWorld) | ||
176 | { | ||
177 | var renderer = dstWorld.getRenderer(); | ||
178 | tex = renderer.getTextureByName(texMapName, wrap, mips ); | ||
179 | this._texture = tex; | ||
180 | dstWorld.textureToLoad( tex ); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | return tex; | ||
185 | } | ||
186 | |||
187 | var __texCounter = 0; | ||
188 | this.loadFromCanvas = function() | ||
189 | { | ||
190 | var NJUtils = require("js/lib/NJUtils").NJUtils; | ||
191 | |||
192 | var srcCanvas = this._srcCanvas; | ||
193 | var wrap = this._wrap; | ||
194 | var mips = this._mips; | ||
195 | |||
196 | this._texMapName = "GLTexture_" + __texCounter; | ||
197 | __texCounter++; | ||
198 | |||
199 | // create the texture | ||
200 | var world = this.getDstWorld(); | ||
201 | tex = world.getGLContext().createTexture(); | ||
202 | this._texture = tex; | ||
203 | tex.texparams = new _texparams(wrap, mips); // defined in renderer.js | ||
204 | tex.image = new Image; | ||
205 | |||
206 | // create the canvas and context to render into | ||
207 | var doc = srcCanvas.ownerDocument; | ||
208 | //this._renderCanvas = doc.createElement("texture_canvas"); | ||
209 | this._renderCanvas = NJUtils.makeNJElement("canvas", "texture_canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); | ||
210 | |||
211 | this.render(); | ||
212 | |||
213 | return tex; | ||
214 | } | ||
215 | |||
216 | this.render = function() | ||
217 | { | ||
218 | if (!this._srcCanvas) | ||
219 | { | ||
220 | console.log( " no source canvas in GLTexture.render" ); | ||
221 | return; | ||
222 | } | ||
223 | var srcCanvas = this._srcCanvas; | ||
224 | |||
225 | if (this._isLocal) | ||
226 | { | ||
227 | this._srcWorld.update(); | ||
228 | this._srcWorld.draw(); | ||
229 | } | ||
230 | |||
231 | var world = this.getDstWorld(); | ||
232 | if (!world) | ||
233 | { | ||
234 | console.log( "no world in GLTexture.render" ); | ||
235 | return; | ||
236 | } | ||
237 | var renderer = world.getRenderer(); | ||
238 | |||
239 | var width = srcCanvas.width, height = srcCanvas.height; | ||
240 | if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height)) | ||
241 | { | ||
242 |