Pyopengl:用带纹理的立方体绘制的 gluSphere 是彩色的

Pyopengl: gluSphere drawn with a textured cube is colored

当我在 draw_shapes 函数中向立方体添加纹理时,即使我执行了 glColor3fv((1, 1, 1)),之后绘制的球体也会着色。我不知道这是为什么,当我移除纹理时,球体变回白色。如何让球体没有任何颜色?

完整代码:

import random
import time
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
array = [(0, 0, 0)]
width = 500
height = 500
random_number = 0
turned = 0
angle = 0
switch = False

vertices = [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)]
faces = [(4, 0, 3, 7), (1, 0, 4, 5), (0, 1, 2, 3), (1, 5, 6, 2), (3, 2, 6, 7), (5, 4, 7, 6)]
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1)]


def draw_shapes():
    global turned, switch, random_number, angle

    glPushMatrix()
    if turned >= 90:
        random_number = random.randint(0, 2)
        turned = angle = 0

    if random_number == 0:
        glRotatef(angle, 0, 1, 0)
    elif random_number == 1:
        glRotatef(angle, 1, 0, 0)
    else:
        glRotatef(angle, 0, 0, 1)

    turned += 1
    angle += 1
    glColor3fv((1, 1, 1))

    glBegin(GL_QUADS)
    for i, face in enumerate(faces):
        for surftex, vertex in enumerate(face):
            if surftex == 0:
                glTexCoord2f(1.0, 1.0)
            elif surftex == 1:
                glTexCoord2f(1.0, 0.0)
            elif surftex == 2:
                glTexCoord2f(0.0, 0.0)
            elif surftex == 3:
                glTexCoord2f(0.0, 1.0)
            glVertex3fv(vertices[vertex])
    glEnd()
    glPopMatrix()
    glPushMatrix()
    glColor3fv((1, 1, 1))
    glTranslatef(-3, 0, 0)
    Quadric = gluNewQuadric()
    gluSphere(Quadric, 1, 50, 50)
    glPopMatrix()





def textureBind():
    image = pygame.image.load("Image.png")
    datas = pygame.image.tostring(image, 'RGBA')

    glEnable(GL_TEXTURE_2D)
    texture_id = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.get_width(), image.get_height(),
                 0, GL_RGBA, GL_UNSIGNED_BYTE, datas)

    return texture_id


def showScreen():
    global width, height
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    draw_shapes()
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    glLightfv(GL_LIGHT0, GL_POSITION, (-2, -2, 2, 1))  # point light from the left, top, front
    glLightfv(GL_LIGHT0, GL_AMBIENT, (1, 1, 1, 1))
    glutSwapBuffers()


def mouseTracker(mousex, mousey):
    print(f"Mouse pos: {mousex}, {mousey}")


def reshapeWindow(x, y):
    global width, height
    width = x
    height = y
    print(x, y)
    glutReshapeWindow(width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, (width / height), 0.0001, 1000)
    glViewport(0, 0, width, height)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0, -1, -10)
    glRotatef(3, 1, 0, 0)


glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(500, 500)
wind = glutCreateWindow("OpenGL")
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)
glutMotionFunc(mouseTracker)
glutPassiveMotionFunc(mouseTracker)
glutReshapeFunc(reshapeWindow)
gluPerspective(45, (width / height), 0.0001, 1000)
gluNewQuadric()

while True:
    glutMainLoopEvent()
    glutPostRedisplay()
    time.sleep(0.01)

OpenGL 是一个状态引擎。一旦设置了状态,它就是持久的。激活纹理时,默认情况下纹理的颜色乘以当前颜色,因为默认情况下纹理环境模式(GL_TEXTURE_ENV_MODE)是GL_MODULATE。参见 glTexEnv. This causes that the color of the texture is "mixed" by the last color which you have set by glColor3f

如果要绘制没有纹理的对象,必须禁用纹理:

def draw_shapes():
    global turned, switch, random_number, angle

    glPushMatrix()
    if turned >= 90:
        random_number = random.randint(0, 2)
        turned = angle = 0

    if random_number == 0:
        glRotatef(angle, 0, 1, 0)
    elif random_number == 1:
        glRotatef(angle, 1, 0, 0)
    else:
        glRotatef(angle, 0, 0, 1)

    turned += 1
    angle += 1
    glColor3fv((1, 1, 1))
    glEnable(GL_TEXTURE_2D) # <--- enable

    glBegin(GL_QUADS)
    for i, face in enumerate(faces):
        for surftex, vertex in enumerate(face):
            if surftex == 0:
                glTexCoord2f(1.0, 1.0)
            elif surftex == 1:
                glTexCoord2f(1.0, 0.0)
            elif surftex == 2:
                glTexCoord2f(0.0, 0.0)
            elif surftex == 3:
                glTexCoord2f(0.0, 1.0)
            glVertex3fv(vertices[vertex])
    glEnd()
    glPopMatrix()
    glPushMatrix()
    
    glColor3fv((1, 1, 1))
    glDisable(GL_TEXTURE_2D) # <--- disable

    glTranslatef(-3, 0, 0)
    Quadric = gluNewQuadric()
    gluSphere(Quadric, 1, 50, 50)
    glPopMatrix()