PyOpengl ,重叠纹理

PyOpengl , overlapping textures

我是 opengl 的新手,使用 python 和 PyOpengl,我有一个问题,我想渲染一个立方体并使用一些图像对其进行纹理处理,立方体有(正面、背面、右侧、左侧边 ) 问题是最后绘制的纹理与所有其他纹理重叠:

link视频显示问题

我的代码:

import pygame
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import sys
import cv2


pygame.init()

screen = pygame.display.set_mode(
    (1024,1024),pygame.DOUBLEBUF|
    pygame.OPENGL|pygame.OPENGLBLIT)


texture=None
def cube():
    
    glBindTexture(GL_TEXTURE_2D, texture[0])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0,0.0); glVertex3fv((-0.5,-0.5,1))
    glTexCoord2f(1.0,0.0); glVertex3fv((0.5,-0.5,1))
    glTexCoord2f(1.0,1.0); glVertex3fv((0.5,0.5,1))
    glTexCoord2f(0.0,1.0); glVertex3fv((-0.5,0.5,1))
    glEnd()
    
    glBindTexture(GL_TEXTURE_2D, texture[1])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0,0.0); glVertex3fv((-0.5,-0.5,-1))
    glTexCoord2f(1.0,0.0); glVertex3fv((0.5,-0.5,-1))
    glTexCoord2f(1.0,1.0); glVertex3fv((0.5,0.5,-1))
    glTexCoord2f(0.0,1.0); glVertex3fv((-0.5,0.5,-1))
    glEnd()
    
    glBindTexture(GL_TEXTURE_2D, texture[2])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0,0.0); glVertex3fv((0.5,-0.5,1))
    glTexCoord2f(1.0,0.0); glVertex3fv((0.5,-0.5,-1))
    glTexCoord2f(1.0,1.0); glVertex3fv((0.5,0.5,-1))
    glTexCoord2f(0.0,1.0); glVertex3fv((0.5,0.5,1))
    glEnd()
    
    glBindTexture(GL_TEXTURE_2D, texture[3])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0,0.0); glVertex3fv((-0.5,-0.5,1))
    glTexCoord2f(1.0,0.0); glVertex3fv((-0.5,-0.5,-1))
    glTexCoord2f(1.0,1.0); glVertex3fv((-0.5,0.5,-1))
    glTexCoord2f(0.0,1.0); glVertex3fv((-0.5,0.5,1))
    glEnd()
    glFlush()
    
    
images=['src','shab','milan','raja']
def loadimgae():
    global texture 
    
    texture = glGenTextures(len(images))
    for i in range(len(images)):
        im = cv2.imread(images[i]+'.jpg');
        im=cv2.flip(im,0)
        im=cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
        im=im.astype(np.float32)
    
    
        glBindTexture(GL_TEXTURE_2D, texture[i])
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameterf(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)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, im.shape[0], im.shape[1], 0, GL_RGB, 
GL_UNSIGNED_BYTE, im)

    
    
def init():
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1, 0.1, 100.0)
    
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0.0,0.0,-5.0)
    
    glEnable(GL_TEXTURE_2D);
    
    loadimgae()
    
   
    
init()
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
    glClear(GL_COLOR_BUFFER_BIT|
            GL_DEPTH_BUFFER_BIT)
    glRotatef(1.0,0.0,1.0,0.0)

    cube()
    
    pygame.display.flip()
    pygame.time.wait(10)

我是初学者,所以可能我忘了写点东西什么的,你能帮帮我吗

有人对此有解决方案吗??

谢谢兄弟们:)

您需要启用 Depth Test。深度测试将新片段的深度与存储在深度缓冲区中的深度进行比较。根据深度测试的结果丢弃片段。默认的深度函数是 GL_LESS。因此,“后面”的片段将被丢弃。

def init():

    # [...]

    glEnable(GL_DEPTH_TEST)