0

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.

J-bob
  • 8,380
  • 11
  • 52
  • 85

1 Answers1

0

You might be able to just use pygame coordinates / viewport to get coordinates in the [0;1] interval (e.g. for texture coordinates) or pygame coordinates / viewport * 2 - 1 for [-1;1] interval (NDC coordinates).

I think using glut for this won't work, because you create your window with pygame and glut knows nothing about that window. I might be wrong though, i know neither glut nor pygames.

And just a tip for googling, OpenGL itself does not know mouse coordinates at all, this is provided by glut.

karyon
  • 1,957
  • 1
  • 11
  • 16
  • What's the difference between OpenGL and GLUT? I just assumed GLUT was a component of OpenGL. – J-bob Jul 21 '16 at 14:23
  • 1
    @J-bob, GLUT is the "OpenGL Utility toolkit" that does input handling, creates windows, and creates the OpenGL context. so it's more about talking to the operating system. It also has some higher-level drawing methods. OpenGL is an API to talk to the GPU or rather, it's driver, so that's about actually moving triangles around, creating shaders etc. – karyon Jul 21 '16 at 14:30