PageRenderTime 113ms CodeModel.GetById 50ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/components/visibleComponents/shapes/GeometryInfo.java

http://mt4j.googlecode.com/
Java | 1076 lines | 431 code | 137 blank | 508 comment | 83 complexity | 75d40b517742930f26d0a0c83ffec95d MD5 | raw file
   1/***********************************************************************
   2 * mt4j Copyright (c) 2008 - 2009, C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
   3 *  
   4 *   This program is free software: you can redistribute it and/or modify
   5 *   it under the terms of the GNU General Public License as published by
   6 *   the Free Software Foundation, either version 3 of the License, or
   7 *   (at your option) any later version.
   8 *
   9 *   This program is distributed in the hope that it will be useful,
  10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 *   GNU General Public License for more details.
  13 *
  14 *   You should have received a copy of the GNU General Public License
  15 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16 *
  17 ***********************************************************************/
  18package org.mt4j.components.visibleComponents.shapes;
  19
  20import java.nio.FloatBuffer;
  21import java.nio.IntBuffer;
  22
  23import javax.media.opengl.GL;
  24
  25import org.mt4j.MTApplication;
  26import org.mt4j.components.visibleComponents.StyleInfo;
  27import org.mt4j.util.MT4jSettings;
  28import org.mt4j.util.math.Tools3D;
  29import org.mt4j.util.math.ToolsBuffers;
  30import org.mt4j.util.math.ToolsVBO;
  31import org.mt4j.util.math.Vector3D;
  32import org.mt4j.util.math.Vertex;
  33
  34import processing.core.PApplet;
  35
  36/**
  37 * A class which holds the vertices and eventually also
  38 * the normals, colors, indices, displaylist ids,and vbo ids of the
  39 * geometry.
  40 * 
  41 * @author C.Ruff
  42 */
  43public class GeometryInfo {
  44	
  45	/** The r. */
  46	private PApplet r;
  47	
  48//	Vertices Stuff	\\
  49	
  50	/** The vertices local. */
  51	private Vertex[] vertices;
  52	
  53	/** The normals. */
  54	private Vector3D[] normals;
  55	
  56	/** The indices. */
  57	private int[] indices; 
  58	
  59	/** The vert buff. */
  60	private FloatBuffer vertBuff;
  61	
  62	/** The color buff. */
  63	private FloatBuffer colorBuff;
  64	
  65	/** The stroke col buff. */
  66	private FloatBuffer strokeColBuff;
  67	
  68	/** The tex buff. */
  69	private FloatBuffer texBuff;
  70	
  71	/** The normals buff. */
  72	private FloatBuffer normalsBuff; 
  73	
  74	/** The indices buff. */
  75	private IntBuffer 	indicesBuff;
  76	
  77	//	 Pure GL VBO indices names \\	
  78	/** The vbo vertices id. */
  79	private int vboVerticesID;
  80	
  81	/** The vbo color id. */
  82	private int vboColorID;
  83	
  84	/** The vbo texture id. */
  85	private int vboTextureID;
  86	
  87	/** The vbo stroke col id. */
  88	private int vboStrokeColID;
  89	
  90	/** The vbo normals id. */
  91	private int vboNormalsID; 	
  92	
  93	// Display list ids
  94	/** The display list i ds. */
  95	private int[] displayListIDs;
  96
  97	/** The indexed. */
  98	private boolean indexed;
  99	
 100	/** The contains normals. */
 101	private boolean containsNormals;
 102	
 103	private boolean textureCoordsNormalized = true;
 104	
 105	
 106	/**
 107	 * Creates a new GeometryInfo.
 108	 * <br>As only vertices are supplied,
 109	 * the normals (+ normal buffers, normal vbos) will be null.
 110	 * <br>The indices array and indices buffer will also be null.
 111	 * 
 112	 * @param pApplet the applet
 113	 * @param vertices the vertices
 114	 */
 115	public GeometryInfo(PApplet pApplet, Vertex[] vertices){
 116		this(pApplet, vertices, null, null);
 117	}
 118	
 119	/**
 120	 * Instantiates a new geometry info.
 121	 * 
 122	 * @param pApplet the applet
 123	 * @param vertices the vertices
 124	 * @param normals the normals
 125	 */
 126	public GeometryInfo(PApplet pApplet, Vertex[] vertices, Vector3D[] normals){
 127		this(pApplet, vertices, normals, null);
 128	}
 129	
 130	/**
 131	 * Instantiates a new geometry info.
 132	 * 
 133	 * @param pApplet the applet
 134	 * @param vertices the vertices
 135	 * @param indices the indices
 136	 */
 137	public GeometryInfo(PApplet pApplet, Vertex[] vertices, int[] indices){
 138		this(pApplet, vertices, null, indices);
 139	}
 140
 141	/**
 142	 * Creates a new GeometryInfo with vertices, normals and indices.
 143	 * <br>The number of normals should match the number of indices or vertices.
 144	 * 
 145	 * @param pApplet the applet
 146	 * @param vertices the vertices
 147	 * @param normals the normals
 148	 * @param indices the indices
 149	 */
 150	public GeometryInfo(PApplet pApplet, Vertex[] vertices, Vector3D[] normals, int[] indices){
 151		this.r = pApplet;
 152		//VBO Ids
 153		this.vboVerticesID 	= -1;
 154		this.vboColorID 	= -1;
 155		this.vboTextureID 	= -1;
 156		this.vboStrokeColID = -1;
 157		this.vboNormalsID 	= -1;
 158		//Displaylist Ids
 159		this.displayListIDs = new int[]{-1, -1};
 160		if (!(vertices.length > 0)){
 161//			System.err.println("Warning in " + this + " : trying to create GeometryInfo with no vertices supplied!");
 162		}
 163		this.reconstruct(vertices, normals, indices, false, false, null);
 164	}
 165	
 166	
 167	/**
 168	 * Reconstructs the geometry with the given parameters.
 169	 * Normals, indices and styleinfo may be null.
 170	 * 
 171	 * @param vertices the vertices
 172	 * @param normals the normals
 173	 * @param indices the indices
 174	 * @param createOrUpdateOGLBuffers the create or update ogl buffers
 175	 * @param createOrUpdateVBO the create or update vbo
 176	 * @param styleInfo the style info
 177	 */
 178	public void reconstruct(
 179			Vertex[] 	vertices, 
 180			Vector3D[] 	normals, 
 181			int[] 		indices, 
 182			boolean 	createOrUpdateOGLBuffers, 
 183			boolean 	createOrUpdateVBO, 
 184			StyleInfo 	styleInfo
 185		){
 186		this.vertices = vertices;
 187		
 188		//Set the indices and normals, 
 189		//also creates buffers and vbos if createOrUpdateOGLBuffers, createOrUpdateVBO are set
 190		this.setIndices(indices, createOrUpdateOGLBuffers);
 191		this.setNormals(normals, createOrUpdateOGLBuffers, createOrUpdateVBO);
 192		
 193		if (createOrUpdateOGLBuffers){
 194			if (styleInfo == null){
 195				styleInfo = new StyleInfo();
 196			}
 197			
 198			//Create new Buffers for verts, color, stroke color, and texture buffers
 199			this.generateNewVertsColStrokeColTexBuffers(styleInfo);
 200			
 201			if (createOrUpdateVBO){ 
 202				//Generate or update the VBOs
 203				this.generateOrUpdateVertColStrokeColTexVBOs();
 204			}
 205		}
 206	}
 207	
 208	
 209	
 210	
 211	//////// INDICES STUFF //////////////////
 212	/**
 213	 * Adds indices to the geometry. Marks the geometry to be indexed. (isIndexed() returns true)
 214	 * <br>If useopenGL is true, a IntBuffer is also created for use with OpenGl.
 215	 * If the indices array is != null, the geometry will return true at isIndexed() afterwards
 216	 * 
 217	 * @param indices the indices
 218	 * @param createOrUpdateOGLBuffers the create or update ogl buffers
 219	 */
 220	public void setIndices(int[] indices, boolean createOrUpdateOGLBuffers/*, boolean createOrUpdateVBO*/) {
 221		if (indices != null && indices.length > 0){
 222			this.setIndexed(true);
 223			this.indices = indices;
 224			
 225			if (MT4jSettings.getInstance().isOpenGlMode() && createOrUpdateOGLBuffers){
 226				//Set Buffer and maybe EBO //TODO create EBO Element Buffer Object?
 227				this.setIndicesBuffer(ToolsBuffers.generateIndicesBuffer(indices));
 228			}
 229		}else{
 230			this.setIndexed(false);
 231		}
 232	}
 233	
 234	/**
 235	 * Gets the indices.
 236	 * 
 237	 * @return the indices
 238	 * 
 239	 * the array if indices
 240	 */
 241	public int[] getIndices(){
 242		return this.indices;
 243	}
 244
 245	/**
 246	 * Sets the indexed.
 247	 * 
 248	 * @param b the new indexed
 249	 */
 250	private void setIndexed(boolean b) {
 251		this.indexed = b;
 252	}
 253	
 254	/**
 255	 * Returns true, if an indices array for the geometry has been set.
 256	 * 
 257	 * @return true, if checks if is indexed
 258	 * 
 259	 * true, if indexed
 260	 */
 261	public boolean isIndexed(){
 262		return this.indexed;
 263	}
 264	//////// INDICES STUFF //////////////////
 265	
 266	
 267	
 268	
 269	//////// NORMALS STUFF //////////////////
 270	/**
 271	 * Adds normals to the geometry info.
 272	 * <br>Also creates/updates the buffers and vbos of the normals if the booleans are set.
 273	 * <br>If the normal vector is != null, the geometry will return true at isContainsNormals() afterwards
 274	 * 
 275	 * @param normals the normals
 276	 * @param createOrUpdateOGLBuffers the create or update ogl buffers
 277	 * @param createOrUpdateVBO the create or update vbo
 278	 */
 279	public void setNormals(Vector3D[] normals, boolean createOrUpdateOGLBuffers, boolean createOrUpdateVBO) {
 280		if (normals != null && normals.length > 0){
 281			this.setContainsNormals(true);
 282			//Set the normal array and say that the geometry contains normals
 283			this.normals = normals;
 284			//Set Buffer and maybe VBO
 285			if (MT4jSettings.getInstance().isOpenGlMode() 
 286				&& createOrUpdateOGLBuffers
 287				){
 288				this.setNormalsBuffer(ToolsBuffers.generateNormalsBuffer(normals));
 289				
 290				if (createOrUpdateVBO){
 291					if (this.getVBONormalsName() == -1){ 
 292						//Create new normal vbo
 293						this.vboNormalsID = ToolsVBO.generateNormalsVBO(this.r, this.getNormalsBuff(), this.getNormals().length);
 294					}else{
 295						//Update normals vbo
 296						this.updateNormalsVBO(this.getNormalsBuff(), false, false);
 297					}
 298				}
 299				
 300			}
 301			
 302			//If the geometry isnt indexed, the number of normals should match the number of vertices!		
 303			if (!this.isIndexed() && normals.length != this.getVertexCount()){
 304				System.err.println("WARNING: The number of normal vectors supplied (to " + this + ")  isnt equal to the number of vertices!" +
 305						"\n Normals: " + normals.length + " Vertices: " + this.getVertexCount());
 306			}
 307		}else{
 308			this.setContainsNormals(false);
 309		}
 310	}
 311	
 312	/**
 313	 * Gets the normals.
 314	 * 
 315	 * @return the normals
 316	 * 
 317	 * The array of normal vectors
 318	 */
 319	public Vector3D[] getNormals() {
 320		return this.normals;
 321	}
 322
 323	/**
 324	 * Sets the contains normals.
 325	 * 
 326	 * @param b the new contains normals
 327	 */
 328	private void setContainsNormals(boolean b){
 329		this.containsNormals = b;
 330	}
 331	
 332	/**
 333	 * Returns true, if a normals array for the geometry has been set.
 334	 * 
 335	 * @return true, if checks if is contains normals
 336	 * 
 337	 * true, if the geometry contains normals
 338	 */
 339	public boolean isContainsNormals(){
 340		return this.containsNormals;
 341	}
 342	//////// NORMALS STUFF //////////////////
 343	
 344	
 345	/**
 346	 * (Re-)Generates buffers for use with gl.drawElements or gl.drawArrays in opengl mode.
 347	 * <p>Generates:
 348	 * <li>Vertex-
 349	 * <li>Color-
 350	 * <li>StrokeColor-
 351	 * <li>Texture-
 352	 * Buffers.
 353	 * <br><strong>NOTE:</strong>DOESENT CREATE A NORMAL OR INDEX BUFFER! THIS IS DONE WITH THE setNormals()/setIndices() METHODs!
 354	 * 
 355	 * @param styleInfo the style info
 356	 */
 357	private void generateNewVertsColStrokeColTexBuffers(StyleInfo styleInfo){
 358			this.generateDefaultVertexBuffer();
 359			this.generateDefaultColorBuffer();
 360			this.generateDefaultStrokeColorBuffer(styleInfo);
 361			this.generateDefaultTextureBuffer();
 362	}
 363	
 364	/**
 365	 * Updates all draw buffers with the current settings
 366	 * 
 367	 * @param styleInfo the style info
 368	 */
 369	public void generateOrUpdateBuffersLocal(StyleInfo styleInfo){
 370////		if (this.getVertBuff() == null){
 371//			this.generateDefaultVertexBuffer();
 372////		}
 373////		if (this.getColorBuff() == null){
 374//			this.generateDefaultColorBuffer();
 375////		}
 376////		if (this.getStrokeColBuff() == null){
 377//			this.generateDefaultStrokeColorBuffer(styleInfo);
 378////		}
 379////		if (this.getTexBuff() == null){
 380//			this.generateDefaultTextureBuffer();
 381////		}
 382		this.generateNewVertsColStrokeColTexBuffers(styleInfo);
 383		
 384		if (this.isContainsNormals()){
 385//			if (this.getNormalsBuff() == null){
 386				this.setNormals(this.getNormals(), true, false);
 387//			}
 388		}
 389		if (this.isIndexed()){
 390//			if (this.getIndexBuff() == null){
 391				this.setIndices(this.getIndices(), true);
 392//			}
 393		}
 394	}
 395	
 396	/**
 397	 * Generate default vertex buffer.
 398	 */
 399	private void generateDefaultVertexBuffer(){
 400		this.setVertexBuffer(ToolsBuffers.generateVertexBuffer(this.getVertices()));
 401	}
 402	
 403	/**
 404	 * Generate default color buffer.
 405	 */
 406	private void generateDefaultColorBuffer(){
 407		this.setColorBuffer(ToolsBuffers.generateColorBuffer(this.getVertices()));
 408	}
 409	
 410	/**
 411	 * Generate default stroke color buffer.
 412	 * 
 413	 * @param styleInfo the style info
 414	 */
 415	private void generateDefaultStrokeColorBuffer(StyleInfo styleInfo){
 416		this.setStrokeColorBuffer(ToolsBuffers.generateStrokeColorBuffer(this.getVertices().length, styleInfo.getStrokeRed(), styleInfo.getStrokeGreen(), styleInfo.getStrokeBlue(), styleInfo.getStrokeAlpha()));
 417	}
 418	
 419	/**
 420	 * Generate default stroke color buffer.
 421	 * 
 422	 * @param r the r
 423	 * @param g the g
 424	 * @param b the b
 425	 * @param a the a
 426	 */
 427	private void generateDefaultStrokeColorBuffer(float r, float g, float b, float a){
 428		this.setStrokeColorBuffer(ToolsBuffers.generateStrokeColorBuffer(this.getVertices().length, r,g,b,a));
 429	}
 430	
 431	/**
 432	 * Generate default texture buffer.
 433	 */
 434	private void generateDefaultTextureBuffer(){
 435		this.setTextureBuffer(ToolsBuffers.generateTextureBuffer(this.getVertices()));
 436	}
 437	
 438	/**
 439	 * Sets the vertex buffer.
 440	 * 
 441	 * @param vertBuff the new vertex buffer
 442	 */
 443	private void setVertexBuffer(FloatBuffer vertBuff){
 444		this.vertBuff = vertBuff;
 445	}
 446	
 447	/**
 448	 * Sets the color buffer.
 449	 * 
 450	 * @param colorBuff the new color buffer
 451	 */
 452	private void setColorBuffer(FloatBuffer colorBuff){
 453		this.colorBuff = colorBuff;
 454	}
 455	
 456	/**
 457	 * Sets the stroke color buffer.
 458	 * 
 459	 * @param strokeColBuff the new stroke color buffer
 460	 */
 461	private void setStrokeColorBuffer(FloatBuffer strokeColBuff){
 462		this.strokeColBuff = strokeColBuff;
 463	}
 464	
 465	/**
 466	 * Sets the texture buffer.
 467	 * 
 468	 * @param texBuff the new texture buffer
 469	 */
 470	private void setTextureBuffer(FloatBuffer texBuff){
 471		this.texBuff = texBuff;
 472	}
 473	
 474	/**
 475	 * Sets the normals buffer.
 476	 * 
 477	 * @param normBuff the new normals buffer
 478	 */
 479	private void setNormalsBuffer(FloatBuffer normBuff){
 480		this.normalsBuff = normBuff;
 481	}
 482	
 483	/**
 484	 * Sets the indices buffer.
 485	 * 
 486	 * @param indicesBuff the new indices buffer
 487	 */
 488	private void setIndicesBuffer(IntBuffer indicesBuff){
 489		this.indicesBuff = indicesBuff;
 490	}
 491	
 492	/////////////// BUFFERS GETTER //////////////////////
 493	/**
 494	 * Gets the color buff.
 495	 * 
 496	 * @return the color buff
 497	 */
 498	public FloatBuffer getColorBuff() {
 499		return this.colorBuff;
 500	}
 501	
 502	/**
 503	 * Gets the index buff.
 504	 * 
 505	 * @return the index buff
 506	 */
 507	public IntBuffer getIndexBuff() {
 508		return this.indicesBuff;
 509	}
 510	
 511	/**
 512	 * Gets the stroke col buff.
 513	 * 
 514	 * @return the stroke col buff
 515	 */
 516	public FloatBuffer getStrokeColBuff() {
 517		return this.strokeColBuff;
 518	}
 519	
 520	/**
 521	 * Gets the tex buff.
 522	 * 
 523	 * @return the tex buff
 524	 */
 525	public FloatBuffer getTexBuff() {
 526		return this.texBuff;
 527	}
 528	
 529	/**
 530	 * Gets the vert buff.
 531	 * 
 532	 * @return the vert buff
 533	 */
 534	public FloatBuffer getVertBuff() {
 535		return this.vertBuff;
 536	}
 537	
 538	/**
 539	 * Gets the normals buff.
 540	 * 
 541	 * @return the normals buff
 542	 */
 543	public FloatBuffer getNormalsBuff() {
 544		return this.normalsBuff;
 545	}
 546	/////////////// BUFFERS GETTER //////////////////////
 547	
 548	
 549	/////////////// VBO GENERATING //////////////////////
 550	/**
 551	 * Generates Vertex Buffer Objects (VBO)
 552	 * from the local buffers
 553	 * for Vertex, Texture, Color and StrokeColor.
 554	 * <b>CREATES THEM ONLY IF THEY DONT EXIST YET!
 555	 * IF THEY EXIST; THEY ARE UPDATED FROM THE BUFFERS!
 556	 * <p>
 557	 * If the geometry had vbos already, we should delete them first
 558	 * usually.
 559	 */
 560	private void generateOrUpdateVertColStrokeColTexVBOs(){
 561		PApplet pa 		= this.getRenderer();
 562		int vertexCount = this.getVertexCount();
 563		//If no vbos exist yet, create them now
 564		if (this.getVBOVerticesName() 	== -1){
 565			this.vboVerticesID 	= ToolsVBO.generateVertexVBO(pa, this.getVertBuff(), vertexCount);
 566		}else{
 567			this.updateVertexVBO(this.getVertBuff(), false, false);
 568		}
 569		if (this.getVBOColorName() 		== -1){
 570			this.vboColorID 	= ToolsVBO.generateColorVBO(pa, this.getColorBuff(), vertexCount);
 571		}else{
 572			this.updateColorVBO(this.getColorBuff());
 573		}
 574		if (this.getVBOStrokeColorName()== -1){
 575			this.vboStrokeColID = ToolsVBO.generateStrokeColorVBO(pa, this.getStrokeColBuff(), vertexCount);
 576		}else{
 577			this.updateStrokeColorVBO(this.getStrokeColBuff());
 578		}
 579		if(this.getVBOTextureName()	== -1){
 580			this.vboTextureID 	= ToolsVBO.generateTextureVBO(pa, this.getTexBuff(), vertexCount);
 581		}else{
 582			this.updateTextureVBO(this.getTexBuff());
 583		}
 584	}
 585	
 586	
 587	/**
 588	 * Generates Vertex Buffer Objects (VBO)
 589	 * from the local buffers
 590	 * for Vertex, Texture, Color, StrokeColor and Normals.
 591	 * <b>CREATES THEM ONLY IF THEY DONT EXIST YET!
 592	 * <p>
 593	 * If the geometry had vbos already, we should delete them first
 594	 * usually.
 595	 */
 596	public void generateOrUpdateAllVBOs(){
 597		//If no normals vbo exists, create it now
 598		if (this.isContainsNormals()){
 599			PApplet pa = this.getRenderer();
 600			if (this.getVBONormalsName() == -1){
 601				this.vboNormalsID 	= ToolsVBO.generateNormalsVBO(pa, this.getNormalsBuff(), this.getNormals().length);
 602			}else{
 603				this.updateNormalsVBO(this.getNormalsBuff(), false, false);
 604			}
 605		}
 606		//Generate/Update other VBOs
 607		this.generateOrUpdateVertColStrokeColTexVBOs();
 608	}
 609	/////////////// VBO GENERATING //////////////////////
 610	
 611	
 612	/////////////// VBO UPDATING //////////////////////
 613//	/**
 614//	 * Updates all vbos with the current buffers.
 615//	 * <br>Will crash if the vbos havent been creates yet.
 616//	 * <br>To create VBOs, use <code>generateAllVbos()</code>
 617//	 */
 618//	public void updateAllVbosLocal() {
 619//		try {
 620//			PApplet pa 		= this.getRenderer();
 621//			int vertexCount = this.getVertexCount();
 622//			ToolsVBO.updateVertexVBO(pa, this.getVertBuff(), vertexCount, this.vboVerticesID);
 623//			ToolsVBO.updateTextureVBO(pa, this.getTexBuff(), vertexCount, this.vboTextureID);
 624//			ToolsVBO.updateColorVBO(pa, this.getColorBuff(), vertexCount, this.vboColorID);
 625//			ToolsVBO.updateStrokeColorVBO(pa, this.getStrokeColBuff(), vertexCount, this.vboStrokeColID);
 626//			
 627//			if (this.isContainsNormals()){
 628//				ToolsVBO.updateNormalsVBO(pa, this.getNormalsBuff(), this.normals.length, this.vboNormalsID);
 629//			}
 630//		} catch (Exception e) {
 631//			e.printStackTrace();
 632//		}
 633//	}
 634	
 635	/**
 636	 * Updates the vertex buffer objects and sets the specified vertexbuffer to the geometryinfo.
 637	 * If setAsNewVertexBuffer is set to true, this also updates the vertex array. (not cheap but the geometry
 638	 * may act inconsistent if the arrays are not also updated - depends on usage).
 639	 * <br>If we only want the vbo to be updated we set both booleans to false
 640	 * 
 641	 * @param vertexBuffer the vertex buffer
 642	 * @param setAsNewVertexBuffer the set as new vertex buffer
 643	 * @param setAsNewVertexArray the set as new vertex array
 644	 */
 645	public void updateVertexVBO(FloatBuffer vertexBuffer, boolean setAsNewVertexBuffer , boolean setAsNewVertexArray){
 646		if (setAsNewVertexArray)
 647			this.vertices = ToolsBuffers.getVertexArray(vertexBuffer);
 648		//FIXME size correctly calculated?
 649		if (setAsNewVertexBuffer)
 650			this.setVertexBuffer(vertexBuffer);
 651		ToolsVBO.updateVertexVBO(this.getRenderer(), vertexBuffer, vertexBuffer.capacity()/(3 /*3 v array data per vertex */ * 4 /*when buffer created v array * 4*/), this.getVBOVerticesName()); 
 652	}
 653	
 654	/**
 655	 * Update texture vbo.
 656	 * 
 657	 * @param textureBuffer the texture buffer
 658	 */
 659	public void updateTextureVBO(FloatBuffer textureBuffer){
 660		this.setTextureBuffer(textureBuffer);
 661		//FIXME size correctly calculated?
 662		ToolsVBO.updateTextureVBO(this.getRenderer(), textureBuffer, textureBuffer.capacity()/(2 /*2 col data per vertex */ * 4 /*when buffer created v array * 4*/), this.getVBOTextureName()); 
 663	}
 664	
 665	/**
 666	 * Update color vbo.
 667	 * 
 668	 * @param colorBuffer the color buffer
 669	 */
 670	public void updateColorVBO(FloatBuffer colorBuffer){
 671		this.setColorBuffer(colorBuffer);
 672		//FIXME size correctly calculated?
 673		ToolsVBO.updateColorVBO(this.getRenderer(), colorBuffer, colorBuffer.capacity()/(4 /*4 col data per vertex */ * 4 /*when buffer created v array * 4*/), this.getVBOColorName());
 674	}
 675	
 676	/**
 677	 * Update stroke color vbo.
 678	 * 
 679	 * @param strokeColorBuffer the stroke color buffer
 680	 */
 681	public void updateStrokeColorVBO(FloatBuffer strokeColorBuffer){
 682		this.setStrokeColorBuffer(strokeColorBuffer);
 683		//FIXME size correctly calculated?
 684		ToolsVBO.updateStrokeColorVBO(this.getRenderer(), strokeColorBuffer, strokeColorBuffer.capacity()/(4 /*4 col data per vertex */ * 4 /*when buffer created v array * 4*/), this.getVBOStrokeColorName());
 685	}
 686	
 687	/**
 688	 * Update normals vbo.
 689	 * 
 690	 * @param normalsBuffer the normals buffer
 691	 * @param setAsNewNormalBuffer the set as new normal buffer
 692	 * @param setAsNewNormalArray the set as new normal array
 693	 */
 694	public void updateNormalsVBO(FloatBuffer normalsBuffer, boolean setAsNewNormalBuffer, boolean setAsNewNormalArray){
 695		if (setAsNewNormalArray)
 696			this.setNormals(ToolsBuffers.getVector3DArray(normalsBuffer), false, false);
 697		if (setAsNewNormalBuffer)
 698			this.setNormalsBuffer(normalsBuffer);
 699		//FIXME size correctly calculated?
 700		ToolsVBO.updateNormalsVBO(this.getRenderer(), normalsBuffer, normalsBuffer.capacity()/(3 /*3 v array data per vertex */ * 4 /*when buffer created v array * 4*/), this.getVBONormalsName()); 
 701	}
 702	/////////////// VBO UPDATING //////////////////////
 703	
 704	
 705	/**
 706	 * Deletes all VBOs of the geometry.
 707	 */
 708	public void deleteAllVBOs(){
 709		if (MT4jSettings.getInstance().isOpenGlMode()){
 710			GL gl = Tools3D.getGL(r);
 711			if (this.getVBOVerticesName() != -1){
 712				gl.glDeleteBuffersARB(1, new int[]{this.getVBOVerticesName()},0);
 713				this.vboVerticesID = -1;
 714			}
 715			if (this.getVBOColorName() != -1){
 716				gl.glDeleteBuffersARB(1, new int[]{this.getVBOColorName()},0);
 717				this.vboColorID = -1;
 718			}
 719			if (this.getVBOStrokeColorName() != -1){
 720				gl.glDeleteBuffersARB(1, new int[]{this.getVBOStrokeColorName()},0);
 721				this.vboStrokeColID = -1;
 722			}
 723			if (this.getVBOTextureName() != -1){
 724				gl.glDeleteBuffersARB(1, new int[]{this.getVBOTextureName()},0);
 725				this.vboTextureID = -1;
 726			}
 727			if (this.getVBONormalsName() != -1){
 728				gl.glDeleteBuffersARB(1, new int[]{this.getVBONormalsName()},0);
 729				this.vboNormalsID = -1;
 730			}
 731		}
 732	}
 733
 734	
 735	//////////////// VBO GETTERS //////////////////////
 736	/**
 737	 * Gets the vBO vertices name.
 738	 * 
 739	 * @return the vBO vertices name
 740	 */
 741	public int getVBOVerticesName(){
 742		return this.vboVerticesID;
 743	}
 744	
 745	/**
 746	 * Gets the vBO color name.
 747	 * 
 748	 * @return the vBO color name
 749	 */
 750	public int getVBOColorName(){
 751		return this.vboColorID;
 752	}
 753	
 754	/**
 755	 * Gets the vBO texture name.
 756	 * 
 757	 * @return the vBO texture name
 758	 */
 759	public int getVBOTextureName(){
 760		return this.vboTextureID;
 761	}
 762	
 763	/**
 764	 * Gets the vBO stroke color name.
 765	 * 
 766	 * @return the vBO stroke color name
 767	 */
 768	public int getVBOStrokeColorName(){
 769		return this.vboStrokeColID;
 770	}
 771	
 772	/**
 773	 * Gets the vBO normals name.
 774	 * 
 775	 * @return the vBO normals name
 776	 */
 777	public int getVBONormalsName(){
 778		return this.vboNormalsID;
 779	}
 780	////////////////VBO GETTERS //////////////////////
 781	
 782
 783	
 784	
 785	
 786	//////////////// DISPLAY LISTS //////////////////////
 787	/**
 788	 * Generates 2 openGL display lists for drawing this shape.
 789	 * <br>One for the interior (with textures etc.) and
 790	 * one for drawing the outline.
 791	 * <br><code>setUseDirectGL</code> has to be set to true first!
 792	 * <br>To use the display lists for drawing, call <code>setUseDisplayList()</code>
 793	 * <br>NOTE: if a display list already existed, we should delete that first!
 794	 * 
 795	 * @param useTexture the use texture
 796	 * @param texture the texture
 797	 * @param fillDrawMode the fill draw mode
 798	 * @param drawSmooth the draw smooth
 799	 * @param strokeWeight the stroke weight
 800	 */
 801//	public void generateDisplayLists(boolean useTexture, PImage texture, int fillDrawMode, boolean drawSmooth, float strokeWeight){
 802	public boolean generateDisplayLists(AbstractShape shape, boolean genFillList, boolean genStrokeList){
 803//		this.setDisplayListIDs(Tools3D.generateDisplayLists(
 804//				this.getRenderer(), fillDrawMode, this.getVertBuff(), this.getTexBuff(), 
 805//				this.getColorBuff(), this.getStrokeColBuff(), this.getIndexBuff(),
 806//				useTexture, texture, drawSmooth, strokeWeight));
 807		//TODO test - automaticall delete old display list before
 808		this.deleteDisplayLists();
 809		
 810//		this.setDisplayListIDs(Tools3D.generateDisplayLists(this.getRenderer(), shape.getFillDrawMode(), this, shape.isTextureEnabled(),  shape.getTexture(), shape.isDrawSmooth(), shape.getStrokeWeight()));
 811		
 812//		/*
 813		int[] displayListIDs = new int[]{-1,-1};
 814
 815		//Create a new empty displaylist
 816		GL gl = Tools3D.getGL(getRenderer());
 817		int listIDFill = gl.glGenLists(1);
 818		if (listIDFill == 0){
 819			System.err.println("Failed to create fill display list");
 820			return false;
 821		}
 822		int listIDOutline = gl.glGenLists(1);
 823		if (listIDOutline == 0){
 824			System.err.println("Failed to create stroke display list");
 825			return false;
 826		}
 827		
 828		boolean noFillb4 = shape.isNoFill();
 829		boolean noStrokeb4 = shape.isNoStroke();
 830		boolean displayListUsageb4 = shape.isUseDisplayList();
 831		//TODO also vbo?
 832		shape.setUseDisplayList(false);
 833		
 834		if (genFillList){
 835			//Start recording display list
 836			gl.glNewList(listIDFill, GL.GL_COMPILE);
 837			shape.setNoFill(false);
 838			shape.setNoStroke(true);
 839			shape.drawPureGl(gl);
 840			shape.setNoFill(noFillb4);
 841			shape.setNoStroke(noStrokeb4);
 842			//End recording
 843			gl.glEndList();
 844			displayListIDs[0] = listIDFill;
 845		}
 846		
 847		if (genStrokeList){
 848		//Start recording display list
 849		gl.glNewList(listIDOutline, GL.GL_COMPILE);
 850		shape.setNoFill(true);
 851		shape.setNoStroke(false);
 852		shape.drawPureGl(gl);
 853		shape.setNoFill(noFillb4);
 854		shape.setNoStroke(noStrokeb4);
 855		//End recording
 856		gl.glEndList();
 857		displayListIDs[1] = listIDOutline;
 858		}
 859
 860		//Set the new display list IDs
 861		setDisplayListIDs(displayListIDs);
 862		
 863		shape.setUseDisplayList(displayListUsageb4);
 864//		 */
 865		return true;
 866	}
 867	
 868	/**
 869	 * Delete the the displaylists of that geometry.
 870	 */
 871	public void deleteDisplayLists(){
 872		if (MT4jSettings.getInstance().isOpenGlMode()){
 873			GL gl = Tools3D.getGL(this.r);
 874			for (int id : this.displayListIDs){
 875				if (id != -1){
 876					gl.glDeleteLists(id, 1);
 877				}
 878			}
 879			this.displayListIDs[0] = -1;
 880			this.displayListIDs[1] = -1;
 881		}
 882	}
 883	
 884	/**
 885	 * Returns the IDs (names) of the display lists if they have
 886	 * been generated! setUseDisplayList has to be called first!.
 887	 * 
 888	 * @return int[2] array where [0] is the list of the fill
 889	 * and [1] the list of the outline
 890	 */
 891	public int[] getDisplayListIDs() {
 892		return this.displayListIDs;
 893	}
 894	
 895	/**
 896	 * Sets the display lists for this shape.
 897	 * <br><strong>The int array has to be of length=2 and
 898	 * contain 2 display list ids, generated with <code>glGenlists</code></strong>
 899	 * 
 900	 * @param ids the ids
 901	 */
 902	public void setDisplayListIDs(int[] ids){
 903		this.displayListIDs = ids;
 904	}
 905	////////////////DISPLAY LISTS //////////////////////
 906	
 907	
 908	
 909
 910	
 911	/**
 912	 * Returns the vertices of this shape without any transformations applied
 913	 * <br> <b>Caution:</b> If you alter them in anyway, changes will only
 914	 * be consistent by calling the setNewVertices() method!.
 915	 * 
 916	 * @return the untransformed vertices
 917	 */
 918	public Vertex[] getVertices(){
 919		return this.vertices;
 920	}
 921	
 922	
 923	
 924	
 925	//////////////// VERTEX COLORS  //////////////////////
 926	//Methods that set the vertex colors for all vertices
 927	
 928	//TODO change the vertex color AND the Color Buffers (+vbo) in the
 929	//same loop for speed!
 930	
 931	/**
 932	 * Sets the vertices color all.
 933	 * 
 934	 * @param r the r
 935	 * @param g the g
 936	 * @param b the b
 937	 * @param a the a
 938	 */
 939	public void setVerticesColorAll(float r, float g, float b, float a){
 940		for (Vertex vertex : this.getVertices()){
 941			vertex.setR(r);
 942			vertex.setG(g);
 943			vertex.setB(b);
 944			vertex.setA(a);
 945		}
 946		
 947		//Dont always create a new buffer -> update old one if possible
 948		if (this.getColorBuff() != null && this.getVertices().length == (this.getColorBuff().limit()/4)){
 949			ToolsBuffers.updateColorBuffer(this.getVertices(), this.getColorBuff());
 950//			System.out.println("UPDATE color buffer");
 951		}else{
 952			this.generateDefaultColorBuffer();
 953//			System.out.println("GENERATE color buffer");
 954		}
 955		
 956		if (this.getVBOColorName() != -1){
 957			this.updateColorVBO(this.getColorBuff());
 958		}
 959	}
 960	
 961	
 962	
 963	///// STROKE COLORS ////////////////////////
 964	/**
 965	 * Sets the stroke color all.
 966	 * 
 967	 * @param r the r
 968	 * @param g the g
 969	 * @param b the b
 970	 * @param a the a
 971	 */
 972	public void setStrokeColorAll(float r, float g, float b, float a){
 973		//Dont always create a new buffer -> update old one if possible
 974		if (this.getStrokeColBuff() != null && this.getVertices().length == (this.getStrokeColBuff().limit()/4)){
 975			ToolsBuffers.updateStrokeColorBuffer(this.getStrokeColBuff(), r, g, b, a);
 976//			System.out.println("UPDATE stroke color buffer");
 977		}else{
 978			this.generateDefaultStrokeColorBuffer(r,g,b,a);
 979//			System.out.println("GENERATE stroke color buffer");
 980		}
 981		
 982//		this.generateDefaultStrokeColorBuffer(r,g,b,a);
 983		
 984		if (this.getVBOStrokeColorName() != -1){
 985			this.updateStrokeColorVBO(this.getStrokeColBuff());
 986		}
 987	}
 988	
 989	/**
 990	 * Generates new color buffers for openGL use.
 991	 * <br>This has to be called after
 992	 * manually changing a vertex color without using a method like
 993	 * setFillColor(..) to take effect.
 994	 * <br>Only makes sense when using OPENGL!
 995	 * <br>Doesent update strokecolors!
 996	 */
 997	public void updateVerticesColorBuffer(){
 998		if (MT4jSettings.getInstance().isOpenGlMode()){
 999			//Dont always create a new buffer -> update old one if possible
1000			if (this.getColorBuff() != null && this.getVertices().length == (this.getColorBuff().limit()/4)){
1001				ToolsBuffers.updateColorBuffer(this.getVertices(), this.getColorBuff());
1002//				System.out.println("UPDATE color buffer");
1003			}else{
1004				this.generateDefaultColorBuffer();
1005//				System.out.println("GENERATE color buffer");
1006			}
1007			if (this.getVBOColorName() != -1){
1008				this.updateColorVBO(this.getColorBuff());
1009			}
1010		}
1011	}
1012	
1013	/**
1014	 * Generates new texture buffer for openGL use.
1015	 * <br>This has to be called after
1016	 * manually changing a vertex u,v texture coordinates in the vertex array.
1017	 * <br>Only makes sense when using OPENGL!
1018	 * 
1019	 * @param updateVBO the update vbo
1020	 */
1021	public void updateTextureBuffer(boolean updateVBO){
1022		if (MT4jSettings.getInstance().isOpenGlMode()){
1023			this.generateDefaultTextureBuffer();
1024			if (updateVBO && this.getVBOTextureName() != -1){
1025				this.updateTextureVBO(this.getTexBuff());
1026			}
1027		}
1028	}
1029	//////////////// VERTEX COLORS  //////////////////////
1030
1031	
1032	/**
1033	 * Gets the vertex count.
1034	 * 
1035	 * @return the vertex count
1036	 */
1037	public int getVertexCount(){
1038		return this.vertices.length;
1039	}
1040	
1041	/**
1042	 * Gets the renderer.
1043	 * 
1044	 * @return the renderer
1045	 */
1046	public PApplet getRenderer(){
1047		return this.r;
1048	}
1049
1050	public boolean isTextureCoordsNormalized() {
1051		return this.textureCoordsNormalized;
1052	}
1053	
1054	public void setTextureCoordsNormalized(boolean normalized){
1055		this.textureCoordsNormalized = normalized;
1056	}
1057	
1058	
1059	@Override
1060	protected void finalize() throws Throwable {
1061		//System.out.println("Finalizing GLTEXTURE - " + this);
1062		if (this.r instanceof MTApplication) {
1063			MTApplication mtApp = (MTApplication) this.r;
1064			mtApp.invokeLater(new Runnable() {
1065				public void run() {
1066					deleteDisplayLists();
1067					deleteAllVBOs();
1068				}
1069			});
1070		}else{
1071			//TODO use registerPre()?
1072			//is the object even valid after finalize() is called??
1073		}
1074		super.finalize();
1075	}
1076}