1
\$\begingroup\$

I've made a gluSphere render in my display and have given it a cube map texture of the Earth. The texture is visible just fine (see below), but I cannot get it to rotate along with the shape. this question is closely related to my issue but the suggested solutions do not work, but instead produce a beachball effect on my sphere (see below) however, it does rotate so I guess thats nice.

What can I do to make my texture rotate with the sphere?

Thanks!

(This code is a slightly altered version of this)

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL.ARB.texture_cube_map import *
import numpy
from PIL import Image as Image
import sys

angle    = 0.1
texture  = 0
# glTexGeni mode
mode = GL_NORMAL_MAP_ARB

# this loads a cube map from six images
def loadCubemap ( faces, path = ""  ):
    texture = glGenTextures ( 1 )
    target_map  = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
    glBindTexture     ( GL_TEXTURE_CUBE_MAP_ARB, texture )
    glPixelStorei     ( GL_PACK_ALIGNMENT,1 )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_REPEAT )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_REPEAT )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
    glEnable          ( GL_TEXTURE_CUBE_MAP_ARB )

    for face in faces:
        if path != "":
            file = path + "/" + face
        else:
            file = face
        image  = Image.open ( file )
        width  = image.size [0]
        height = image.size [1]
        image  = image.tobytes()
        gluBuild2DMipmaps ( target_map, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image )
        target_map = target_map + 1
    return texture

#this will check if the OpenGL ARB extension is working
def extensionInit ():
    if not glInitTextureCubeMapARB ():
        print("ARB_texture_cubemap not working !")
        sys.exit ( 1 )

def init ():

    glClearColor(0, 0, 0, 1)
    glClearDepth ( 1.0 )
    glDepthFunc  ( GL_LEQUAL )
    glEnable     ( GL_DEPTH_TEST )
    glHint       ( GL_POLYGON_SMOOTH_HINT,         GL_NICEST )
    glHint       ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST )

#this will help when resizing the window
def reshape ( width, height ):
    glViewport     ( 0, 0, width, height )
    glMatrixMode   ( GL_PROJECTION )
    glLoadIdentity ()
    gluPerspective ( 55.0, float(width)/float (height), 1.0, 60.0 )
    glMatrixMode   ( GL_MODELVIEW )
    glLoadIdentity ()
    gluLookAt      ( 0.0, 6.0, 0.0,
                     4.0, -4.0, 4.0,
                     0.0, 0.5, 0.0 )

def display ():
    global texture, mode

    glClear   ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
    glEnable  ( GL_TEXTURE_CUBE_MAP_ARB )
    glEnable  ( GL_TEXTURE_GEN_S )
    glEnable  ( GL_TEXTURE_GEN_T )
    glEnable  ( GL_TEXTURE_GEN_R )
    glTexGeni ( GL_S, GL_TEXTURE_GEN_MODE, mode )
    glTexGeni ( GL_T, GL_TEXTURE_GEN_MODE, mode )
    glTexGeni ( GL_R, GL_TEXTURE_GEN_MODE, mode )

    glBindTexture   ( GL_TEXTURE_CUBE_MAP_ARB, texture )
    glPushMatrix    ()
    glTranslatef    ( 2, 2, 2 )
    glRotate       ( angle,  0, 10, 0 )
    glRotate       ( angle,  10, 0 , 0 )

    glutSolidSphere(1.5, 10, 10)

    glPopMatrix     ()
    glutSwapBuffers ()

# keyboard bindings
def keyPressed ( *args ):
    key = args [0]
    if key == '\033' or key == b'\x1b':
        sys.exit ()

# rotation
def animate ():
    global angle
    angle  = 0.01 * glutGet ( GLUT_ELAPSED_TIME )
    glutPostRedisplay ()

# main function
def main ():
    global texture

    glutInit               ( sys.argv )
    glutInitDisplayMode    ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH )
    glutInitWindowSize     ( 600, 400 )
    glutInitWindowPosition ( 0, 0 )

    glutCreateWindow ( "Render the Earth" )
    glutDisplayFunc  ( display )
    glutIdleFunc     ( animate )
    glutReshapeFunc  ( reshape )
    glutKeyboardFunc ( keyPressed )

    init          ()
    extensionInit ()

    texture = loadCubemap ( ( "east1.png",
                              "west.png",
                              "north.png",
                              "south.png",
                              "center.png",
                              "east2.png" ), "cubemap" )
    glutMainLoop()

if __name__ == "__main__":
  print("Hit ESC key to quit.")
  main()

This is the result of mode = GL_NORMAL_MAP_ARB: Cubemap "successfully" rendered to a sphere This is what I get when I do mode = GL_OBJECT_LINEAR (suggested here): enter image description here

\$\endgroup\$
  • \$\begingroup\$ How do you know the model is rotating if the texture remains in the same place? Also, why are using a magnitude of 10 on the rotation axis? It probably doesn't matter, but it's a little odd. (And of course, you should know that the fixed function pipeline (calls like glRotate and glTranslate), glu, and glut have all been deprecated for many years.) \$\endgroup\$ – user1118321 Oct 15 '19 at 2:08
  • \$\begingroup\$ I am able to see the 'sphere' rotate because It does not have very many vertices (so I can see the polygon move). And I am aware that this stuff has been depreciated for a while, I just figured I would try it out. \$\endgroup\$ – njasi Oct 15 '19 at 3:24
  • \$\begingroup\$ Well, if the model's moving and the texture isn't, then I'm guessing the texture coordinates are not correct. It's been too many years since I've used glTexGen for me to remember how it works, but I'd start by looking there. \$\endgroup\$ – user1118321 Oct 15 '19 at 6:57
  • \$\begingroup\$ @user1118321 It turns out that glutSolidSphere doesn't even generate texture coordinates (or something like that) \$\endgroup\$ – njasi Oct 16 '19 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.