TurtleGL-3d 1.2.5__tar.gz → 1.4.1__tar.gz

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 (34) hide show
  1. turtlegl_3d-1.4.1/PKG-INFO +743 -0
  2. turtlegl_3d-1.4.1/README.md +519 -0
  3. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/pyproject.toml +1 -1
  4. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/setup.py +1 -1
  5. turtlegl_3d-1.4.1/src/TurtleGL_3d.egg-info/PKG-INFO +743 -0
  6. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/TurtleGL_3d.egg-info/SOURCES.txt +2 -0
  7. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/__init__.py +2 -1
  8. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/airplane_game.py +4 -3
  9. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/christmas tree/turtleGL_christmas_tree_example.py +6 -2
  10. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/racing_game.py +1 -1
  11. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/texture example/turtleGL_texture_example.py +5 -2
  12. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_anime_example.py +4 -5
  13. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_cabin_example.py +4 -2
  14. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_cube_example.py +4 -2
  15. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_focal_example.py +4 -1
  16. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_from_file_example.py +4 -1
  17. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_grating_example.py +7 -2
  18. turtlegl_3d-1.4.1/src/turtleGL/example/turtleGL_light_example.py +28 -0
  19. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_obj_example.py +9 -8
  20. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_plot3D_example.py +2 -1
  21. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/example/turtleGL_volume_example.py +3 -1
  22. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/src/camera.py +171 -91
  23. turtlegl_3d-1.4.1/src/turtleGL/src/ray.py +95 -0
  24. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/src/scene.py +12 -0
  25. turtlegl_3d-1.2.5/PKG-INFO +0 -554
  26. turtlegl_3d-1.2.5/README.md +0 -330
  27. turtlegl_3d-1.2.5/src/TurtleGL_3d.egg-info/PKG-INFO +0 -554
  28. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/LICENSE +0 -0
  29. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/setup.cfg +0 -0
  30. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/TurtleGL_3d.egg-info/dependency_links.txt +0 -0
  31. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/TurtleGL_3d.egg-info/requires.txt +0 -0
  32. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/TurtleGL_3d.egg-info/top_level.txt +0 -0
  33. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/src/plot3d.py +0 -0
  34. {turtlegl_3d-1.2.5 → turtlegl_3d-1.4.1}/src/turtleGL/src/volume.py +0 -0
@@ -0,0 +1,743 @@
1
+ Metadata-Version: 2.4
2
+ Name: TurtleGL-3d
3
+ Version: 1.4.1
4
+ Summary: A 3D grafics library based on turtle
5
+ Home-page: https://github.com/MichaelHyan/turtleGL
6
+ Author: Han Yan
7
+ Author-email: Han Yan <3367461801@qq.com>
8
+ License:
9
+ Apache License
10
+ Version 2.0, January 2004
11
+ http://www.apache.org/licenses/
12
+
13
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
14
+
15
+ 1. Definitions.
16
+
17
+ "License" shall mean the terms and conditions for use, reproduction,
18
+ and distribution as defined by Sections 1 through 9 of this document.
19
+
20
+ "Licensor" shall mean the copyright owner or entity authorized by
21
+ the copyright owner that is granting the License.
22
+
23
+ "Legal Entity" shall mean the union of the acting entity and all
24
+ other entities that control, are controlled by, or are under common
25
+ control with that entity. For the purposes of this definition,
26
+ "control" means (i) the power, direct or indirect, to cause the
27
+ direction or management of such entity, whether by contract or
28
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
29
+ outstanding shares, or (iii) beneficial ownership of such entity.
30
+
31
+ "You" (or "Your") shall mean an individual or Legal Entity
32
+ exercising permissions granted by this License.
33
+
34
+ "Source" form shall mean the preferred form for making modifications,
35
+ including but not limited to software source code, documentation
36
+ source, and configuration files.
37
+
38
+ "Object" form shall mean any form resulting from mechanical
39
+ transformation or translation of a Source form, including but
40
+ not limited to compiled object code, generated documentation,
41
+ and conversions to other media types.
42
+
43
+ "Work" shall mean the work of authorship, whether in Source or
44
+ Object form, made available under the License, as indicated by a
45
+ copyright notice that is included in or attached to the work
46
+ (an example is provided in the Appendix below).
47
+
48
+ "Derivative Works" shall mean any work, whether in Source or Object
49
+ form, that is based on (or derived from) the Work and for which the
50
+ editorial revisions, annotations, elaborations, or other modifications
51
+ represent, as a whole, an original work of authorship. For the purposes
52
+ of this License, Derivative Works shall not include works that remain
53
+ separable from, or merely link (or bind by name) to the interfaces of,
54
+ the Work and Derivative Works thereof.
55
+
56
+ "Contribution" shall mean any work of authorship, including
57
+ the original version of the Work and any modifications or additions
58
+ to that Work or Derivative Works thereof, that is intentionally
59
+ submitted to Licensor for inclusion in the Work by the copyright owner
60
+ or by an individual or Legal Entity authorized to submit on behalf of
61
+ the copyright owner. For the purposes of this definition, "submitted"
62
+ means any form of electronic, verbal, or written communication sent
63
+ to the Licensor or its representatives, including but not limited to
64
+ communication on electronic mailing lists, source code control systems,
65
+ and issue tracking systems that are managed by, or on behalf of, the
66
+ Licensor for the purpose of discussing and improving the Work, but
67
+ excluding communication that is conspicuously marked or otherwise
68
+ designated in writing by the copyright owner as "Not a Contribution."
69
+
70
+ "Contributor" shall mean Licensor and any individual or Legal Entity
71
+ on behalf of whom a Contribution has been received by Licensor and
72
+ subsequently incorporated within the Work.
73
+
74
+ 2. Grant of Copyright License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ copyright license to reproduce, prepare Derivative Works of,
78
+ publicly display, publicly perform, sublicense, and distribute the
79
+ Work and such Derivative Works in Source or Object form.
80
+
81
+ 3. Grant of Patent License. Subject to the terms and conditions of
82
+ this License, each Contributor hereby grants to You a perpetual,
83
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
84
+ (except as stated in this section) patent license to make, have made,
85
+ use, offer to sell, sell, import, and otherwise transfer the Work,
86
+ where such license applies only to those patent claims licensable
87
+ by such Contributor that are necessarily infringed by their
88
+ Contribution(s) alone or by combination of their Contribution(s)
89
+ with the Work to which such Contribution(s) was submitted. If You
90
+ institute patent litigation against any entity (including a
91
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
92
+ or a Contribution incorporated within the Work constitutes direct
93
+ or contributory patent infringement, then any patent licenses
94
+ granted to You under this License for that Work shall terminate
95
+ as of the date such litigation is filed.
96
+
97
+ 4. Redistribution. You may reproduce and distribute copies of the
98
+ Work or Derivative Works thereof in any medium, with or without
99
+ modifications, and in Source or Object form, provided that You
100
+ meet the following conditions:
101
+
102
+ (a) You must give any other recipients of the Work or
103
+ Derivative Works a copy of this License; and
104
+
105
+ (b) You must cause any modified files to carry prominent notices
106
+ stating that You changed the files; and
107
+
108
+ (c) You must retain, in the Source form of any Derivative Works
109
+ that You distribute, all copyright, patent, trademark, and
110
+ attribution notices from the Source form of the Work,
111
+ excluding those notices that do not pertain to any part of
112
+ the Derivative Works; and
113
+
114
+ (d) If the Work includes a "NOTICE" text file as part of its
115
+ distribution, then any Derivative Works that You distribute must
116
+ include a readable copy of the attribution notices contained
117
+ within such NOTICE file, excluding those notices that do not
118
+ pertain to any part of the Derivative Works, in at least one
119
+ of the following places: within a NOTICE text file distributed
120
+ as part of the Derivative Works; within the Source form or
121
+ documentation, if provided along with the Derivative Works; or,
122
+ within a display generated by the Derivative Works, if and
123
+ wherever such third-party notices normally appear. The contents
124
+ of the NOTICE file are for informational purposes only and
125
+ do not modify the License. You may add Your own attribution
126
+ notices within Derivative Works that You distribute, alongside
127
+ or as an addendum to the NOTICE text from the Work, provided
128
+ that such additional attribution notices cannot be construed
129
+ as modifying the License.
130
+
131
+ You may add Your own copyright statement to Your modifications and
132
+ may provide additional or different license terms and conditions
133
+ for use, reproduction, or distribution of Your modifications, or
134
+ for any such Derivative Works as a whole, provided Your use,
135
+ reproduction, and distribution of the Work otherwise complies with
136
+ the conditions stated in this License.
137
+
138
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
139
+ any Contribution intentionally submitted for inclusion in the Work
140
+ by You to the Licensor shall be under the terms and conditions of
141
+ this License, without any additional terms or conditions.
142
+ Notwithstanding the above, nothing herein shall supersede or modify
143
+ the terms of any separate license agreement you may have executed
144
+ with Licensor regarding such Contributions.
145
+
146
+ 6. Trademarks. This License does not grant permission to use the trade
147
+ names, trademarks, service marks, or product names of the Licensor,
148
+ except as required for reasonable and customary use in describing the
149
+ origin of the Work and reproducing the content of the NOTICE file.
150
+
151
+ 7. Disclaimer of Warranty. Unless required by applicable law or
152
+ agreed to in writing, Licensor provides the Work (and each
153
+ Contributor provides its Contributions) on an "AS IS" BASIS,
154
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
155
+ implied, including, without limitation, any warranties or conditions
156
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
157
+ PARTICULAR PURPOSE. You are solely responsible for determining the
158
+ appropriateness of using or redistributing the Work and assume any
159
+ risks associated with Your exercise of permissions under this License.
160
+
161
+ 8. Limitation of Liability. In no event and under no legal theory,
162
+ whether in tort (including negligence), contract, or otherwise,
163
+ unless required by applicable law (such as deliberate and grossly
164
+ negligent acts) or agreed to in writing, shall any Contributor be
165
+ liable to You for damages, including any direct, indirect, special,
166
+ incidental, or consequential damages of any character arising as a
167
+ result of this License or out of the use or inability to use the
168
+ Work (including but not limited to damages for loss of goodwill,
169
+ work stoppage, computer failure or malfunction, or any and all
170
+ other commercial damages or losses), even if such Contributor
171
+ has been advised of the possibility of such damages.
172
+
173
+ 9. Accepting Warranty or Additional Liability. While redistributing
174
+ the Work or Derivative Works thereof, You may choose to offer,
175
+ and charge a fee for, acceptance of support, warranty, indemnity,
176
+ or other liability obligations and/or rights consistent with this
177
+ License. However, in accepting such obligations, You may act only
178
+ on Your own behalf and on Your sole responsibility, not on behalf
179
+ of any other Contributor, and only if You agree to indemnify,
180
+ defend, and hold each Contributor harmless for any liability
181
+ incurred by, or claims asserted against, such Contributor by reason
182
+ of your accepting any such warranty or additional liability.
183
+
184
+ END OF TERMS AND CONDITIONS
185
+
186
+ APPENDIX: How to apply the Apache License to your work.
187
+
188
+ To apply the Apache License to your work, attach the following
189
+ boilerplate notice, with the fields enclosed by brackets "[]"
190
+ replaced with your own identifying information. (Don't include
191
+ the brackets!) The text should be enclosed in the appropriate
192
+ comment syntax for the file format. We also recommend that a
193
+ file or class name and description of purpose be included on the
194
+ same "printed page" as the copyright notice for easier
195
+ identification within third-party archives.
196
+
197
+ Copyright [yyyy] [name of copyright owner]
198
+
199
+ Licensed under the Apache License, Version 2.0 (the "License");
200
+ you may not use this file except in compliance with the License.
201
+ You may obtain a copy of the License at
202
+
203
+ http://www.apache.org/licenses/LICENSE-2.0
204
+
205
+ Unless required by applicable law or agreed to in writing, software
206
+ distributed under the License is distributed on an "AS IS" BASIS,
207
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208
+ See the License for the specific language governing permissions and
209
+ limitations under the License.
210
+
211
+ Project-URL: Homepage, https://github.com/MichaelHyan/turtleGL
212
+ Classifier: Programming Language :: Python :: 3
213
+ Classifier: License :: OSI Approved :: MIT License
214
+ Classifier: Operating System :: OS Independent
215
+ Requires-Python: >=3.8
216
+ Description-Content-Type: text/markdown
217
+ License-File: LICENSE
218
+ Requires-Dist: numpy>=1.24.4
219
+ Requires-Dist: opencv-python>=4.12.0
220
+ Dynamic: author
221
+ Dynamic: home-page
222
+ Dynamic: license-file
223
+ Dynamic: requires-python
224
+
225
+ # 🐢 TurtleGL-3d
226
+
227
+ **A 3D graphics library based on Python turtle and OpenGL-style rendering techniques.**
228
+
229
+ An object-oriented, intuitive Python 3D graphics library based on the turtle library.
230
+
231
+ ---
232
+
233
+ ## ✨ Features
234
+
235
+ - 🎥 **Camera System** — Independent camera object supporting perspective / cabinet / isometric / orthographic projection modes.
236
+ - 🎨 **Scene Management** — Structured scene object supporting line, face, and texture data management.
237
+ - 📐 **3D Function Plotting** — 3D function image plotting based on `plot3d` object.
238
+ - 📦 **Volume Calculation** — Ray method volume calculation supporting multi-directional checks and partitioning acceleration.
239
+ - 🖼️ **Texture Mapping** — Homography matrix-based texture mapping.
240
+ - 🌗 **Shading & Normal** — Three rendering modes: material preview / shadow mode / normal preview.
241
+ - 💡 **Lighting System** — Independent `ray` object supporting sunlight, point light, and directional ray with brightness control.
242
+ - 📁 **OBJ Import** — Support for importing `.obj` 3D models with normal correction.
243
+ - 🎬 **Image & Video Export** — OpenCV-based image export and video compositing.
244
+ - 🔄 **Transform** — Spatial transformations: rotation, translation, scaling, etc.
245
+ - 📊 **Depth Sorting** — Depth sorting algorithms for different projection modes.
246
+
247
+ ---
248
+
249
+ ## 📦 Installation
250
+
251
+ ```bash
252
+ pip install TurtleGL-3d
253
+ ```
254
+
255
+ ### Dependencies
256
+
257
+ - Python >= 3.8
258
+ - numpy >= 1.24.4
259
+ - opencv-python >= 4.12.0
260
+
261
+ ---
262
+
263
+ ## 🚀 Quick Start
264
+
265
+ ### Basic Example: Drawing a Colorful Cube
266
+
267
+ ```python
268
+ import turtleGL
269
+
270
+ # Instantiate the camera
271
+ camera = turtleGL.camera()
272
+ camera.camera_position = [100, 100, 100]
273
+ camera.to_target([0, 0, 0]) # Face the origin
274
+ camera.camera_focal = 300 # Focal length
275
+ camera.type = 1 # Perspective mode
276
+ camera.rend = 1 # Shadow mode
277
+
278
+ # Instantiate the lighting
279
+ ray = turtleGL.ray()
280
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
281
+
282
+ # Instantiate the scene
283
+ scene = turtleGL.scene()
284
+ scene.face = [
285
+ [[[50, 50, 0], [-50, 50, 0], [-50, -50, 0], [50, -50, 0]], '#FF0000'],
286
+ [[[50, 50, 100], [-50, 50, 100], [-50, -50, 100], [50, -50, 100]], '#00FF00'],
287
+ [[[50, 50, 0], [50, 50, 100], [50, -50, 100], [50, -50, 0]], '#0000FF'],
288
+ [[[-50, 50, 0], [-50, -50, 0], [-50, -50, 100], [-50, 50, 100]], '#FFFF00'],
289
+ [[[50, 50, 0], [-50, 50, 0], [-50, 50, 100], [50, 50, 100]], '#FF00FF'],
290
+ [[[-50, -50, 0], [50, -50, 0], [50, -50, 100], [-50, -50, 100]], '#00FFFF'],
291
+ ]
292
+
293
+ # Depth sorting and drawing
294
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
295
+ camera.done()
296
+ ```
297
+
298
+ ### Importing an OBJ Model
299
+
300
+ ```python
301
+ import turtleGL, math
302
+
303
+ camera = turtleGL.camera('OBJ Example')
304
+ camera.camera_position = [-101, -121, -150]
305
+ camera.to_target([0, 0, 50])
306
+ camera.camera_focal = 500
307
+ camera.type = 1
308
+ camera.rend = 1
309
+
310
+ ray = turtleGL.ray()
311
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
312
+
313
+ scene = turtleGL.scene()
314
+ scene.import_obj('model.obj', 50, '#66ccff') # Scale 50x, specify color
315
+ scene.check_obj_norm('model.obj') # Correct normals
316
+ scene.generate_line('#ffffff') # Generate edges
317
+
318
+ # Rotation animation
319
+ for i in range(360):
320
+ camera.clear()
321
+ camera.camera_position = [150 * math.cos(math.radians(i)),
322
+ 150 * math.sin(math.radians(i)),
323
+ 150]
324
+ camera.to_target([0, 0, 0])
325
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
326
+ camera.update()
327
+ ```
328
+
329
+ ### 3D Function Plotting
330
+
331
+ ```python
332
+ import turtleGL
333
+
334
+ camera = turtleGL.camera('Plot3D Example')
335
+ camera.type = 0 # Cabinet mode
336
+
337
+ scene = turtleGL.plot3d()
338
+ scene.xlim = [-100, 100]
339
+ scene.ylim = [-100, 100]
340
+ scene.step = 10
341
+
342
+ def function(x, y):
343
+ return 0.01 * (x**2 - y**2)
344
+
345
+ scene.generate_face(function)
346
+ scene.generate_line(function, color='#000000')
347
+ camera.draw_from_scene(scene.sort_all_cabin())
348
+ camera.done()
349
+ ```
350
+
351
+ ### Volume Calculation
352
+
353
+ ```python
354
+ import turtleGL
355
+
356
+ camera = turtleGL.camera()
357
+ camera.camera_position = [10, 120, 10]
358
+ camera.to_target([0, 0, 0])
359
+ camera.camera_focal = 500
360
+ camera.type = 1
361
+ camera.rend = 1
362
+
363
+ ray = turtleGL.ray()
364
+ ray.add_sunlight([1, 1, -1])
365
+
366
+ scene = turtleGL.scene()
367
+ scene.import_obj('model.obj', 50, '#66ccff')
368
+ scene.check_obj_norm('model.obj')
369
+ scene.triangulation() # Triangulation is required for volume calculation
370
+ scene.generate_line('#ff0000')
371
+
372
+ volume = turtleGL.volume()
373
+ volume.sample_distance = 5 # Sampling distance
374
+ volume.check = True # Multi-directional check
375
+ volume.allow_edge = True # Allow edge intersections
376
+ volume.volume(scene.face) # Calculate volume
377
+
378
+ camera.draw_from_scene(scene.sort_line_avg(camera.camera_position), ray)
379
+ for i in volume.points:
380
+ camera.dot(i)
381
+ camera.done()
382
+ ```
383
+
384
+ ### Texture Mapping & Image Export
385
+
386
+ ```python
387
+ import turtleGL
388
+
389
+ camera = turtleGL.camera('Texture Example')
390
+ camera.camera_position = [201, 201, 131]
391
+ camera.to_target([0, 0, 50])
392
+ camera.camera_focal = 500
393
+ camera.type = 1
394
+ camera.rend = 1
395
+
396
+ ray = turtleGL.ray()
397
+ ray.add_sunlight([1, 1, -1])
398
+
399
+ scene = turtleGL.scene()
400
+ scene.tex = [
401
+ [[[50, 50, 100], [-50, 50, 100], [-50, -50, 100], [50, -50, 100]], 'grass_up.png'],
402
+ [[[50, 50, 0], [-50, 50, 0], [-50, -50, 0], [50, -50, 0]], 'grass_bottom.png'],
403
+ ]
404
+
405
+ # Render and export using OpenCV
406
+ camera.image_size = [700, 700]
407
+ camera.create_image('#ffffff')
408
+ camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position), ray)
409
+ camera.imshow() # Display
410
+ camera.imwrite('output.png') # Save image
411
+ ```
412
+
413
+ ---
414
+
415
+ ## 📖 API Reference
416
+
417
+ ### Camera Object
418
+
419
+ The camera object handles 3D to 2D projection and rendering.
420
+
421
+ #### Attributes
422
+
423
+ | Attribute | Type | Default | Description |
424
+ |-----------|------|---------|-------------|
425
+ | `camera_position` | `[x,y,z]` | `[0,0,0]` | Camera position |
426
+ | `camera_direction` | `[x,y,z]` | `[0,0,1]` | Camera look direction |
427
+ | `camera_rotation` | `float` | `0` | Camera rotation angle (radians), causing left/right tilt |
428
+ | `camera_focal` | `float` | `1` | Focal length |
429
+ | `point_behind_cam_type` | `int` | `0` | Handling method for points behind the camera (0/1/2/3) |
430
+ | `point_behind_cam_allow_count` | `int` | `0` | Number of allowed behind-camera points during material rendering |
431
+ | `rend` | `int` | `0` | Rendering type: 0 material preview / 1 shadow mode / 2 normal preview |
432
+ | `pensize` | `int` | `2` | Pen size (only affects lines) |
433
+ | `pencolor` | `str` | `'#000000'` | Pen color (only affects lines) |
434
+ | `type` | `int` | `1` | Camera type: 0 cabinet / 1 perspective / 2 isometric / -1 orthographic |
435
+ | `grating_size` | `[x,y]` | `[500,400]` | Raster rendering area size |
436
+ | `grating_length` | `int` | `1` | Raster sampling step |
437
+ | `image_size` | `[x,y]` | `[500,400]` | Export image size |
438
+ | `image` | `ndarray` | `[]` | Currently stored OpenCV image |
439
+
440
+ #### Handling Points Behind the Camera
441
+
442
+ | Mode | Description |
443
+ |------|-------------|
444
+ | `0` | No processing; points behind the camera appear in the opposite direction |
445
+ | `1` | Flip UV to bring points back to normal orientation |
446
+ | `2` | Use orthographic mode with small deviation |
447
+ | `3` | Use scaled orthographic perspective |
448
+
449
+ #### Rendering Modes
450
+
451
+ | Mode | Description |
452
+ |------|-------------|
453
+ | `0` — Material Preview | Faces display the specified color directly |
454
+ | `1` — Shadow Mode | Shading calculated based on the `ray` lighting object; light intensity is computed from sunlight, point light, and directional ray sources |
455
+ | `2` — Normal Mode | Normal direction cosine relative to camera direction > 0 displays blue, otherwise red |
456
+
457
+ #### Methods
458
+
459
+ ```python
460
+ # Basic Setup
461
+ setposition([x,y,z]) # Set camera position
462
+ setdirection([x,y,z]) # Set camera direction
463
+ setfocal(x) # Set focal length
464
+ settype(x) # Set projection type (supports 'focal'/'cabin'/'isometric')
465
+ to_target([x,y,z]) # Point camera toward target
466
+ status() # Print current camera attributes
467
+
468
+ # Coordinate Mapping
469
+ pointfocal([x,y,z]) # Perspective: 3D → 2D mapping, returns [[u,v], bool]
470
+ pointcabinet([x,y,z]) # Cabinet: 3D → 2D mapping
471
+ pointisometric([x,y,z]) # Isometric: 3D → 2D mapping
472
+ pointorthografic([x,y,z]) # Orthographic: 3D → 2D mapping
473
+ pointfocal_inverse([u,v]) # Perspective: 2D → 3D inverse mapping
474
+
475
+ # Drawing (turtle rendering)
476
+ dot([x,y,z], color) # Draw a single point
477
+ drawline(linedata) # Draw an edge
478
+ drawface(facedata) # Draw a face
479
+ drawtex(facedata) # Draw texture
480
+ draw_from_scene(scenedata, ray) # Draw integrated data with lighting
481
+ draw_axis(l) # Draw coordinate axes
482
+ write(point, str) # Write text at a 3D position
483
+
484
+ # Drawing (OpenCV rendering)
485
+ drawline_cv2(linedata) # Draw edge with OpenCV
486
+ drawface_cv2(facedata) # Draw face with OpenCV
487
+ drawtex_cv2(facedata) # Draw texture with OpenCV
488
+ draw_from_scene_cv2(scenedata, ray) # Draw integrated data with OpenCV with lighting
489
+
490
+ # Image Export
491
+ create_image(bgcolor) # Initialize image (can be called repeatedly to clear)
492
+ imshow() # Display current image
493
+ imwrite(path) # Save image to file
494
+ capture(path, index) # Capture frame by index (for video frames)
495
+ to_video(path, fps=30) # Compose video
496
+
497
+ # Raster Algorithm (experimental)
498
+ grating(face) # Perform raster calculation
499
+ grating_cv2(face) # Raster with OpenCV
500
+ show_grating_limit() # Show rendering area border
501
+
502
+ # Utilities
503
+ tracer(t) # Control animation switch
504
+ delay() # Delay
505
+ clear() # Clear canvas
506
+ bgcolor(color) # Set background color
507
+ update() # Update canvas
508
+ done() # Prevent window from closing automatically
509
+ ```
510
+
511
+ ---
512
+
513
+ ### Scene Object
514
+
515
+ The scene object stores line, face, and texture data, and supports spatial transformations and depth sorting.
516
+
517
+ #### Data Format
518
+
519
+ ```python
520
+ # Edge data
521
+ [[[x1,y1,z1], [x2,y2,z2]], '#RRGGBB']
522
+
523
+ # Face data (enter vertices in counterclockwise order; normal points outward)
524
+ [[[x1,y1,z1], [x2,y2,z2], ..., [xn,yn,zn]], '#RRGGBB']
525
+
526
+ # Texture data (4 vertices, color replaced by image path)
527
+ [[[x1,y1,z1], [x2,y2,z2], [x3,y3,z3], [x4,y4,z4]], 'texture.png']
528
+ ```
529
+
530
+ #### Attributes
531
+
532
+ | Attribute | Description |
533
+ |-----------|-------------|
534
+ | `line` | List of edge data |
535
+ | `face` | List of face data |
536
+ | `tex` | List of texture data |
537
+ | `center` | Center point of the scene |
538
+
539
+ #### Methods
540
+
541
+ ```python
542
+ # Add Data
543
+ addline([[x1,y1,z1],[x2,y2,z2],'#color']) # Add an edge
544
+ addface([[x1,y1,z1],...,[xn,yn,zn],'#color'])# Add a face
545
+
546
+ # Import/Export
547
+ import_line(path) # Import line data (CSV)
548
+ import_face(path) # Import face data (CSV)
549
+ export_line(path) # Export line data (CSV)
550
+ export_face(path) # Export face data (CSV)
551
+
552
+ # OBJ Model
553
+ import_obj(path, scale, color) # Import OBJ model (random color if color is empty)
554
+ import_obj_normal(path) # Import OBJ normal data
555
+ check_obj_norm(path) # Correct face orientation based on normals
556
+ add_obj(filepath, scale, color)# Import OBJ and append to existing face data
557
+
558
+ # Spatial Transformations
559
+ rotate(rotate_vector, center) # Rotate around center (rotation vector in radians)
560
+ move(move_vector) # Translate
561
+ scale(scale_vector, center) # Scale
562
+ rotate_edge() # Cycle edges (change triangulation partition)
563
+
564
+ # Depth Sorting — Perspective/Orthographic Modes
565
+ sort_line_avg(camera_pos) # Sort edges (modifies object attribute and returns)
566
+ sort_face_avg(camera_pos) # Sort faces
567
+ sort_tex_avg(camera_pos) # Sort textures
568
+ sort_all_avg(camera_pos) # Sort all (returns data without modifying object)
569
+
570
+ # Depth Sorting — Cabinet Mode
571
+ sort_line_cabin() # Sort edges
572
+ sort_face_cabin() # Sort faces
573
+ sort_tex_cabin() # Sort textures
574
+ sort_all_cabin() # Sort all
575
+
576
+ # Depth Sorting — Isometric Mode
577
+ sort_line_isometric() # Sort edges
578
+ sort_face_isometric() # Sort faces
579
+ sort_tex_isometric() # Sort textures
580
+ sort_all_isometric() # Sort all
581
+
582
+ # Other
583
+ reverse_normvect(i) # Reverse normal direction of the i-th face
584
+ generate_line(color) # Generate edges from face data
585
+ triangulation() # Triangulate faces
586
+ get_center() # Calculate and return the scene center point
587
+ ```
588
+
589
+ ---
590
+
591
+ ### Plot3D Object
592
+
593
+ Object for drawing 3D function graphs, similar to Scene but data generation depends on the target function.
594
+
595
+ #### Attributes
596
+
597
+ | Attribute | Default | Description |
598
+ |-----------|---------|-------------|
599
+ | `xlim` | `[-10, 10]` | X domain |
600
+ | `ylim` | `[-10, 10]` | Y domain |
601
+ | `step` | `1` | Sampling step |
602
+ | `line` | `[]` | Edge data |
603
+ | `face` | `[]` | Face data |
604
+ | `center` | `[0,0,0]` | Center point |
605
+
606
+ #### Methods
607
+
608
+ ```python
609
+ generate_face(func, color=True) # Generate face data for the function (auto color by height)
610
+ generate_line(func, color) # Generate edge data for the function
611
+ rotate(rotate_vector, center) # Rotate
612
+ move(move_vector) # Translate
613
+ scale(scale_vector, center) # Scale
614
+ # Same depth sorting methods as Scene
615
+ ```
616
+
617
+ ---
618
+
619
+ ### Volume Object
620
+
621
+ Volume calculation object using ray casting to determine if a point is inside a closed triangular mesh.
622
+
623
+ #### Attributes
624
+
625
+ | Attribute | Default | Description |
626
+ |-----------|---------|-------------|
627
+ | `points` | `[]` | List of interior points after calculation |
628
+ | `sample_distance` | `1` | Sampling grid spacing |
629
+ | `grid_limit` | `inf` | Threshold for enabling partitioning acceleration (triangle count) |
630
+ | `check` | `True` | Enable multi-directional check |
631
+ | `allow_edge` | `True` | Allow counting edge intersections |
632
+
633
+ #### Methods
634
+
635
+ ```python
636
+ volume(scene_face_data) # Calculate volume, return all interior points
637
+ ```
638
+
639
+ > ⚠️ The partitioning acceleration (`grid_limit`) is only recommended for convex polyhedra; irregular shapes may produce erroneous results.
640
+
641
+ ---
642
+
643
+ ### Ray Object
644
+
645
+ Lighting object that manages light sources and computes shading values for faces.
646
+
647
+ #### Attributes
648
+
649
+ | Attribute | Default | Description |
650
+ |-----------|---------|-------------|
651
+ | `sunlight` | `[]` | List of sunlight sources `[direction, brightness]` |
652
+ | `pointlight` | `[]` | List of point light sources `[position, brightness]` |
653
+ | `ray` | `[]` | List of directional ray sources `[point, direction, brightness]` |
654
+
655
+ #### Methods
656
+
657
+ ```python
658
+ add_sunlight(vec, brightness=1) # Add sunlight (directional light), brightness defaults to 1
659
+ add_pointlight(pos, brightness) # Add point light at position with brightness
660
+ add_ray(point, vec, brightness) # Add directional ray from point in direction with brightness
661
+ get_value(point1, point2, point3) # Compute combined shading value for a triangle face (returns -1 to 1)
662
+ ```
663
+
664
+ #### Light Types
665
+
666
+ | Type | Description |
667
+ |------|-------------|
668
+ | Sunlight | Directional light; shading based on angle between light direction and face normal |
669
+ | Point Light | Positional light; shading based on angle and distance attenuation |
670
+ | Directional Ray | Light from a point in a direction; shading based on angle between ray and face normal |
671
+
672
+ ---
673
+
674
+ ## 🧪 Experimental: Rasterization
675
+
676
+ Raster algorithm is experimental and not yet stable.
677
+
678
+ ```python
679
+ scene.triangulation() # Triangulate faces (raster mode only supports triangles)
680
+ camera.grating_size = [500, 400] # Set rendering area size
681
+ camera.show_grating_limit() # Show rendering area border
682
+ camera.grating(face, ray) # Execute raster calculation
683
+ ```
684
+
685
+ In rendering mode `rend=1`, raster mode uses the `ray` lighting object to compute light paths and shadows.
686
+
687
+ ---
688
+
689
+ ## 🎬 Image & Video Export
690
+
691
+ Since turtle itself does not support image capture, the library provides OpenCV-based image/video export:
692
+
693
+ ```python
694
+ # Initialization
695
+ camera.image_size = [500, 400]
696
+ camera.create_image('#ffffff') # Create image (can be called repeatedly to clear)
697
+
698
+ # Rendering
699
+ camera.draw_from_scene_cv2(scene_data) # Render with OpenCV
700
+
701
+ # Export
702
+ camera.imwrite('output.png') # Save image
703
+
704
+ # Video creation
705
+ camera.capture('frames', i) # Save frames by index → ./frames/00000001.png
706
+ camera.to_video('frames') # Compose video → frames.mp4
707
+ ```
708
+
709
+ ---
710
+
711
+ ## 📁 Project Structure
712
+
713
+ ```
714
+ turtleGL-3d/
715
+ ├── pyproject.toml
716
+ ├── setup.py
717
+ ├── LICENSE
718
+ ├── README.md
719
+ ├── README_zh.md
720
+ └── src/
721
+ └── turtleGL/
722
+ ├── __init__.py
723
+ ├── src/
724
+ │ ├── camera.py # Camera object
725
+ │ ├── scene.py # Scene object
726
+ │ ├── plot3d.py # 3D function plot object
727
+ │ ├── ray.py # Lighting object
728
+ │ └── volume.py # Volume calculation object
729
+ └── example/
730
+ ```
731
+
732
+ ---
733
+
734
+ ## 📄 License
735
+
736
+ This project is licensed under the Apache License 2.0 — see the [LICENSE](LICENSE) file for details.
737
+
738
+ ---
739
+
740
+ ## 🔗 Links
741
+
742
+ - **Homepage**: [https://github.com/MichaelHyan/turtleGL](https://github.com/MichaelHyan/turtleGL)
743
+ - **PyPI**: [https://pypi.org/project/TurtleGL-3d/](https://pypi.org/project/TurtleGL-3d/)