我正在尝试实现一个单击并拖动时移动的相机。它使用glutMouseFunc来注册鼠标点击,这只是:
def on_click(self, button, state, x, y):
if button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
self.dragging = True
self.drag_x_origin = x
self.drag_y_origin = y
else:
self.dragging = False
在glutPassiveMotionFunc中它有:
def mouse_movement(self, x, y):
print "----------------------"
if self.dragging:
print "+++++++++++"
此时应该打印“++++++++++++++++++++++++++++++++++++++++++++++++它锁定了。当我运行该程序时,我得到一个无休止的系列“----------------------”,直到我点击,然后它就停止了。我在显示功能中打印,仍然有效,但由于某种原因,此功能锁定。谁能明白为什么?
大部分完整的代码:
class Camera():
def __init__(self):
self.camera_angle_horizontal = 1.0
self.camera_angle_vertical = 1.0
self.drag_x_origin = 0.0
self.drag_y_origin = 0.0
self.dragging = False
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(100, 100)
glutCreateWindow('cube')
self.init()
glutMouseFunc(self.on_click)
glutPassiveMotionFunc(self.mouse_movement)
glutDisplayFunc(self.display)
glutReshapeFunc(self.reshape)
glutKeyboardFunc(self.keyboard)
glutMainLoop()
def init(self):
glClearColor(0.0, 0.0, 0.0, 0.0)
glShadeModel(GL_FLAT)
def display(self):
print self.dragging, self.camera_angle_vertical, self.camera_angle_horizontal
...
glutPostRedisplay()
def reshape(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, w / h, 1.0, 20.0)
#glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
glMatrixMode(GL_MODELVIEW)
def on_click(self, button, state, x, y):
if button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
self.dragging = True
else:
self.dragging = False
def mouse_movement(self, x, y):
print "----------------------"
if self.dragging:
print "+++++++++++"
c = Camera()
答案 0 :(得分:0)
当我应该只使用glutPassiveMotionFunc(self.mouse_movement)
gluteMotionFunc(self.mouse_movement)