1
1
2
+ Creating Geometry
3
+ =================
4
+
5
+ In order to render something to the screen we need geometry as vertex arrays.
6
+
7
+ We have the following options:
8
+
9
+ * Using the :py:mod: `demosys.geometry ` module (or extend the geometry module)
10
+ * Loading scenes/meshes from file using the supported file formats (or create new loaders of other formats)
11
+ * Generating your own geometry programmatically
12
+
2
13
The geometry module
3
- ===================
14
+ -------------------
4
15
5
16
The ``demosys.geometry `` module currently provides some simple
6
17
functions to generate VAOs for simple things.
@@ -12,27 +23,32 @@ Examples:
12
23
from demosys import geometry
13
24
# Create a fullscreen quad for overing the entire screen
14
25
vao = geometry.quad_fs()
26
+
15
27
# Create a 1 x 1 quad on the xy plane
16
28
vao = geometry.quad_2f(with = 1.0 , height = 1.0 )
29
+
17
30
# Create a unit cube
18
31
vao = geometry.cube(1.0 , 1.0 , 1.0 )
32
+
19
33
# Create a bounding box
20
34
vao = geometry.bbox()
35
+
21
36
# Create a sphere
22
37
vao = geometry.sphere(radius = 0.5 , sectors = 32 , rings = 16 )
38
+
23
39
# Random 10.000 random points in 3d
24
40
vao = geometry.points_random_3d(10_000 )
25
41
26
42
.. Note :: Improvements or suggestions can be made by through pull
27
43
requests or issues on github.
28
44
29
- See the `` geometry ` ` reference for more info.
45
+ See the :py:mod: ` demosys. geometry ` reference for more info.
30
46
31
47
Scene/Mesh File Formats
32
48
-----------------------
33
49
34
50
The ``demosys.scene.loaders `` currently support loading
35
- wavefront obj files and gltf.
51
+ wavefront obj files and gltf 2.0 files .
36
52
37
53
You can create your own scene loader by adding the loader
38
54
class to ``SCENE_LOADERS ``.
@@ -49,8 +65,13 @@ Generating Custom Geometry
49
65
50
66
To efficiently generate geometry in Python we must avoid as much memory
51
67
allocation as possible. If performance doesn't matter, then take this
52
- section lightly. Lbraries like ``numpy `` can also be used to generate
53
- geometry.
68
+ section lightly.
69
+
70
+ There are many lbraries out there such as ``numpy `` capable of generating
71
+ geometry fairly efficiently. Here we mainly focus on creating it ourselves
72
+ using pure python code. We're also using the :py:class: `demosys.opengl.vao.VAO `
73
+ for vertex buffer construction. This can easily be translated into using
74
+ ``moderngl.VertexArray `` directly if needed.
54
75
55
76
The naive way of generating geometry would probably look something like this:
56
77
@@ -75,8 +96,7 @@ The naive way of generating geometry would probably look something like this:
75
96
76
97
This works perfectly fine, but we allocate a new list for every iteration
77
98
and pyrr internally creates a numpy array. The ``points `` list will also
78
- have to dynamically expand. This gets exponentially more ugly as the ``count ``
79
- value increases.
99
+ have to dynamically expand. This gets more ugly as the ``count `` value increases.
80
100
81
101
We move on to version 2:
82
102
@@ -94,7 +114,7 @@ We move on to version 2:
94
114
95
115
points_data = numpy.array(points, dtype = numpy.float32)
96
116
97
- This version is orders of magnitude faster because we don't allocate memory
117
+ This version is at least and order of magnitude faster because we don't allocate memory
98
118
in the loop. It has one glaring flaw. It's **not a very pleasant read **
99
119
even for such simple task, and it will not get any better if we add more complexity.
100
120
@@ -114,7 +134,7 @@ Let's move on to version 3:
114
134
115
135
Using generators in Python like this is much a cleaner way. We also take
116
136
advantage of numpy's ``fromiter() `` that basically slurps up all the
117
- numbers we emit with yield into its internal buffers. By also telling
137
+ numbers we yield into its internal buffers. By also telling
118
138
numpy what the final size of the buffer will be using the ``count ``
119
139
parameter, it will pre-allocate this not having to dynamically increase
120
140
its internal buffer.
0 commit comments