I'm using OpenGL through Pygame to render things and I want to get OpenGL mouse information. I know that I can get mouse position & click state directly through Pygame, but I need mouse position in OpenGL's coordinates, not just pixel coordinates of the viewport. The issue is that I can't get OpenGL's mouse callbacks to fire. Consider the following code:
import pygame
from pygame.locals import DOUBLEBUF, OPENGL
from OpenGL.GLUT import glutMouseFunc, glutInit
def mouse_handler(button, state, x, y):
print("mouse handler: ",button, state, x, y)
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glutInit()
glutMouseFunc(mouse_handler)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame_mouse_pos = pygame.mouse.get_pos()
print(pygame_mouse_pos)
When you run this code it will start a continuous printout of the mouse position according to Pygame, which is obtained from the line pygame_mouse_pos = pygame.mouse.get_pos()
. But I can't get the OpenGL mouse callback, set up with glutMouseFunc(mouse_handler)
, to fire. Can somebody tell me what I'm doing wrong? This code runs in both Python 2 and 3 and I get the same results in each.