diff options
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r-- | js/lib/rdge/texture.js | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js new file mode 100644 index 00000000..b349ee62 --- /dev/null +++ b/js/lib/rdge/texture.js | |||
@@ -0,0 +1,206 @@ | |||
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 | /////////////////////////////////////////////////////////////////////// | ||
10 | // Class GLTexture | ||
11 | // GL representation of a texture. | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | function 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 | this.isAnimated = function() { return this._isAnimated; } | ||
41 | |||
42 | /////////////////////////////////////////////////////////////////////// | ||
43 | // Methods | ||
44 | /////////////////////////////////////////////////////////////////////// | ||
45 | |||
46 | this.loadFromFile = function( texMapName, wrap, mips ) | ||
47 | { | ||
48 | var tex = this._texture; | ||
49 | this._srcCanvas = null; | ||
50 | |||
51 | // only load if something has changed | ||
52 | if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? | ||
53 | { | ||
54 | this._texMapName = texMapName.slice(); | ||
55 | this._wrap = wrap; | ||
56 | this._mips = mips; | ||
57 | |||
58 | var dstWorld = this.getDstWorld(); | ||
59 | if (dstWorld) | ||
60 | { | ||
61 | var renderer = dstWorld.getRenderer(); | ||
62 | tex = renderer.getTextureByName(texMapName, wrap, mips ); | ||
63 | this._texture = tex; | ||
64 | dstWorld.textureToLoad( tex ); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | return tex; | ||
69 | } | ||
70 | |||
71 | var __texCounter = 0; | ||
72 | this.loadFromCanvas = function( srcCanvas, wrap, mips ) | ||
73 | { | ||
74 | this._srcCanvas = srcCanvas; | ||
75 | |||
76 | this._texMapName = "GLTexture_" + __texCounter; | ||
77 | __texCounter++; | ||
78 | |||
79 | // set default values for wrap and mips | ||
80 | if (wrap === undefined) | ||
81 | wrap = "REPEAT"; | ||
82 | if (mips === undefined) | ||
83 | mips = true; | ||
84 | |||
85 | // we animate only if the source is an animated GLWorld | ||
86 | if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) | ||
87 | this._isAnimated = srcCanvas.elementModel.shapeModel.GLWorld._hasAnimatedMaterials; | ||
88 | |||
89 | // create the texture | ||
90 | var world = this.getDstWorld(); | ||
91 | tex = world.getGLContext().createTexture(); | ||
92 | this._texture = tex; | ||
93 | tex.texparams = new _texparams(wrap, mips); // defined in renderer.js | ||
94 | tex.image = new Image; | ||
95 | |||
96 | // create the canvas and context to render into | ||
97 | var doc = srcCanvas.ownerDocument; | ||
98 | this._renderCanvas = doc.createElement("canvas"); | ||
99 | |||
100 | // cache whether this is a 2D canvas or 3D canvas | ||
101 | var srcCtx = srcCanvas.getContext("2d"); | ||
102 | this._is3D = false; | ||
103 | if (!srcCtx) this._is3D = true; | ||
104 | |||
105 | this.rerender(); | ||
106 | |||
107 | return tex; | ||
108 | } | ||
109 | |||
110 | this.rerender = function() | ||
111 | { | ||
112 | if (!this._srcCanvas) | ||
113 | { | ||
114 | console.log( " no source canvas in GLTexture.rerender" ); | ||
115 | return; | ||
116 | } | ||
117 | var srcCanvas = this._srcCanvas; | ||
118 | |||
119 | var world = this.getDstWorld(); | ||
120 | if (!world) | ||
121 | { | ||
122 | console.log( "no world in GLTexture.rerender" ); | ||
123 | return; | ||
124 | } | ||
125 | var renderer = world.getRenderer(); | ||
126 | |||
127 | var imageData; | ||
128 | var width = srcCanvas.width, height = srcCanvas.height; | ||
129 | if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height)) | ||
130 | { | ||
131 | width = this.nextHighestPowerOfTwo( width ); | ||
132 | height = this.nextHighestPowerOfTwo( height ); | ||
133 | //width = 64; height = 64; | ||
134 | } | ||
135 | |||
136 | // create a canvas to be used as the image for the texture map | ||
137 | var doc = srcCanvas.ownerDocument; | ||
138 | var renderCanvas = this._renderCanvas; | ||
139 | if (!renderCanvas) | ||
140 | { | ||
141 | console.log( "no render canvas in GLTexture.rerender" ); | ||
142 | return; | ||
143 | } | ||
144 | renderCanvas.width = width; | ||
145 | renderCanvas.height = height; | ||
146 | var renderCtx = renderCanvas.getContext("2d"); | ||
147 | |||
148 | // create the texture | ||
149 | var tex = this._texture; | ||
150 | if (!tex) | ||
151 | { | ||
152 | console.log( "no texture in GLTexture.rerender" ); | ||
153 | return; | ||
154 | } | ||
155 | |||
156 | var srcCtx; | ||
157 | if (!this._is3D) | ||
158 | { | ||
159 | srcCtx = srcCanvas.getContext("2d"); | ||
160 | imageData = srcCtx.getImageData( 0, 0, width, height ); | ||
161 | renderCtx.putImageData( imageData, 0, 0 ); | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | srcCtx = srcCanvas.getContext("experimental-webgl"); | ||
166 | if (srcCtx) | ||
167 | { | ||
168 | renderCtx.drawImage(srcCanvas, 0, 0); | ||
169 | // var data = new Uint8Array(width * height * 4); | ||
170 | // srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data); | ||
171 | // imageData = renderCtx.createImageData(width, height); | ||
172 | // var nBytes = width*height*4; | ||
173 | // for (var i=0; i<nBytes; i++) | ||
174 | // imageData.data[i] = data[i]; | ||
175 | // renderCtx.putImageData( imageData, 0, 0 ); | ||
176 | |||
177 | } | ||
178 | } | ||
179 | |||
180 | ///////////////// | ||
181 | tex.image = renderCanvas; | ||
182 | renderer.commitTexture( tex ); | ||
183 | |||
184 | return tex; | ||
185 | } | ||
186 | |||
187 | this.isPowerOfTwo = function(x) | ||
188 | { | ||
189 | return (x & (x - 1)) == 0; | ||
190 | } | ||
191 | |||
192 | this.nextHighestPowerOfTwo = function(x) | ||
193 | { | ||
194 | --x; | ||
195 | for (var i = 1; i < 32; i <<= 1) { | ||
196 | x = x | x >> i; | ||
197 | } | ||
198 | return x + 1; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | if (typeof exports === "object") { | ||
203 | exports.Texture = Texture; | ||
204 | } | ||
205 | |||
206 | |||