Guest User

Untitled

a guest
Jul 24th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. package ecumene.opengl;
  2.  
  3. import java.nio.FloatBuffer;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.opengl.GL11;
  7. import org.lwjgl.opengl.GL15;
  8. import org.lwjgl.opengl.GL30;
  9.  
  10. /**
  11. * Links many vertices together and bind their attributes
  12. * into one vertex buffer object
  13. *
  14. * @author Ecumene
  15. */
  16. public class Model {
  17. /*The model information*/
  18. private Vertex[] model;
  19.  
  20. /**The model's per-vertex positions attribute*/
  21. private VertexAttrib positions;
  22. /**The model's per-vertex texture uv attribute*/
  23. private VertexAttrib textureUVs;
  24. /**The model's per-vertex normal attribute*/
  25. private VertexAttrib normals;
  26. /**The model's per-vertex color attribute*/
  27. private VertexAttrib colors;
  28. /**The model's drawing order*/
  29. private float[] indices;
  30.  
  31. /**The vertex attribute array buffer*/
  32. private int va;
  33.  
  34. /**The vertex indices id*/
  35. private int vi;
  36.  
  37. /**
  38. * Initializes the model with an array of vertices
  39. *
  40. * @param model The model information
  41. */
  42. public Model(Vertex ... model){
  43. this.model = model;
  44. }
  45.  
  46. /**
  47. * Initializes the model with a size
  48. *
  49. * @param size The amount of vertices the model is intended
  50. * to hold
  51. */
  52. public Model(int size){
  53. model = new Vertex[size];
  54. }
  55.  
  56. /**
  57. * Adds a vertex to the current model
  58. *
  59. * @param index The index to add the vertex at
  60. * @param vertices The vertex to add
  61. */
  62. public void add(int index, Vertex vertex){
  63. model[index] = vertex;
  64. }
  65.  
  66. /**
  67. * Sets the drawing order of the mode's vertices
  68. * (Sets indices)
  69. * @param values The drawing order to set
  70. */
  71. public void setDrawingOrder(float...values){
  72. indices = values;
  73. }
  74.  
  75. /**
  76. * Links the model's vertex data with the vertex attribute objects
  77. *
  78. * @param positions The vertex positions vertex attribute ID
  79. * @param textures The vertex texture coordinate vertex attribute ID
  80. * @param colors The vertex color vertex attribute ID
  81. * @param normals The vertex normal vertex attribute ID
  82. */
  83. public void link(int positions, int textures, int colors, int normals){
  84. float[] vertexPos = new float[model.length * 4];
  85. float[] textureUV = new float[model.length * 2];
  86. float[] color = new float[model.length * 4];
  87. float[] normal = new float[model.length * 3];
  88.  
  89. for(int i = 0; i < model.length; i++){
  90. vertexPos[4*i+0] = model[i].position.x;
  91. vertexPos[4*i+1] = model[i].position.y;
  92. vertexPos[4*i+2] = model[i].position.z;
  93. vertexPos[4*i+3] = model[i].position.w;
  94.  
  95. textureUV[2*i+0] = model[i].textureUV.x;
  96. textureUV[2*i+1] = model[i].textureUV.x;
  97.  
  98. color[4*i+0] = model[i].color.getRed();
  99. color[4*i+1] = model[i].color.getGreen();
  100. color[4*i+2] = model[i].color.getBlue();
  101. color[4*i+3] = model[i].color.getAlpha();
  102.  
  103. normal[3*i+0] = model[i].normal.x;
  104. normal[3*i+1] = model[i].normal.y;
  105. normal[3*i+2] = model[i].normal.z;
  106. }
  107.  
  108. FloatBuffer indicesBuffer = BufferUtils.createFloatBuffer(indices.length);
  109. indicesBuffer.put(indices);
  110. indicesBuffer.flip();
  111.  
  112. va = GL15.glGenBuffers();
  113. GL30.glBindVertexArray(va);
  114. this.positions = new VertexAttrib(positions, 4, vertexPos);
  115. this.textureUVs = new VertexAttrib(textures, 2, textureUV);
  116. this.colors = new VertexAttrib(colors, 4, color);
  117. this.normals = new VertexAttrib(normals, 3, normal);
  118. GL30.glBindVertexArray(0);
  119.  
  120. vi = GL15.glGenBuffers();
  121. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
  122. GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
  123. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  124. System.out.println("linkeed");
  125. }
  126.  
  127. /**
  128. * Draws the vertex attribute object
  129. * @param mode The mode to draw
  130. */
  131. public void render(int mode){
  132. GL30.glBindVertexArray(va);
  133. positions.bind();
  134. textureUVs.bind();
  135. colors.bind();
  136. normals.bind();
  137. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
  138. GL11.glDrawElements(mode, indices.length, GL11.GL_UNSIGNED_BYTE, 0);
  139. GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
  140. System.out.println("rendered");
  141. positions.unbind();
  142. textureUVs.unbind();
  143. colors.unbind();
  144. normals.unbind();
  145. System.out.println("\n");
  146. GL30.glBindVertexArray(0);
  147. }
  148.  
  149. /**
  150. * @return The model's vertices
  151. */
  152. public Vertex[] getVertices(){
  153. return model;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment