Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sample data file:
- /*
- ncols 10
- nrows 9
- xllcenter 1562450.0
- yllcenter 6723100.0
- cellsize 50.0
- nodata_value -32766
- 70.5 70.2 69.2 68.5 70.2 71.0 68.5 70.5 71.6 70.1
- 71.5 70.5 69.4 68.3 67.2 67.2 70.2 71.7 69.9 69.0
- 70.8 69.9 68.9 68.0 67.0 67.2 69.9 70.6 69.2 68.5
- 70.0 69.6 69.3 69.0 68.7 68.4 68.2 68.0 67.8 67.6
- 69.5 69.2 68.9 68.7 68.6 68.4 68.3 68.1 68.0 67.9
- 69.4 69.1 68.8 68.6 68.5 68.3 68.2 68.1 68.0 67.9
- 69.5 69.2 68.8 68.5 68.1 67.8 67.4 67.0 66.7 66.9
- 69.7 68.4 69.0 71.2 70.0 69.4 68.8 68.2 67.6 67.0
- 71.7 71.4 71.1 70.7 70.3 69.9 69.4 69.0 68.6 68.2
- */
- //The header data has been parsed and removed from "lines" by now, so this function only sees lines with height values.
- private void parseHeightValues(List<String> lines){
- final int lineCount = lines.size();
- final int nrows = nrows();
- final int ncols = ncols();
- final float scaleFactor = 1.0f;
- final float cellsize = 1.0f;//((float) cellsize());
- final float centeroffset = cellsize * 0.5f;
- vertices = new Vertex[ncols*nrows];
- int index = 0;
- for(int row = 0; row < nrows; row++){
- final String[] values = split(lines.get(row));
- final int colCount = values.length;
- final float y = (row * cellsize) + centeroffset;
- for(int col = 0; col < ncols; col++){
- final float x = (col * cellsize) + centeroffset;
- final float height = parseHeightValue(values[col]);
- final Vertex vert = new Vertex(x*scaleFactor, height*scaleFactor, y*scaleFactor);
- final float s = (row / (float)(nrows-1));
- final float t = (col / (float)(ncols-1));
- vert.texcoords(s, t);
- vertices[index++] = vert;
- }
- }
- }
- void generateVertexIndices(){
- final int nrows = nrows();
- final int ncols = ncols();
- final int nfaces = (nrows-1)*(ncols-1); //the number of rectangles needed to be drawn for n rows and n cols of verts.
- final int sizeOfBuffer = nfaces*2*3; //two triangles per rectangular face.
- int index = 0;
- indices = new int[sizeOfBuffer];
- for (int j=0; j<nrows-1; ++j){
- final int row1 = j * (nrows);
- final int row2 = (j+1) * (nrows);
- for (int i=0; i<ncols-1; ++i){
- // triangle 1
- indices[index++] = (row1+i);
- indices[index++] = (row2+i);
- indices[index++] = (row2+i+1);
- // triangle 2
- indices[index++] = (row2+i+1);
- indices[index++] = (row1+i+1);
- indices[index++] = (row1+i);
- }
- }
- }
- private void calculateNormals(){
- for (int i = 0; i < indices.length; i+=3){
- //3 indices = 1 triangle = 1 face.
- final Vertex v1 = vertices[indices[i]];
- final Vertex v2 = vertices[indices[i+1]];
- final Vertex v3 = vertices[indices[i+2]];
- final Vec3 e1 = Vec3.subtract(v1.pos, v2.pos);
- final Vec3 e2 = Vec3.subtract(v3.pos, v2.pos);
- final Vec3 norm = Vec3.cross(e1, e2);
- //a vert can participate in several faces, so first just add all the normals together.
- vertices[indices[i]].normal.add(norm);
- vertices[indices[i+1]].normal.add(norm);
- vertices[indices[i+2]].normal.add(norm);
- }
- //finally, normalize the normals. They will now be properly weighted no matter how many faces influence each of them.
- for(int i = 0; i < vertices.length; ++i){
- vertices[i].normal.normalize();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement