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