0

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:

1st image

2nd image

3rd image

4th image

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.

Spektre
  • 49,595
  • 11
  • 110
  • 380
Ron08
  • 1
  • 2
  • I have debugged my code and It seems that my vertices and triangles are fine – Ron08 Dec 13 '20 at 17:26
  • Related: [Sphere Calculations](https://stackoverflow.com/questions/49080505/sphere-calculations/49081855#49081855) – Rabbid76 Dec 13 '20 at 17:33
  • 1
    this looks like you have wrongly textured the connection between start and end of longitude ... if you increase longitude angle from `0 ... 360` then the last patch texture coordinates corresponds to `360-delta ... 0` angles so the whole texture is put into last patch. To remedy this you need to duplicate the points on the end with different texture coordinate so you got `360-delta ... 360` corresponding angles ...where delta is the patch size in longitude angle `(360/pathes-1)` – Spektre Dec 14 '20 at 08:27
  • Thank you for your response. I already have duplicated my vertices, being my mesh something like this: https://blog.coredumping.com/wp-content/uploads/2013/01/UVMAP-300x146.png. I don't know what's happenig apart from what you said because I still get the same problem :( – Ron08 Dec 14 '20 at 18:35
  • the problem is that you are computing the texture coordinate like this `float angle = 0.5f * atan2(coord._2, coord._0) / 3.14159f + 0.5f; float height = 0.5f * coord._1 + 0.5f; glTexCoord2f(angle, height);` using `atan2` can not distinguish the case `360` and `0` deg... you need to set the last chunk coordinates differently. Also check what range the `atan2` is on your implementation (IIRC I saw range `<0..2*Pi>` and also `<-pi..+pi>` the latter could have the problem on `-pi/+pi` crossing instead of `0/2Pi`) – Spektre Dec 15 '20 at 06:39
  • see [Applying map of the earth texture a Sphere](https://stackoverflow.com/a/31804515/2521214) – Spektre Dec 15 '20 at 06:42
  • Okay, thank you for your help @Spektre – Ron08 Dec 15 '20 at 16:12

0 Answers0