TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ Heightmap() [2/2]

Heightmap::Heightmap ( const std::string  path)

Definition at line 9 of file Heightmap.cpp.

10{
11 m_Path = path;
12 int width, height, channels;
13 stbi_set_flip_vertically_on_load(0);
14 void* data = stbi_load_16(path.c_str(), &width, &height, &channels, 1);
15 if (!data)
16 {
17 std::cout << "Failed to load heightmap (" << path << "): " << stbi_failure_reason() << std::endl;
18 return;
19 }
20 m_Data = static_cast<uint16_t*>(data);
21 m_Width = width;
22 m_Height = height;
23
24 glGenTextures(1, &m_RendererID);
25 glBindTexture(GL_TEXTURE_2D, m_RendererID);
26 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
27
28 std::vector<uint8_t> texData;
29 for (int i = 0; i < width * height; ++i)
30 {
31 uint8_t value = static_cast<float>(m_Data[i]) / (2 << 15) * 255;
32 texData.push_back(value);
33 texData.push_back(value);
34 texData.push_back(value);
35 texData.push_back(255);
36 }
37
38 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.data());
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
43 glGenerateMipmap(GL_TEXTURE_2D);
44}
@ value
the parser finished reading a JSON value