Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1.  
  2. package ksg.lab1;
  3.  
  4. import com.jogamp.common.nio.Buffers;
  5. import ksg.common.GameObject;
  6. import com.jogamp.opengl.GL2;
  7. import com.jogamp.opengl.util.texture.Texture;
  8.  
  9. import java.awt.image.BufferedImage;
  10. import java.nio.FloatBuffer;
  11. import java.nio.IntBuffer;
  12.  
  13. import ksg.common.Vector3;
  14.  
  15. /**
  16. * @author MK
  17. */
  18.  
  19. // Klasa reprezentująca model terenu otrzymanego za pomocą VBO.
  20. public class GroundVbo extends GameObject {
  21. int[] buffers;
  22. protected FloatBuffer vertices, texCoords, normals;
  23. protected IntBuffer indices;
  24. protected int verticesHandle, texCoordsHandle, normalsHandle, indicesHandle;
  25. protected Texture texture = null;
  26. protected int diffuseTextureHandle, displacementTextureHandle;
  27. protected BufferedImage displacementMap;
  28.  
  29. //zad5
  30. private int columns = 128;
  31. private int rows = 128;
  32. private float[] vertexArray = new float[columns * rows * 3];
  33. private float[] texCoordArray = new float[columns * rows * 2];
  34. private float[] normalArray = new float[columns * rows * 3];
  35. private int[] indexArray = new int[columns * rows * 6];
  36. private float elevation = 0.00000000000000000000000000000000000001f;
  37.  
  38. public GroundVbo(Vector3 position, Texture texture, BufferedImage displacementMap) {
  39. super();
  40. setPosition(position);
  41. this.texture = texture;
  42. this.displacementMap = displacementMap;
  43. }
  44.  
  45. public GroundVbo(float x, float y, float z, Texture texture, BufferedImage displacementMap) {
  46. this(new Vector3(x, y, z), texture, displacementMap);
  47. }
  48.  
  49. public void generateBuffers(GL2 gl) {
  50.  
  51. for (int yy = 0; yy < rows; yy++) {
  52. elevation += elevation;
  53. for (int xx = 0; xx < columns; xx++) {
  54. int rgb = displacementMap.getRGB(xx * 7, yy * 7);
  55. int r = (rgb >> 16);
  56. int g = (rgb >> 8) & 0xff;
  57. int b = (rgb) & 0xff;
  58. int gray = (r + g + b) / 3;
  59. int offset = 3 * (yy * columns + xx);
  60. vertexArray[offset] = 100 - (xx * 10);
  61. vertexArray[offset + 1] += gray;
  62. vertexArray[offset + 2] = 100 - (yy * 10);
  63. }
  64. }
  65.  
  66. int i = 0;
  67. for (int yy = 0; yy < rows - 1; yy++) {
  68. for (int xx = 0; xx < columns - 1; xx++) {
  69. int offset = yy * columns + xx;
  70. indexArray[i] = (short) (offset);
  71. indexArray[i + 1] = (short) (offset + columns);
  72. indexArray[i + 2] = (short) (offset + 1);
  73.  
  74. indexArray[i + 3] = (short) (offset + 1);
  75. indexArray[i + 4] = (short) (offset + columns);
  76. indexArray[i + 5] = (short) (offset + columns + 1);
  77. i += 6;
  78. }
  79. }
  80.  
  81. // Stworzenie bufora przechowującego informacje o położeniu wierzchołków (3 składowe: x,y,z).
  82. // float[] vertexArray = {-500, 0, -500,
  83. // 500, 0, -500,
  84. // -500, 0, 500,
  85. // 500, 0, 500};
  86. vertices = Buffers.newDirectFloatBuffer(vertexArray.length);
  87. vertices.put(vertexArray);
  88. vertices.flip();
  89.  
  90. // Stworzenie bufora przechowującego informacje o mapowaniu tekstury (2 składowe: u,v).
  91. // float[] texCoordArray = {0f, 0f,
  92. // 1f, 0f,
  93. // 1f, 1f,
  94. // 0f, 1f};
  95. texCoords = Buffers.newDirectFloatBuffer(texCoordArray.length);
  96. texCoords.put(texCoordArray);
  97. texCoords.flip();
  98.  
  99. // Stworzenie bufora przechowującego normalne wierzchołków (3 składowe: x,y,z).
  100. // float[] normalArray = {0, 0, 1,
  101. // 0, 0, 1,
  102. // 0, 0, 1,
  103. // 0, 0, 1};
  104. normals = Buffers.newDirectFloatBuffer(normalArray.length);
  105. normals.put(normalArray);
  106. normals.flip();
  107.  
  108. // Stworzenie bufora opisującego sposób budowania trójkątów na podstawie indeksów wierzchołków.
  109. // 0---1
  110. // | /|
  111. // | / |
  112. // |/ |
  113. // 2---3
  114. // int[] indexArray = {0, 2, 1, 1, 2, 3};
  115. indices = Buffers.newDirectIntBuffer(indexArray.length);
  116. indices.put(indexArray);
  117. indices.flip();
  118.  
  119. buffers = new int[4]; // "uchwyty" buforów przechowujących informacje o modelu
  120. gl.glGenBuffers(buffers.length, buffers, 0);
  121.  
  122. // Podpięcie bufora pozycji wierzchołków.
  123. verticesHandle = buffers[0];
  124. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, verticesHandle);
  125. gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.capacity() * Buffers.SIZEOF_FLOAT,
  126. vertices, GL2.GL_STATIC_DRAW);
  127. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
  128.  
  129. // Podpięcie bufora mapowania tekstury.
  130. texCoordsHandle = buffers[1];
  131. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, texCoordsHandle);
  132. gl.glBufferData(GL2.GL_ARRAY_BUFFER, texCoords.capacity() * Buffers.SIZEOF_FLOAT,
  133. texCoords, GL2.GL_STATIC_DRAW);
  134. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
  135.  
  136. // Podpięcie bufora normalnych.
  137. normalsHandle = buffers[2];
  138. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalsHandle);
  139. gl.glBufferData(GL2.GL_ARRAY_BUFFER, normals.capacity() * Buffers.SIZEOF_FLOAT,
  140. normals, GL2.GL_STATIC_DRAW);
  141. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
  142.  
  143. // Podpięcie bufora indeksów.
  144. indicesHandle = buffers[3];
  145. gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indicesHandle);
  146. gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * Buffers.SIZEOF_INT,
  147. indices, GL2.GL_STATIC_DRAW);
  148. gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
  149. }
  150.  
  151. @Override
  152. public void draw(GL2 gl) {
  153. gl.glPushMatrix(); // odłożenie aktualnego stanu macierzy transformacji na stos
  154. gl.glTranslatef(position.x, position.y, position.z); // ustawienie pozycji
  155.  
  156. // Włączenie tekstury.
  157. gl.glEnable(texture.getTarget());
  158. gl.glActiveTexture(gl.GL_TEXTURE0);
  159. gl.glBindTexture(texture.getTarget(), texture.getTextureObject());
  160.  
  161. // Włączenie poszczególnych buforów.
  162. gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  163. gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
  164. gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
  165.  
  166. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, texCoordsHandle);
  167. gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, 0);
  168. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalsHandle);
  169. gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);
  170. gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, verticesHandle);
  171. gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
  172.  
  173. gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indicesHandle);
  174. gl.glDrawElements(GL2.GL_TRIANGLES, indices.capacity(), GL2.GL_UNSIGNED_INT, 0); // narysowanie modelu
  175.  
  176. // Wyłączenie buforów.
  177. gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
  178. gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
  179. gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
  180.  
  181. gl.glDisable(texture.getTarget()); // wyłączenie tekstury
  182.  
  183. gl.glUseProgram(0); // użycie domyślnego programu cieniującego
  184.  
  185. gl.glPopMatrix(); // przywrócenie poprzedniego stanu macierzy transformacji modeli
  186. }
  187.  
  188. @Override
  189. public void freeMemory(GL2 gl) {
  190. gl.glDeleteBuffers(buffers.length, buffers, 0);
  191. }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement