From 9cdf3b85137c42220a64588c087a563e24464169 Mon Sep 17 00:00:00 2001 From: Quantum Date: Tue, 27 Nov 2018 01:27:59 -0500 Subject: [PATCH] Make punyverse runnable on macOS --- .gitignore | 4 ++++ punyverse/_cyopengl.pxi | 9 ++++++++- punyverse/main.py | 10 ++++++++-- punyverse/shaders/model.fragment.glsl | 2 +- punyverse/shaders/planet.fragment.glsl | 8 ++++---- punyverse/ui.py | 2 ++ setup.py | 10 +++++++++- 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index e8e9cf1..99bcaa6 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,7 @@ library.zip # Our special launcher !/punyverse/launcher.c + +# macOS +.DS_Store +._.DS_Store diff --git a/punyverse/_cyopengl.pxi b/punyverse/_cyopengl.pxi index 70e96bb..50d775d 100644 --- a/punyverse/_cyopengl.pxi +++ b/punyverse/_cyopengl.pxi @@ -2,7 +2,14 @@ IF UNAME_SYSNAME == "Windows": cdef extern from "windows.h": pass -cdef extern from "GL/gl.h": +IF UNAME_SYSNAME == "Darwin": + cdef extern from "OpenGL/gl.h": + pass +ELSE: + cdef extern from "GL/gl.h": + pass + +cdef extern from *: ctypedef unsigned int GLenum ctypedef unsigned char GLboolean ctypedef unsigned int GLbitfield diff --git a/punyverse/main.py b/punyverse/main.py index def81b2..11f2aa4 100644 --- a/punyverse/main.py +++ b/punyverse/main.py @@ -1,4 +1,5 @@ import argparse +import sys import pyglet @@ -8,6 +9,8 @@ DEBUG = False def main(): + macos = sys.platform == 'darwin' + parser = argparse.ArgumentParser(prog='punyverse', description=''' Python simulator of a puny universe. ''') @@ -22,12 +25,15 @@ def main(): action='store_true') args = parser.parse_args() + versioning = dict(major_version=3, minor_version=3) pyglet.options['debug_gl'] = args.debug + if macos: + pyglet.options['shadow_window'] = False + versioning = dict(major_version=4, minor_version=1, forward_compatible=True) template = pyglet.gl.Config(depth_size=args.depth, double_buffer=True, sample_buffers=args.multisample > 1, - samples=args.multisample, - major_version=3, minor_version=3) + samples=args.multisample, **versioning) platform = pyglet.window.get_platform() display = platform.get_default_display() diff --git a/punyverse/shaders/model.fragment.glsl b/punyverse/shaders/model.fragment.glsl index c2d09e9..ab3b38c 100644 --- a/punyverse/shaders/model.fragment.glsl +++ b/punyverse/shaders/model.fragment.glsl @@ -34,7 +34,7 @@ void main() { float diffuseIntensity = max(dot(v_normal, incident), 0.0); float shininess = pow(max(dot(normalize(v_camDirection), reflected), 0), u_material.shininess); - vec3 diffuse = u_material.hasDiffuse ? texture2D(u_material.diffuseMap, v_uv).rgb : vec3(1); + vec3 diffuse = u_material.hasDiffuse ? texture(u_material.diffuseMap, v_uv).rgb : vec3(1); vec3 ambient = u_material.ambient * u_sun.ambient * diffuse; vec3 specular = u_material.specular * u_sun.specular * max(shininess, 0) * diffuseIntensity; diffuse *= u_material.diffuse * u_sun.diffuse * diffuseIntensity; diff --git a/punyverse/shaders/planet.fragment.glsl b/punyverse/shaders/planet.fragment.glsl index f12730a..7ce0d62 100644 --- a/punyverse/shaders/planet.fragment.glsl +++ b/punyverse/shaders/planet.fragment.glsl @@ -35,10 +35,10 @@ uniform Sun u_sun; uniform Surface u_planet; void main() { - vec3 normal = u_planet.hasNormal ? normalize(v_TBN * texture2D(u_planet.normalMap, v_uv).rgb * 2 - 1) : v_normal; - vec3 diffuse = texture2D(u_planet.diffuseMap, v_uv).rgb; - vec3 specular = u_planet.hasSpecular ? texture2D(u_planet.specularMap, v_uv).rgb : vec3(1); - vec3 emission = u_planet.hasEmission ? texture2D(u_planet.emissionMap, v_uv).rgb : vec3(1); + vec3 normal = u_planet.hasNormal ? normalize(v_TBN * texture(u_planet.normalMap, v_uv).rgb * 2 - 1) : v_normal; + vec3 diffuse = texture(u_planet.diffuseMap, v_uv).rgb; + vec3 specular = u_planet.hasSpecular ? texture(u_planet.specularMap, v_uv).rgb : vec3(1); + vec3 emission = u_planet.hasEmission ? texture(u_planet.emissionMap, v_uv).rgb : vec3(1); vec3 incident = normalize(u_sun.position - v_position); vec3 reflected = normalize(reflect(-incident, normal)); diff --git a/punyverse/ui.py b/punyverse/ui.py index feb7cac..2d02612 100644 --- a/punyverse/ui.py +++ b/punyverse/ui.py @@ -199,6 +199,8 @@ class Punyverse(pyglet.window.Window): if not width or not height: # Sometimes this happen for no reason? return + if hasattr(self, 'get_viewport_size'): + width, height = self.get_viewport_size() glViewport(0, 0, width, height) self.world.resize(width, height) diff --git a/setup.py b/setup.py index 47e05ac..47534de 100644 --- a/setup.py +++ b/setup.py @@ -26,9 +26,16 @@ else: if os.name == 'nt': gl_libs = ['opengl32'] +elif sys.platform == 'darwin': + gl_libs = [] else: gl_libs = ['GL'] +if sys.platform == 'darwin': + extra_compile_args = extra_link_args = ['-framework', 'OpenGL'] +else: + extra_compile_args = extra_link_args = [] + with open(os.path.join(os.path.dirname(__file__), 'README.md')) as f: long_description = f.read() @@ -124,7 +131,8 @@ setup( ], }, ext_modules=cythonize([ - Extension('punyverse._glgeom', sources=[pyx_path('punyverse/_glgeom.pyx')], libraries=gl_libs), + Extension('punyverse._glgeom', sources=[pyx_path('punyverse/_glgeom.pyx')], libraries=gl_libs, + extra_compile_args=extra_compile_args, extra_link_args=extra_link_args), ]) + extra_libs, cmdclass={'build_ext': build_ext},