1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20  define(function() {
 21 
 22 /**************************************************************************************************************/
 23 
 24 /** 
 25 	@constructor
 26 	TilePool constructor
 27  */
 28 var TilePool = function(rc)
 29 {
 30 	// Private properties
 31 	var gl = rc.gl;
 32 	var glTextures = [];
 33 	var glBuffers = [];
 34 	var self = this;
 35 	
 36 	// Public properties
 37 	this.numCreatedTextures = 0;
 38 	this.numReusedTextures = 0;
 39 	
 40 	// Private methods
 41 
 42 	/**************************************************************************************************************/
 43 
 44 	/**
 45 		Create a new GL texture
 46 	 */
 47 	var createNewGLTexture = function(image)
 48 	{
 49 		var glTexture = gl.createTexture();
 50 		gl.bindTexture(gl.TEXTURE_2D, glTexture);
 51 		gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
 52 		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
 53 		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
 54 		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
 55 		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
 56 		gl.generateMipmap(gl.TEXTURE_2D);
 57 		
 58 		self.numCreatedTextures++;
 59 		
 60 		return glTexture;
 61 	}
 62 
 63 	/**************************************************************************************************************/
 64 
 65 	/**
 66 		Reuse a GL texture
 67 	 */
 68 	var reuseGLTexture = function(image)
 69 	{
 70 		var glTexture = glTextures.pop();
 71 		gl.bindTexture(gl.TEXTURE_2D, glTexture);
 72 		//gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
 73 		gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
 74 		gl.generateMipmap(gl.TEXTURE_2D);
 75 		
 76 		self.numReusedTextures++;
 77 		
 78 		return glTexture;
 79 	}
 80 		
 81 	// Public methods
 82 	
 83 	/**************************************************************************************************************/
 84 
 85 	/**
 86 		Create a GL texture to be used by a tile
 87 	 */
 88 	this.createGLTexture = function(image)
 89 	{
 90 		if ( glTextures.length > 0 )
 91 		{
 92 			return reuseGLTexture(image);
 93 		}
 94 		else
 95 		{
 96 			return createNewGLTexture(image);
 97 		}
 98 	};
 99 
100 	/**************************************************************************************************************/
101 
102 	/**
103 		Create a GL texture to be used by a tile
104 	 */
105 	this.createGLBuffer = function(vertices)
106 	{
107 		var vb;
108 		if ( glBuffers.length > 0 )
109 		{
110 			vb = glBuffers.pop();
111 		}
112 		else
113 		{
114 			vb = gl.createBuffer();
115 		}
116 		gl.bindBuffer(gl.ARRAY_BUFFER, vb);
117 		gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
118 		
119 		return vb;
120 	};
121 
122 	/**************************************************************************************************************/
123 
124 	/**
125 		Dispose a texture
126 	 */
127 	this.disposeGLTexture = function(texture)
128 	{
129 		glTextures.push(texture);
130 	}
131 
132 	/**************************************************************************************************************/
133 
134 	/**
135 		Dispose a texture
136 	 */
137 	this.disposeGLBuffer = function(buffer)
138 	{
139 		glBuffers.push(buffer);
140 	}
141 
142 	/**************************************************************************************************************/
143 
144 	/**
145 		Dispose all
146 	 */
147 	this.disposeAll = function()
148 	{
149 		for ( var i = 0;  i < glTextures.length; i++ ) {
150 			gl.deleteTexture( glTextures[i] );
151 		}
152 		glTextures.length = 0;
153 		
154 		for ( var i = 0;  i < glBuffers.length; i++ ) {
155 			gl.deleteBuffer( glBuffers[i] );
156 		}
157 		glBuffers.length = 0;
158 	}
159 
160 	/**************************************************************************************************************/	
161 };
162 
163 return TilePool;
164 
165 });
166 
167