Salo7ty
Salo7ty

Reputation: 495

PyOpengl , overlapping textures

i am new in opengl using python with PyOpengl , i have a problem , i am tying to render a cube and texture it using some images , the cube has ( face , back , right side , left side ) the problem is that the textures that drawing in last is overlapping all other textures like that :

enter image description here

enter image description here

the link to video shows the problem

my code :

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)

i am begginer so maybe i forget somthing to write or anything , can u help me please

any one have solution for that ??

thanks brothers :)

Upvotes: 1

Views: 452

Answers (1)

Rabbid76
Rabbid76

Reputation: 211230

You need to enable the Depth Test. The depth test compares the depth of a new fragment with the depth stored in the depth buffer. Fragments are discarded depending on the result of the depth test. The default depth function is GL_LESS. Therefore, fragments that are in the "back" are discarded.

def init():

    # [...]

    glEnable(GL_DEPTH_TEST)

Upvotes: 1

Related Questions