API Documentation
1 /* 2 GLGE WebGL Graphics Engine 3 Copyright (c) 2010, Paul Brunt 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions are met: 8 * Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright 11 notice, this list of conditions and the following disclaimer in the 12 documentation and/or other materials provided with the distribution. 13 * Neither the name of GLGE nor the 14 names of its contributors may be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @fileOverview 31 * @name glge_texturecanvas.js 32 * @author me@paulbrunt.co.uk 33 */ 34 35 36 (function(GLGE){ 37 38 39 40 41 /** 42 * @class A canvase texture to be included in a material 43 * @param {string} uid the unique id for this texture 44 * @see GLGE.Material 45 * @augments GLGE.QuickNotation 46 * @augments GLGE.JSONLoader 47 */ 48 GLGE.TextureCanvas=function(uid){ 49 this.canvas=document.createElement("canvas"); 50 //temp canvas to force chrome to update FIX ME when bug sorted! 51 this.t=document.createElement("canvas"); 52 this.t.width=1; 53 this.t.height=1; 54 GLGE.Assets.registerAsset(this,uid); 55 } 56 GLGE.augment(GLGE.QuickNotation,GLGE.TextureCanvas); 57 GLGE.augment(GLGE.JSONLoader,GLGE.TextureCanvas); 58 GLGE.augment(GLGE.Events,GLGE.TextureCanvas); 59 GLGE.TextureCanvas.prototype.className="TextureCanvas"; 60 GLGE.TextureCanvas.prototype.glTexture=null; 61 GLGE.TextureCanvas.prototype.autoUpdate=true; 62 /** 63 * Gets the auto update flag 64 * @return {boolean} The auto update flag 65 */ 66 GLGE.TextureCanvas.prototype.getAutoUpdate=function(){ 67 return this.autoUpdate; 68 }; 69 /** 70 * Sets the auto update flag 71 * @param {boolean} value The auto update flag 72 */ 73 GLGE.TextureCanvas.prototype.setAutoUpdate=function(value){ 74 this.autoUpdate=value; 75 return this; 76 }; 77 /** 78 * Gets the canvas used by the texture 79 * @return {canvas} The textures image url 80 */ 81 GLGE.TextureCanvas.prototype.getCanvas=function(){ 82 return this.canvas; 83 }; 84 /** 85 * Sets the canvas used by the texture 86 * @param {canvas} canvas The canvas to use 87 */ 88 GLGE.TextureCanvas.prototype.setCanvas=function(canvas){ 89 this.canvas=canvas; 90 return this; 91 }; 92 /** 93 * Sets the canvas height 94 * @param {number} value The canvas height 95 */ 96 GLGE.TextureCanvas.prototype.setHeight=function(value){ 97 this.canvas.height=value; 98 return this; 99 }; 100 /** 101 * Sets the canvas width 102 * @param {number} value The canvas width 103 */ 104 GLGE.TextureCanvas.prototype.setWidth=function(value){ 105 this.canvas.width=value; 106 return this; 107 }; 108 109 /** 110 * gets the canvas height 111 * @returns {number} The canvas height 112 */ 113 GLGE.TextureCanvas.prototype.getHeight=function(){ 114 return this.canvas.height; 115 }; 116 /** 117 * gets the canvas width 118 * @returns {number} The canvas width 119 */ 120 GLGE.TextureCanvas.prototype.getWidth=function(){ 121 return this.canvas.width; 122 }; 123 124 /** 125 * does the canvas texture GL stuff 126 * @private 127 **/ 128 GLGE.TextureCanvas.prototype.doTexture=function(gl){ 129 this.gl=gl; 130 //create the texture if it's not already created 131 if(!this.glTexture){ 132 this.glTexture=gl.createTexture(); 133 gl.bindTexture(gl.TEXTURE_2D, this.glTexture); 134 this.updateCanvas(gl); 135 }else{ 136 gl.bindTexture(gl.TEXTURE_2D, this.glTexture); 137 if(this.autoUpdate || this.doUpdate) this.updateCanvas(gl); 138 } 139 this.doUpdate=false; 140 141 142 return true; 143 } 144 /** 145 * Manually updates the canvas Texture 146 */ 147 GLGE.TextureCanvas.prototype.update=function(){ 148 this.doUpdate=true; 149 } 150 /** 151 * Updates the canvas texture 152 * @private 153 */ 154 GLGE.TextureCanvas.prototype.updateCanvas=function(gl){ 155 var canvas = this.canvas; 156 gl.bindTexture(gl.TEXTURE_2D, this.glTexture); 157 158 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.t); //force chrome to update remove when chrome bug fixed 159 160 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas); 161 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 162 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 163 gl.generateMipmap(gl.TEXTURE_2D); 164 } 165 166 167 })(GLGE);