ojas11

terrain class

Mar 14th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. package terrains;
  2.  
  3. import models.RawModel;
  4. import renderEngine.Loader;
  5. import textures.ModelTexture;
  6.  
  7. public class Terrain {
  8.  
  9. private static final float SIZE = 800;
  10. private static final int VERTEX_COUNT = 120;
  11.  
  12. private float x;
  13. private float z;
  14. private RawModel model;
  15. private ModelTexture texture;
  16.  
  17. public Terrain(int gridX, int GridZ, Loader loader, ModelTexture texture) {
  18. this.texture = texture;
  19. this.x = gridX * SIZE;
  20. this.z = GridZ * SIZE;
  21. this.model = generateTerrain(loader);
  22. }
  23.  
  24. public float getX( ) {
  25. return x;
  26. }
  27.  
  28. public float getZ( ) {
  29. return z;
  30. }
  31.  
  32. public RawModel getModel( ) {
  33. return model;
  34. }
  35.  
  36. public ModelTexture getTexture( ) {
  37. return texture;
  38. }
  39.  
  40. private RawModel generateTerrain(Loader loader){
  41. int count = VERTEX_COUNT * VERTEX_COUNT;
  42. float[] vertices = new float[count * 3];
  43. float[] normals = new float[count * 3];
  44. float[] textureCoords = new float[count*2];
  45. int[] indices = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)];
  46. int vertexPointer = 0;
  47. for(int i=0;i<VERTEX_COUNT;i++){
  48. for(int j=0;j<VERTEX_COUNT;j++){
  49. vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * SIZE;
  50. vertices[vertexPointer*3+1] = 0;
  51. vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
  52. normals[vertexPointer*3] = 0;
  53. normals[vertexPointer*3+1] = 1;
  54. normals[vertexPointer*3+2] = 0;
  55. textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1);
  56. textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1);
  57. vertexPointer++;
  58. }
  59. }
  60. int pointer = 0;
  61. for(int gz=0;gz<VERTEX_COUNT-1;gz++){
  62. for(int gx=0;gx<VERTEX_COUNT-1;gx++){
  63. int topLeft = (gz*VERTEX_COUNT)+gx;
  64. int topRight = topLeft + 1;
  65. int bottomLeft = ((gz+1)*VERTEX_COUNT)+gx;
  66. int bottomRight = bottomLeft + 1;
  67. indices[pointer++] = topLeft;
  68. indices[pointer++] = bottomLeft;
  69. indices[pointer++] = topRight;
  70. indices[pointer++] = topRight;
  71. indices[pointer++] = bottomLeft;
  72. indices[pointer++] = bottomRight;
  73. }
  74. }
  75. return loader.loadToVAO(vertices, textureCoords, normals, indices);
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment