Created
May 23, 2017 11:18
-
-
Save devilholk/fe30b0e0d337e077a20024ade414fbdc to your computer and use it in GitHub Desktop.
Testing using ctypes to use glfw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Some testing | |
# Run this script with python 3 | |
# Use the interactive mode, like: | |
# $ python3 -i gltest.py | |
# You need to have the glfw and of course GL runtimes | |
# Once you start the script you get a window | |
# You will also get the python interactive prompt in the terminal you started | |
# the script in | |
# Here you can adjust variables sx, sy, sr and s and directly observe the effect | |
# The rending loop is in another thread | |
# The render loop exits either on ctrl+c in the terminal or if the window is closed | |
# Closing the window is the better way since it will clean up more things | |
import ctypes, time, threading, math | |
glfw = ctypes.CDLL('libglfw.so') | |
gl = ctypes.CDLL('libGL.so') | |
void_pointer = ctypes.POINTER(None) | |
int_pointer = ctypes.POINTER(ctypes.c_int) | |
glfwCreateWindow = glfw.glfwCreateWindow | |
glfwInit = glfw.glfwInit | |
glfwMakeContextCurrent = glfw.glfwMakeContextCurrent | |
glfwGetFramebufferSize = glfw.glfwGetFramebufferSize | |
glfwSwapBuffers = glfw.glfwSwapBuffers | |
glfwPollEvents = glfw.glfwPollEvents | |
glBindTexture = gl.glBindTexture | |
glBindTexture.argtypes= [ctypes.c_int, ctypes.c_int] | |
glClear = gl.glClear | |
glBegin = gl.glBegin | |
glEnd = gl.glEnd | |
glVertex3f = gl.glVertex3f | |
glGenTextures = gl.glGenTextures | |
glGenTextures.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_int)] | |
glColor4f = gl.glColor4f | |
glfwWindowShouldClose = glfw.glfwWindowShouldClose | |
glfwDestroyWindow = glfw.glfwDestroyWindow | |
glfwDestroyWindow.argtypes = [void_pointer] | |
glfwTerminate = glfw.glfwTerminate | |
glfwWindowShouldClose.argtypes = [void_pointer] | |
glColor4f.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float] | |
glViewport = gl.glViewport | |
glOrtho = gl.glOrtho | |
glOrtho.argtypes =[ctypes.c_double,ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double] | |
glMatrixMode = gl.glMatrixMode | |
glMatrixMode.argtypes = [ctypes.c_int] | |
glLoadIdentity = gl.glLoadIdentity | |
glViewport.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int] | |
glVertex3f.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float] | |
glTexCoord2f = gl.glTexCoord2f | |
glTexCoord2f.argtypes = [ctypes.c_float, ctypes.c_float] | |
glTexImage2D = gl.glTexImage2D | |
glTexImage2D.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, void_pointer] | |
glfwCreateWindow.restype = void_pointer | |
glfwCreateWindow.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_char_p, void_pointer, void_pointer] | |
glfwMakeContextCurrent.argtypes = [void_pointer] | |
glfwGetFramebufferSize.argtypes = [void_pointer, int_pointer, int_pointer] | |
glEnable = gl.glEnable | |
glEnable.argtypes = [ctypes.c_int] | |
glfwSwapBuffers.argtypes = [void_pointer] | |
glClear.argtypes = [ctypes.c_int] | |
gl.GL_COLOR_BUFFER_BIT = ctypes.c_int(0x00004000) | |
gl.GL_QUADS = ctypes.c_int(0x0007) | |
gl.GL_MATRIX_MODE = ctypes.c_int(0x0BA0) | |
gl.GL_MODELVIEW = ctypes.c_int(0x1700) | |
gl.GL_PROJECTION = ctypes.c_int(0x1701) | |
gl.GL_TEXTURE = ctypes.c_int(0x1702) | |
gl.GL_BLEND = ctypes.c_int(0x0BE2) | |
gl.GL_BLEND_SRC = ctypes.c_int(0x0BE1) | |
gl.GL_BLEND_DST = ctypes.c_int(0x0BE0) | |
gl.GL_ZERO = ctypes.c_int(0) | |
gl.GL_ONE = ctypes.c_int(1) | |
gl.GL_SRC_COLOR = ctypes.c_int(0x0300) | |
gl.GL_ONE_MINUS_SRC_COLOR = ctypes.c_int(0x0301) | |
gl.GL_SRC_ALPHA = ctypes.c_int(0x0302) | |
gl.GL_ONE_MINUS_SRC_ALPHA = ctypes.c_int(0x0303) | |
gl.GL_DST_ALPHA = ctypes.c_int(0x0304) | |
gl.GL_ONE_MINUS_DST_ALPHA = ctypes.c_int(0x0305) | |
gl.GL_DST_COLOR = ctypes.c_int(0x0306) | |
gl.GL_ONE_MINUS_DST_COLOR = ctypes.c_int(0x0307) | |
gl.GL_SRC_ALPHA_SATURATE = ctypes.c_int(0x0308) | |
gl.GL_TEXTURE_ENV = ctypes.c_int(0x2300) | |
gl.GL_TEXTURE_ENV_MODE = ctypes.c_int(0x2200) | |
gl.GL_TEXTURE_1D = ctypes.c_int(0x0DE0) | |
gl.GL_TEXTURE_2D = ctypes.c_int(0x0DE1) | |
gl.GL_TEXTURE_WRAP_S = ctypes.c_int(0x2802) | |
gl.GL_TEXTURE_WRAP_T = ctypes.c_int(0x2803) | |
gl.GL_TEXTURE_MAG_FILTER = ctypes.c_int(0x2800) | |
gl.GL_TEXTURE_MIN_FILTER = ctypes.c_int(0x2801) | |
gl.GL_TEXTURE_ENV_COLOR = ctypes.c_int(0x2201) | |
gl.GL_TEXTURE_GEN_S = ctypes.c_int(0x0C60) | |
gl.GL_TEXTURE_GEN_T = ctypes.c_int(0x0C61) | |
gl.GL_TEXTURE_GEN_R = ctypes.c_int(0x0C62) | |
gl.GL_TEXTURE_GEN_Q = ctypes.c_int(0x0C63) | |
gl.GL_TEXTURE_GEN_MODE = ctypes.c_int(0x2500) | |
gl.GL_TEXTURE_BORDER_COLOR = ctypes.c_int(0x1004) | |
gl.GL_TEXTURE_WIDTH = ctypes.c_int(0x1000) | |
gl.GL_TEXTURE_HEIGHT = ctypes.c_int(0x1001) | |
gl.GL_TEXTURE_BORDER = ctypes.c_int(0x1005) | |
gl.GL_TEXTURE_COMPONENTS = ctypes.c_int(0x1003) | |
gl.GL_TEXTURE_RED_SIZE = ctypes.c_int(0x805C) | |
gl.GL_TEXTURE_GREEN_SIZE = ctypes.c_int(0x805D) | |
gl.GL_TEXTURE_BLUE_SIZE = ctypes.c_int(0x805E) | |
gl.GL_TEXTURE_ALPHA_SIZE = ctypes.c_int(0x805F) | |
gl.GL_TEXTURE_LUMINANCE_SIZE = ctypes.c_int(0x8060) | |
gl.GL_TEXTURE_INTENSITY_SIZE = ctypes.c_int(0x8061) | |
gl.GL_NEAREST_MIPMAP_NEAREST = ctypes.c_int(0x2700) | |
gl.GL_NEAREST_MIPMAP_LINEAR = ctypes.c_int(0x2702) | |
gl.GL_LINEAR_MIPMAP_NEAREST = ctypes.c_int(0x2701) | |
gl.GL_LINEAR_MIPMAP_LINEAR = ctypes.c_int(0x2703) | |
gl.GL_OBJECT_LINEAR = ctypes.c_int(0x2401) | |
gl.GL_OBJECT_PLANE = ctypes.c_int(0x2501) | |
gl.GL_EYE_LINEAR = ctypes.c_int(0x2400) | |
gl.GL_EYE_PLANE = ctypes.c_int(0x2502) | |
gl.GL_SPHERE_MAP = ctypes.c_int(0x2402) | |
gl.GL_DECAL = ctypes.c_int(0x2101) | |
gl.GL_MODULATE = ctypes.c_int(0x2100) | |
gl.GL_NEAREST = ctypes.c_int(0x2600) | |
gl.GL_REPEAT = ctypes.c_int(0x2901) | |
gl.GL_CLAMP = ctypes.c_int(0x2900) | |
gl.GL_S = ctypes.c_int(0x2000) | |
gl.GL_T = ctypes.c_int(0x2001) | |
gl.GL_R = ctypes.c_int(0x2002) | |
gl.GL_Q = ctypes.c_int(0x2003) | |
gl.GL_RESCALE_NORMAL = ctypes.c_int(0x803A) | |
gl.GL_CLAMP_TO_EDGE = ctypes.c_int(0x812F) | |
gl.GL_MAX_ELEMENTS_VERTICES = ctypes.c_int(0x80E8) | |
gl.GL_MAX_ELEMENTS_INDICES = ctypes.c_int(0x80E9) | |
gl.GL_BGR = ctypes.c_int(0x80E0) | |
gl.GL_BGRA = ctypes.c_int(0x80E1) | |
gl.GL_UNSIGNED_BYTE_3_3_2 = ctypes.c_int(0x8032) | |
gl.GL_UNSIGNED_BYTE_2_3_3_REV = ctypes.c_int(0x8362) | |
gl.GL_UNSIGNED_SHORT_5_6_5 = ctypes.c_int(0x8363) | |
gl.GL_UNSIGNED_SHORT_5_6_5_REV = ctypes.c_int(0x8364) | |
gl.GL_UNSIGNED_SHORT_4_4_4_4 = ctypes.c_int(0x8033) | |
gl.GL_UNSIGNED_SHORT_4_4_4_4_REV = ctypes.c_int(0x8365) | |
gl.GL_UNSIGNED_SHORT_5_5_5_1 = ctypes.c_int(0x8034) | |
gl.GL_UNSIGNED_SHORT_1_5_5_5_REV = ctypes.c_int(0x8366) | |
gl.GL_UNSIGNED_INT_8_8_8_8 = ctypes.c_int(0x8035) | |
gl.GL_UNSIGNED_INT_8_8_8_8_REV = ctypes.c_int(0x8367) | |
gl.GL_UNSIGNED_INT_10_10_10_2 = ctypes.c_int(0x8036) | |
gl.GL_UNSIGNED_INT_2_10_10_10_REV = ctypes.c_int(0x8368) | |
gl.GL_LIGHT_MODEL_COLOR_CONTROL = ctypes.c_int(0x81F8) | |
gl.GL_SINGLE_COLOR = ctypes.c_int(0x81F9) | |
gl.GL_SEPARATE_SPECULAR_COLOR = ctypes.c_int(0x81FA) | |
gl.GL_TEXTURE_MIN_LOD = ctypes.c_int(0x813A) | |
gl.GL_TEXTURE_MAX_LOD = ctypes.c_int(0x813B) | |
gl.GL_TEXTURE_BASE_LEVEL = ctypes.c_int(0x813C) | |
gl.GL_TEXTURE_MAX_LEVEL = ctypes.c_int(0x813D) | |
gl.GL_SMOOTH_POINT_SIZE_RANGE = ctypes.c_int(0x0B12) | |
gl.GL_SMOOTH_POINT_SIZE_GRANULARITY = ctypes.c_int(0x0B13) | |
gl.GL_SMOOTH_LINE_WIDTH_RANGE = ctypes.c_int(0x0B22) | |
gl.GL_SMOOTH_LINE_WIDTH_GRANULARITY = ctypes.c_int(0x0B23) | |
gl.GL_ALIASED_POINT_SIZE_RANGE = ctypes.c_int(0x846D) | |
gl.GL_ALIASED_LINE_WIDTH_RANGE = ctypes.c_int(0x846E) | |
gl.GL_PACK_SKIP_IMAGES = ctypes.c_int(0x806B) | |
gl.GL_PACK_IMAGE_HEIGHT = ctypes.c_int(0x806C) | |
gl.GL_UNPACK_SKIP_IMAGES = ctypes.c_int(0x806D) | |
gl.GL_UNPACK_IMAGE_HEIGHT = ctypes.c_int(0x806E) | |
gl.GL_TEXTURE_3D = ctypes.c_int(0x806F) | |
gl.GL_PROXY_TEXTURE_3D = ctypes.c_int(0x8070) | |
gl.GL_TEXTURE_DEPTH = ctypes.c_int(0x8071) | |
gl.GL_TEXTURE_WRAP_R = ctypes.c_int(0x8072) | |
gl.GL_MAX_3D_TEXTURE_SIZE = ctypes.c_int(0x8073) | |
gl.GL_TEXTURE_BINDING_3D = ctypes.c_int(0x806A) | |
gl.GL_NONE = ctypes.c_int(0) | |
gl.GL_LEFT = ctypes.c_int(0x0406) | |
gl.GL_RIGHT = ctypes.c_int(0x0407) | |
gl.GL_FRONT_LEFT = ctypes.c_int(0x0400) | |
gl.GL_FRONT_RIGHT = ctypes.c_int(0x0401) | |
gl.GL_BACK_LEFT = ctypes.c_int(0x0402) | |
gl.GL_BACK_RIGHT = ctypes.c_int(0x0403) | |
gl.GL_AUX0 = ctypes.c_int(0x0409) | |
gl.GL_AUX1 = ctypes.c_int(0x040A) | |
gl.GL_AUX2 = ctypes.c_int(0x040B) | |
gl.GL_AUX3 = ctypes.c_int(0x040C) | |
gl.GL_COLOR_INDEX = ctypes.c_int(0x1900) | |
gl.GL_RED = ctypes.c_int(0x1903) | |
gl.GL_GREEN = ctypes.c_int(0x1904) | |
gl.GL_BLUE = ctypes.c_int(0x1905) | |
gl.GL_ALPHA = ctypes.c_int(0x1906) | |
gl.GL_LUMINANCE = ctypes.c_int(0x1909) | |
gl.GL_LUMINANCE_ALPHA = ctypes.c_int(0x190A) | |
gl.GL_ALPHA_BITS = ctypes.c_int(0x0D55) | |
gl.GL_RED_BITS = ctypes.c_int(0x0D52) | |
gl.GL_GREEN_BITS = ctypes.c_int(0x0D53) | |
gl.GL_BLUE_BITS = ctypes.c_int(0x0D54) | |
gl.GL_INDEX_BITS = ctypes.c_int(0x0D51) | |
gl.GL_SUBPIXEL_BITS = ctypes.c_int(0x0D50) | |
gl.GL_AUX_BUFFERS = ctypes.c_int(0x0C00) | |
gl.GL_READ_BUFFER = ctypes.c_int(0x0C02) | |
gl.GL_DRAW_BUFFER = ctypes.c_int(0x0C01) | |
gl.GL_DOUBLEBUFFER = ctypes.c_int(0x0C32) | |
gl.GL_STEREO = ctypes.c_int(0x0C33) | |
gl.GL_BITMAP = ctypes.c_int(0x1A00) | |
gl.GL_COLOR = ctypes.c_int(0x1800) | |
gl.GL_DEPTH = ctypes.c_int(0x1801) | |
gl.GL_STENCIL = ctypes.c_int(0x1802) | |
gl.GL_DITHER = ctypes.c_int(0x0BD0) | |
gl.GL_RGB = ctypes.c_int(0x1907) | |
gl.GL_RGBA = ctypes.c_int(0x1908) | |
gl.GL_BYTE = ctypes.c_int(0x1400) | |
gl.GL_UNSIGNED_BYTE = ctypes.c_int(0x1401) | |
gl.GL_SHORT = ctypes.c_int(0x1402) | |
gl.GL_UNSIGNED_SHORT = ctypes.c_int(0x1403) | |
gl.GL_INT = ctypes.c_int(0x1404) | |
gl.GL_UNSIGNED_INT = ctypes.c_int(0x1405) | |
gl.GL_FLOAT = ctypes.c_int(0x1406) | |
gl.GL_2_BYTES = ctypes.c_int(0x1407) | |
gl.GL_3_BYTES = ctypes.c_int(0x1408) | |
gl.GL_4_BYTES = ctypes.c_int(0x1409) | |
gl.GL_DOUBLE = ctypes.c_int(0x140A) | |
gl.GL_FOG = ctypes.c_int(0x0B60) | |
gl.GL_FOG_MODE = ctypes.c_int(0x0B65) | |
gl.GL_FOG_DENSITY = ctypes.c_int(0x0B62) | |
gl.GL_FOG_COLOR = ctypes.c_int(0x0B66) | |
gl.GL_FOG_INDEX = ctypes.c_int(0x0B61) | |
gl.GL_FOG_START = ctypes.c_int(0x0B63) | |
gl.GL_FOG_END = ctypes.c_int(0x0B64) | |
gl.GL_LINEAR = ctypes.c_int(0x2601) | |
gl.GL_EXP = ctypes.c_int(0x0800) | |
gl.GL_EXP2 = ctypes.c_int(0x0801) | |
glTexParameteri = gl.glTexParameteri | |
glTexParameteri.argtypes = [ctypes.c_int, ctypes.c_int] | |
glRotatef = gl.glRotatef | |
glTranslatef =gl.glTranslatef | |
glRotatef.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float] | |
glTranslatef.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float] | |
assert glfwInit() == 1, 'Failed to init GLFW' | |
w = glfwCreateWindow(640, 480, b"Hello World!", None, None) | |
assert w, 'Failed to create window' | |
sx = 100 | |
sy = 100 | |
sr = 45 | |
s = 100 | |
def render_test_loop(window): | |
glfwMakeContextCurrent(window) | |
width = ctypes.c_int(0) | |
height = ctypes.c_int(0) | |
textures = (ctypes.c_int * 1)(0) | |
glGenTextures(1, textures) | |
test = (ctypes.c_char * 4*64*64)() | |
glBindTexture(gl.GL_TEXTURE_2D, textures[0]) | |
glTexImage2D(gl.GL_TEXTURE_2D, 0,gl.GL_RGBA, 64, 64, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, test) | |
glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) | |
glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) | |
glEnable(gl.GL_TEXTURE_2D) | |
glEnable(gl.GL_BLEND) | |
while not glfwWindowShouldClose(window): | |
rx = math.sin(time.time())*30+30 | |
ry = math.cos(time.time())*30+30 | |
for x in range(64): | |
for y in range(64): | |
r = int(time.time()*-250 + (((x-rx)**2 + (y-ry)**2)**0.5) * 30 ) & 0xff | |
test[x][y] = r, r, r, 0x80 | |
glBindTexture(gl.GL_TEXTURE_2D, textures[0]) | |
glTexImage2D(gl.GL_TEXTURE_2D, 0,gl.GL_RGBA, 64, 64, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, test) | |
glfwGetFramebufferSize(window, int_pointer(width), int_pointer(height)) | |
glViewport(0,0, width.value-1, height.value-1) | |
glMatrixMode(gl.GL_PROJECTION) | |
glLoadIdentity() | |
glOrtho(0, width.value-1, height.value-1, 0,0,1) | |
glMatrixMode(gl.GL_MODELVIEW) | |
glLoadIdentity() | |
glClear(gl.GL_COLOR_BUFFER_BIT) | |
glColor4f(1.0, 1.0, 1.0, 1.0) | |
glTranslatef(sx, sy, 0) | |
glRotatef(sr, 0, 0, 1) | |
glBegin(gl.GL_QUADS) | |
glVertex3f(-s, -s, 0) | |
glTexCoord2f(0, 0) | |
glVertex3f(s, -s, 0) | |
glTexCoord2f(1, 0) | |
glVertex3f(s, s, 0) | |
glTexCoord2f(1, 1) | |
glVertex3f(-s, s, 0) | |
glTexCoord2f(0, 1) | |
glEnd() | |
glfwSwapBuffers(window) | |
glfwPollEvents() | |
glfwDestroyWindow(window) | |
glfwTerminate() | |
t = threading.Thread(target=render_test_loop, args=(w,)) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment