I'm trying to generate some simple planets in OpenGL using libnoise (including noiseutils), but the terrain is not correctly connecting where the sphere "completes itself" and I was wondering if someone could explain to me why this is happening and/or point me in the direction to solving this little problem.
I'm generating spheres with a slightly adapted version of this algorithm, with this being my full code:
static void GenerateSphere( VertexBuffer& vertices, IndexBuffer32& indices, uint rings, uint sectors, float radius, const HeightModule* heights )
{
static const double pi = glm::pi<double>();
static const double pi_2 = glm::half_pi<double>();
// texture-coordinate conversion helpers
const float rr = 1.0f / float( rings - 1 );
const float ss = 1.0f / float( sectors - 1 );
// clear buffers
vertices.Clear();
indices.Clear();
// iterate through each ring and sector to create the vertices
float height = 0;
Vertex v;
for ( uint r = 0; r < rings; ++r )
{
for ( uint s = 0; s < sectors; ++s )
{
// calculate point
glm::vec3 point(
float( cos( 2 * pi * s * ss ) * sin( pi * r * rr ) ),
float( sin( -pi_2 + pi * r * rr ) ),
float( sin( 2 * pi * s * ss ) * sin( pi * r * rr ) )
);
// set vertex data
height = radius + heights->GetHeight( s, r );
v.TexCoord = glm::vec2( s * ss, r * rr );
v.Normal = point;
v.Position = point * height;
vertices.Add( v );
// set index data
indices.Add( r * sectors + s );
indices.Add( r * sectors + ( s + 1 ) );
indices.Add( ( r + 1 ) * sectors + ( s + 1 ) );
indices.Add( ( r + 1 ) * sectors + s );
}
}
}
VertexBuffer and IndexBuffer32 are simple wrappers for their OpenGL counterparts, and HeightModule is a wrapper for some libnoise and noiseutils functions (like NoiseMap, NoiseMapBuilderSphere, and some noise modules).
To illustrate what I mean by heights not matching where the sphere "completes itself," here is a screenshot:
As you can see on the left, the vertices do not quite reach each other (it's like this all the way from pole to pole) and the texture coordinates are messed up on the connecting lines. The blue line you see on the right extends from the origin all the way to the north pole, and after some debugging I've found that all of the vertices generated when r == 0 are the same with the position being [0,-radius,0] and the normal being [0,-1,0] (the texture coordinates appear to the correct though, as they all differ). I suspect the terrain problem is caused by this, but I have no clue how I would go about fixing that.