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