Esempio n. 1
0
RawModel Terrain::generateTerrain(Loader &loader, std::string heightMapFile){
	RawImageData heightMap = loader.loadRawImageData(heightMapFile);

	processHeightMap(heightMap); 

	vertex_count = heightMap.getHeight();
	heights.resize(vertex_count, std::vector<float>(vertex_count, 0));
	
	std::vector<float> normals;
	std::vector<float> textureCoords;
	std::vector<int> indices;
	for (int i = 0; i < vertex_count; i++) {
		for (int j = 0; j < vertex_count; j++){
			vertices.push_back((float)j / ((float)vertex_count - 1) * size);
			float height = getHeight(j, i);
			heights[j][i] = height;
			vertices.push_back( height );
			vertices.push_back((float)i / ((float)vertex_count - 1) * size);
			glm::vec3 normal = calculateNormal(j, i);
			normals.push_back( normal.x );
			normals.push_back( normal.y );
			normals.push_back( normal.z );
			textureCoords.push_back((float)j / ((float)vertex_count - 1));
			textureCoords.push_back((float)i / ((float)vertex_count - 1));
		}
	}

	for (int gz = 0; gz < vertex_count - 1; gz++){
		for (int gx = 0; gx < vertex_count - 1; gx++){
			int topLeft = (gz*vertex_count) + gx;
			int topRight = topLeft + 1;
			int bottomLeft = ((gz + 1)*vertex_count) + gx;
			int bottomRight = bottomLeft + 1;
			indices.push_back( topLeft );
			indices.push_back( bottomLeft );
			indices.push_back( topRight );
			indices.push_back( topRight );
			indices.push_back( bottomLeft );
			indices.push_back( bottomRight );
		}
	}

	return loader.loadToVao(vertices, textureCoords, normals, indices);
}