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 /** @constructor
 25 	Mesh constructor
 26  */
 27 var Mesh = function(renderContext)
 28 {
 29 	this.renderContext = renderContext;
 30 	this.vertexBuffer = null;
 31 	this.tcoordBuffer = null;
 32 	this.indexBuffer = null;
 33 	this.colorBuffer = null;
 34 	this.numVertices = 0;
 35 	this.mode = renderContext.gl.TRIANGLES;
 36 }
 37 
 38 /**************************************************************************************************************/
 39 
 40 /*
 41 	Mesh setVertices method
 42  */
 43 Mesh.prototype.setVertices = function(vertices)
 44 {
 45 	var gl = this.renderContext.gl;
 46 	if ( this.vertexBuffer == null )
 47 	{
 48 		this.vertexBuffer = gl.createBuffer();
 49 	}
 50 	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
 51 	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
 52 	this.numVertices = vertices.length;
 53 }
 54 
 55 /**************************************************************************************************************/
 56 
 57 /*
 58 	Mesh setTexCoords method
 59  */
 60 Mesh.prototype.setTexCoords = function(tcoords)
 61 {
 62 	var gl = this.renderContext.gl;
 63 	if ( this.tcoordBuffer == null )
 64 	{
 65 		this.tcoordBuffer = gl.createBuffer();
 66 	}
 67 	gl.bindBuffer(gl.ARRAY_BUFFER, this.tcoordBuffer);
 68 	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tcoords), gl.STATIC_DRAW);
 69 }
 70 
 71 /**************************************************************************************************************/
 72 
 73 /*
 74 	Mesh setColors method
 75  */
 76 Mesh.prototype.setColors = function(colors)
 77 {
 78 	var gl = this.renderContext.gl;
 79 	if ( this.colorBuffer == null )
 80 	{
 81 		this.colorBuffer = gl.createBuffer();
 82 	}
 83 	gl.bindBuffer(gl.ARRAY_BUFFER, this.colorBuffer);
 84 	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
 85 }
 86 
 87 /**************************************************************************************************************/
 88 
 89 /*
 90 	Mesh setIndices method
 91  */
 92 Mesh.prototype.setIndices = function(indices)
 93 {
 94 	var gl = this.renderContext.gl;
 95 	if ( this.indexBuffer == null )
 96 	{
 97 		this.indexBuffer = gl.createBuffer();
 98 	}
 99 	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
100 	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
101 	this.numIndices = indices.length;
102 }
103 
104 /**************************************************************************************************************/
105 
106 /*
107 	Convert to wireframe (for debug purposes)
108  */
109 Mesh.prototype.setIndicesToWireframe = function(indices)
110 {
111 	this.mode = this.renderContext.gl.LINES;
112 	
113 	// Convert indices
114 	var wireframeIndices = [];
115 	wireframeIndices.length = 2 * indices.length;
116 	
117 	for ( var i =0;  i < indices.length; i += 3 )
118 	{
119 		wireframeIndices[2*i] = indices[i];
120 		wireframeIndices[2*i+1] = indices[i+1];
121 		
122 		wireframeIndices[2*i+2] = indices[i+1];
123 		wireframeIndices[2*i+3] = indices[i+2];
124 		
125 		wireframeIndices[2*i+4] = indices[i+2];
126 		wireframeIndices[2*i+5] = indices[i];
127 	}
128 	
129 	this.setIndices( wireframeIndices );
130 }
131 
132 /**************************************************************************************************************/
133 
134 /*
135 	Mesh render method
136  */
137 Mesh.prototype.render = function(attributes)
138 {
139 	var gl = this.renderContext.gl;
140 	
141 	// Warning : use quoted strings to access properties of the attributes, to work correclty in advanced mode with closure compiler
142 	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
143 	gl.vertexAttribPointer(attributes['vertex'], 3, gl.FLOAT, false, 0, 0);
144 	if ( attributes.hasOwnProperty('tcoord') )
145 	{
146 		gl.bindBuffer(gl.ARRAY_BUFFER, this.tcoordBuffer);
147 		gl.vertexAttribPointer(attributes['tcoord'], 2, gl.FLOAT, false, 0, 0);
148 	}
149 	if ( attributes.hasOwnProperty('color') )
150 	{
151 		gl.bindBuffer(gl.ARRAY_BUFFER, this.colorBuffer);
152 		gl.vertexAttribPointer(attributes['color'], 4, gl.FLOAT, false, 0, 0);
153 	}
154 	if ( this.indexBuffer ) 
155 	{
156 		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
157 		gl.drawElements(this.mode, this.numIndices, gl.UNSIGNED_SHORT, 0);
158 	}
159 	else
160 	{
161 		gl.drawArrays(this.mode, 0, this.numVertices / 3);
162 	}
163 }
164 
165 /**************************************************************************************************************/
166 
167 /*
168 	Mesh dispose method
169  */
170 Mesh.prototype.dispose = function()
171 {
172 	var gl = this.renderContext.gl;
173 	if ( this.indexBuffer )
174 		gl.deleteBuffer(this.indexBuffer);
175 	if ( this.vertexBuffer )
176 		gl.deleteBuffer(this.vertexBuffer);
177 	if ( this.tcoordBuffer )
178 		gl.deleteBuffer(this.tcoordBuffer);
179 	if ( this.colorBuffer )
180 		gl.deleteBuffer(this.colorBuffer);
181 	
182 	this.indexBuffer = null;
183 	this.vertexBuffer = null;
184 	this.tcoordBuffer = null;
185 	this.colorBuffer = null;
186 }
187 
188 /**************************************************************************************************************/
189 
190 return Mesh;
191 
192 });
193 
194