Advertisement
Guest User

Untitled

a guest
Dec 13th, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. public class Mesh {
  2.  
  3. ..some variables...
  4.  
  5. public Mesh() {
  6. this.vaoID = glGenVertexArrays();
  7. this.posVBOID = glGenBuffers();
  8. this.textureVBOID = glGenBuffers();
  9. this.normalVBOID = glGenBuffers();
  10. this.indicesVBOID = glGenBuffers();
  11. }
  12.  
  13. public void render() {
  14. System.out.println("1");
  15. Texture texture = material.getTexture();
  16. if(texture != null){
  17. //Activate the first texture unit
  18. glActiveTexture(GL_TEXTURE0);
  19. //Bind the texture
  20. texture.bind();
  21. }
  22. System.out.println("2");
  23. //Draw the mesh
  24. glBindVertexArray(vaoID);
  25. glEnableVertexAttribArray(0);
  26. glEnableVertexAttribArray(1);
  27. glEnableVertexAttribArray(2);
  28. System.out.println("3");
  29.  
  30. glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_INT, 0);
  31. System.out.println("4");
  32. //Restore state
  33. glDisableVertexAttribArray(0);
  34. glDisableVertexAttribArray(1);
  35. glDisableVertexAttribArray(2);
  36. glBindVertexArray(0);
  37.  
  38. glBindTexture(GL_TEXTURE_2D, 0);
  39. }
  40.  
  41. public Mesh flushToBuffers(){
  42. this.indicesCount = this.indices.length;
  43.  
  44. glBindVertexArray(this.vaoID);
  45.  
  46. if(vertices != null){
  47. //Position VBO
  48. FloatBuffer posBuffer = BufferUtils.createFloatBuffer(this.vertices.length * 3);
  49. posBuffer.put(CollectionUtils.Vector3fArrayToFloatArray(this.vertices)).flip();
  50. glBindBuffer(GL_ARRAY_BUFFER, this.posVBOID);
  51. glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
  52. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
  53. }
  54.  
  55. if(textureCoords != null){
  56. //Texture Coordinates VBO
  57. FloatBuffer textCoordsBuffer = BufferUtils.createFloatBuffer(this.textureCoords.length * 2);
  58. textCoordsBuffer.put(CollectionUtils.Vector2fArrayToFloatArray(this.textureCoords)).flip();
  59. glBindBuffer(GL_ARRAY_BUFFER, this.textureVBOID);
  60. glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW);
  61. glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
  62. }
  63.  
  64. if(normals != null){
  65. //Normals VBO
  66. FloatBuffer vecNormalsBuffer = BufferUtils.createFloatBuffer(this.normals.length * 3);
  67. vecNormalsBuffer.put(CollectionUtils.Vector3fArrayToFloatArray(this.normals)).flip();
  68. glBindBuffer(GL_ARRAY_BUFFER, this.normalVBOID);
  69. glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW);
  70. glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0);
  71. }
  72.  
  73. if(indices != null){
  74. //Indices VBO
  75. IntBuffer indicesBuffer = BufferUtils.createIntBuffer(this.indices.length);
  76. indicesBuffer.put(this.indices).flip();
  77. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.indicesVBOID);
  78. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
  79. }
  80.  
  81. glBindBuffer(GL_ARRAY_BUFFER, 0);
  82. glBindVertexArray(0);
  83.  
  84. return this;
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement