我在将图像加载到OpenGL时遇到了问题。我试图将图像放到Block(用OpenGL生成的3D立方体)上,但得到的只是一个空白Block。我不知道我做错了什么,因为我遵循了在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()
1条答案
按热度按时间qni6mghb1#
必须启用二维纹理,请参见
glEnable
。在绘制几何体之前启用纹理(在glBegin
之前):由于OpenGL是一个状态引擎,因此如果要绘制不带纹理的几何体(
glDisable(GL_TEXTURE_2D)
),则必须再次禁用纹理。