PageRenderTime 39ms CodeModel.GetById 10ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/util/math/ToolsVBO.java

http://mt4j.googlecode.com/
Java | 243 lines | 92 code | 19 blank | 132 comment | 0 complexity | c29bb82f236776f0069e9a0c67c0387d 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.util.math;
 19
 20import java.nio.FloatBuffer;
 21
 22import javax.media.opengl.GL;
 23
 24import processing.core.PApplet;
 25
 26import com.sun.opengl.util.BufferUtil;
 27
 28/**
 29 * Methods to build VBOs and get their binding names	(Ids).
 30 * 
 31 * @author Christopher Ruff
 32 */
 33public class ToolsVBO {
 34	
 35	//TODO some methods are redundant
 36	//for floatbuffer vbo updates with vectors (3lements) can be updated the same way i.e.
 37	
 38	//////////////////////////////////////////////////////////////
 39	// 	Methods to build VBOs and get their binding names		//
 40	//////////////////////////////////////////////////////////////
 41	/**
 42	 * Generate vertex vbo.
 43	 * 
 44	 * @param pa the pa
 45	 * @param vertexBuffer the vertex buffer
 46	 * @param vertexCount the vertex count
 47	 * 
 48	 * @return the int
 49	 */
 50	public static int generateVertexVBO(PApplet pa, FloatBuffer vertexBuffer, int vertexCount){
 51		int[] vboVertices = new int[1]; 
 52		GL gl = Tools3D.getGL(pa);
 53			gl.glGenBuffers(1, vboVertices, 0);  // Get A Valid Name
 54			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboVertices[0]);  // Bind The Buffer
 55			// Load The Data
 56			gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 3 * BufferUtil.SIZEOF_FLOAT, vertexBuffer, GL.GL_STATIC_DRAW);
 57			//Unbind VBOs 
 58			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
 59			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
 60		return vboVertices[0];
 61	}
 62	
 63	/**
 64	 * Update vertex vbo.
 65	 * 
 66	 * @param pa the pa
 67	 * @param vertexBuffer the vertex buffer
 68	 * @param vertexCount the vertex count
 69	 * @param vboName the vbo name
 70	 */
 71	public static void updateVertexVBO(PApplet pa, FloatBuffer vertexBuffer, int vertexCount, int vboName){
 72		GL gl = Tools3D.getGL(pa);
 73		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);  // Bind The Buffer
 74		// Load The Data
 75		gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 3 * BufferUtil.SIZEOF_FLOAT, vertexBuffer, GL.GL_STATIC_DRAW);
 76		//Unbind VBOs 
 77		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
 78		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
 79	}
 80	
 81	/**
 82	 * Generate texture vbo.
 83	 * 
 84	 * @param pa the pa
 85	 * @param textureBuffer the texture buffer
 86	 * @param vertexCount the vertex count
 87	 * 
 88	 * @return the int
 89	 */
 90	public static int generateTextureVBO(PApplet pa, FloatBuffer textureBuffer, int vertexCount){
 91		int[] vboTexCoords = new int[1];// Texture Coordinate VBO Name
 92		GL gl = Tools3D.getGL(pa);
 93			gl.glGenBuffers(1, vboTexCoords, 0);  // Get A Valid Name
 94			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboTexCoords[0]); // Bind The Buffer
 95			// Load The Data
 96			gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 2 * BufferUtil.SIZEOF_FLOAT, textureBuffer, GL.GL_STATIC_DRAW);
 97			//Unbind VBOs 
 98			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
 99			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
100		return vboTexCoords[0];
101	}
102	
103	/**
104	 * Update texture vbo.
105	 * 
106	 * @param pa the pa
107	 * @param textureBuffer the texture buffer
108	 * @param vertexCount the vertex count
109	 * @param vboName the vbo name
110	 */
111	public static void updateTextureVBO(PApplet pa, FloatBuffer textureBuffer, int vertexCount, int vboName){
112		GL gl = Tools3D.getGL(pa);
113			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); // Bind The Buffer
114			// Load The Data
115			gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 2 * BufferUtil.SIZEOF_FLOAT, textureBuffer, GL.GL_STATIC_DRAW);
116			//Unbind VBOs 
117			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
118			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
119	}
120	
121	/**
122	 * Generate color vbo.
123	 * 
124	 * @param pa the pa
125	 * @param colorBuffer the color buffer
126	 * @param vertexCount the vertex count
127	 * 
128	 * @return the int
129	 */
130	public static int generateColorVBO(PApplet pa, FloatBuffer colorBuffer, int vertexCount){
131		int[] vboColor = new int[1];// vertexcolor Coordinate VBO Name
132		GL gl = Tools3D.getGL(pa);
133			gl.glGenBuffers(1, vboColor, 0);  // Get A Valid Name
134			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboColor[0]); // Bind The Buffer
135			// Load The Data
136			gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 4 * BufferUtil.SIZEOF_FLOAT, colorBuffer, GL.GL_STATIC_DRAW);
137			//Unbind VBOs 
138			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
139			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
140		return vboColor[0];
141	}
142	
143	/**
144	 * Update color vbo.
145	 * 
146	 * @param pa the pa
147	 * @param colorBuffer the color buffer
148	 * @param vertexCount the vertex count
149	 * @param vboName the vbo name
150	 */
151	public static void updateColorVBO(PApplet pa, FloatBuffer colorBuffer, int vertexCount, int vboName){
152		GL gl = Tools3D.getGL(pa);
153			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); // Bind The Buffer
154			// Load The Data
155			gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCount * 4 * BufferUtil.SIZEOF_FLOAT, colorBuffer, GL.GL_STATIC_DRAW);
156			//Unbind VBOs 
157			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
158			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
159	}
160	
161	/**
162	 * Generate stroke color vbo.
163	 * 
164	 * @param pa the pa
165	 * @param strokeColBuffer the stroke col buffer
166	 * @param vertexCount the vertex count
167	 * 
168	 * @return the int
169	 */
170	public static int generateStrokeColorVBO(PApplet pa, FloatBuffer strokeColBuffer, int vertexCount){
171		int[] vboStrokeColor = new int[1];// stroke Coordinate VBO Name
172		GL gl = Tools3D.getGL(pa);
173			gl.glGenBuffers(1, vboStrokeColor, 0);  // Get A Valid Name
174			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboStrokeColor[0]); // Bind The Buffer
175			// Load The Data
176			gl.glBufferData(GL.GL_ARRAY_BUFFER,vertexCount * 4 * BufferUtil.SIZEOF_FLOAT, strokeColBuffer, GL.GL_STATIC_DRAW);
177			//Unbind VBOs 
178			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
179			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
180		return vboStrokeColor[0];
181	}
182	
183	/**
184	 * Update stroke color vbo.
185	 * 
186	 * @param pa the pa
187	 * @param strokeColBuffer the stroke col buffer
188	 * @param vertexCount the vertex count
189	 * @param vboName the vbo name
190	 */
191	public static void updateStrokeColorVBO(PApplet pa, FloatBuffer strokeColBuffer, int vertexCount, int vboName){
192		GL gl = Tools3D.getGL(pa);
193			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); // Bind The Buffer
194			// Load The Data
195			gl.glBufferData(GL.GL_ARRAY_BUFFER,vertexCount * 4 * BufferUtil.SIZEOF_FLOAT, strokeColBuffer, GL.GL_STATIC_DRAW);
196			//Unbind VBOs 
197			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
198			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
199	}
200	
201	
202	/**
203	 * Generate normals vbo.
204	 * 
205	 * @param pa the pa
206	 * @param normalsBuffer the normals buffer
207	 * @param normalsCount the normals count
208	 * 
209	 * @return the int
210	 */
211	public static int generateNormalsVBO(PApplet pa, FloatBuffer normalsBuffer, int normalsCount){
212		int[] vboNormals = new int[1]; 
213		GL gl = Tools3D.getGL(pa);
214			gl.glGenBuffers(1, vboNormals, 0);  // Get A Valid Name
215			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboNormals[0]);  // Bind The Buffer
216			// Load The Data
217			gl.glBufferData(GL.GL_ARRAY_BUFFER, normalsCount * 3 * BufferUtil.SIZEOF_FLOAT, normalsBuffer, GL.GL_STATIC_DRAW);
218			//Unbind VBOs 
219			gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
220			gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
221		return vboNormals[0];
222	}
223	
224	
225	/**
226	 * Update normals vbo.
227	 * 
228	 * @param pa the pa
229	 * @param normalsBuffer the normals buffer
230	 * @param normalsCount the normals count
231	 * @param vboName the vbo name
232	 */
233	public static void updateNormalsVBO(PApplet pa, FloatBuffer normalsBuffer, int normalsCount, int vboName){
234		GL gl = Tools3D.getGL(pa);
235		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);  // Bind The Buffer
236		// Load The Data
237		gl.glBufferData(GL.GL_ARRAY_BUFFER, normalsCount * 3 * BufferUtil.SIZEOF_FLOAT, normalsBuffer, GL.GL_STATIC_DRAW);
238		//Unbind VBOs 
239		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
240		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
241	}
242
243}