我正在尝试为 pyGLFW + Vispy + PyOpenGL 建立一个基本环境。尝试画画时,我得到
RuntimeError:OpenGL 出错(定期检查):GL_INVALID_OPERATION
追溯:
File "D:/Users/Emil/Documents/workspace/SynkFPS/newtest/start.py", line 109, in <module>
win.run()
File "D:/Users/Emil/Documents/workspace/SynkFPS/newtest/start.py", line 104, in run
self.display()
File "D:/Users/Emil/Documents/workspace/SynkFPS/newtest/start.py", line 96, in display
self.program.draw(gl.GL_TRIANGLE_STRIP)
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\program.py", line 491, in draw
self.activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\globject.py", line 56, in activate
self._activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\program.py", line 209, in _activate
self._activate_variables()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\program.py", line 316, in _activate_variables
attribute.activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\globject.py", line 56, in activate
self._activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\variable.py", line 335, in _activate
self._update()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\variable.py", line 362, in _update
self.data.activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\globject.py", line 56, in activate
self._activate()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\buffer.py", line 180, in _activate
self._update_data()
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\buffer.py", line 202, in _update_data
gl.check_error('periodic check')
File "C:\Python27\lib\site-packages\vispy-0.3.0-py2.7.egg\vispy\gloo\gl\__init__.py", line 181, in check_error
raise err
RuntimeError: OpenGL got errors (periodic check): GL_INVALID_OPERATION
我该怎么办?我试过不画画,效果很好。问题从 win.run() 开始,然后是 Window 中的 self.display() -> display() 中的 self.program.draw(...)。
#!usr/bin/env python
from __future__ import division
import ctypes as ct
import OpenGL.GL as gl
import pyglfw.pyglfw as glfw
import numpy as np
from OpenGL.arrays import vbo
from vispy.gloo import Program, VertexBuffer, IndexBuffer, VertexShader
# String globals -
# --------------
VERTEX_SHADER = """
uniform float scale;
attribute vec4 color;
attribute vec2 position;
varying vec4 v_color;
void main()
{
gl_Position = vec4(scale*position, 0.0, 1.0);
v_color = color;
}
"""
FRAGMENT_SHADER = """
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
}
"""
class Window(object):
""" Open glfw window """
def __init__(self, width, height):
# Basic setup
# -----------
glfw.init()
self.width = width
self.height = height
self.aspect = self.width / self.height
glfw.Window.hint(context_version=(4, 2))
self.win = glfw.Window(self.width, self.height, b"sample test example")
self.win.make_current()
# Data, FIXME will be implemented outside this class
self.data = np.zeros(4, [("position", np.float32, 2),
("color", np.float32, 4)])
self.data['color'] = [(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 1)]
self.data['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
# OpenGL Initialization
gl.glViewport(0, 0, self.width, self.height)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glClearColor(0, 0, 0, 0)
gl.glPolygonOffset(1, 1)
gl.glEnable(gl.GL_LINE_SMOOTH)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
gl.glLineWidth(0.75)
self.program = Program(VERTEX_SHADER, FRAGMENT_SHADER, count=4)
self.program['color'] = [(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 1, 0, 1)]
self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.program['scale'] = 1.0
# Set glfw callbacks
self.win.set_mouse_button_callback(self.on_mouse_button)
self.win.set_key_callback(self.on_keyboard)
self.win.set_window_size_callback(self.on_resize)
def on_mouse_button(self, win, button, action, mods):
print("[!]Pressed. Button:{} Action:{} Mods:{}".format(button, action, mods))
def on_keyboard(self, win, key, scancode, action, mods):
print("[!]Pressed Key:{} Scancode:{} Action:{} Mods:{}".format(key, scancode, action, mods))
if action == glfw.Window.PRESS:
if key == glfw.Keys.LEFT:
pass
elif key == glfw.Keys.RIGHT:
pass
elif key == glfw.Keys.ESCAPE:
self.win.should_close = True
def on_resize(self, win, width, height):
self.width = width
self.height = height
self.aspect = width / height
gl.glViewport(0, 0, self.width, self.height)
def display(self):
self.program.draw(gl.GL_TRIANGLE_STRIP)
def run(self):
glfw.set_time(0.0)
while not self.win.should_close:
t = glfw.get_time()
glfw.poll_events()
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
self.display()
self.win.swap_buffers()
win = Window(640, 480)
win.run()