aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/texture.js
blob: 28dc868ab3b02231b094c3d3dcfd4080ec2d5c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Material = require("js/lib/rdge/materials/material").Material;

///////////////////////////////////////////////////////////////////////
// Class GLTexture
//      GL representation of a texture.
///////////////////////////////////////////////////////////////////////
function Texture( dstWorld )
{
    ///////////////////////////////////////////////////////////////////////
    // Instance variables
    ///////////////////////////////////////////////////////////////////////
	this._texture;

	// texture attributes
	this._texMapName;
	this._wrap;
	this._mips;

	this._srcCanvas;	// the canvas generating the texture map.
	this._dstWorld;		// the world that will use the texture map
	this._dstWorld = dstWorld;

    ///////////////////////////////////////////////////////////////////////
    // Property Accessors
    ///////////////////////////////////////////////////////////////////////
	this.getTexture		= function()	{  return this._texture;	}

	this.setSrcWorld	= function(w)	{  this._srcWorld = w;		}
	this.getSrcWorld	= function()	{  return this._srcWorld;	}

	this.setDstWorld	= function(w)	{  this._dstWorld = w;		}
	this.getDstWorld	= function()	{  return this._dstWorld;	}

    ///////////////////////////////////////////////////////////////////////
    // Methods
    ///////////////////////////////////////////////////////////////////////

	this.loadFromFile = function( texMapName,  wrap,  mips )
	{
		var tex = this._texture;
		this._srcCanvas = null;

		// only load if something has changed
		if (this._texMapName !== texMapName)	// does RDGE allow us to change wrap or mips?
		{
			this._texMapName = texMapName.slice();
			this._wrap = wrap;
			this._mips = mips;

			var dstWorld = this.getDstWorld();
			if (dstWorld)
			{
				var renderer = dstWorld.getRenderer();
				tex = renderer.getTextureByName(texMapName, wrap, mips );
				this._texture = tex;
				dstWorld.textureToLoad( tex );
			}
		}

		return tex;
	}

    var __texCounter = 0;

    /*
	this.loadFromCanvas = function( srcCanvas,  wrap, mips )
	{
		this._texMapName = "GLTexture_" + __texCounter;
		__texCounter++;

		//if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
		var world = this.getDstWorld();
		var renderer = world.getRenderer();

		// set default values for wrap and mips
        if (wrap === undefined)
            wrap = "REPEAT";
        if (mips === undefined)
            mips = true;

		var imageData;
		var width = srcCanvas.width,  height = srcCanvas.height;
		width = 64;  height = 64;	// some even power of 2 for now...

		// create a canvas to be used as the image for the texture map
		var doc = srcCanvas.ownerDocument;
		var dstCanvas = doc.createElement("canvas");
		dstCanvas.width = width;
		dstCanvas.height = height;
		var dstCtx = dstCanvas.getContext("2d");

		var tex;
		var srcCtx = srcCanvas.getContext("2d");
		if (srcCtx)
		{
			tex = renderer.getTextureByName(this._texMapName, wrap, mips );
			imageData = srcCtx.getImageData( 0, 0, width, height );
		    dstCtx.putImageData( imageData, 0, 0 );
		}
		else
		{
			tex = renderer.getTextureByName(this._texMapName, wrap, mips );
			//tex = world.getGLContext().createTexture();
			tex.image = new Image;
			tex.wrap = wrap;
			tex.mips = mips;

			srcCtx = srcCanvas.getContext("experimental-webgl");
			if (srcCtx)
			{
//				var data = new Uint8Array(width * height * 4);
//				srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
//				console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] );
//						
//				//imageData.data = data;
//				imageData = dstCtx.createImageData(width, height);
//				var nBytes = width*height*4;
//				for (var i=0;  i<nBytes;  i++)
//					imageData.data[i] = data[i];
//		        dstCtx.putImageData( imageData, 0, 0 );

				var data = new Uint8Array(width * height * 4);
				srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
                for (var i=1;  i<10;  i++)
                {
				    console.log( "row " + i + ": " + data[i*4*width] + ", " + data[i*4*width+1] + ", " + data[i*4*width+2] + ", " + data[i*4*width+3] );
                }

                dstCtx.drawImage(srcCanvas, 0, 0);
			}
		}


		/////////////////
		tex.image = dstCanvas;

		this._texture = tex;
		return tex;
	}
    */

    this.loadFromCanvas = function( srcCanvas,  wrap, mips )
	{
		this._srcCanvas = srcCanvas;

		this._texMapName = "GLTexture_" + __texCounter;
		__texCounter++;

		// set default values for wrap and mips
        if (wrap === undefined)
            wrap = "REPEAT";
        if (mips === undefined)
            mips = true;

		// we animate only if the source is an animated GLWorld
		if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel  && srcCanvas.elementModel.shapeModel.GLWorld)
			this._isAnimated = srcCanvas.elementModel.shapeModel.GLWorld._hasAnimatedMaterials;

		// create the texture
		var world = this.getDstWorld();
		tex = world.getGLContext().createTexture();
		this._texture = tex;
		tex.texparams = new _texparams(wrap, mips);	// defined in renderer.js
		tex.image = new Image;

		// create the canvas and context to render into
		var doc = srcCanvas.ownerDocument;
		this._renderCanvas = doc.createElement("canvas");

		// cache whether this is a 2D canvas or 3D canvas
		var srcCtx = srcCanvas.getContext("2d");
		this._is3D = false;
		if (!srcCtx)  this.is3D = true;

		this.rerender();

		return tex;
	}

	this.rerender = function()
	{
		if (!this._srcCanvas)
		{
			console.log( " no source canvas in GLTexture.rerender" );
			return;
		}
		var srcCanvas = this._srcCanvas;

		var world = this.getDstWorld();
		if (!world)
		{
			console.log( "no world in GLTexture.rerender" );
			return;
		}
		var renderer = world.getRenderer();

		var imageData;
		var width = srcCanvas.width,  height = srcCanvas.height;
		if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height))
		{
			width = this.nextHighestPowerOfTwo( width );
			height = this.nextHighestPowerOfTwo( height );
		}

		// create a canvas to be used as the image for the texture map
		var doc = srcCanvas.ownerDocument;
		var renderCanvas = this._renderCanvas;
		if (!renderCanvas)
		{
			console.log( "no render canvas in GLTexture.rerender" );
			return;
		}
		renderCanvas.width = width;
		renderCanvas.height = height;
		var renderCtx = renderCanvas.getContext("2d");

		// create the texture
		var tex = this._texture;
		if (!tex)
		{
			console.log( "no texture in GLTexture.rerender" );
			return;
		}

		var srcCtx;
		if (!this.is3D)
		{
			srcCtx = srcCanvas.getContext("2d");
			imageData = srcCtx.getImageData( 0, 0, width, height );
		}
		else
		{
			srcCtx = srcCanvas.getContext("experimental-webgl");
			if (srcCtx)
			{
				var data = new Uint8Array(width * height * 4);
				srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
						
				imageData = renderCtx.createImageData(width, height);
				var nBytes = width*height*4;
				for (var i=0;  i<nBytes;  i++)
					imageData.data[i] = data[i];
			}
		}
		renderCtx.putImageData( imageData, 0, 0 );

		/////////////////
		tex.image = renderCanvas;
		renderer.commitTexture( tex );

		return tex;
    }

 
	this.isPowerOfTwo = function(x)
	{
		return (x & (x - 1)) == 0;
	}
 
	this.nextHighestPowerOfTwo = function(x)
	{
		--x;
		for (var i = 1; i < 32; i <<= 1) {
			x = x | x >> i;
		}
		return x + 1;
	}
}

if (typeof exports === "object") {
    exports.Texture = Texture;
}