2
votes

I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting a C tutorial to Python. Specifically the part below.

#define BUFSIZE 512
GLuint selectBuf[BUFSIZE]

void startPicking(int cursorX, int cursorY) {
    GLint viewport[4];

    glSelectBuffer(BUFSIZE,selectBuf);
    glRenderMode(GL_SELECT);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glGetIntegerv(GL_VIEWPORT,viewport);
    gluPickMatrix(cursorX,viewport[3]-cursorY,
            5,5,viewport);
    gluPerspective(45,ratio,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
    glInitNames();
}

I'm not sure how to turn declare arrays of GLuint or GLint such that glSelectBuffer and glPickMatrix work. Does anyone know how to do this in Python with Pyglet? Thanks.

3

3 Answers

5
votes

I haven't tried your particular example, but the normal way to declare arrays is in the ctypes documentation. Essentially you would create an array type like this:

FourGLints = GLint * 4
viewport = FourGLints(0, 1, 2, 3)
1
votes

I've had good luck with PyOpenGL.

http://pyopengl.sourceforge.net/

It's pretty straightforward, and using a C tutorial would be easier with it, I believe.

1
votes

Exactly what sort of trouble are you having? Pyglet's OpenGL implementation is a thin wrapper over the DLL and pretty much maps the C calls one-for-one. It's hard to imagine there would be any other library that could be better in terms of following a C tutorial.

For example, this introduction is pretty much identical to the C equivalent when it comes to the OpenGL calls:

from pyglet.gl import *

# Direct OpenGL commands to this window.
window = pyglet.window.Window()

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glBegin(GL_TRIANGLES)
    glVertex2f(0, 0)
    glVertex2f(window.width, 0)
    glVertex2f(window.width, window.height)
    glEnd()

pyglet.app.run()