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.js285
1 files changed, 285 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
new file mode 100644
index 00000000..c72864b8
--- /dev/null
+++ b/js/lib/rdge/texture.js
@@ -0,0 +1,285 @@
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
9///////////////////////////////////////////////////////////////////////
10// Class GLTexture
11// GL representation of a texture.
12///////////////////////////////////////////////////////////////////////
13function Texture( dstWorld )
14{
15 ///////////////////////////////////////////////////////////////////////
16 // Instance variables
17 ///////////////////////////////////////////////////////////////////////
18 this._texture;
19
20 // texture attributes
21 this._texMapName;
22 this._wrap;
23 this._mips;
24
25 this._srcCanvas; // the canvas generating the texture map.
26 this._dstWorld; // the world that will use the texture map
27 this._dstWorld = dstWorld;
28
29 ///////////////////////////////////////////////////////////////////////
30 // Property Accessors
31 ///////////////////////////////////////////////////////////////////////
32 this.getTexture = function() { return this._texture; }
33
34 this.setSrcWorld = function(w) { this._srcWorld = w; }
35 this.getSrcWorld = function() { return this._srcWorld; }
36
37 this.setDstWorld = function(w) { this._dstWorld = w; }
38 this.getDstWorld = function() { return this._dstWorld; }
39
40 ///////////////////////////////////////////////////////////////////////
41 // Methods
42 ///////////////////////////////////////////////////////////////////////
43
44 this.loadFromFile = function( texMapName, wrap, mips )
45 {
46 var tex = this._texture;
47 this._srcCanvas = null;
48
49 // only load if something has changed
50 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
51 {
52 this._texMapName = texMapName.slice();
53 this._wrap = wrap;
54 this._mips = mips;
55
56 var dstWorld = this.getDstWorld();
57 if (dstWorld)
58 {
59 var renderer = dstWorld.getRenderer();
60 tex = renderer.getTextureByName(texMapName, wrap, mips );
61 this._texture = tex;
62 dstWorld.textureToLoad( tex );
63 }
64 }
65
66 return tex;
67 }
68
69 var __texCounter = 0;
70
71 /*
72 this.loadFromCanvas = function( srcCanvas, wrap, mips )
73 {
74 this._texMapName = "GLTexture_" + __texCounter;
75 __texCounter++;
76
77 //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
78 var world = this.getDstWorld();
79 var renderer = world.getRenderer();
80
81 // set default values for wrap and mips
82 if (wrap === undefined)
83 wrap = "REPEAT";
84 if (mips === undefined)
85 mips = true;
86
87 var imageData;
88 var width = srcCanvas.width, height = srcCanvas.height;
89 width = 64; height = 64; // some even power of 2 for now...
90
91 // create a canvas to be used as the image for the texture map
92 var doc = srcCanvas.ownerDocument;
93 var dstCanvas = doc.createElement("canvas");
94 dstCanvas.width = width;
95 dstCanvas.height = height;
96 var dstCtx = dstCanvas.getContext("2d");
97
98 var tex;
99 var srcCtx = srcCanvas.getContext("2d");
100 if (srcCtx)
101 {
102 tex = renderer.getTextureByName(this._texMapName, wrap, mips );
103 imageData = srcCtx.getImageData( 0, 0, width, height );
104 dstCtx.putImageData( imageData, 0, 0 );
105 }
106 else
107 {
108 tex = renderer.getTextureByName(this._texMapName, wrap, mips );
109 //tex = world.getGLContext().createTexture();
110 tex.image = new Image;
111 tex.wrap = wrap;
112 tex.mips = mips;
113
114 srcCtx = srcCanvas.getContext("experimental-webgl");
115 if (srcCtx)
116 {
117// var data = new Uint8Array(width * height * 4);
118// srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
119// console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] );
120//
121// //imageData.data = data;
122// imageData = dstCtx.createImageData(width, height);
123// var nBytes = width*height*4;
124// for (var i=0; i<nBytes; i++)
125// imageData.data[i] = data[i];
126// dstCtx.putImageData( imageData, 0, 0 );
127
128 var data = new Uint8Array(width * height * 4);
129 srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
130 for (var i=1; i<10; i++)
131 {
132 console.log( "row " + i + ": " + data[i*4*width] + ", " + data[i*4*width+1] + ", " + data[i*4*width+2] + ", " + data[i*4*width+3] );
133 }
134
135 dstCtx.drawImage(srcCanvas, 0, 0);
136 }
137 }
138
139
140 /////////////////
141 tex.image = dstCanvas;
142
143 this._texture = tex;
144 return tex;
145 }
146 */
147
148 this.loadFromCanvas = function( srcCanvas, wrap, mips )
149 {
150 this._srcCanvas = srcCanvas;
151
152 this._texMapName = "GLTexture_" + __texCounter;
153 __texCounter++;
154
155 // set default values for wrap and mips
156 if (wrap === undefined)
157 wrap = "REPEAT";
158 if (mips === undefined)
159 mips = true;
160
161 // we animate only if the source is an animated GLWorld
162 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
163 this._isAnimated = srcCanvas.elementModel.shapeModel.GLWorld._hasAnimatedMaterials;
164
165 // create the texture
166 var world = this.getDstWorld();
167 tex = world.getGLContext().createTexture();
168 this._texture = tex;
169 tex.texparams = new _texparams(wrap, mips); // defined in renderer.js
170 tex.image = new Image;
171
172 // create the canvas and context to render into
173 var doc = srcCanvas.ownerDocument;
174 this._renderCanvas = doc.createElement("canvas");
175
176 // cache whether this is a 2D canvas or 3D canvas
177 var srcCtx = srcCanvas.getContext("2d");
178 this._is3D = false;
179 if (!srcCtx) this.is3D = true;
180
181 this.rerender();
182
183 return tex;
184 }
185
186 this.rerender = function()
187 {
188 if (!this._srcCanvas)
189 {
190 console.log( " no source canvas in GLTexture.rerender" );
191 return;
192 }
193 var srcCanvas = this._srcCanvas;
194
195 var world = this.getDstWorld();
196 if (!world)
197 {
198 console.log( "no world in GLTexture.rerender" );
199 return;
200 }
201 var renderer = world.getRenderer();
202
203 var imageData;
204 var width = srcCanvas.width, height = srcCanvas.height;
205 if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height))
206 {
207 width = this.nextHighestPowerOfTwo( width );
208 height = this.nextHighestPowerOfTwo( height );
209 }
210 width = 64; height = 64;
211
212 // create a canvas to be used as the image for the texture map
213 var doc = srcCanvas.ownerDocument;
214 var renderCanvas = this._renderCanvas;
215 if (!renderCanvas)
216 {
217 console.log( "no render canvas in GLTexture.rerender" );
218 return;
219 }
220 renderCanvas.width = width;
221 renderCanvas.height = height;
222 var renderCtx = renderCanvas.getContext("2d");
223
224 // create the texture
225 var tex = this._texture;
226 if (!tex)
227 {
228 console.log( "no texture in GLTexture.rerender" );
229 return;
230 }
231
232 var srcCtx;
233 if (!this.is3D)
234 {
235 srcCtx = srcCanvas.getContext("2d");
236 imageData = srcCtx.getImageData( 0, 0, width, height );
237 }
238 else
239 {
240 srcCtx = srcCanvas.getContext("experimental-webgl");