Cleanup ctypes bindings.
This commit is contained in:
parent
eaff99a5b6
commit
84ea17f1cc
231
__init__.py
231
__init__.py
@ -14,12 +14,38 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ctypes
|
||||
import struct
|
||||
from array import array
|
||||
from pathlib import Path
|
||||
|
||||
_lib = ctypes.cdll.LoadLibrary(Path(__file__).parent / "engine.so")
|
||||
|
||||
class vec3(ctypes.Structure):
|
||||
_fields_ = ('x', ctypes.c_float), ('y', ctypes.c_float), ('z', ctypes.c_float)
|
||||
|
||||
def set(self, x, y, z):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
|
||||
class vec4(ctypes.Structure):
|
||||
_fields_ = ('x', ctypes.c_float), ('y', ctypes.c_float), ('z', ctypes.c_float), ('w', ctypes.c_float)
|
||||
|
||||
def set(self, x, y, z, w):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.w = w
|
||||
|
||||
class mat3(ctypes.Structure):
|
||||
_fields_ = ('x', vec3), ('y', vec3), ('z', vec3)
|
||||
|
||||
class mat4(ctypes.Structure):
|
||||
_fields_ = ('x', vec4), ('y', vec4), ('z', vec4), ('w', vec4)
|
||||
|
||||
vec3_right = vec3(1.0, 0.0, 0.0)
|
||||
vec3_forward = vec3(0.0, 1.0, 0.0)
|
||||
vec3_up = vec3(0.0, 0.0, 1.0)
|
||||
|
||||
def _flag(x):
|
||||
return 1 << x
|
||||
|
||||
@ -59,57 +85,10 @@ INSTANCE_FLAG_VISIBLE = _flag(1)
|
||||
|
||||
BATCH_MAX_SIZE = 65536
|
||||
|
||||
#TODO: remove from engine
|
||||
vec2_zero = (0.0, 0.0)
|
||||
vec3_zero = (0.0, 0.0, 0.0)
|
||||
vec3_right = (1.0, 0.0, 0.0)
|
||||
vec3_forward = (0.0, 1.0, 0.0)
|
||||
vec3_up = (0.0, 0.0, 1.0)
|
||||
vec4_zero = (0.0, 0.0, 0.0, 1.0)
|
||||
mat3_identity = (
|
||||
1.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 1.0)
|
||||
mat4_identity = (
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 1.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0)
|
||||
|
||||
def vec2(v = vec2_zero):
|
||||
assert len(v) == 2
|
||||
return array('f', v)
|
||||
|
||||
def vec3(v = vec3_zero):
|
||||
assert len(v) == 3
|
||||
return array('f', v)
|
||||
|
||||
def vec4(v = vec4_zero):
|
||||
assert len(v) == 4
|
||||
return array('f', v)
|
||||
|
||||
def mat3(m = mat3_identity):
|
||||
assert len(m) == 9
|
||||
return array('f', m)
|
||||
|
||||
def mat4(m = mat4_identity):
|
||||
assert len(m) == 16
|
||||
return array('f', m)
|
||||
|
||||
_vec2_t = (ctypes.c_float * 2)
|
||||
_vec2 = _vec2_t.from_buffer
|
||||
|
||||
_vec3_t = (ctypes.c_float * 3)
|
||||
_vec3 = _vec3_t.from_buffer
|
||||
|
||||
_vec4_t = (ctypes.c_float * 4)
|
||||
_vec4 = _vec4_t.from_buffer
|
||||
|
||||
_mat3_t = (ctypes.c_float * 9)
|
||||
_mat3 = _mat3_t.from_buffer
|
||||
|
||||
_mat4_t = (ctypes.c_float * 16)
|
||||
_mat4 = _mat4_t.from_buffer
|
||||
_vec3p = ctypes.POINTER(vec3)
|
||||
_vec4p = ctypes.POINTER(vec4)
|
||||
_mat3p = ctypes.POINTER(mat3)
|
||||
_mat4p = ctypes.POINTER(mat4)
|
||||
|
||||
def _voidp(x):
|
||||
return x.buffer_info()[0]
|
||||
@ -130,88 +109,56 @@ def _floatp(x):
|
||||
assert x.typecode == 'f'
|
||||
return x.buffer_info()[0]
|
||||
|
||||
_mat3_rotation = _lib.rk_mat3_rotation
|
||||
_mat3_rotation.argtypes = (
|
||||
_mat3_t, # ret
|
||||
_vec3_t, # axis
|
||||
mat3_rotation = _lib.rk_mat3_rotation
|
||||
mat3_rotation.argtypes = (
|
||||
_mat3p, # ret
|
||||
_vec3p, # axis
|
||||
ctypes.c_float) # angle
|
||||
|
||||
def mat3_rotation(ret, axis, angle):
|
||||
assert len(ret) == 9 and len(axis) == 3
|
||||
_mat3_rotation(_mat3(ret), _vec3(axis), angle)
|
||||
mat3_mul_vec3 = _lib.rk_mat3_mul_vec3
|
||||
mat3_mul_vec3.argtypes = (
|
||||
_vec3p, # ret
|
||||
_mat3p, # a
|
||||
_vec3p) # b
|
||||
|
||||
_mat3_mul_vec3 = _lib.rk_mat3_mul_vec3
|
||||
_mat3_mul_vec3.argtypes = (
|
||||
_vec3_t, # ret
|
||||
_mat3_t, # a
|
||||
_vec3_t) # b
|
||||
mat3_mul_mat3 = _lib.rk_mat3_mul_mat3
|
||||
mat3_mul_mat3.argtypes = (
|
||||
_mat3p, # ret
|
||||
_mat3p, # a
|
||||
_mat3p) # b
|
||||
|
||||
def mat3_mul_vec3(ret, a, b):
|
||||
assert len(ret) == 3 and len(a) == 9 and len(b) == 3
|
||||
_mat3_mul_vec3(_vec3(ret), _mat3(a), _vec3(b))
|
||||
|
||||
_mat3_mul_mat3 = _lib.rk_mat3_mul_mat3
|
||||
_mat3_mul_mat3.argtypes = (
|
||||
_mat3_t, # ret
|
||||
_mat3_t, # a
|
||||
_mat3_t) # b
|
||||
|
||||
def mat3_mul_mat3(ret, a, b):
|
||||
assert len(ret) == 9 and len(a) == 9 and len(b) == 9
|
||||
_mat3_mul_mat3(_mat3(ret), _mat3(a), _mat3(b))
|
||||
|
||||
_mat4_projection = _lib.rk_mat4_projection
|
||||
_mat4_projection.argtypes = (
|
||||
_mat4_t, # ret
|
||||
mat4_projection = _lib.rk_mat4_projection
|
||||
mat4_projection.argtypes = (
|
||||
_mat4p, # ret
|
||||
ctypes.c_float, # hfov
|
||||
ctypes.c_float, # ratio
|
||||
ctypes.c_float, # near
|
||||
ctypes.c_float) # far
|
||||
|
||||
def mat4_projection(ret, hfov, ratio, near, far):
|
||||
assert len(ret) == 16
|
||||
_mat4_projection(_mat4(ret), hfov, ratio, near, far)
|
||||
mat4_lookat = _lib.rk_mat4_lookat
|
||||
mat4_lookat.argtypes = (
|
||||
_mat4p, # ret
|
||||
_vec3p, # position
|
||||
_vec3p) # lookat
|
||||
|
||||
_mat4_lookat = _lib.rk_mat4_lookat
|
||||
_mat4_lookat.argtypes = (
|
||||
_mat4_t, # ret
|
||||
_vec3_t, # position
|
||||
_vec3_t) # lookat
|
||||
|
||||
def mat4_lookat(ret, position, lookat):
|
||||
assert len(ret) == 16 and len(position) == 3 and len(lookat) == 3
|
||||
_mat4_lookat(_mat4(ret), _vec3(position), _vec3(lookat))
|
||||
|
||||
_mat4_mul_vec3 = _lib.rk_mat4_mul_vec3
|
||||
_mat4_mul_vec3.argtypes = (
|
||||
_vec3_t, # ret
|
||||
_mat4_t, # a
|
||||
_vec3_t, # b
|
||||
mat4_mul_vec3 = _lib.rk_mat4_mul_vec3
|
||||
mat4_mul_vec3.argtypes = (
|
||||
_vec3p, # ret
|
||||
_mat4p, # a
|
||||
_vec3p, # b
|
||||
ctypes.c_float) # w
|
||||
|
||||
def mat4_mul_vec3(ret, a, b, w):
|
||||
assert len(ret) == 3 and len(a) == 16 and len(b) == 3
|
||||
_mat4_mul_vec3(_vec3(ret), _mat4(a), _vec3(b), w)
|
||||
mat4_mul_vec4 = _lib.rk_mat4_mul_vec4
|
||||
mat4_mul_vec4.argtypes = (
|
||||
_vec4p, # ret
|
||||
_mat4p, # a
|
||||
_vec4p) # b
|
||||
|
||||
_mat4_mul_vec4 = _lib.rk_mat4_mul_vec4
|
||||
_mat4_mul_vec4.argtypes = (
|
||||
_vec4_t, # ret
|
||||
_mat4_t, # a
|
||||
_vec4_t) # b
|
||||
|
||||
def mat4_mul_vec4(ret, a, b):
|
||||
assert len(ret) == 4 and len(a) == 16 and len(b) == 4
|
||||
_mat4_mul_vec4(_vec4(ret), _mat4(a), _vec4(b))
|
||||
|
||||
_mat4_mul_mat4 = _lib.rk_mat4_mul_mat4
|
||||
_mat4_mul_mat4.argtypes = (
|
||||
_mat4_t, # ret
|
||||
_mat4_t, # a
|
||||
_mat4_t) # b
|
||||
|
||||
def mat4_mul_mat4(ret, a, b):
|
||||
assert len(ret) == 16 and len(a) == 16 and len(b) == 16
|
||||
_mat4_mul_mat4(_mat4(ret), _mat4(a), _mat4(b))
|
||||
mat4_mul_mat4 = _lib.rk_mat4_mul_mat4
|
||||
mat4_mul_mat4.argtypes = (
|
||||
_mat4p, # ret
|
||||
_mat4p, # a
|
||||
_mat4p) # b
|
||||
|
||||
initialize = _lib.rk_initialize
|
||||
initialize.restype = ctypes.c_void_p
|
||||
@ -306,41 +253,25 @@ set_input_float.argtypes = (
|
||||
ctypes.c_void_p, # input
|
||||
ctypes.c_float) # value
|
||||
|
||||
_set_input_vec3 = _lib.rk_set_input_vec3
|
||||
_set_input_vec3.argtypes = (
|
||||
set_input_vec3 = _lib.rk_set_input_vec3
|
||||
set_input_vec3.argtypes = (
|
||||
ctypes.c_void_p, # input
|
||||
_vec3_t) # value
|
||||
_vec3p) # value
|
||||
|
||||
def set_input_vec3(input, value):
|
||||
assert len(value) == 3
|
||||
_set_input_vec3(input, _vec3(value))
|
||||
|
||||
_set_input_mat3 = _lib.rk_set_input_mat3
|
||||
_set_input_mat3.argtypes = (
|
||||
set_input_mat3 = _lib.rk_set_input_mat3
|
||||
set_input_mat3.argtypes = (
|
||||
ctypes.c_void_p, # input
|
||||
_mat3_t) # value
|
||||
_mat3p) # value
|
||||
|
||||
def set_input_mat3(input, value):
|
||||
assert len(value) == 9
|
||||
_set_input_mat3(input, _mat3(value))
|
||||
|
||||
_set_input_mat4 = _lib.rk_set_input_mat4
|
||||
_set_input_mat4.argtypes = (
|
||||
set_input_mat4 = _lib.rk_set_input_mat4
|
||||
set_input_mat4.argtypes = (
|
||||
ctypes.c_void_p, # input
|
||||
_mat4_t) # value
|
||||
_mat4p) # value
|
||||
|
||||
def set_input_mat4(input, value):
|
||||
assert len(value) == 16
|
||||
_set_input_mat4(input, _mat4(value))
|
||||
|
||||
_set_param_vec3 = _lib.rk_set_param_vec3
|
||||
_set_param_vec3.argtypes = (
|
||||
set_param_vec3 = _lib.rk_set_param_vec3
|
||||
set_param_vec3.argtypes = (
|
||||
ctypes.c_uint, # layout
|
||||
_vec3_t) # value
|
||||
|
||||
def set_param_vec3(param, value):
|
||||
assert len(value) == 3
|
||||
_set_param_vec3(param, _vec3(value))
|
||||
_vec3p) # value
|
||||
|
||||
select_texture = _lib.rk_select_texture
|
||||
select_texture.argtypes = (
|
||||
@ -367,7 +298,7 @@ _draw_batch.argtypes = (
|
||||
def draw_batch(batch, flags, meshes, params):
|
||||
size = len(flags)
|
||||
assert len(meshes) == size
|
||||
_draw_batch(batch, size, _ubytep(flags), _uintp(meshes), _voidp(params))
|
||||
_draw_batch(batch, size, _ubytep(flags), _uintp(meshes), params)
|
||||
|
||||
unselect_vertices = _lib.rk_unselect_vertices
|
||||
unselect_vertices.argtypes = (
|
||||
|
Loading…
Reference in New Issue
Block a user