How does PyOpenGL do this magic with glGenBuffers?

I am trying to avoid the slow and high level frontbind interface of PyOpenGL and specifically use the original implementation ...

The problem I am facing, I can not understand how the external interface turns the backend function glGenBuffers( n, buffers )into buffers = glGenBuffers( n ).

All I want to know is what I pass to the argument buffersto the backend function

+4
source share
1 answer

Here are some examples of creating one or more buffers using glGenBuffers. If you must use function calls such as glGenBuffers(n, buffers), you can directly use ctypes or use PyOpenGL GLint, which completes the data type of the ctypes type.

import ctypes
import numpy as np
from OpenGL.GL import *
import pygame

pygame.init()
screen = pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF) #initialize OpenGL

py_id = glGenBuffers(1) #typical way to generate a single index
c_id = ctypes.c_int() #using ctypes to generate a second index
glGenBuffers(1, c_id)
gl_id = GLint() #using GL wrapper to generate a third index
glGenBuffers(1, gl_id)
print py_id, c_id.value, gl_id.value #should print 1 2 3

n = 10
py_list = glGenBuffers(n) #typical way to generate multiple indices
c_list = (ctypes.c_int * n)() #using ctypes to generate multiple indices
glGenBuffers(n, c_list)
gl_list = (GLint * n)() #using GL wrapper to generate multiple indices
glGenBuffers(n, gl_list)
print py_list, list(py_list) #note the default is a numpy array!
print np.array(c_list), list(c_list)
print np.array(gl_list), list(gl_list)

, . , glGenBuffers , , buffers = glGenBuffers(n). , . , !

Update: __call__, , . , , :

import ctypes
import numpy as np
from OpenGL.GL import *
import pygame
import timeit

pygame.init()
screen = pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF) #initialize OpenGL

def test(func, repeats):
    start_time = timeit.default_timer()
    for i in range(repeats):
        func()
    elapsed = timeit.default_timer() - start_time
    print elapsed

def func1():
    py_id = glGenBuffers(1)

def func2():
    c_id = ctypes.c_int()
    glGenBuffers(1, c_id)

def func3():
    gl_id = GLint()
    glGenBuffers(1, gl_id)

def func4():
    n = 1
    py_list = glGenBuffers(n)

def func5():
    n = 1
    c_list = (ctypes.c_int * n)()
    glGenBuffers(n, c_list)

def func6():
    n = 1
    gl_list = (GLint * n)()
    glGenBuffers(n, gl_list)

def func7():
    n = 100
    py_list = glGenBuffers(n)

def func8():
    n = 100
    c_list = (ctypes.c_int * n)()
    glGenBuffers(n, c_list)

def func9():
    n = 100
    gl_list = (GLint * n)()
    glGenBuffers(n, gl_list)

test(func1, 1000000)#4.12597930903
test(func2, 1000000)#5.2951610055
test(func3, 1000000)#5.17853478658
test(func4, 1000000)#4.06362866711
test(func5, 1000000)#3.45259988251
test(func6, 1000000)#3.43240155354
test(func7, 1000000)#4.128162421
test(func8, 1000000)#3.57384911559
test(func9, 1000000)#3.52125689729

, , , , , n = 1. , , , - . , , GLint() , ctypes , . , python , , OpenGL !

+2

Source: https://habr.com/ru/post/1662744/


All Articles