Why my texture is not showing PyOpenGL

前端 未结 1 1354
南旧
南旧 2020-12-11 10:57

I tried everything but still I don\'t get my error. I am trying to put a texture on my sphere object.

\"\"\"
Minimal texture on sphere demo
This is demo for          


        
1条回答
  •  执念已碎
    2020-12-11 11:34

    In read_texture() after you generate a texture name you don't bind it. So the subsequent texture related calls are going to the default texture and not your newly created texture.

    texture_id = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texture_id) # This is what's missing
    

    Also since your image is a PNG image. Then list(img.getdata()) will be a list of tuples containing (R, G, B, A). So when calling glTexImage2D you need to tell that your data is GL_RGBA and not GL_RGB.

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, img_data)
    

    You can also automate it.

    format = GL_RGB if img.mode == "RGB" else GL_RGBA
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
                 format, GL_UNSIGNED_BYTE, img_data)
    

    You can also convert it to not have an alpha channel.

    img = img.convert("RGB")
    

    Which of course needs to be done before calling img.getdata().

    Now you should see the following:

    Also an important note. You're calling read_texture() in draw_sphere(). This means that every time draw_sphere() is called, the image is loaded and a new texture is created. You really don't want to do that. Instead before calling glutMainLoop() call read_texture() and store the result as a global name.

    0 讨论(0)
提交回复
热议问题