i'm trying to use a texture to draw the Earth over a sphere. My sphere is composed by by quads, each quad is composed by two triangles:
void Sphere(float radius, int n_lat, int n_lon)
{
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
TextureManager::Inst()->BindTexture(1);
glBegin(GL_TRIANGLES);
for (float i = 0; i < n_lat; i += 1.f)
for (float j = 0; j < n_lon; j += 1.f)
{
Vector3 p;
GLfloat u;
GLfloat v;
// -- first triangle
p = SphereCoord(i, j, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
p = SphereCoord(i + 1, j, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
p = SphereCoord(i + 1, j + 1, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
// -- second triangle
p = SphereCoord(i, j, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
p = SphereCoord(i + 1, j + 1, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
p = SphereCoord(i, j + 1, n_lat, n_lon);
glNormal3(p);
u=atan2f(p.y,p.x)/2*PI+0.5;
v=0.5-asinf(p.z)/PI;
glTexCoord2f(u,v);
glVertex3(p * radius);
}
glEnd();
}
With this code don't obtain a correct texture mapping. Does anyone can give me some tips?
It looks like the texture is in someway repeated on the longitude axis:
The problem is in your the calculation of your u:
u=atan2f(p.y,p.x)/2*PI+0.5;
You are diving it by 2 then multiplying the result by PI. You should be diving by 2 and PI
u=atan2f(p.y,p.x)/(2*PI)+0.5;