Alright, it's fixed! Yay finally. Thought I'd share the result for those interested. This is the working relevant code I'm working on:
void Game::generateTerrain()
{
noise::module::Perlin perlinNoise;
cout<< "Generating Terrain: " << endl;
// Initialize the map variables.
mapWidth = 4;
mapHeight = 4;
chunkWidth = 8;
chunkHeight = 8;
blockSize = 32;
mapDepth = 5;
layer = 0;
for(int mx = 0; mx < mapWidth; mx++)
{
for(int my = 0; my < mapHeight; my++)
{
for(int x = 0; x < chunkWidth; x++)
{
for(int y = 0; y < chunkHeight; y++)
{
// Get the depth value using Perlin Noise.
float nOffX = ((float)x + (float)mx) / (float)(chunkWidth - 1.0f);
float nOffY = ((float)y + (float)my) / (float)(chunkHeight - 1.0f);
float nX = 128.0f + nOffX * (256.0f - 128.0f);
float nY = 128.0f + nOffY * (256.0f - 128.0f);
int depth = lround(perlinNoise.GetValue(nX,nY, 0.0f) * 10.0f);
depth = noise::ClampValue(depth, 0, mapDepth);
for(int z = -1; z < depth - layer; z++)
{
// Calculate the isometric x and y coordinates
// Each corner of the rectangle that holds the cube forms a right triangle with an opposite side of 16 pixels.
// We know this because the whole side of the rectangle is 32 pixels long and the side we can easily measure
// is half of that length (where the cube's bottom point meets the line.
// This opposite side is at an approximate 26.565 degree angle, which is the angle of 2:1 resolution isometric projection.
// This gives us the equation (adjacent) a / 16 = tan(26.565) or solving for a = 16 * tan(26.565) which equates to approximately 8 pixels.
// This means that our adjacent side is roughly 1/4 of our rectangle side.
// To get our isometric values we want the rectangles to overlap so that we have an "overlap rectangle of 16 x 8 pixels.
// This is the same as saying because we have a 2:1 perspective we need the overlap rectangle to be twice as wide as high.
// Or the equation is 1/2 width x 1/4 height or 1/8 x width X height for the area of the overlap rectangle.
// So to get the "isometricX" value we adjust along both the x and y 2D axis like this: ((x - y) * (blockSize / 2))
// And to get the "isometricY" value we adjust along both the x and y 2D axis like this: ((x + y) * (blockSize / 4))
// To input the z or "depth" value we adjust along both the x and y 2D axis like this:
// isoX = ((x - y) * blockSize / 2) + (z * blockSize / 2)
// isoY = ((y + x) * blockSize / 4) + (z * blockSize / 2)
// chunkOffsetX = (mx - my) * chunkWidth * blockSize / 2
// chunkOffsetY = (mx + my) * chunkHeight * blockSize / 4
int isoX = ((mx - my) * chunkWidth * blockSize / 2) + ((x - y) * blockSize / 2);
int isoY = ((mx + my) * chunkHeight * blockSize / 4) + ((x + y) * blockSize / 4) - (z * blockSize / 2);
cout << isoX << ", " << isoY << endl;
sf::Vertex v1, v2, v3, v4;
v1.position = sf::Vector2f(isoX , isoY);
v2.position = sf::Vector2f(isoX + blockSize , isoY);
v3.position = sf::Vector2f(isoX + blockSize , isoY + blockSize);
v4.position = sf::Vector2f(isoX , isoY + blockSize);
v1.texCoords = sf::Vector2f(0, 0);
v2.texCoords = sf::Vector2f(32, 0);
v3.texCoords = sf::Vector2f(32, 32);
v4.texCoords = sf::Vector2f(0, 32);
terrain.append(v1);
terrain.append(v2);
terrain.append(v3);
terrain.append(v4);
}
}
}
}
}
}
And the output looks like this so far:
So once I realized there was an append method to the VertexArray it was rather easy to add the Quads at the correct index. Don't know if this will cause problems later with trying to reference the blocks for removal or adding new blocks but I think I'm on the right track at least.
Right now the terrain is not looking the way I want but that is more due to the noise algorithm setup I'm using I think. And of course if you look carefully all the chunks are the same (which is not what I want) so I need to fix that as well.
On a side note, running this program makes my graphics card "hum" or whistle very faintly ( at least I think this is the graphics card ), I don't know if this because of Linux or if it is because of something else (perhaps I'm doing something wrong). The total number of vertices for this map is something like 20000 (well below the limits of a modern graphics card like mine). So I'm confused as to why it is doing this. Anyways I will test the code in windows, to see if the problem is the same there.