I'm new with textures in OpenGL and I'm having some issues applying a texture to a sphere. I have calculated all the vertices and triangles for a Sphere with radius=1. To store my vertices and triangles I'm working with an array of floats that store vertices coordinates and another one of int that store the index of the vertices array for the triangles. My code to apply the texture is the following:
glTexImage2D(GL_TEXTURE_2D,0,3,Image.width(),Image.height(),0,GL_RGB,GL_UNSIGNED_BYTE,Image.bits());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
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_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
for (int i = 0; i < Triangles.size(); i++) {
uvCylinder(Vertices[Triangles[i]._0]);
glVertex3fv((GLfloat *) &Vertices[Triangles[i]._0]);
uvCylinder(Vertices[Triangles[i]._1]);
glVertex3fv((GLfloat *) &Vertices[Triangles[i]._1]);
uvCylinder(Vertices[Triangles[i]._2]);
glVertex3fv((GLfloat *) &Vertices[Triangles[i]._2]);
}
glEnd();
glDisable(GL_TEXTURE_2D);
inline void _object3D::uvCylinder(_vertex3f coord) {
float angle = 0.5f * atan2(coord._2, coord._0) / 3.14159f + 0.5f;
float height = 0.5f * coord._1 + 0.5f;
glTexCoord2f(angle, height);
}
So the results that I'm getting are:
As you can see in the 3rd image, the result is the inverse of the image of the Earth. Also, I'm getting a strange thing in the 2nd one. I don't know where could the error be, I mean, the problem is with the calculation with my vertices and triangles or on how I'm applying texture?. Also I have thought that maybe there's a problem with the sizes that I'm using to create the Sphere.