From 2f66a9ab38f761b7796800bb37f8146f140db40b Mon Sep 17 00:00:00 2001 From: hwc487 Date: Mon, 26 Mar 2012 11:31:23 -0700 Subject: WebGL & Canvas as data for texture maps. --- js/lib/rdge/texture.js | 377 +++++++++++++++++++------------------------------ 1 file changed, 149 insertions(+), 228 deletions(-) (limited to 'js/lib/rdge/texture.js') diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index c72864b8..b349ee62 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -15,267 +15,188 @@ function Texture( dstWorld ) /////////////////////////////////////////////////////////////////////// // Instance variables /////////////////////////////////////////////////////////////////////// - this._texture; + this._texture; - // texture attributes - this._texMapName; - this._wrap; - this._mips; + // 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; + 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.getTexture = function() { return this._texture; } - this.setSrcWorld = function(w) { this._srcWorld = w; } - this.getSrcWorld = function() { return this._srcWorld; } + 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; } + this.setDstWorld = function(w) { this._dstWorld = w; } + this.getDstWorld = function() { return this._dstWorld; } + + this.isAnimated = function() { return this._isAnimated; } /////////////////////////////////////////////////////////////////////// // 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; - } + 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._srcCanvas = srcCanvas; - /* - 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(); + this._texMapName = "GLTexture_" + __texCounter; + __texCounter++; - // set default values for wrap and mips + // 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 1000) nPrint = 1000; - for (var i=0; i> i; - } - return x + 1; - } + this.nextHighestPowerOfTwo = function(x) + { + --x; + for (var i = 1; i < 32; i <<= 1) { + x = x | x >> i; + } + return x + 1; + } } if (typeof exports === "object") { -- cgit v1.2.3