我找到了使用 pyglet 渲染立方体的代码,并且观看了该代码附带的视频,它在 Nvidia 显卡上正确显示了立方体渲染。当我在我的 AMD 显卡上尝试代码时,它崩溃了,我该如何修复这个问题?
github链接:https://github.com/obiwac/python-minecraft-clone/tree/master/episode-7
视频链接:https://www.youtube.com/watch?v=U9Ldr_PeRA8
我得到了什么:
视频显示的内容:
来自 main.py 的代码
import math
import ctypes
import pyglet
pyglet.options["shadow_window"] = False
pyglet.options["debug_gl"] = False
import pyglet.gl as gl
import matrix
import shader
import camera
import block_type
import texture_manager
class Window(pyglet.window.Window):
def __init__(self, **args):
super().__init__(**args)
# create blocks
self.texture_manager = texture_manager.Texture_manager(16, 16, 256)
self.cobblestone = block_type.Block_type(self.texture_manager, "cobblestone", {"all": "cobblestone"})
self.grass = block_type.Block_type(self.texture_manager, "grass", {"top": "grass", "bottom": "dirt", "sides": "grass_side"})
self.dirt = block_type.Block_type(self.texture_manager, "dirt", {"all": "dirt"})
self.stone = block_type.Block_type(self.texture_manager, "stone", {"all": "stone"})
self.sand = block_type.Block_type(self.texture_manager, "sand", {"all": "sand"})
self.planks = block_type.Block_type(self.texture_manager, "planks", {"all": "planks"})
self.log = block_type.Block_type(self.texture_manager, "log", {"top": "log_top", "bottom": "log_top", "sides": "log_side"})
self.texture_manager.generate_mipmaps()
# create vertex array object
self.vao = gl.GLuint(0)
gl.glGenVertexArrays(1, ctypes.byref(self.vao))
gl.glBindVertexArray(self.vao)
# create vertex position vbo
self.vertex_position_vbo = gl.GLuint(0)
gl.glGenBuffers(1, ctypes.byref(self.vertex_position_vbo))
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertex_position_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.grass.vertex_positions)),
(gl.GLfloat * len(self.grass.vertex_positions)) (*self.grass.vertex_positions),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(0)
# create tex coord vbo
self.tex_coord_vbo = gl.GLuint(0)
gl.glGenBuffers(1, ctypes.byref(self.tex_coord_vbo))
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.tex_coord_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.grass.tex_coords)),
(gl.GLfloat * len(self.grass.tex_coords)) (*self.grass.tex_coords),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(1)
# create shading value vbo
self.shading_value_vbo = gl.GLuint(0)
gl.glGenBuffers(1, ctypes.byref(self.shading_value_vbo))
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.shading_value_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.grass.shading_values)),
(gl.GLfloat * len(self.grass.shading_values)) (*self.grass.shading_values),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(2, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(2)
# create index buffer object
self.ibo = gl.GLuint(0)
gl.glGenBuffers(1, self.ibo)
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.ibo)
gl.glBufferData(
gl.GL_ELEMENT_ARRAY_BUFFER,
ctypes.sizeof(gl.GLuint * len(self.grass.indices)),
(gl.GLuint * len(self.grass.indices)) (*self.grass.indices),
gl.GL_STATIC_DRAW)
# create shader
self.shader = shader.Shader("vert.glsl", "frag.glsl")
self.shader_sampler_location = self.shader.find_uniform(b"texture_array_sampler")
self.shader.use()
# pyglet stuff
pyglet.clock.schedule_interval(self.update, 1.0 / 60)
self.mouse_captured = False
# camera stuff
self.camera = camera.Camera(self.shader, self.width, self.height)
def on_draw(self):
self.camera.update_matrices()
# bind textures
gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_manager.texture_array)
gl.glUniform1i(self.shader_sampler_location, 0)
# draw stuff
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
self.clear()
gl.glDrawElements(
gl.GL_TRIANGLES,
len(self.grass.indices),
gl.GL_UNSIGNED_INT,
None)
*注意:省略了部分相机移动代码,要查看完整代码,请参阅github链接。
我已经与不和谐的创建者进行了交谈,他已确认他使用的是 Nvidia 卡,非常感谢解决此问题的任何帮助!
最佳答案
您必须确保 pyglet 窗口的默认帧缓冲区具有深度缓冲区。请参阅Creating an OpenGL context :
config = pyglet.gl.Config(depth_size = 24)
window = pyglet.window.Window(config = config)
如果您的硬件不支持 24 位深度缓冲区,您可能需要尝试 16 位:
config = pyglet.gl.Config(depth_size = 16)
window = pyglet.window.Window(config = config)
关于python - openGL(pyglet) 3d 场景无法在 AMD 显卡上正确渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64828949/