pyglet 2.1.12__py3-none-any.whl → 3.0.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. pyglet/__init__.py +67 -61
  2. pyglet/__init__.pyi +15 -8
  3. pyglet/app/__init__.py +22 -13
  4. pyglet/app/async_app.py +212 -0
  5. pyglet/app/base.py +2 -1
  6. pyglet/app/{xlib.py → linux.py} +3 -3
  7. pyglet/config/__init__.py +101 -0
  8. pyglet/config/gl/__init__.py +30 -0
  9. pyglet/config/gl/egl.py +120 -0
  10. pyglet/config/gl/macos.py +262 -0
  11. pyglet/config/gl/windows.py +267 -0
  12. pyglet/config/gl/x11.py +142 -0
  13. pyglet/customtypes.py +43 -2
  14. pyglet/display/__init__.py +8 -6
  15. pyglet/display/base.py +3 -63
  16. pyglet/display/cocoa.py +12 -17
  17. pyglet/display/emscripten.py +39 -0
  18. pyglet/display/headless.py +23 -30
  19. pyglet/display/wayland.py +157 -0
  20. pyglet/display/win32.py +4 -17
  21. pyglet/display/xlib.py +19 -27
  22. pyglet/display/xlib_vidmoderestore.py +2 -2
  23. pyglet/enums.py +183 -0
  24. pyglet/event.py +0 -1
  25. pyglet/experimental/geoshader_sprite.py +15 -13
  26. pyglet/experimental/hidraw.py +6 -15
  27. pyglet/experimental/multitexture_sprite.py +31 -19
  28. pyglet/experimental/particles.py +13 -35
  29. pyglet/font/__init__.py +251 -85
  30. pyglet/font/base.py +116 -61
  31. pyglet/font/dwrite/__init__.py +349 -204
  32. pyglet/font/dwrite/dwrite_lib.py +27 -5
  33. pyglet/font/fontconfig.py +14 -6
  34. pyglet/font/freetype.py +138 -87
  35. pyglet/font/freetype_lib.py +19 -0
  36. pyglet/font/group.py +179 -0
  37. pyglet/font/harfbuzz/__init__.py +3 -3
  38. pyglet/font/pyodide_js.py +310 -0
  39. pyglet/font/quartz.py +319 -126
  40. pyglet/font/ttf.py +45 -3
  41. pyglet/font/user.py +14 -19
  42. pyglet/font/win32.py +45 -21
  43. pyglet/graphics/__init__.py +8 -787
  44. pyglet/graphics/allocation.py +115 -1
  45. pyglet/graphics/api/__init__.py +77 -0
  46. pyglet/graphics/api/base.py +299 -0
  47. pyglet/graphics/api/gl/__init__.py +58 -0
  48. pyglet/graphics/api/gl/base.py +24 -0
  49. pyglet/graphics/{vertexbuffer.py → api/gl/buffer.py} +104 -159
  50. pyglet/graphics/api/gl/cocoa/context.py +76 -0
  51. pyglet/graphics/api/gl/context.py +391 -0
  52. pyglet/graphics/api/gl/default_shaders.py +0 -0
  53. pyglet/graphics/api/gl/draw.py +627 -0
  54. pyglet/graphics/api/gl/egl/__init__.py +0 -0
  55. pyglet/graphics/api/gl/egl/context.py +92 -0
  56. pyglet/graphics/api/gl/enums.py +76 -0
  57. pyglet/graphics/api/gl/framebuffer.py +315 -0
  58. pyglet/graphics/api/gl/gl.py +5463 -0
  59. pyglet/graphics/api/gl/gl_info.py +188 -0
  60. pyglet/graphics/api/gl/global_opengl.py +226 -0
  61. pyglet/{gl → graphics/api/gl}/lib.py +34 -18
  62. pyglet/graphics/api/gl/shader.py +1476 -0
  63. pyglet/graphics/api/gl/shapes.py +55 -0
  64. pyglet/graphics/api/gl/sprite.py +102 -0
  65. pyglet/graphics/api/gl/state.py +219 -0
  66. pyglet/graphics/api/gl/text.py +190 -0
  67. pyglet/graphics/api/gl/texture.py +1526 -0
  68. pyglet/graphics/{vertexarray.py → api/gl/vertexarray.py} +11 -13
  69. pyglet/graphics/api/gl/vertexdomain.py +751 -0
  70. pyglet/graphics/api/gl/win32/__init__.py +0 -0
  71. pyglet/graphics/api/gl/win32/context.py +108 -0
  72. pyglet/graphics/api/gl/win32/wgl_info.py +24 -0
  73. pyglet/graphics/api/gl/xlib/__init__.py +0 -0
  74. pyglet/graphics/api/gl/xlib/context.py +174 -0
  75. pyglet/{gl → graphics/api/gl/xlib}/glx_info.py +26 -31
  76. pyglet/graphics/api/gl1/__init__.py +0 -0
  77. pyglet/{gl → graphics/api/gl1}/gl_compat.py +3 -2
  78. pyglet/graphics/api/gl2/__init__.py +0 -0
  79. pyglet/graphics/api/gl2/buffer.py +320 -0
  80. pyglet/graphics/api/gl2/draw.py +600 -0
  81. pyglet/graphics/api/gl2/global_opengl.py +122 -0
  82. pyglet/graphics/api/gl2/shader.py +200 -0
  83. pyglet/graphics/api/gl2/shapes.py +51 -0
  84. pyglet/graphics/api/gl2/sprite.py +79 -0
  85. pyglet/graphics/api/gl2/text.py +175 -0
  86. pyglet/graphics/api/gl2/vertexdomain.py +364 -0
  87. pyglet/graphics/api/webgl/__init__.py +233 -0
  88. pyglet/graphics/api/webgl/buffer.py +302 -0
  89. pyglet/graphics/api/webgl/context.py +234 -0
  90. pyglet/graphics/api/webgl/draw.py +590 -0
  91. pyglet/graphics/api/webgl/enums.py +76 -0
  92. pyglet/graphics/api/webgl/framebuffer.py +360 -0
  93. pyglet/graphics/api/webgl/gl.py +1537 -0
  94. pyglet/graphics/api/webgl/gl_info.py +130 -0
  95. pyglet/graphics/api/webgl/shader.py +1346 -0
  96. pyglet/graphics/api/webgl/shapes.py +92 -0
  97. pyglet/graphics/api/webgl/sprite.py +102 -0
  98. pyglet/graphics/api/webgl/state.py +227 -0
  99. pyglet/graphics/api/webgl/text.py +187 -0
  100. pyglet/graphics/api/webgl/texture.py +1227 -0
  101. pyglet/graphics/api/webgl/vertexarray.py +54 -0
  102. pyglet/graphics/api/webgl/vertexdomain.py +616 -0
  103. pyglet/graphics/api/webgl/webgl_js.pyi +307 -0
  104. pyglet/{image → graphics}/atlas.py +33 -32
  105. pyglet/graphics/base.py +10 -0
  106. pyglet/graphics/buffer.py +245 -0
  107. pyglet/graphics/draw.py +578 -0
  108. pyglet/graphics/framebuffer.py +26 -0
  109. pyglet/graphics/instance.py +178 -69
  110. pyglet/graphics/shader.py +267 -1553
  111. pyglet/graphics/state.py +83 -0
  112. pyglet/graphics/texture.py +703 -0
  113. pyglet/graphics/vertexdomain.py +695 -538
  114. pyglet/gui/ninepatch.py +10 -10
  115. pyglet/gui/widgets.py +120 -10
  116. pyglet/image/__init__.py +20 -1973
  117. pyglet/image/animation.py +12 -12
  118. pyglet/image/base.py +730 -0
  119. pyglet/image/codecs/__init__.py +9 -0
  120. pyglet/image/codecs/bmp.py +53 -30
  121. pyglet/image/codecs/dds.py +53 -31
  122. pyglet/image/codecs/gdiplus.py +38 -14
  123. pyglet/image/codecs/gdkpixbuf2.py +0 -2
  124. pyglet/image/codecs/js_image.py +99 -0
  125. pyglet/image/codecs/ktx2.py +161 -0
  126. pyglet/image/codecs/pil.py +1 -1
  127. pyglet/image/codecs/png.py +1 -1
  128. pyglet/image/codecs/wic.py +11 -2
  129. pyglet/info.py +26 -24
  130. pyglet/input/__init__.py +8 -0
  131. pyglet/input/base.py +163 -105
  132. pyglet/input/controller.py +13 -19
  133. pyglet/input/controller_db.py +39 -24
  134. pyglet/input/emscripten/__init__.py +18 -0
  135. pyglet/input/emscripten/gamepad_js.py +397 -0
  136. pyglet/input/linux/__init__.py +11 -5
  137. pyglet/input/linux/evdev.py +10 -11
  138. pyglet/input/linux/x11_xinput.py +2 -2
  139. pyglet/input/linux/x11_xinput_tablet.py +1 -1
  140. pyglet/input/macos/__init__.py +7 -2
  141. pyglet/input/macos/darwin_gc.py +559 -0
  142. pyglet/input/win32/__init__.py +1 -1
  143. pyglet/input/win32/directinput.py +34 -29
  144. pyglet/input/win32/xinput.py +11 -61
  145. pyglet/lib.py +3 -3
  146. pyglet/libs/__init__.py +1 -1
  147. pyglet/{gl → libs/darwin}/agl.py +1 -1
  148. pyglet/libs/darwin/cocoapy/__init__.py +2 -2
  149. pyglet/libs/darwin/cocoapy/cocoahelpers.py +181 -0
  150. pyglet/libs/darwin/cocoapy/cocoalibs.py +31 -0
  151. pyglet/libs/darwin/cocoapy/cocoatypes.py +27 -0
  152. pyglet/libs/darwin/cocoapy/runtime.py +81 -45
  153. pyglet/libs/darwin/coreaudio.py +4 -4
  154. pyglet/{gl → libs/darwin}/lib_agl.py +9 -8
  155. pyglet/libs/darwin/quartzkey.py +1 -3
  156. pyglet/libs/egl/__init__.py +2 -0
  157. pyglet/libs/egl/egl_lib.py +576 -0
  158. pyglet/libs/egl/eglext.py +51 -5
  159. pyglet/libs/linux/__init__.py +0 -0
  160. pyglet/libs/linux/egl/__init__.py +0 -0
  161. pyglet/libs/linux/egl/eglext.py +22 -0
  162. pyglet/libs/linux/glx/__init__.py +0 -0
  163. pyglet/{gl → libs/linux/glx}/glx.py +13 -14
  164. pyglet/{gl → libs/linux/glx}/glxext_arb.py +408 -192
  165. pyglet/{gl → libs/linux/glx}/glxext_mesa.py +1 -1
  166. pyglet/{gl → libs/linux/glx}/glxext_nv.py +345 -164
  167. pyglet/{gl → libs/linux/glx}/lib_glx.py +3 -2
  168. pyglet/libs/linux/wayland/__init__.py +0 -0
  169. pyglet/libs/linux/wayland/client.py +1068 -0
  170. pyglet/libs/linux/wayland/lib_wayland.py +207 -0
  171. pyglet/libs/linux/wayland/wayland_egl.py +38 -0
  172. pyglet/libs/{wayland → linux/wayland}/xkbcommon.py +26 -0
  173. pyglet/libs/{x11 → linux/x11}/xf86vmode.py +4 -4
  174. pyglet/libs/{x11 → linux/x11}/xinerama.py +2 -2
  175. pyglet/libs/{x11 → linux/x11}/xinput.py +10 -10
  176. pyglet/libs/linux/x11/xrandr.py +0 -0
  177. pyglet/libs/{x11 → linux/x11}/xrender.py +1 -1
  178. pyglet/libs/shared/__init__.py +0 -0
  179. pyglet/libs/shared/spirv/__init__.py +0 -0
  180. pyglet/libs/shared/spirv/lib_shaderc.py +85 -0
  181. pyglet/libs/shared/spirv/lib_spirv_cross.py +126 -0
  182. pyglet/libs/win32/__init__.py +27 -5
  183. pyglet/libs/win32/constants.py +59 -48
  184. pyglet/libs/win32/context_managers.py +20 -3
  185. pyglet/libs/win32/dinput.py +105 -88
  186. pyglet/{gl → libs/win32}/lib_wgl.py +52 -26
  187. pyglet/libs/win32/types.py +58 -23
  188. pyglet/{gl → libs/win32}/wgl.py +32 -25
  189. pyglet/{gl → libs/win32}/wglext_arb.py +364 -2
  190. pyglet/media/__init__.py +9 -10
  191. pyglet/media/codecs/__init__.py +12 -1
  192. pyglet/media/codecs/base.py +99 -96
  193. pyglet/media/codecs/ffmpeg.py +2 -2
  194. pyglet/media/codecs/ffmpeg_lib/libavformat.py +3 -8
  195. pyglet/media/codecs/webaudio_pyodide.py +111 -0
  196. pyglet/media/drivers/__init__.py +9 -4
  197. pyglet/media/drivers/base.py +4 -4
  198. pyglet/media/drivers/openal/__init__.py +1 -1
  199. pyglet/media/drivers/openal/adaptation.py +3 -3
  200. pyglet/media/drivers/pulse/__init__.py +1 -1
  201. pyglet/media/drivers/pulse/adaptation.py +3 -3
  202. pyglet/media/drivers/pyodide_js/__init__.py +8 -0
  203. pyglet/media/drivers/pyodide_js/adaptation.py +288 -0
  204. pyglet/media/drivers/xaudio2/adaptation.py +3 -3
  205. pyglet/media/player.py +276 -193
  206. pyglet/media/player_worker_thread.py +1 -1
  207. pyglet/model/__init__.py +39 -29
  208. pyglet/model/codecs/base.py +4 -4
  209. pyglet/model/codecs/gltf.py +3 -3
  210. pyglet/model/codecs/obj.py +71 -43
  211. pyglet/resource.py +129 -78
  212. pyglet/shapes.py +147 -177
  213. pyglet/sprite.py +47 -164
  214. pyglet/text/__init__.py +44 -54
  215. pyglet/text/caret.py +12 -7
  216. pyglet/text/document.py +19 -17
  217. pyglet/text/formats/html.py +2 -2
  218. pyglet/text/formats/structured.py +10 -40
  219. pyglet/text/layout/__init__.py +20 -13
  220. pyglet/text/layout/base.py +176 -287
  221. pyglet/text/layout/incremental.py +9 -10
  222. pyglet/text/layout/scrolling.py +7 -95
  223. pyglet/window/__init__.py +183 -172
  224. pyglet/window/cocoa/__init__.py +62 -51
  225. pyglet/window/cocoa/pyglet_delegate.py +2 -25
  226. pyglet/window/cocoa/pyglet_view.py +9 -8
  227. pyglet/window/dialog/__init__.py +184 -0
  228. pyglet/window/dialog/base.py +99 -0
  229. pyglet/window/dialog/darwin.py +121 -0
  230. pyglet/window/dialog/linux.py +72 -0
  231. pyglet/window/dialog/windows.py +194 -0
  232. pyglet/window/emscripten/__init__.py +779 -0
  233. pyglet/window/headless/__init__.py +44 -28
  234. pyglet/window/key.py +2 -0
  235. pyglet/window/mouse.py +2 -2
  236. pyglet/window/wayland/__init__.py +377 -0
  237. pyglet/window/win32/__init__.py +101 -46
  238. pyglet/window/xlib/__init__.py +104 -66
  239. {pyglet-2.1.12.dist-info → pyglet-3.0.dev1.dist-info}/METADATA +2 -3
  240. pyglet-3.0.dev1.dist-info/RECORD +322 -0
  241. {pyglet-2.1.12.dist-info → pyglet-3.0.dev1.dist-info}/WHEEL +1 -1
  242. pyglet/gl/__init__.py +0 -208
  243. pyglet/gl/base.py +0 -499
  244. pyglet/gl/cocoa.py +0 -309
  245. pyglet/gl/gl.py +0 -4625
  246. pyglet/gl/gl.pyi +0 -2320
  247. pyglet/gl/gl_compat.pyi +0 -3097
  248. pyglet/gl/gl_info.py +0 -190
  249. pyglet/gl/headless.py +0 -166
  250. pyglet/gl/wgl_info.py +0 -36
  251. pyglet/gl/wglext_nv.py +0 -1096
  252. pyglet/gl/win32.py +0 -268
  253. pyglet/gl/xlib.py +0 -295
  254. pyglet/image/buffer.py +0 -274
  255. pyglet/image/codecs/s3tc.py +0 -354
  256. pyglet/libs/x11/xrandr.py +0 -166
  257. pyglet-2.1.12.dist-info/RECORD +0 -234
  258. /pyglet/{libs/wayland → graphics/api/gl/cocoa}/__init__.py +0 -0
  259. /pyglet/libs/{egl → linux/egl}/egl.py +0 -0
  260. /pyglet/libs/{egl → linux/egl}/lib.py +0 -0
  261. /pyglet/libs/{ioctl.py → linux/ioctl.py} +0 -0
  262. /pyglet/libs/{wayland → linux/wayland}/gbm.py +0 -0
  263. /pyglet/libs/{x11 → linux/x11}/__init__.py +0 -0
  264. /pyglet/libs/{x11 → linux/x11}/cursorfont.py +0 -0
  265. /pyglet/libs/{x11 → linux/x11}/xlib.py +0 -0
  266. /pyglet/libs/{x11 → linux/x11}/xsync.py +0 -0
  267. {pyglet-2.1.12.dist-info/licenses → pyglet-3.0.dev1.dist-info}/LICENSE +0 -0
pyglet/model/__init__.py CHANGED
@@ -32,7 +32,7 @@ from math import pi, sin, cos
32
32
  from typing import TYPE_CHECKING
33
33
 
34
34
  import pyglet
35
- from pyglet import gl, graphics
35
+ from pyglet import graphics
36
36
  from pyglet.math import Mat4
37
37
 
38
38
  from .codecs import add_default_codecs as _add_default_codecs
@@ -41,11 +41,10 @@ from .codecs.base import Material, Scene, SimpleMaterial
41
41
 
42
42
  if TYPE_CHECKING:
43
43
  from typing import BinaryIO, TextIO
44
- from pyglet.image import Texture
44
+ from pyglet.graphics import Texture
45
45
  from pyglet.graphics import Batch, Group
46
- from pyglet.graphics.shader import ShaderProgram
46
+ from pyglet.graphics import ShaderProgram
47
47
  from pyglet.graphics.vertexdomain import VertexList
48
- from pyglet.image import Texture
49
48
  from pyglet.model.codecs import ModelDecoder
50
49
 
51
50
 
@@ -68,13 +67,19 @@ def load(filename: str, file: BinaryIO | TextIO | None = None, decoder: ModelDec
68
67
 
69
68
 
70
69
  def get_default_shader() -> ShaderProgram:
71
- return pyglet.gl.current_context.create_program((MaterialGroup.default_vert_src, 'vertex'),
72
- (MaterialGroup.default_frag_src, 'fragment'))
70
+ return pyglet.graphics.api.get_cached_shader(
71
+ "default_model_material",
72
+ (MaterialGroup.default_vert_src, 'vertex'),
73
+ (MaterialGroup.default_frag_src, 'fragment'),
74
+ )
73
75
 
74
76
 
75
77
  def get_default_textured_shader() -> ShaderProgram:
76
- return pyglet.gl.current_context.create_program((TexturedMaterialGroup.default_vert_src, 'vertex'),
77
- (TexturedMaterialGroup.default_frag_src, 'fragment'))
78
+ return pyglet.graphics.api.get_cached_shader(
79
+ "default_model_textured_material",
80
+ (TexturedMaterialGroup.default_vert_src, 'vertex'),
81
+ (TexturedMaterialGroup.default_frag_src, 'fragment'),
82
+ )
78
83
 
79
84
 
80
85
  class Model:
@@ -123,7 +128,7 @@ class Model:
123
128
  batch = graphics.Batch()
124
129
 
125
130
  for group, vlist in zip(self.groups, self.vertex_lists):
126
- self._batch.migrate(vlist, gl.GL_TRIANGLES, group, batch)
131
+ self._batch.migrate(vlist, graphics.GeometryMode.TRIANGLES, group, batch)
127
132
 
128
133
  self._batch = batch
129
134
 
@@ -204,24 +209,29 @@ class TexturedMaterialGroup(BaseMaterialGroup):
204
209
  texture: Texture, order: int = 0, parent: Group | None = None):
205
210
  super().__init__(material, program, order, parent)
206
211
  self.texture = texture
207
-
208
- def set_state(self) -> None:
209
- gl.glActiveTexture(gl.GL_TEXTURE0)
210
- gl.glBindTexture(self.texture.target, self.texture.id)
211
- self.program.use()
212
- self.program['model'] = self.matrix
213
-
214
- def __hash__(self) -> int:
215
- return hash((self.texture.target, self.texture.id, self.program, self.order, self.parent))
216
-
217
- def __eq__(self, other) -> bool:
218
- return (self.__class__ is other.__class__ and
219
- self.material == other.material and
220
- self.texture.target == other.texture.target and
221
- self.texture.id == other.texture.id and
222
- self.program == other.program and
223
- self.order == other.order and
224
- self.parent == other.parent)
212
+ self.set_texture(self.texture)
213
+ self.set_shader_program(program)
214
+ self.uniforms = {"model": self.matrix}
215
+ self.set_shader_uniforms(program, self.uniforms)
216
+ #
217
+ # def set_state(self) -> None:
218
+ # # gl.glActiveTexture(gl.GL_TEXTURE0)
219
+ # # gl.glBindTexture(self.texture.target, self.texture.id)
220
+ # # self.program.use()
221
+ # # self.program['model'] = self.matrix
222
+ # pass
223
+ #
224
+ # def __hash__(self) -> int:
225
+ # return hash((self.texture.target, self.texture.id, self.program, self.order, self.parent))
226
+ #
227
+ # def __eq__(self, other) -> bool:
228
+ # return (self.__class__ is other.__class__ and
229
+ # self.material == other.material and
230
+ # self.texture.target == other.texture.target and
231
+ # self.texture.id == other.texture.id and
232
+ # self.program == other.program and
233
+ # self.order == other.order and
234
+ # self.parent == other.parent)
225
235
 
226
236
 
227
237
  class MaterialGroup(BaseMaterialGroup):
@@ -354,7 +364,7 @@ class Cube(Model):
354
364
  7, 6, 4, 6, 5, 4, # back
355
365
  3, 2, 0, 2, 1, 0] # front
356
366
 
357
- return self._program.vertex_list_indexed(len(vertices) // 3, pyglet.gl.GL_TRIANGLES, indices,
367
+ return self._program.vertex_list_indexed(len(vertices) // 3, graphics.GeometryMode.TRIANGLES, indices,
358
368
  batch=self._batch, group=self._group,
359
369
  POSITION=('f', vertices),
360
370
  NORMAL=('f', normals),
@@ -412,7 +422,7 @@ class Sphere(Model):
412
422
  indices.extend([first, second, second + 1])
413
423
  indices.extend([first, second + 1, first + 1])
414
424
 
415
- return self._program.vertex_list_indexed(len(vertices) // 3, pyglet.gl.GL_TRIANGLES, indices,
425
+ return self._program.vertex_list_indexed(len(vertices) // 3, graphics.GeometryMode.TRIANGLES, indices,
416
426
  batch=self._batch, group=self._group,
417
427
  POSITION=('f', vertices),
418
428
  NORMAL=('f', normals),
@@ -1,12 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
- from abc import ABC, abstractmethod
3
+ from abc import ABC
4
4
  from typing import TYPE_CHECKING
5
5
 
6
6
  if TYPE_CHECKING:
7
7
  from typing import Sequence
8
8
  from pyglet.model import Model
9
- from pyglet.graphics import Batch, Group
9
+ from pyglet.graphics import Batch, Group, GeometryMode
10
10
 
11
11
 
12
12
  class Scene(ABC):
@@ -75,7 +75,7 @@ class Attribute:
75
75
  class Primitive:
76
76
  """Geometry to be rendered, with optional material."""
77
77
  def __init__(self, attributes: list[Attribute], indices: Sequence[int] | None,
78
- mode: int, material: Material | None = None) -> None:
78
+ mode: GeometryMode, material: Material | None = None) -> None:
79
79
  self.attributes = attributes
80
80
  self.indices = indices
81
81
  self.mode = mode
@@ -107,7 +107,7 @@ class SimpleMaterial(Material):
107
107
  self.texture_name = texture_name
108
108
 
109
109
  def __repr__(self):
110
- return f"Material(name='{self.name}', texture='{self.texture_name}'"
110
+ return f"Material(name='{self.name}', texture='{self.texture_name}')"
111
111
 
112
112
 
113
113
  class PBRMaterial(Material):
@@ -8,9 +8,9 @@ from urllib.request import urlopen
8
8
 
9
9
  import pyglet
10
10
 
11
- from pyglet.gl import GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FLOAT, GL_DOUBLE
12
- from pyglet.gl import GL_INT, GL_UNSIGNED_INT, GL_ELEMENT_ARRAY_BUFFER, GL_ARRAY_BUFFER
13
- from pyglet.gl import GL_REPEAT
11
+ from pyglet.graphics.api.gl import GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FLOAT, GL_DOUBLE
12
+ from pyglet.graphics.api.gl import GL_INT, GL_UNSIGNED_INT, GL_ELEMENT_ARRAY_BUFFER, GL_ARRAY_BUFFER
13
+ from pyglet.graphics.api.gl import GL_REPEAT
14
14
 
15
15
  from . import ModelDecodeException, ModelDecoder
16
16
  from .base import Scene
@@ -4,27 +4,29 @@ import os
4
4
 
5
5
  import pyglet
6
6
 
7
- from pyglet.gl import GL_TRIANGLES
8
7
  from pyglet.util import asstr
9
8
 
10
9
  from . import ModelDecodeException, ModelDecoder
11
10
  from .base import SimpleMaterial, Mesh, Primitive, Attribute, Node, Scene
12
- from .. import Model, MaterialGroup, TexturedMaterialGroup
13
- from ...graphics import Batch, Group
11
+ from pyglet.model import Model, MaterialGroup, TexturedMaterialGroup
12
+ from pyglet.graphics import Batch, Group, GeometryMode
14
13
 
15
14
 
16
15
  def _new_mesh(name, material):
17
16
  # The three primitive types used in .obj files:
18
- attributes = [Attribute('POSITION', 'f', 'VEC3', 0, []),
19
- Attribute('NORMAL', 'f', 'VEC3', 0, []),
20
- Attribute('TEXCOORD_0', 'f', 'VEC3', 0, [])]
21
- primitive = Primitive(attributes=attributes, indices=None, material=material, mode=GL_TRIANGLES)
17
+ attributes = [
18
+ Attribute('POSITION', 'f', 'VEC3', 0, []),
19
+ Attribute('NORMAL', 'f', 'VEC3', 0, []),
20
+ Attribute('TEXCOORD_0', 'f', 'VEC3', 0, []),
21
+ ]
22
+
23
+ primitive = Primitive(attributes=attributes, indices=None, material=material, mode=GeometryMode.TRIANGLES)
22
24
  mesh = Mesh(primitives=[primitive], name=name)
23
25
  return mesh
24
26
 
25
27
 
26
28
  def load_material_library(filename):
27
- file = open(filename, 'r')
29
+ file = open(filename)
28
30
 
29
31
  name = None
30
32
  diffuse = [1.0, 1.0, 1.0]
@@ -73,7 +75,7 @@ def load_material_library(filename):
73
75
  texture_name = values[1]
74
76
 
75
77
  except BaseException as ex:
76
- raise ModelDecodeException('Parsing error in {0}.'.format((filename, ex)))
78
+ raise ModelDecodeException(f'Parsing error in {(filename, ex)}.')
77
79
 
78
80
  file.close()
79
81
 
@@ -93,7 +95,7 @@ def parse_obj_file(filename, file=None) -> list[Mesh]:
93
95
 
94
96
  try:
95
97
  if file is None:
96
- with open(filename, 'r') as f:
98
+ with open(filename) as f:
97
99
  file_contents = f.read()
98
100
  else:
99
101
  file_contents = asstr(file.read())
@@ -132,7 +134,7 @@ def parse_obj_file(filename, file=None) -> list[Mesh]:
132
134
 
133
135
  elif values[0] == 'mtllib':
134
136
  material_abspath = os.path.join(location, values[1])
135
- materials = load_material_library(filename=material_abspath)
137
+ materials = load_material_library(filename=material_abspath)
136
138
 
137
139
  elif values[0] in ('usemtl', 'usemat'):
138
140
  material = materials.get(values[1])
@@ -140,49 +142,75 @@ def parse_obj_file(filename, file=None) -> list[Mesh]:
140
142
  mesh.primitives[0].material = material
141
143
 
142
144
  elif values[0] == 'o':
143
- mesh = _new_mesh(name=values[1], material=default_material)
145
+ mesh = _new_mesh(values[1], material=default_material)
144
146
  meshes.append(mesh)
145
147
 
146
148
  elif values[0] == 'f':
149
+ # --- FACES
150
+
147
151
  if material is None:
148
152
  material = SimpleMaterial()
149
153
  if mesh is None:
150
154
  mesh = _new_mesh(name='unknown', material=material)
151
155
  meshes.append(mesh)
152
156
 
153
- # For fan triangulation, remember first and latest vertices
154
- n1 = None
155
- nlast = None
156
- t1 = None
157
- tlast = None
158
- v1 = None
159
- vlast = None
160
-
161
- for i, v in enumerate(values[1:]):
162
- v_i, t_i, n_i = (list(map(int, [j or 0 for j in v.split('/')])) + [0, 0])[:3]
157
+ primitive = mesh.primitives[0]
158
+
159
+ pos_attr = primitive.attributes[0]
160
+
161
+ # optional normals and teture uvs
162
+ normal_attr = primitive.attributes[1] if len(primitive.attributes) > 1 else None
163
+ tex_uv_attr = primitive.attributes[2] if len(primitive.attributes) > 2 else None
164
+
165
+ face_vertices = []
166
+ for v in values[1:]:
167
+ parts = v.split('/')
168
+
169
+ # vertex index (should not be empty, raise error instead?)
170
+ v_i = int(parts[0]) if parts[0] else 0
171
+
172
+ # texcoord index (optional)
173
+ t_i = 0
174
+ if len(parts) >= 2 and parts[1]:
175
+ t_i = int(parts[1])
176
+
177
+ # normal index (optional)
178
+ n_i = 0
179
+ if len(parts) == 3 and parts[2]:
180
+ n_i = int(parts[2])
181
+
182
+ # handle negative indices
163
183
  if v_i < 0:
164
- v_i += len(vertices) - 1
184
+ v_i = len(vertices) + v_i
165
185
  if t_i < 0:
166
- t_i += len(tex_coords) - 1
186
+ t_i = len(tex_coords) + t_i
167
187
  if n_i < 0:
168
- n_i += len(normals) - 1
169
-
170
- mesh.primitives[0].attributes[0].array += vertices[v_i]
171
- mesh.primitives[0].attributes[1].array += normals[n_i]
172
- mesh.primitives[0].attributes[2].array += tex_coords[t_i]
188
+ n_i = len(normals) + n_i
173
189
 
174
- if i >= 3:
175
- mesh.primitives[0].attributes[0].array += v1 + vlast
176
- mesh.primitives[0].attributes[1].array += n1 + nlast
177
- mesh.primitives[0].attributes[2].array += t1 + tlast
190
+ face_vertices.append((v_i, t_i, n_i))
178
191
 
179
- if i == 0:
180
- n1 = normals[n_i]
181
- t1 = tex_coords[t_i]
182
- v1 = vertices[v_i]
183
- nlast = normals[n_i]
184
- tlast = tex_coords[t_i]
185
- vlast = vertices[v_i]
192
+ # For fan triangulation, remember first and latest vertices
193
+ for i in range(2, len(face_vertices)):
194
+ for idx in (0, i - 1, i):
195
+ v_i, t_i, n_i = face_vertices[idx]
196
+
197
+ pos_attr.array += vertices[v_i]
198
+
199
+ # normals (optional)
200
+ if normal_attr is not None:
201
+ if n_i != 0:
202
+ primitive.has_normals = True
203
+ normal_attr.array += normals[n_i]
204
+ else:
205
+ normal_attr.array += [0.0, 0.0, 1.0]
206
+
207
+ # texcoords (optional)
208
+ if tex_uv_attr is not None:
209
+ if t_i != 0:
210
+ primitive.has_tex_coords = True
211
+ tex_uv_attr.array += tex_coords[t_i]
212
+ else:
213
+ tex_uv_attr.array += [0.0, 0.0]
186
214
 
187
215
  for mesh in meshes:
188
216
  for primitive in mesh.primitives:
@@ -204,17 +232,17 @@ class OBJScene(Scene):
204
232
 
205
233
  if material.texture_name:
206
234
  program = pyglet.model.get_default_textured_shader()
207
- texture = pyglet.resource.texture(material.texture_name)
235
+ texture = pyglet.resource.texture(material.texture_name, atlas=False)
208
236
  matgroup = TexturedMaterialGroup(material, program, texture, parent=group)
209
237
  else:
210
238
  program = pyglet.model.get_default_shader()
211
239
  matgroup = MaterialGroup(material, program, parent=group)
212
240
 
213
- data = {a.name: (a.fmt, a.array) for a in mesh.primitives[0].attributes}
241
+ data = {a.name: (a.fmt, a.array) for a in mesh.primitives[0].attributes if a.name in program.attributes}
214
242
  # Add additional material data:
215
243
  data['COLOR_0'] = 'f', material.diffuse * count
216
244
 
217
- vertex_lists.append(program.vertex_list(count, GL_TRIANGLES, batch, matgroup, **data))
245
+ vertex_lists.append(program.vertex_list(count, GeometryMode.TRIANGLES, batch, matgroup, **data))
218
246
  groups.append(matgroup)
219
247
 
220
248
  return [Model(vertex_lists=vertex_lists, groups=groups, batch=batch)]