aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/texture.js
diff options
context:
space:
mode:
authorhwc4872012-03-22 13:52:09 -0700
committerhwc4872012-03-22 13:52:09 -0700
commit00cd98e49c959906d7c44bb6adcdef1f3d5148ae (patch)
treefa8c64af563263ad1f27f48e70d45f86a9711b25 /js/lib/rdge/texture.js
parent57cc00a5ef3ab525e54a030d7692b2d9eefaa68b (diff)
downloadninja-00cd98e49c959906d7c44bb6adcdef1f3d5148ae.tar.gz
Merge branch 'master' of github.com:Motorola-Mobility/ninja-internal into Texture
Conflicts: assets/shaders/plasma.frag.glsl js/helper-classes/RDGE/GLLine.js js/helper-classes/RDGE/MaterialsLibrary.js js/lib/drawing/world.js js/lib/geom/circle.js js/lib/geom/rectangle.js js/lib/rdge/materials/flat-material.js js/lib/rdge/materials/material.js js/panels/Materials/Materials.xml js/panels/Materials/materials-popup.reel/materials-popup.js js/preloader/Preloader.js
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r--js/lib/rdge/texture.js149
1 files changed, 149 insertions, 0 deletions
diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js
new file mode 100644
index 00000000..f9b3d4c3
--- /dev/null
+++ b/js/lib/rdge/texture.js
@@ -0,0 +1,149 @@
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 this.loadFromCanvas = function( srcCanvas, wrap, mips )
70 {
71 this._texMapName = "GLTexture_" + this.texCounter;
72 this.texCounter++;
73
74 //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
75 var world = this.getDstWorld();
76 var renderer = world.getRenderer();
77
78 var imageData;
79 var width = srcCanvas.width, height = srcCanvas.height;
80 width = 128; height = 64; // some even power of 2 for now...
81
82 // create a canvas to be used as the image for the texture map
83 var doc = srcCanvas.ownerDocument;
84 var dstCanvas = doc.createElement("canvas");
85 dstCanvas.width = width;
86 dstCanvas.height = height;
87 var dstCtx = dstCanvas.getContext("2d");
88
89 var tex;
90 var srcCtx = srcCanvas.getContext("2d");
91 if (srcCtx)
92 {
93 tex = renderer.getTextureByName(this._texMapName, wrap, mips );
94 imageData = srcCtx.getImageData( 0, 0, width, height );
95 dstCtx.putImageData( imageData, 0, 0 );
96 }
97 else
98 {
99 tex = renderer.getTextureByName(this._texMapName, wrap, mips );
100 //tex = world.getGLContext().createTexture();
101 tex.image = new Image;
102 tex.wrap = wrap;
103 tex.mips = mips;
104
105 srcCtx = srcCanvas.getContext("experimental-webgl");
106 if (srcCtx)
107 {
108// var data = new Uint8Array(width * height * 4);
109// srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data);
110// console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] );
111//
112// //imageData.data = data;
113// imageData = dstCtx.createImageData(width, height);
114// var nBytes = width*height*4;
115// for (var i=0; i<nBytes; i++)
116// imageData.data[i] = data[i];
117// dstCtx.putImageData( imageData, 0, 0 );
118
119 dstCtx.drawImage(srcCanvas, 0, 0);
120 }
121 }
122
123
124 /////////////////
125 tex.image = dstCanvas;
126
127 this._texture = tex;
128 return tex;
129 }
130
131 this.findPreviousWorld = function()
132 {
133 var prevWorld;
134 for ( var w in _worldStack )
135 {
136 world = _worldStack[w];
137 if (world == this.getWorld()) return prevWorld;
138 prevWorld = world;
139 }
140 }
141
142 var texCounter = 0;
143}
144
145if (typeof exports === "object") {
146 exports.Texture = Texture;
147}
148
149