Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include "HeightMap.h"
  2.  
  3.  
  4. HeightMap::HeightMap(const std::string& name)
  5. {
  6.     std::ifstream file(name.c_str(), ios::binary);
  7.  
  8.     if (!file) return;
  9.  
  10.     numVertices = RAW_WIDTH * RAW_HEIGHT;
  11.     numIndices = (RAW_WIDTH-1) * (RAW_HEIGHT-1)*6;
  12.  
  13.     vertices = new Vector3[numVertices];
  14.     textureCoords = new Vector2[numVertices];
  15.     colours = new Vector4[numVertices]; //NEW
  16.     indices = new GLuint[numIndices];
  17.  
  18.     //Load in all char data from file
  19.     unsigned char* data = new unsigned char[numVertices];
  20.     file.read((char*) data, numVertices*sizeof(unsigned char));
  21.     file.close();
  22.  
  23.     //Create our vertices and texture data from file
  24.     for (int x = 0; x < RAW_WIDTH; ++x){
  25.         for (int z=0; z < RAW_HEIGHT; ++z){
  26.             int offset = (x * RAW_WIDTH) + z;
  27.  
  28.             //Scaled by a pre determined factor (modifying the terrain loaded
  29.             //from file)
  30.             vertices[offset] = Vector3(x * HEIGHTMAP_X,
  31.                 data[offset] * HEIGHTMAP_Y, z * HEIGHTMAP_Z);
  32.  
  33.             /*if (vertices[offset].y < 100.0f)
  34.                 colours[offset] = Vector4(0.2f, 0.2f, 0.2f, 0.2f);
  35.             else
  36.                 colours[offset] = Vector4(1.0f, 1.0f, 1.0f, 1.0f);*/
  37.  
  38.             colours[offset] = Vector4(vertices[offset].y / 200.0f,
  39.                 vertices[offset].y / 200.0f, vertices[offset].y / 200.0f,
  40.                 vertices[offset].y / 200.0f);
  41.  
  42.             textureCoords[offset] = Vector2(
  43.                 x * HEIGHTMAP_TEX_X, z * HEIGHTMAP_TEX_Z);
  44.         }
  45.     }
  46.  
  47.     delete data;
  48.  
  49.     numIndices = 0;
  50.  
  51.     for (int x = 0; x < RAW_WIDTH-1; ++x){
  52.         for (int z = 0; z < RAW_HEIGHT-1; ++z){
  53.             int a = (x * (RAW_WIDTH)) + z;
  54.             int b = ((x+1) * RAW_WIDTH) + z;
  55.             int c = ((x+1) * RAW_WIDTH) + (z+1);
  56.             int d = (x * RAW_WIDTH) + (z+1);
  57.  
  58.             indices[numIndices++] = c;
  59.             indices[numIndices++] = b;
  60.             indices[numIndices++] = a;
  61.  
  62.             indices[numIndices++] = a;
  63.             indices[numIndices++] = d;
  64.             indices[numIndices++] = c;
  65.         }
  66.     }
  67.  
  68.     BufferData();
  69. }
  70.  
  71.  
  72. HeightMap::~HeightMap(void)
  73. {
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement