实现pyglet打破了我曾经工作过的framebuffer OpenGL代码

时间:2010-03-23 17:58:38

标签: python opengl framebuffer pyglet pyopengl

这个问题重复了我之前的问题,但我之前的问题是失败的,因为我没有正确复制一些重要信息,所以我必须重做它。

我在调用OpenGL函数时遇到错误。也许pyglet没有正确初始化OpenGL?错误发生在以前有效的简单函数中:

 def setup_framebuffer(surface):
   #Create texture if not done already
   if surface.texture is None:
      create_texture(surface)
   #Render child to parent
   if surface.frame_buffer is None:
      surface.frame_buffer =  glGenFramebuffersEXT(1)
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, surface.frame_buffer)
   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface.texture, 0)
   glPushAttrib(GL_VIEWPORT_BIT)
   glViewport(0,0,surface._scale[0],surface._scale[1])
   glMatrixMode(GL_PROJECTION)
   glLoadIdentity() #Load the projection matrix
   gluOrtho2D(0,surface._scale[0],0,surface._scale[1])

错误是:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,surface.frame_buffer) ctypes.ArgumentError:参数2 ::错误类型

错误的类型?然后是glGenFramebuffersEXT(1)现在提供错误的类型?为什么会这样?

在调用该函数之前,我初始化了一个管理我的游戏的类实例。这是 init 方法:

pyglet.options['audio'] = ('alsa','openal','directsound','silent')
  self.keys = [False] * 323
  self.events = []
  self.title = title
  self.game_size = game_size
  self.first_screen = (1280,720) #Take 120 pixels from the height because the menu bar, window bar and dock takes space
  config = pyglet.gl.Config(alpha_size=8,double_buffer=True,sample_buffers=1,samples=4)
  self.window = pyglet.window.Window(game_size[0],game_size[1],title,True,config=config)
  self.window.set_handler('on_draw',self.game_loop)
  self.window.set_handler('on_resize',self.reshaped)
  self.window.set_handler('on_key_press',self.keydown)
  self.window.set_handler('on_key_release',self.keyup)
  self.window.set_handler('on_mouse_press',self.mouse_func)
  glViewport(0,0,self.first_screen[0],self.first_screen[1]) #Creates the viewport which is mapped to the window
  glEnable(GL_BLEND) #Enable alpha blending
  glEnable(GL_TEXTURE_2D) #Enable 2D Textures
  glEnable(GL_MULTISAMPLE) #Enable Multisampling anti-aliasing
  glEnable(GL_POLYGON_SMOOTH) #Enable antialiased polygons
  glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity() #Load the projection matrix
  gluOrtho2D(0,1280,720,0) #Set an orthorgraphic view
  self.game_gap = (0,0)
  self.on_exit = on_exit
  self.mod_key = 1024 if sys.platform == "darwin" else 64
  Surface.__init__(self,game_size)
  self.screen_change = True
  self.frames = [time.time()]
  self.fps = 60
  self.last_time = 0
  self.fade_surface = Surface([1280,720])
  pyglet.font.add_file(os.path.dirname(sys.argv[0]) + "/NEUROPOL.ttf")
  pyglet.font.load('NEUROPOL')

Surface是我创建的一个类,它有点像pygame.Surface类,但使用的是OpenGL纹理。

该方法设置Window和OpenGL(可能不正确,这是什么问题?)并在调用之后我为我的游戏设置了一些东西,它使用setup_framebuffer函数渲染纹理。然后调用pyglet.app.run()应该运行我的game_loop方法,因为我做了self.window.set_handler('on_draw,self.game_loop),但我的游戏在它到达之前就崩溃了。

这是我第一次使用pyglet。文档没有向我解释我做错了什么。有人可以帮忙吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

glBindFramebufferEXT需要指向缓冲区的指针。 AFAIK,在这种情况下你必须使用ctypes

from pyglet.gl import *
from ctypes import c_uint, byref

fb = c_uint()
glGenFramebuffersEXT(1, byref(fb))
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb) 

搜索pyglet mailing list以获取更好的示例。顺便说一下:

>> glGenFramebuffersEXT(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: this function takes at least 2 arguments (1 given)

修改 我应该猜到你正在使用pyOpenGL:

fb = int(glGenFramebuffersEXT(1))
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb)