bool Terrain::loadHeightmap(const string& rawFile, int width) { const float HEIGHT_SCALE = 10.0f; std::ifstream fileIn(rawFile.c_str(), std::ios::binary); if (!fileIn.good()) { std::cout << "File does not exist" << std::endl; return false; } string stringBuffer(std::istreambuf_iterator<char>(fileIn), (std::istreambuf_iterator<char>())); fileIn.close(); if (stringBuffer.size() != (width * width)) { std::cout << "Image size does not match passed width" << std::endl; return false; } vector<float> heights; heights.reserve(width * width); for (int i = 0; i < (width * width); ++i) { float value = (float)(unsigned char)stringBuffer[i] / 256.0f; heights.push_back(value * HEIGHT_SCALE); m_colors.push_back(Color(value, value, value)); } glGenBuffers(1, &m_colorBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_colors.size() * 3, &m_colors[0], GL_STATIC_DRAW); generateVertices(heights, width); generateIndices(width); generateTexCoords(width); generateNormals(); generateWaterVertices(width); generateWaterIndices(width); generateWaterTexCoords(width); m_width = width; return true; }
/** * Build a parametric surface. */ NewParametricShaderNode::NewParametricShaderNode() : ParametricMeshShaderNode(50, 50) { _scale = 1.0f; // Compute vertices and normal vectors // computeVertexAndNormals(-M_PI/2, M_PI/2, -M_PI/2, M_PI/2); // computeVertexAndNormals(0, M_PI, 0, M_PI * 2); // computeVertexAndNormals(0, 2 * M_PI, 0, 1); computeVertexAndNormals(0, 2 * M_PI, 0, 2 * M_PI); for (int i = 0; i < _numVertices; i++) { log("[%d] Pos=(%.2f %.2f %.2f) N=(%.2f %.2f %.2f) uv=(%.2f %.2f)\n", i, _vertices[i].position[0], _vertices[i].position[1], _vertices[i].position[2], _vertices[i].normal[0], _vertices[i].normal[1], _vertices[i].normal[2], _vertices[i].texCoord[0], _vertices[i].texCoord[1]); } // Compute the number of indices _numIndices = (_sampleU - 1) * (_sampleV - 1) * 2 * 3; // Generate and fill in the index array. generateIndices(); }