You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/wiki/introduction/storage.md
+67-16
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ categories = ["introduction"]
7
7
[extra]
8
8
chapters = true
9
9
chapter_prev = {text = "Choosing A Language", link = "/wiki/introduction/language"}
10
-
chapter_next = false
10
+
chapter_next = {text = "World Generation", link = "/wiki/introduction/generation"}
11
11
+++
12
12
13
13
As noted in the [theory section](./#what-is-a-voxel-in-theory), a voxel can be ***anything***; the only limit is your imagination... and the amount of memory and disk-space you have! Speaking of which, how *are* voxels represented in practice?
@@ -21,13 +21,13 @@ Also, for the purpose of clarity, we will *not* be using pseudocode.
21
21
22
22
{% warn_notice() %} **The following sections are a work-in-progress.** {% end %}
23
23
24
-
### Basic Storage
24
+
### Storing Voxels
25
25
26
-
For a start, let's assume that our voxels store... nothing.
26
+
For a start, let's assume that our voxels store a single byte each...
27
27
28
28
```rust
29
29
/// This represents a single voxel sample/instance.
30
-
typeVoxel=(); //using the empty 'unit type' for now.
30
+
typeVoxel=u8; //A single byte.
31
31
```
32
32
33
33
Since a voxel *outside* a grid is, by [definition](./#what-is-a-voxel-in-theory), *not* a voxel, we will have to put it into a grid of voxels...
The line marked with `SCHEME` declares a *spatialindexing scheme* for us, which defines the *order* and *importance* of the `x,y,z` axes, but also how to turn coordinates into a usable index. Neat!
142
+
The line marked with `SCHEME` declares a (spatial) **indexing scheme** for us, which defines the *order* and *importance* of the `x,y,z` axes, but also how to turn coordinates into a usable index. Neat!
142
143
{% end %}
143
144
144
145
And so our example becomes this:
145
146
146
147
```rust
147
-
// Create the volume... somehow.
148
-
letmutvolume=VoxelGrid { /* ??? */ };
148
+
// Create the volume...
149
+
letmutvolume=VoxelGrid {
150
+
values: [ 0 ; GRID_SIZE*GRID_SIZE*GRID_SIZE]
151
+
};
149
152
150
153
// Have some coordinates...
151
154
let (x,y,z) = (/**/, /**/, /**/);
@@ -162,11 +165,59 @@ if the coordinates are ever out of bounds, crash our program; but at least you'l
162
165
163
166
But how to we fill it? And just what type should `Voxel` be?!
164
167
168
+
We will answer the second question first... after fixing a glaring issue.
169
+
170
+
### The Heap
171
+
172
+
If you tried increasing the `GRID_SIZE` a little too much,
173
+
you *might* run into a problem: a stack overflow!
174
+
175
+
Right now our `VoxelGrid` is defined like this:
176
+
177
+
```rust
178
+
pubstructVoxelGrid {
179
+
values: [Voxel; GRID_SIZE*GRID_SIZE*GRID_SIZE];
180
+
}
181
+
```
182
+
183
+
...and 'created' like this...
184
+
185
+
```rust
186
+
// Create the volume...
187
+
letmutvolume=VoxelGrid {
188
+
values: [ 0 ; GRID_SIZE*GRID_SIZE*GRID_SIZE]
189
+
};
190
+
```
191
+
192
+
That line right there? It allocates our `VoxelGrid` on the stack! Which is bad,
193
+
as stack memory is quite limited; putting huge things on it can (obviously)
194
+
cause a stack-overflow, but will also obliterate our CPU's cache... :(
195
+
196
+
Thankfully avoiding this is an easy fix: Allocate it on the heap!
197
+
198
+
First turn the array into a vector (which is a thin abstraction over `malloc`)...
199
+
200
+
```rust
201
+
pubstructVoxelGrid {
202
+
values:Vec<Voxel>;
203
+
}
204
+
```
205
+
206
+
...create it right on the heap, like this...
207
+
208
+
```rust
209
+
// Create the volume...
210
+
letmutvolume=VoxelGrid {
211
+
values:vec![ 0 ; GRID_SIZE*GRID_SIZE*GRID_SIZE]
212
+
};
213
+
```
214
+
215
+
...and we're done! Now on to defining our voxel type(s)...
216
+
165
217
### Types of Voxel
166
218
167
219
{% todo_notice() %} Types of voxels. {% end %}
168
220
169
-
### Basic Generation
170
-
171
-
{% todo_notice() %} Filling a volume via [Procedural Generation](/wiki/procgen). {% end %}
0 commit comments