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.js153
1 files changed, 142 insertions, 11 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
index f9b3d4c3..28dc868a 100644
--- a/js/lib/rdge/texture.js
+++ b/js/lib/rdge/texture.js
@@ -66,18 +66,27 @@ function Texture( dstWorld )
66 return tex; 66 return tex;
67 } 67 }
68 68
69 var __texCounter = 0;
70
71 /*
69 this.loadFromCanvas = function( srcCanvas, wrap, mips ) 72 this.loadFromCanvas = function( srcCanvas, wrap, mips )
70 { 73 {
71 this._texMapName = "GLTexture_" + this.texCounter; 74 this._texMapName = "GLTexture_" + __texCounter;
72 this.texCounter++; 75 __texCounter++;
73 76
74 //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) 77 //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
75 var world = this.getDstWorld(); 78 var world = this.getDstWorld();
76 var renderer = world.getRenderer(); 79 var renderer = world.getRenderer();
77 80
81 // set default values for wrap and mips
82 if (wrap === undefined)
83 wrap = "REPEAT";
84 if (mips === undefined)
85 mips = true;
86
78 var imageData; 87 var imageData;
79 var width = srcCanvas.width, height = srcCanvas.height; 88 var width = srcCanvas.width, height = srcCanvas.height;
80 width = 128; height = 64; // some even power of 2 for now... 89 width = 64; height = 64; // some even power of 2 for now...
81 90
82 // create a canvas to be used as the image for the texture map 91 // create a canvas to be used as the image for the texture map
83 var doc = srcCanvas.ownerDocument; 92 var doc = srcCanvas.ownerDocument;
@@ -116,6 +125,13 @@ function Texture( dstWorld )
116// imageData.data[i] = data[i]; 125// imageData.data[i] = data[i];
117// dstCtx.putImageData( imageData, 0, 0 ); 126// dstCtx.putImageData( imageData, 0, 0 );
118 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
119 dstCtx.drawImage(srcCanvas, 0, 0); 135 dstCtx.drawImage(srcCanvas, 0, 0);
120 } 136 }
121 } 137 }
@@ -127,19 +143,134 @@ function Texture( dstWorld )
127 this._texture = tex; 143 this._texture = tex;
128 return tex; 144 return tex;
129 } 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 }
130 185
131 this.findPreviousWorld = function() 186 this.rerender = function()
132 { 187 {
133 var prevWorld; 188 if (!this._srcCanvas)
134 for ( var w in _worldStack )
135 { 189 {
136 world = _worldStack[w]; 190 console.log( " no source canvas in GLTexture.rerender" );
137 if (world == this.getWorld()) return prevWorld; 191 return;
138 prevWorld = world; 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 );
139 } 209 }
140 }
141 210
142 var texCounter = 0; 211 // create a canvas to be used as the image for the texture map
212 var doc = srcCanvas.ownerDocument;
213 var renderCanvas = this._renderCanvas;
214 if (!renderCanvas)
215 {
216 console.log( "no render canvas in GLTexture.rerender" );
217 return;
218 }
219 renderCanvas.width = width;
220 renderCanvas.height = height;
221 var renderCtx = renderCanvas.getContext("2d");
222
223 // create the texture
224 var tex = this._texture;
225 if (!tex)
226 {
227 console.log( "no texture in GLTexture.rerender" );
228 return;
229 }
230
231 var srcCtx;
232 if (!this.is3D)
233 {
234 srcCtx = srcCanvas.getContext("2d");
235 imageData = srcCtx.getImageData( 0, 0, width, height );
236 }
237 else
238 {
239 srcCtx = srcCanvas.getContext("experimental-webgl");
240 if (srcCtx)
241 {
242 var data = new Uint8Array(width * height * 4);
243 srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
244
245 imageData = renderCtx.createImageData(width, height);
246 var nBytes = width*height*4;
247 for (var i=0; i<nBytes; i++)
248 imageData.data[i] = data[i];
249 }
250 }
251 renderCtx.putImageData( imageData, 0, 0 );
252
253 /////////////////
254 tex.image = renderCanvas;
255 renderer.commitTexture( tex );
256
257 return tex;
258 }
259
260
261 this.isPowerOfTwo = function(x)
262 {
263 return (x & (x - 1)) == 0;
264 }
265
266 this.nextHighestPowerOfTwo = function(x)
267 {
268 --x;
269 for (var i = 1; i < 32; i <<= 1) {
270 x = x | x >> i;
271 }
272 return x + 1;
273 }
143} 274}
144 275
145if (typeof exports === "object") { 276if (typeof exports === "object") {