otmvoxel released 11-08-94
voxel landscape explanation/demo
by Voltaire/OTM
all source Copyright (C) 1994 Zach Mortensen
email -
mortens1@nersc.gov
see OTMVOXEL.NFO and OTM-94.NFO for more information
OPENING WORDS
I make the assumption that you have at least some experience in
writing 3d code. You should not attempt to understand voxels if you
cannot understand the basics of 3d. If you are interested in the 3d
engine used to make this demo, it is availible via ftp at
hornet.eng.ufl.edu
the archive is
/demos/code/graph/library/V3DT090.ZIP
I highly reccomend picking it up (unbiased opinion of the author ;))
WHAT ARE VOXELS?
A voxel is an approximation of volume, much in the same way a pixel
is an approximation for area. Imagine a voxel as a cube in space,
it has length, width and depth. Just pixels have a fixed length and
width, voxels have a fixed length, width, and depth.
---------
|\ \
| \ \
| \ \
| --------- ---------
| | | | |
| | | | |
| | voxel | | pixel |
\ | | | |
\ | | | |
\|_______| ---------
gotta love my ascii art...heheh
Now that you know what voxels are...
HOW ARE VOXELS USED?
Because they can approximate volume, voxels can be used to create
very complex formations in three dimensions. Commanche made
voxels famous for creating somewhat realistic landscapes in
realtime. Many recent demos have also made use of voxels in
landscapes, ocean waves, etc.
But how do they do it so quickly? It would seem that in order
to draw a voxel, one must keep track of its verticies, rotate
and translate them, and draw a cube in every frame. This is
obviously too slow to do in realtime, because landscapes usually
contain upwards of 1000 voxels.
Here's the quick and dirty shortcut: If we limit our perspective
so that we only view voxels from the front, each cube looks like
this:
---------
| |
| |
| voxel |
| |
| |
---------
Wow...that looks an awful lot like a square, and we can draw squares
MUCH faster than we can draw polygons with arbitrary angles that
are required to draw a cube in 3d. More importantly, we only need
to keep track of one point.
P1
\
x--------
| |
| |
| voxel |
| |
| |
---------
SO WHAT DO WE DO NOW?
Well, now that we know voxels can be approximated by a point and
a square, we need to define a pattern for our voxels that looks
somewhat like a landscape. It's best to start out simple, by
defining a parametric curve in 3d such as
x = 10t
y = 20(sin x + sin z) + 25
z = 10t
make sure you convert x and z to radians before you take their sine
when you are calculating the y value. These equations form something
that vaguely resembles a hill. Make a 10x10 array of points,
assign a voxel to each point, and you can rotate your hill, translate
it, etc. Several problems soon become evident, though. First, our
hill seems to have some unsightly holes in it. This is due to the
fact that we have attempted to project a voxel without changing its
size. When a voxel is closer to the viewer, it must be larger. Of
course, it must also appear to be smaller when it is far away.
Remember to scale your voxels to a size determined by their distance
from the viewer. This is a simple 3d projection, the same way 2d
screen coordinates are determined from 3d world coordinates.
newSize = SIZE * DIST / z3d
where SIZE is the set size for a voxel and DIST is the distance
between the viewer and the screen. I like to use SIZE = 16 and DIST =
1024. Of course, your landscape will look better if you make your
voxels smaller and closer together.
Another obvious problem is that our hill appears as a blob of one
color. In order to get around this, we need to shade our voxels
in some inventive way.
MADE IN THE SHADE
this is perhaps the easiest way to shade a voxel:
P1
\
x--------
|5555555|
|4444444|
|3333333|
|2222222|
|1111111|
---------
If we assign the the y value of each voxel's point to the starting
color value (represented above by 5), we get a hill that is smoothly
shaded from top to bottom. Of course, this requires a carefully
chosen palette, but I'll leave that up to you. Some popular ideas
include shading the palette from blue (low colors = water) to green
(middle colors = land) to white (upper colors = snowcapped mountains).
I use a grossly simple green to white gradient.
Other methods of shading (I haven't tried these, but they seem like
they would work):
---------
|1234567|
|1234567|
|1234567| (light appears to be coming from one side)
|1234567|
|1234567|
---------
---------
|1234567| (If I'm not mistaken, this is the type of shading
|2345678| that was used in Mars)
|3456789|
|456789A|
|56789AB|
---------
give these a try and see what happens...
LIMITATIONS OF SHADING
Although shading makes our hill look much better, it does limit the
way in which we can use our landscape. If we use this easy method of
shading, we cannot rotate our landscape around any axis but the y.
Rotation around the y axis does not change the y coordinate of a
point, which is the basis for this shading technique. The shading
routine we use simply assumes that color will change in the vertical
direction. By rotating the vertical direction, (rotating around the
x or z axis), we make the shading appear incorrect.
ONWARD AND UPWARD
Now that we have a disgustingly simple landscape working properly,
we are ready to move on to bigger and better things. First, we
must find an equation that will create a more exciting landscape.
I have found that plasmas and certain fractals are best suited for
this purpose. Extruding a plasma or fractal into a landscape
is an easy process:
for every point (x,y,color) in the plasma or fractal image, create a
3d point (x,color,y) in the landscape.
This produces some truly incredible results. Plasmas are spectacular
with their hills and valleys, and some complex canyon landscapes can
be obtained by using a section of the Mandelbrot set. Feel
free to experiment with any other of your favorite fractals as well.
One thing that is an absolute necessity is that you depth sort your
points from back to front. If you do not, your landscape will look
VERY bad.
Now that we have a potentially HUGE landscape, we must also decide
where to stop displaying voxels. This is extremely simple, just weed
out points that are too far away while you are sorting. It's up to
you to decide how far you want your landscape to extend.
FINAL WORDS
I honestly hope this explanation along with the example code helps to
clarify the uses of voxels. If for some reason my explanation seems
vague in any way, feel free to email me with any questions. I do not
claim to be the world's most talented coder, and I realize that I make
my share of mistakes. Just make sure you point them out to me ;)
Volt