ulfben

(grid) Landscape generation

Mar 25th, 2021 (edited)
377
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sample data file:
  2. /*
  3. ncols 10
  4. nrows 9
  5. xllcenter 1562450.0
  6. yllcenter 6723100.0
  7. cellsize  50.0
  8. nodata_value -32766
  9. 70.5   70.2   69.2   68.5   70.2   71.0   68.5   70.5   71.6   70.1
  10. 71.5   70.5   69.4   68.3   67.2   67.2   70.2   71.7   69.9   69.0
  11. 70.8   69.9   68.9   68.0   67.0   67.2   69.9   70.6   69.2   68.5
  12. 70.0   69.6   69.3   69.0   68.7   68.4   68.2   68.0   67.8   67.6
  13. 69.5   69.2   68.9   68.7   68.6   68.4   68.3   68.1   68.0   67.9
  14. 69.4   69.1   68.8   68.6   68.5   68.3   68.2   68.1   68.0   67.9
  15. 69.5   69.2   68.8   68.5   68.1   67.8   67.4   67.0   66.7   66.9
  16. 69.7   68.4   69.0   71.2   70.0   69.4   68.8   68.2   67.6   67.0
  17. 71.7   71.4   71.1   70.7   70.3   69.9   69.4   69.0   68.6   68.2
  18. */
  19.  
  20. //The header data has been parsed and removed from "lines" by now, so this function only sees lines with height values.
  21. private void parseHeightValues(List<String> lines){
  22.         final int lineCount = lines.size();
  23.         final int nrows = nrows();
  24.         final int ncols = ncols();
  25.         final float scaleFactor = 1.0f;
  26.         final float cellsize = 1.0f;//((float) cellsize());
  27.         final float centeroffset = cellsize * 0.5f;
  28.         vertices = new Vertex[ncols*nrows];
  29.         int index = 0;
  30.         for(int row = 0; row < nrows; row++){
  31.             final String[] values = split(lines.get(row));
  32.             final int colCount = values.length;          
  33.             final float y = (row * cellsize) + centeroffset;
  34.             for(int col = 0; col < ncols; col++){
  35.                 final float x = (col * cellsize) + centeroffset;
  36.                 final float height = parseHeightValue(values[col]);
  37.                 final Vertex vert = new Vertex(x*scaleFactor, height*scaleFactor, y*scaleFactor);
  38.                 final float s = (row / (float)(nrows-1));
  39.                 final float t = (col / (float)(ncols-1));
  40.                 vert.texcoords(s, t);
  41.                 vertices[index++] = vert;
  42.             }
  43.         }
  44.     }
  45.  
  46.     void generateVertexIndices(){
  47.         final int nrows = nrows();
  48.         final int ncols = ncols();
  49.         final int nfaces = (nrows-1)*(ncols-1); //the number of rectangles needed to be drawn for n rows and n cols of verts.
  50.         final int sizeOfBuffer = nfaces*2*3; //two triangles per rectangular face.
  51.         int index = 0;
  52.         indices = new int[sizeOfBuffer];
  53.         for (int j=0; j<nrows-1; ++j){
  54.             final int row1 = j * (nrows);
  55.             final int row2 = (j+1) * (nrows);
  56.             for (int i=0; i<ncols-1; ++i){
  57.                 // triangle 1
  58.                 indices[index++] = (row1+i);
  59.                 indices[index++] = (row2+i);
  60.                 indices[index++] = (row2+i+1);
  61.  
  62.                 // triangle 2
  63.                 indices[index++] = (row2+i+1);
  64.                 indices[index++] = (row1+i+1);
  65.                 indices[index++] = (row1+i);
  66.             }
  67.         }        
  68.     }
  69.  
  70.     private void calculateNormals(){
  71.         for (int i = 0; i < indices.length; i+=3){
  72.             //3 indices = 1 triangle = 1 face.
  73.             final Vertex v1 = vertices[indices[i]];
  74.             final Vertex v2 = vertices[indices[i+1]];
  75.             final Vertex v3 = vertices[indices[i+2]];
  76.             final Vec3 e1 = Vec3.subtract(v1.pos, v2.pos);
  77.             final Vec3 e2 = Vec3.subtract(v3.pos, v2.pos);
  78.             final Vec3 norm = Vec3.cross(e1, e2);
  79.             //a vert can participate in several faces, so first just add all the normals together.
  80.             vertices[indices[i]].normal.add(norm);
  81.             vertices[indices[i+1]].normal.add(norm);
  82.             vertices[indices[i+2]].normal.add(norm);
  83.         }
  84.         //finally, normalize the normals. They will now be properly weighted no matter how many faces influence each of them.
  85.         for(int i = 0; i < vertices.length; ++i){
  86.             vertices[i].normal.normalize();
  87.         }
  88.     }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×