I'm generating a basic terrain and it looks something like this:
// Load the vertex and index array with the terrain data.
for (j = 0; j<(m_terrainHeight - 1); j++)
{
for (i = 0; i<(m_terrainWidth - 1); i++)
{
index1 = (m_terrainHeight * j) + i; // Top left.
index2 = (m_terrainHeight * (j + 1)) + i; // Bottom left.
index3 = (m_terrainHeight * j) + (i + 1); // Top right.
index4 = (m_terrainHeight * (j + 1)) + (i + 1); // Bottom right.
// Top left
vertices[index].position = glm::vec3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
vertices[index].texture = glm::vec2(m_heightMap[index1].tu = 0.0f, m_heightMap[index1].tv = 0.0f);
indices[index] = index;
index++;
// Bottom left.
vertices[index].position = glm::vec3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z);
vertices[index].texture = glm::vec2(m_heightMap[index2].tu = 0.0f, m_heightMap[index2].tv = 1.0f);
indices[index] = index;
index++;
// Top right.
vertices[index].position = glm::vec3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z);
vertices[index].texture = glm::vec2(m_heightMap[index3].tu = 1.0f, m_heightMap[index3].tv = 0.0f);
indices[index] = index;
index++;
// Top right.
vertices[index].position = glm::vec3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z);
vertices[index].texture = glm::vec2(m_heightMap[index3].tu = 1.0f, m_heightMap[index3].tv = 0.0f);
indices[index] = index;
index++;
// Bottom left.
vertices[index].position = glm::vec3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z);
vertices[index].texture = glm::vec2(m_heightMap[index2].tu = 0.0f, m_heightMap[index2].tv = 1.0f);
indices[index] = index;
index++;
// Bottom right
vertices[index].position = glm::vec3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
vertices[index].texture = glm::vec2(m_heightMap[index4].tu = 1.0f, m_heightMap[index4].tv = 1.0f);
indices[index] = index;
index++;
}
}
Now If I wanted to find which grid square the player is on, we could simply (if the camera position is relative to the terrain)
//Grid square the camera is on
int gridX = (int)std::floor(cameraX / gridSquareSize);
int gridZ = (int)std::floor(cameraZ / gridSquareSize);
Continuing, we know that each face consists of two triangles, and the top triangle is represented by three vertices which index corresponds to top left, bottom left and top right. The bottom triangle would then be top right, bottom left and bottom right. Now I'm having a hard time trying to find height values (y-values) at these points because if I try to do
//Top left triangle
glm::vec3(0, vertices[(m_terrainHeight * gridZ) + gridX].position.y, 0) //Top left
glm::vec3(0, vertices[(m_terrainHeight * (gridZ) + 1) + gridX].position.y, 1) //Bottom left
glm::vec3(1, vertices[((m_terrainHeight * gridZ)) + (gridX + 1)].position.y, 0) //Top right
we conclude that the top left and bottom left position will be okay, but the top right position won't get the correct index, because as it is now (if we say that gridX = 0 and gridZ = 0), the index will generate a value of 1, which is not the correct height value at that position because the index we want should be 2 (top right). So, is there a way I could write the code so I would get the correct index for the triangles of the terrain?