How can I fix textures and show two objects correctly with PyOpenGL?

27 Views Asked by At

I should write a code which shows one object a time in the screen using PyOpenGL. Altough I cannot make it work as intended. I created classes for the environment where I'll be adding objects (Environment) and for the objects (GLObjects).

Started with two objects (basset and box). When I add the basset before the box in the environment, I obtain the following:

Box at PyOpenGL

That is, a box with the basset texture.

I dont know what is going wrong and how to fix it. Above, there are some fragments of code related to the question. The complete project can be found at https://github.com/vitorfrois/usp/tree/main/6thPeriod/GraphicalComputing/T1.

basset = GLObject('basset')
basset.init_obj()
env.add_object(basset)

box = GLObject('caixa')
box.init_obj()
env.add_object(box)
mat_transform = Matrix.multiply(y_rotation, z_rotation, scale, 
logger.info(mat_transform)
loc, 1, GL_TRUE, mat_transform)
draw_obj()
def load_texture_from_file(img_textura, texture_id):
        logger.info(f"tid: {texture_id}")
        glBindTexture(GL_TEXTURE_2D, texture_id)
        
        img = Image.open(img_textura)
        img_width = img.size[0]
        img_height = img.size[1]
        img_format = GL_RGB if img.mode == "RGB" else GL_RGBA
        image_data = img.tobytes("raw", "RGB", 0, -1)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img_width, img_height, 0, img_format, GL_UNSIGNED_BYTE, image_data)
        glGenerateMipmap(GL_TEXTURE_2D)

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

        # glBindTexture(GL_TEXTURE_2D, 0)

        return texture_id
    def init_obj(self):
        modelo = ObjHelper.read_file(f'resources/{self.name}.obj')

        ### inserindo vertices do modelo no vetor de vertices
        logger.info(f'Processando modelo {self.name}')
        for face in modelo['faces']:
            for vertice_id in face[0]:
                self.list_vertices.append( modelo['vertices'][vertice_id-1] )
            for texture_coord in face[1]:
                self.list_texture.append( modelo['texture'][texture_coord-1] )

        self.number = glGenTextures(1)
        self.n_vertices = len(self.list_vertices)
        ### inserindo coordenadas de textura do modelo no vetor de texturas
        for extension in TEXTURES_EXT:
            try:
                ObjHelper.load_texture_from_file(f'resources/{self.name}.{extension}', self.number)
                break
            except FileNotFoundError:
                logger.error(f"Did not found {extension} texture file")

        self.init_vertices(self.list_vertices)
        self.init_texture(self.list_texture)
        logger.info(f'Texture ID: {self.number}, ')

    def send_obj_vertices(self, env):
        glBindBuffer(GL_ARRAY_BUFFER, env.buffer[0])
        glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW)
        stride = self.vertices.strides[0]

        loc = env.get_loc()
        glEnableVertexAttribArray(loc)
        glVertexAttribPointer(loc, 3, GL_FLOAT, False, stride, offset)

    def send_obj_texture(self, env):
        if len(self.texture) == 0:
            logger.info('No texture list!')
            return

        glBindBuffer(GL_ARRAY_BUFFER, env.buffer[1])
        glBufferData(GL_ARRAY_BUFFER, self.texture.nbytes, self.texture, GL_STATIC_DRAW)
        stride = self.texture.strides[0]    

        texture_loc = env.get_texture_loc()
        glEnableVertexAttribArray(texture_loc)
        glVertexAttribPointer(texture_loc, 2, GL_FLOAT, False, stride, offset)
0

There are 0 best solutions below