为什么 OpenGL 不加载纹理(通用图像)?

问题描述 投票:0回答:2

我在将图像加载到 OpenGL 时遇到问题。我试图将图像放到一个块上(使用 OpenGL 生成的 3D 立方体),但我得到的只是一个空白块。我不确定我做错了什么,因为我遵循了在 OpenGL 中加载纹理的通常步骤。谁能就可能导致此问题的原因提供一些指导?

from OpenGL.GLU import *
from OpenGL.GL import *
import os

class BlockTexture:
    def __init__(self, name: str, block_id: int, texture: list):
        if not isinstance(name, str):
            raise TypeError("Name must be a string.")
        if not isinstance(block_id, int):
            raise TypeError("Block Id must be an integer.")
        if not isinstance(texture, list) or len(texture) != 3:
            raise ValueError("Texture must be a list of 3 elements.")
        self.name = name
        self.block_id = block_id
        self.texture = texture

    def load_texture(self, filepath):
        textureSurface = pygame.image.load(filepath)
        textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
        width = textureSurface.get_width()
        height = textureSurface.get_height()

        texture = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, texture)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
        return texture

    def test_texture_file(self):
        texture_ids = []
        for texture in self.texture:
            if not self.test_texture_filepath(texture):
                raise ValueError("Invalid texture file path.")
            texture_file, texture_ext = os.path.splitext(texture)
            if texture_ext.lower() not in ['.png', '.jpg', '.jpeg']:
                raise ValueError("Invalid texture file type.")
            texture_ids.append(self.load_texture(texture))
        return texture_ids

    def test_texture_filepath(self, filepath):
        if not isinstance(filepath, str):
            return False
        if not os.path.isfile(filepath):
            return False
        return True

class Block(BlockTexture):
    def __init__(self, name, block_id, texture):
        super().__init__(name, block_id, texture)
        self.texture_ids = self.test_texture_file()

    def draw(self, x, y, z):
        # Top Face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[0])
        glBegin(GL_QUADS)
        glTexCoord2f(0, 1)
        glVertex3f(x, y + 1, z)
        glTexCoord2f(1, 1)
        glVertex3f(x + 1, y + 1, z)
        glTexCoord2f(1, 0)
        glVertex3f(x + 1, y + 1, z + 1)
        glTexCoord2f(0, 0)
        glVertex3f(x, y + 1, z + 1)
        glEnd()

        # Front Face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[1])
        glBegin(GL_QUADS)
        glTexCoord2f(0, 1)
        glVertex3f(x + 1, y + 1, z + 1)
        glTexCoord2f(1, 1)
        glVertex3f(x, y + 1, z + 1)
        glTexCoord2f(1, 0)
        glVertex3f(x, y, z + 1)
        glTexCoord2f(0, 0)
        glVertex3f(x + 1, y, z + 1)
        glEnd()

        # Back Face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[1])
        glBegin(GL_QUADS)
        glTexCoord2f(1, 1)
        glVertex3f(x, y + 1, z)
        glTexCoord2f(0, 1)
        glVertex3f(x + 1, y + 1, z)
        glTexCoord2f(0, 0)
        glVertex3f(x + 1, y, z)
        glTexCoord2f(1, 0)
        glVertex3f(x, y, z)
        glEnd()

        # Left face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[1])
        glBegin(GL_QUADS)
        glTexCoord2f(0, 1)
        glVertex3f(x, y + 1, z + 1)
        glTexCoord2f(1, 1)
        glVertex3f(x, y + 1, z)
        glTexCoord2f(1, 0)
        glVertex3f(x, y, z)
        glTexCoord2f(0, 0)
        glVertex3f(x, y, z + 1)
        glEnd()

        # Right face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[1])
        glBegin(GL_QUADS)
        glTexCoord2f(1, 1)
        glVertex3f(x + 1, y + 1, z)
        glTexCoord2f(0, 1)
        glVertex3f(x + 1, y + 1, z + 1)
        glTexCoord2f(0, 0)
        glVertex3f(x + 1, y, z + 1)
        glTexCoord2f(1, 0)
        glVertex3f(x + 1, y, z)
        glEnd()

        # Bottom face
        glBindTexture(GL_TEXTURE_2D, self.texture_ids[0])
        glBegin(GL_QUADS)
        glTexCoord2f(0, 0)
        glVertex3f(x, y, z + 1)
        glTexCoord2f(1, 0)
        glVertex3f(x + 1, y, z + 1)
        glTexCoord2f(1, 1)
        glVertex3f(x + 1, y, z)
        glTexCoord2f(0, 1)
        glVertex3f(x, y, z)
        glEnd()

在尝试将旋转立方体加载到 pygame 窗口并遇到问题后,我尝试使用其他框架并将图像大小更改为 16x16 以外的大小。然而,这些尝试并没有解决问题,我仍然无法让图像正常显示。在这一点上,我不确定这个问题的原因。我试过测试不同的图像格式并将它们设置为背景。效果非常好。但我不明白为什么它不把图像放在块中。这是初始化 pygame 窗口:

import pygame
from pygame.locals import *

def test_pygame_opengl():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)

    GrassBlock = Block(
        name="grass_block",
        block_id=1,
        texture=[
           "grass_block_top16x16.png",
           "grass_block_side16x16.png",
           "grass_block_bottom16x16.png"
        ])

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        GrassBlock.draw(0, 0, 0)
        pygame.display.flip()
        pygame.time.wait(10)

test_pygame_opengl()
python opengl pygame pyopengl
2个回答
0
投票

必须启用二维纹理,请参阅

glEnable
。在绘制几何之前启用纹理(在
glBegin
之前):

glEnable(GL_TEXTURE_2D)

由于OpenGL是一个状态引擎,如果你想绘制没有纹理的几何体,你必须再次禁用纹理(

glDisable(GL_TEXTURE_2D)
)。


-2
投票

12412424124124235dfgdfgfdgdfgdfgfdgsadfasfasdasdasddsfsdf

© www.soinside.com 2019 - 2024. All rights reserved.