2

I copied the corrected code here to apply my own texture from a solar imaging dataset onto an OpenGL sphere. In doing so I noticed that the texture does not wrap entirely around the sphere - only the "front half" - and the lighting shadow is not applied (also evident in the answer to that question).

How do you modify the code to wrap the texture around the entire sphere?

Here is the texture image which is data in a 360-degree longitude x sine(latitude) format (which actually should be transformed to longitude x latitude before applying to the sphere, but that's a detail...):

enter image description here

And here's the result of the texture mapping which shows that the image is only applied to the front/visible hemisphere:

enter image description here

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
TEB
  • 23
  • 4
  • Do you mean you want a transparent sphere? If not, what's the point of rendering the hidden half? – Ripi2 Dec 20 '17 at 20:43
  • @Ripi2: no - see clarification in original question. I see your point about "rendering the hidden half", but the goal here is to eventually have a rotating sphere that shows the entire 360-degree dataset on the sphere. So it would seem the place to start is getting the texture on the entire 3D object and then proceeding with interactive rotation. – TEB Dec 20 '17 at 20:55
  • Applying a texture is wrapping the rectangle of the image to the desired form, a circle in your case. What did you expect? – Ripi2 Dec 20 '17 at 21:00
  • Was expecting texture applied to a 3D sphere, not a circle. – TEB Dec 20 '17 at 21:05
  • If you want part of the texture, then you must calculate the part that is shown and pass its coordinates by using `glTexCoord` – Ripi2 Dec 20 '17 at 21:06

1 Answers1

0

To wrap a texture around a sphere you have to distribute the texture coordinates quadratic around the sphere. This is provided by gluNewQuadric, gluQuadricTexture and gluSphere:

class MyWnd:
    def __init__(self):
        self.texture_id = 0
        self.angle = 0

    def run_scene(self):
        glutInit()
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
        glutInitWindowSize(400, 400)
        glutCreateWindow(b'Minimal sphere OpenGL')
        self.lightning()
        self.texture_id = self.read_texture('data/worldmap1.jpg')
        glutDisplayFunc(self.draw_sphere)
        glMatrixMode(GL_PROJECTION)
        gluPerspective(40, 1, 1, 40)
        glutMainLoop()

    def draw_sphere(self):
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(math.cos(self.angle)*4, math.sin(self.angle)*4, 0, 0, 0, 0, 0, 0, 1)
        self.angle = self.angle+0.04
        glEnable(GL_DEPTH_TEST)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glBindTexture(GL_TEXTURE_2D, self.texture_id)
    
        glEnable(GL_TEXTURE_2D)

        qobj = gluNewQuadric()
        gluQuadricTexture(qobj, GL_TRUE)
        gluSphere(qobj, 1, 50, 50)
        gluDeleteQuadric(qobj)

        glDisable(GL_TEXTURE_2D)
    
        glutSwapBuffers()
        glutPostRedisplay()        

See the preview:

See also Immediate mode and legacy OpenGL

Rabbid76
  • 202,892
  • 27
  • 131
  • 174