TurtleGL-3d 1.2.7__tar.gz → 1.4.2__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 (31) hide show
  1. {turtlegl_3d-1.2.7/src/TurtleGL_3d.egg-info → turtlegl_3d-1.4.2}/PKG-INFO +56 -13
  2. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/README.md +55 -12
  3. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/pyproject.toml +1 -1
  4. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/setup.py +1 -1
  5. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2/src/TurtleGL_3d.egg-info}/PKG-INFO +56 -13
  6. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/TurtleGL_3d.egg-info/SOURCES.txt +2 -0
  7. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/__init__.py +2 -1
  8. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/airplane_game.py +4 -3
  9. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/christmas tree/turtleGL_christmas_tree_example.py +6 -2
  10. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/racing_game.py +1 -1
  11. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/texture example/turtleGL_texture_example.py +5 -2
  12. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_anime_example.py +4 -5
  13. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_cabin_example.py +4 -2
  14. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_cube_example.py +4 -2
  15. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_focal_example.py +4 -1
  16. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_from_file_example.py +4 -1
  17. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_grating_example.py +5 -3
  18. turtlegl_3d-1.4.2/src/turtleGL/example/turtleGL_light_example.py +28 -0
  19. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_obj_example.py +9 -8
  20. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_plot3D_example.py +2 -1
  21. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/example/turtleGL_volume_example.py +3 -1
  22. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/src/camera.py +66 -71
  23. turtlegl_3d-1.4.2/src/turtleGL/src/ray.py +108 -0
  24. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/LICENSE +0 -0
  25. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/setup.cfg +0 -0
  26. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/TurtleGL_3d.egg-info/dependency_links.txt +0 -0
  27. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/TurtleGL_3d.egg-info/requires.txt +0 -0
  28. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/TurtleGL_3d.egg-info/top_level.txt +0 -0
  29. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/src/plot3d.py +0 -0
  30. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/src/scene.py +0 -0
  31. {turtlegl_3d-1.2.7 → turtlegl_3d-1.4.2}/src/turtleGL/src/volume.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: TurtleGL-3d
3
- Version: 1.2.7
3
+ Version: 1.4.2
4
4
  Summary: A 3D grafics library based on turtle
5
5
  Home-page: https://github.com/MichaelHyan/turtleGL
6
6
  Author: Han Yan
@@ -238,6 +238,7 @@ An object-oriented, intuitive Python 3D graphics library based on the turtle lib
238
238
  - 📦 **Volume Calculation** — Ray method volume calculation supporting multi-directional checks and partitioning acceleration.
239
239
  - 🖼️ **Texture Mapping** — Homography matrix-based texture mapping.
240
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.
241
242
  - 📁 **OBJ Import** — Support for importing `.obj` 3D models with normal correction.
242
243
  - 🎬 **Image & Video Export** — OpenCV-based image export and video compositing.
243
244
  - 🔄 **Transform** — Spatial transformations: rotation, translation, scaling, etc.
@@ -274,6 +275,10 @@ camera.camera_focal = 300 # Focal length
274
275
  camera.type = 1 # Perspective mode
275
276
  camera.rend = 1 # Shadow mode
276
277
 
278
+ # Instantiate the lighting
279
+ ray = turtleGL.ray()
280
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
281
+
277
282
  # Instantiate the scene
278
283
  scene = turtleGL.scene()
279
284
  scene.face = [
@@ -286,7 +291,7 @@ scene.face = [
286
291
  ]
287
292
 
288
293
  # Depth sorting and drawing
289
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
294
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
290
295
  camera.done()
291
296
  ```
292
297
 
@@ -299,10 +304,12 @@ camera = turtleGL.camera('OBJ Example')
299
304
  camera.camera_position = [-101, -121, -150]
300
305
  camera.to_target([0, 0, 50])
301
306
  camera.camera_focal = 500
302
- camera.ray = [1, 1, -1]
303
307
  camera.type = 1
304
308
  camera.rend = 1
305
309
 
310
+ ray = turtleGL.ray()
311
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
312
+
306
313
  scene = turtleGL.scene()
307
314
  scene.import_obj('model.obj', 50, '#66ccff') # Scale 50x, specify color
308
315
  scene.check_obj_norm('model.obj') # Correct normals
@@ -315,7 +322,7 @@ for i in range(360):
315
322
  150 * math.sin(math.radians(i)),
316
323
  150]
317
324
  camera.to_target([0, 0, 0])
318
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
325
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
319
326
  camera.update()
320
327
  ```
321
328
 
@@ -353,6 +360,9 @@ camera.camera_focal = 500
353
360
  camera.type = 1
354
361
  camera.rend = 1
355
362
 
363
+ ray = turtleGL.ray()
364
+ ray.add_sunlight([1, 1, -1])
365
+
356
366
  scene = turtleGL.scene()
357
367
  scene.import_obj('model.obj', 50, '#66ccff')
358
368
  scene.check_obj_norm('model.obj')
@@ -365,7 +375,7 @@ volume.check = True # Multi-directional check
365
375
  volume.allow_edge = True # Allow edge intersections
366
376
  volume.volume(scene.face) # Calculate volume
367
377
 
368
- camera.draw_from_scene(scene.sort_line_avg(camera.camera_position))
378
+ camera.draw_from_scene(scene.sort_line_avg(camera.camera_position), ray)
369
379
  for i in volume.points:
370
380
  camera.dot(i)
371
381
  camera.done()
@@ -383,6 +393,9 @@ camera.camera_focal = 500
383
393
  camera.type = 1
384
394
  camera.rend = 1
385
395
 
396
+ ray = turtleGL.ray()
397
+ ray.add_sunlight([1, 1, -1])
398
+
386
399
  scene = turtleGL.scene()
387
400
  scene.tex = [
388
401
  [[[50, 50, 100], [-50, 50, 100], [-50, -50, 100], [50, -50, 100]], 'grass_up.png'],
@@ -392,7 +405,7 @@ scene.tex = [
392
405
  # Render and export using OpenCV
393
406
  camera.image_size = [700, 700]
394
407
  camera.create_image('#ffffff')
395
- camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position))
408
+ camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position), ray)
396
409
  camera.imshow() # Display
397
410
  camera.imwrite('output.png') # Save image
398
411
  ```
@@ -415,9 +428,7 @@ The camera object handles 3D to 2D projection and rendering.
415
428
  | `camera_focal` | `float` | `1` | Focal length |
416
429
  | `point_behind_cam_type` | `int` | `0` | Handling method for points behind the camera (0/1/2/3) |
417
430
  | `point_behind_cam_allow_count` | `int` | `0` | Number of allowed behind-camera points during material rendering |
418
- | `ray` | `[x,y,z]` | `[0,0,-1]` | Light direction, used for shading |
419
431
  | `rend` | `int` | `0` | Rendering type: 0 material preview / 1 shadow mode / 2 normal preview |
420
- | `shade_value` | `int` | `128` | Multiply factor for shading (0-255) |
421
432
  | `pensize` | `int` | `2` | Pen size (only affects lines) |
422
433
  | `pencolor` | `str` | `'#000000'` | Pen color (only affects lines) |
423
434
  | `type` | `int` | `1` | Camera type: 0 cabinet / 1 perspective / 2 isometric / -1 orthographic |
@@ -440,7 +451,7 @@ The camera object handles 3D to 2D projection and rendering.
440
451
  | Mode | Description |
441
452
  |------|-------------|
442
453
  | `0` — Material Preview | Faces display the specified color directly |
443
- | `1` — Shadow Mode | Shadows calculated based on angle between light direction and normal; backlit faces use the multiply factor |
454
+ | `1` — Shadow Mode | Shading calculated based on the `ray` lighting object; light intensity is computed from sunlight, point light, and directional ray sources |
444
455
  | `2` — Normal Mode | Normal direction cosine relative to camera direction > 0 displays blue, otherwise red |
445
456
 
446
457
  #### Methods
@@ -466,7 +477,7 @@ dot([x,y,z], color) # Draw a single point
466
477
  drawline(linedata) # Draw an edge
467
478
  drawface(facedata) # Draw a face
468
479
  drawtex(facedata) # Draw texture
469
- draw_from_scene(scenedata) # Draw integrated data
480
+ draw_from_scene(scenedata, ray) # Draw integrated data with lighting
470
481
  draw_axis(l) # Draw coordinate axes
471
482
  write(point, str) # Write text at a 3D position
472
483
 
@@ -474,7 +485,7 @@ write(point, str) # Write text at a 3D position
474
485
  drawline_cv2(linedata) # Draw edge with OpenCV
475
486
  drawface_cv2(facedata) # Draw face with OpenCV
476
487
  drawtex_cv2(facedata) # Draw texture with OpenCV
477
- draw_from_scene_cv2(scenedata)# Draw integrated data with OpenCV
488
+ draw_from_scene_cv2(scenedata, ray) # Draw integrated data with OpenCV with lighting
478
489
 
479
490
  # Image Export
480
491
  create_image(bgcolor) # Initialize image (can be called repeatedly to clear)
@@ -629,6 +640,37 @@ volume(scene_face_data) # Calculate volume, return all interior points
629
640
 
630
641
  ---
631
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
+
632
674
  ## 🧪 Experimental: Rasterization
633
675
 
634
676
  Raster algorithm is experimental and not yet stable.
@@ -637,10 +679,10 @@ Raster algorithm is experimental and not yet stable.
637
679
  scene.triangulation() # Triangulate faces (raster mode only supports triangles)
638
680
  camera.grating_size = [500, 400] # Set rendering area size
639
681
  camera.show_grating_limit() # Show rendering area border
640
- camera.grating(face) # Execute raster calculation
682
+ camera.grating(face, ray) # Execute raster calculation
641
683
  ```
642
684
 
643
- In rendering mode `rend=1`, raster mode does not use shadow calculation but computes the light path.
685
+ In rendering mode `rend=1`, raster mode uses the `ray` lighting object to compute light paths and shadows.
644
686
 
645
687
  ---
646
688
 
@@ -682,6 +724,7 @@ turtleGL-3d/
682
724
  │ ├── camera.py # Camera object
683
725
  │ ├── scene.py # Scene object
684
726
  │ ├── plot3d.py # 3D function plot object
727
+ │ ├── ray.py # Lighting object
685
728
  │ └── volume.py # Volume calculation object
686
729
  └── example/
687
730
  ```
@@ -14,6 +14,7 @@ An object-oriented, intuitive Python 3D graphics library based on the turtle lib
14
14
  - 📦 **Volume Calculation** — Ray method volume calculation supporting multi-directional checks and partitioning acceleration.
15
15
  - 🖼️ **Texture Mapping** — Homography matrix-based texture mapping.
16
16
  - 🌗 **Shading & Normal** — Three rendering modes: material preview / shadow mode / normal preview.
17
+ - 💡 **Lighting System** — Independent `ray` object supporting sunlight, point light, and directional ray with brightness control.
17
18
  - 📁 **OBJ Import** — Support for importing `.obj` 3D models with normal correction.
18
19
  - 🎬 **Image & Video Export** — OpenCV-based image export and video compositing.
19
20
  - 🔄 **Transform** — Spatial transformations: rotation, translation, scaling, etc.
@@ -50,6 +51,10 @@ camera.camera_focal = 300 # Focal length
50
51
  camera.type = 1 # Perspective mode
51
52
  camera.rend = 1 # Shadow mode
52
53
 
54
+ # Instantiate the lighting
55
+ ray = turtleGL.ray()
56
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
57
+
53
58
  # Instantiate the scene
54
59
  scene = turtleGL.scene()
55
60
  scene.face = [
@@ -62,7 +67,7 @@ scene.face = [
62
67
  ]
63
68
 
64
69
  # Depth sorting and drawing
65
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
70
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
66
71
  camera.done()
67
72
  ```
68
73
 
@@ -75,10 +80,12 @@ camera = turtleGL.camera('OBJ Example')
75
80
  camera.camera_position = [-101, -121, -150]
76
81
  camera.to_target([0, 0, 50])
77
82
  camera.camera_focal = 500
78
- camera.ray = [1, 1, -1]
79
83
  camera.type = 1
80
84
  camera.rend = 1
81
85
 
86
+ ray = turtleGL.ray()
87
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
88
+
82
89
  scene = turtleGL.scene()
83
90
  scene.import_obj('model.obj', 50, '#66ccff') # Scale 50x, specify color
84
91
  scene.check_obj_norm('model.obj') # Correct normals
@@ -91,7 +98,7 @@ for i in range(360):
91
98
  150 * math.sin(math.radians(i)),
92
99
  150]
93
100
  camera.to_target([0, 0, 0])
94
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
101
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
95
102
  camera.update()
96
103
  ```
97
104
 
@@ -129,6 +136,9 @@ camera.camera_focal = 500
129
136
  camera.type = 1
130
137
  camera.rend = 1
131
138
 
139
+ ray = turtleGL.ray()
140
+ ray.add_sunlight([1, 1, -1])
141
+
132
142
  scene = turtleGL.scene()
133
143
  scene.import_obj('model.obj', 50, '#66ccff')
134
144
  scene.check_obj_norm('model.obj')
@@ -141,7 +151,7 @@ volume.check = True # Multi-directional check
141
151
  volume.allow_edge = True # Allow edge intersections
142
152
  volume.volume(scene.face) # Calculate volume
143
153
 
144
- camera.draw_from_scene(scene.sort_line_avg(camera.camera_position))
154
+ camera.draw_from_scene(scene.sort_line_avg(camera.camera_position), ray)
145
155
  for i in volume.points:
146
156
  camera.dot(i)
147
157
  camera.done()
@@ -159,6 +169,9 @@ camera.camera_focal = 500
159
169
  camera.type = 1
160
170
  camera.rend = 1
161
171
 
172
+ ray = turtleGL.ray()
173
+ ray.add_sunlight([1, 1, -1])
174
+
162
175
  scene = turtleGL.scene()
163
176
  scene.tex = [
164
177
  [[[50, 50, 100], [-50, 50, 100], [-50, -50, 100], [50, -50, 100]], 'grass_up.png'],
@@ -168,7 +181,7 @@ scene.tex = [
168
181
  # Render and export using OpenCV
169
182
  camera.image_size = [700, 700]
170
183
  camera.create_image('#ffffff')
171
- camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position))
184
+ camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position), ray)
172
185
  camera.imshow() # Display
173
186
  camera.imwrite('output.png') # Save image
174
187
  ```
@@ -191,9 +204,7 @@ The camera object handles 3D to 2D projection and rendering.
191
204
  | `camera_focal` | `float` | `1` | Focal length |
192
205
  | `point_behind_cam_type` | `int` | `0` | Handling method for points behind the camera (0/1/2/3) |
193
206
  | `point_behind_cam_allow_count` | `int` | `0` | Number of allowed behind-camera points during material rendering |
194
- | `ray` | `[x,y,z]` | `[0,0,-1]` | Light direction, used for shading |
195
207
  | `rend` | `int` | `0` | Rendering type: 0 material preview / 1 shadow mode / 2 normal preview |
196
- | `shade_value` | `int` | `128` | Multiply factor for shading (0-255) |
197
208
  | `pensize` | `int` | `2` | Pen size (only affects lines) |
198
209
  | `pencolor` | `str` | `'#000000'` | Pen color (only affects lines) |
199
210
  | `type` | `int` | `1` | Camera type: 0 cabinet / 1 perspective / 2 isometric / -1 orthographic |
@@ -216,7 +227,7 @@ The camera object handles 3D to 2D projection and rendering.
216
227
  | Mode | Description |
217
228
  |------|-------------|
218
229
  | `0` — Material Preview | Faces display the specified color directly |
219
- | `1` — Shadow Mode | Shadows calculated based on angle between light direction and normal; backlit faces use the multiply factor |
230
+ | `1` — Shadow Mode | Shading calculated based on the `ray` lighting object; light intensity is computed from sunlight, point light, and directional ray sources |
220
231
  | `2` — Normal Mode | Normal direction cosine relative to camera direction > 0 displays blue, otherwise red |
221
232
 
222
233
  #### Methods
@@ -242,7 +253,7 @@ dot([x,y,z], color) # Draw a single point
242
253
  drawline(linedata) # Draw an edge
243
254
  drawface(facedata) # Draw a face
244
255
  drawtex(facedata) # Draw texture
245
- draw_from_scene(scenedata) # Draw integrated data
256
+ draw_from_scene(scenedata, ray) # Draw integrated data with lighting
246
257
  draw_axis(l) # Draw coordinate axes
247
258
  write(point, str) # Write text at a 3D position
248
259
 
@@ -250,7 +261,7 @@ write(point, str) # Write text at a 3D position
250
261
  drawline_cv2(linedata) # Draw edge with OpenCV
251
262
  drawface_cv2(facedata) # Draw face with OpenCV
252
263
  drawtex_cv2(facedata) # Draw texture with OpenCV
253
- draw_from_scene_cv2(scenedata)# Draw integrated data with OpenCV
264
+ draw_from_scene_cv2(scenedata, ray) # Draw integrated data with OpenCV with lighting
254
265
 
255
266
  # Image Export
256
267
  create_image(bgcolor) # Initialize image (can be called repeatedly to clear)
@@ -405,6 +416,37 @@ volume(scene_face_data) # Calculate volume, return all interior points
405
416
 
406
417
  ---
407
418
 
419
+ ### Ray Object
420
+
421
+ Lighting object that manages light sources and computes shading values for faces.
422
+
423
+ #### Attributes
424
+
425
+ | Attribute | Default | Description |
426
+ |-----------|---------|-------------|
427
+ | `sunlight` | `[]` | List of sunlight sources `[direction, brightness]` |
428
+ | `pointlight` | `[]` | List of point light sources `[position, brightness]` |
429
+ | `ray` | `[]` | List of directional ray sources `[point, direction, brightness]` |
430
+
431
+ #### Methods
432
+
433
+ ```python
434
+ add_sunlight(vec, brightness=1) # Add sunlight (directional light), brightness defaults to 1
435
+ add_pointlight(pos, brightness) # Add point light at position with brightness
436
+ add_ray(point, vec, brightness) # Add directional ray from point in direction with brightness
437
+ get_value(point1, point2, point3) # Compute combined shading value for a triangle face (returns -1 to 1)
438
+ ```
439
+
440
+ #### Light Types
441
+
442
+ | Type | Description |
443
+ |------|-------------|
444
+ | Sunlight | Directional light; shading based on angle between light direction and face normal |
445
+ | Point Light | Positional light; shading based on angle and distance attenuation |
446
+ | Directional Ray | Light from a point in a direction; shading based on angle between ray and face normal |
447
+
448
+ ---
449
+
408
450
  ## 🧪 Experimental: Rasterization
409
451
 
410
452
  Raster algorithm is experimental and not yet stable.
@@ -413,10 +455,10 @@ Raster algorithm is experimental and not yet stable.
413
455
  scene.triangulation() # Triangulate faces (raster mode only supports triangles)
414
456
  camera.grating_size = [500, 400] # Set rendering area size
415
457
  camera.show_grating_limit() # Show rendering area border
416
- camera.grating(face) # Execute raster calculation
458
+ camera.grating(face, ray) # Execute raster calculation
417
459
  ```
418
460
 
419
- In rendering mode `rend=1`, raster mode does not use shadow calculation but computes the light path.
461
+ In rendering mode `rend=1`, raster mode uses the `ray` lighting object to compute light paths and shadows.
420
462
 
421
463
  ---
422
464
 
@@ -458,6 +500,7 @@ turtleGL-3d/
458
500
  │ ├── camera.py # Camera object
459
501
  │ ├── scene.py # Scene object
460
502
  │ ├── plot3d.py # 3D function plot object
503
+ │ ├── ray.py # Lighting object
461
504
  │ └── volume.py # Volume calculation object
462
505
  └── example/
463
506
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "TurtleGL-3d"
7
- version = "1.2.7"
7
+ version = "1.4.2"
8
8
  authors = [
9
9
  {name = "Han Yan", email = "3367461801@qq.com"}
10
10
  ]
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="TurtleGL-3d",
8
- version="1.2.7",
8
+ version="1.4.2",
9
9
  author="Han Yan",
10
10
  author_email="3367461801@qq.com",
11
11
  description="A 3D grafics library based on turtle",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: TurtleGL-3d
3
- Version: 1.2.7
3
+ Version: 1.4.2
4
4
  Summary: A 3D grafics library based on turtle
5
5
  Home-page: https://github.com/MichaelHyan/turtleGL
6
6
  Author: Han Yan
@@ -238,6 +238,7 @@ An object-oriented, intuitive Python 3D graphics library based on the turtle lib
238
238
  - 📦 **Volume Calculation** — Ray method volume calculation supporting multi-directional checks and partitioning acceleration.
239
239
  - 🖼️ **Texture Mapping** — Homography matrix-based texture mapping.
240
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.
241
242
  - 📁 **OBJ Import** — Support for importing `.obj` 3D models with normal correction.
242
243
  - 🎬 **Image & Video Export** — OpenCV-based image export and video compositing.
243
244
  - 🔄 **Transform** — Spatial transformations: rotation, translation, scaling, etc.
@@ -274,6 +275,10 @@ camera.camera_focal = 300 # Focal length
274
275
  camera.type = 1 # Perspective mode
275
276
  camera.rend = 1 # Shadow mode
276
277
 
278
+ # Instantiate the lighting
279
+ ray = turtleGL.ray()
280
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
281
+
277
282
  # Instantiate the scene
278
283
  scene = turtleGL.scene()
279
284
  scene.face = [
@@ -286,7 +291,7 @@ scene.face = [
286
291
  ]
287
292
 
288
293
  # Depth sorting and drawing
289
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
294
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
290
295
  camera.done()
291
296
  ```
292
297
 
@@ -299,10 +304,12 @@ camera = turtleGL.camera('OBJ Example')
299
304
  camera.camera_position = [-101, -121, -150]
300
305
  camera.to_target([0, 0, 50])
301
306
  camera.camera_focal = 500
302
- camera.ray = [1, 1, -1]
303
307
  camera.type = 1
304
308
  camera.rend = 1
305
309
 
310
+ ray = turtleGL.ray()
311
+ ray.add_sunlight([1, 1, -1]) # Add sunlight direction
312
+
306
313
  scene = turtleGL.scene()
307
314
  scene.import_obj('model.obj', 50, '#66ccff') # Scale 50x, specify color
308
315
  scene.check_obj_norm('model.obj') # Correct normals
@@ -315,7 +322,7 @@ for i in range(360):
315
322
  150 * math.sin(math.radians(i)),
316
323
  150]
317
324
  camera.to_target([0, 0, 0])
318
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
325
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position), ray)
319
326
  camera.update()
320
327
  ```
321
328
 
@@ -353,6 +360,9 @@ camera.camera_focal = 500
353
360
  camera.type = 1
354
361
  camera.rend = 1
355
362
 
363
+ ray = turtleGL.ray()
364
+ ray.add_sunlight([1, 1, -1])
365
+
356
366
  scene = turtleGL.scene()
357
367
  scene.import_obj('model.obj', 50, '#66ccff')
358
368
  scene.check_obj_norm('model.obj')
@@ -365,7 +375,7 @@ volume.check = True # Multi-directional check
365
375
  volume.allow_edge = True # Allow edge intersections
366
376
  volume.volume(scene.face) # Calculate volume
367
377
 
368
- camera.draw_from_scene(scene.sort_line_avg(camera.camera_position))
378
+ camera.draw_from_scene(scene.sort_line_avg(camera.camera_position), ray)
369
379
  for i in volume.points:
370
380
  camera.dot(i)
371
381
  camera.done()
@@ -383,6 +393,9 @@ camera.camera_focal = 500
383
393
  camera.type = 1
384
394
  camera.rend = 1
385
395
 
396
+ ray = turtleGL.ray()
397
+ ray.add_sunlight([1, 1, -1])
398
+
386
399
  scene = turtleGL.scene()
387
400
  scene.tex = [
388
401
  [[[50, 50, 100], [-50, 50, 100], [-50, -50, 100], [50, -50, 100]], 'grass_up.png'],
@@ -392,7 +405,7 @@ scene.tex = [
392
405
  # Render and export using OpenCV
393
406
  camera.image_size = [700, 700]
394
407
  camera.create_image('#ffffff')
395
- camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position))
408
+ camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position), ray)
396
409
  camera.imshow() # Display
397
410
  camera.imwrite('output.png') # Save image
398
411
  ```
@@ -415,9 +428,7 @@ The camera object handles 3D to 2D projection and rendering.
415
428
  | `camera_focal` | `float` | `1` | Focal length |
416
429
  | `point_behind_cam_type` | `int` | `0` | Handling method for points behind the camera (0/1/2/3) |
417
430
  | `point_behind_cam_allow_count` | `int` | `0` | Number of allowed behind-camera points during material rendering |
418
- | `ray` | `[x,y,z]` | `[0,0,-1]` | Light direction, used for shading |
419
431
  | `rend` | `int` | `0` | Rendering type: 0 material preview / 1 shadow mode / 2 normal preview |
420
- | `shade_value` | `int` | `128` | Multiply factor for shading (0-255) |
421
432
  | `pensize` | `int` | `2` | Pen size (only affects lines) |
422
433
  | `pencolor` | `str` | `'#000000'` | Pen color (only affects lines) |
423
434
  | `type` | `int` | `1` | Camera type: 0 cabinet / 1 perspective / 2 isometric / -1 orthographic |
@@ -440,7 +451,7 @@ The camera object handles 3D to 2D projection and rendering.
440
451
  | Mode | Description |
441
452
  |------|-------------|
442
453
  | `0` — Material Preview | Faces display the specified color directly |
443
- | `1` — Shadow Mode | Shadows calculated based on angle between light direction and normal; backlit faces use the multiply factor |
454
+ | `1` — Shadow Mode | Shading calculated based on the `ray` lighting object; light intensity is computed from sunlight, point light, and directional ray sources |
444
455
  | `2` — Normal Mode | Normal direction cosine relative to camera direction > 0 displays blue, otherwise red |
445
456
 
446
457
  #### Methods
@@ -466,7 +477,7 @@ dot([x,y,z], color) # Draw a single point
466
477
  drawline(linedata) # Draw an edge
467
478
  drawface(facedata) # Draw a face
468
479
  drawtex(facedata) # Draw texture
469
- draw_from_scene(scenedata) # Draw integrated data
480
+ draw_from_scene(scenedata, ray) # Draw integrated data with lighting
470
481
  draw_axis(l) # Draw coordinate axes
471
482
  write(point, str) # Write text at a 3D position
472
483
 
@@ -474,7 +485,7 @@ write(point, str) # Write text at a 3D position
474
485
  drawline_cv2(linedata) # Draw edge with OpenCV
475
486
  drawface_cv2(facedata) # Draw face with OpenCV
476
487
  drawtex_cv2(facedata) # Draw texture with OpenCV
477
- draw_from_scene_cv2(scenedata)# Draw integrated data with OpenCV
488
+ draw_from_scene_cv2(scenedata, ray) # Draw integrated data with OpenCV with lighting
478
489
 
479
490
  # Image Export
480
491
  create_image(bgcolor) # Initialize image (can be called repeatedly to clear)
@@ -629,6 +640,37 @@ volume(scene_face_data) # Calculate volume, return all interior points
629
640
 
630
641
  ---
631
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
+
632
674
  ## 🧪 Experimental: Rasterization
633
675
 
634
676
  Raster algorithm is experimental and not yet stable.
@@ -637,10 +679,10 @@ Raster algorithm is experimental and not yet stable.
637
679
  scene.triangulation() # Triangulate faces (raster mode only supports triangles)
638
680
  camera.grating_size = [500, 400] # Set rendering area size
639
681
  camera.show_grating_limit() # Show rendering area border
640
- camera.grating(face) # Execute raster calculation
682
+ camera.grating(face, ray) # Execute raster calculation
641
683
  ```
642
684
 
643
- In rendering mode `rend=1`, raster mode does not use shadow calculation but computes the light path.
685
+ In rendering mode `rend=1`, raster mode uses the `ray` lighting object to compute light paths and shadows.
644
686
 
645
687
  ---
646
688
 
@@ -682,6 +724,7 @@ turtleGL-3d/
682
724
  │ ├── camera.py # Camera object
683
725
  │ ├── scene.py # Scene object
684
726
  │ ├── plot3d.py # 3D function plot object
727
+ │ ├── ray.py # Lighting object
685
728
  │ └── volume.py # Volume calculation object
686
729
  └── example/
687
730
  ```
@@ -16,6 +16,7 @@ src/turtleGL/example/turtleGL_cube_example.py
16
16
  src/turtleGL/example/turtleGL_focal_example.py
17
17
  src/turtleGL/example/turtleGL_from_file_example.py
18
18
  src/turtleGL/example/turtleGL_grating_example.py
19
+ src/turtleGL/example/turtleGL_light_example.py
19
20
  src/turtleGL/example/turtleGL_obj_example.py
20
21
  src/turtleGL/example/turtleGL_plot3D_example.py
21
22
  src/turtleGL/example/turtleGL_volume_example.py
@@ -23,5 +24,6 @@ src/turtleGL/example/christmas tree/turtleGL_christmas_tree_example.py
23
24
  src/turtleGL/example/texture example/turtleGL_texture_example.py
24
25
  src/turtleGL/src/camera.py
25
26
  src/turtleGL/src/plot3d.py
27
+ src/turtleGL/src/ray.py
26
28
  src/turtleGL/src/scene.py
27
29
  src/turtleGL/src/volume.py
@@ -16,4 +16,5 @@ limitations under the License.
16
16
  from .src.camera import camera
17
17
  from .src.scene import scene
18
18
  from .src.plot3d import plot3d
19
- from .src.volume import volume
19
+ from .src.volume import volume
20
+ from .src.ray import ray
@@ -6,8 +6,9 @@ camera.camera_position = [0,400,0]
6
6
  camera.to_target([0,0,0])
7
7
  camera.camera_focal = 300
8
8
  camera.type = 1
9
- camera.rend = 1
10
-
9
+ camera.rend = 0
10
+ camera.bgcolor('#000000')
11
+ ray = turtleGL.ray()
11
12
  class Player:
12
13
  def __init__(self):
13
14
  self.x = 0
@@ -189,7 +190,7 @@ try:
189
190
  scene.line = []
190
191
 
191
192
  for face in scene.face:
192
- camera.drawface(face)
193
+ camera.drawface(face,ray)
193
194
 
194
195
  camera.write([-280, 0, 250], f'得分: {game.score}', font=("Arial", 16, "bold"))
195
196
  camera.draw_axis(5)
@@ -56,13 +56,17 @@ for i in scene.line:
56
56
  scene.face = tree_face #暂存列表数据传递到scene数据
57
57
  scene.line = tree_line
58
58
 
59
+ ray = turtleGL.ray()
60
+ ray.add_sunlight([-1,-1,-1])
61
+
59
62
  #仅显示
60
63
  camera.bgcolor('#000000')
64
+ camera.show_bar = True
61
65
  for i in range(1200):
62
66
  camera.clear()
63
67
  camera.camera_position = [150*math.cos(math.radians(i)),150*math.sin(math.radians(i)),150]
64
68
  camera.to_target([0,0,50])
65
- camera.draw_from_scene(scene.sort_all_avg(camera.camera_position))
69
+ camera.draw_from_scene(scene.sort_all_avg(camera.camera_position),ray)
66
70
  camera.update()
67
71
  print(i)
68
72
 
@@ -73,7 +77,7 @@ for i in range(1200):
73
77
  camera.create_image('#000000') #创建绘制区空白图像,暂存在camera.image属性中
74
78
  camera.camera_position = [150*math.cos(math.radians(i)),150*math.sin(math.radians(i)),150]
75
79
  camera.to_target([0,0,50])
76
- camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position)) #使用cv2绘制,不显示,暂存在camera.image
80
+ camera.draw_from_scene_cv2(scene.sort_all_avg(camera.camera_position),ray) #使用cv2绘制,不显示,暂存在camera.image
77
81
  camera.capture('tree',i) #输出暂存图像到.\tree\{i:08d}.png
78
82
  #camera.imshow() 显示cv2绘制的图像
79
83
  #camera.imwrite('tree.png') #输出暂存图像到.\tree.png
@@ -185,7 +185,7 @@ class RacingGame:
185
185
  self.cam.setposition([self.player_x, 5, self.player_z - 8])
186
186
  self.cam.setdirection([0, -0.5, 1])
187
187
 
188
- self.cam.draw_from_scene(scene)
188
+ self.cam.draw_from_scene(scene,None)
189
189
 
190
190
  self.cam.write([self.player_x - 15, 8, self.player_z], f'分数: {self.score}', font=("Arial", 16, "bold"))
191
191
  self.cam.write([self.player_x - 15, 6, self.player_z], f'速度: {int(self.speed*50)}', font=("Arial", 16, "bold"))