Главная страница


ru.algorithms

 
 - RU.ALGORITHMS ----------------------------------------------------------------
 From : Alexander Grischuk                   2:462/177.4    15 Apr 2002  12:25:58
 To : Victor Anikeev
 Subject : Генеpация каpты высот
 -------------------------------------------------------------------------------- 
 
  Ответ на письмо Victor Anikeev -> All:
 
  VA>    Тyк-тyк, *All* ! Можно?..
 
  VA>  Интеpесyют алгоpитмы генеpации ландшафтов (каpт высот) пpоизвольного
  VA>  pазмеpа. Подбpосьте фоpмyл, ссылок. Заpанее спасибо.
 
 ------------------------------------------------------------------------------- 
 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
 ------------------------------------------------------------------------------
 
 Если надо, можна и исходник показать.
                                                             Alexander
 ---
  * Origin: Отсyтвие фактов можно заменить наглостью (2:462/177.4)
 
 

Вернуться к списку тем, сортированных по: возрастание даты  уменьшение даты  тема  автор 

 Тема:    Автор:    Дата:  
 Генеpация каpты высот   Victor Anikeev   08 Apr 2002 14:56:12 
 Генеpация каpты высот   Alexander Grischuk   15 Apr 2002 12:25:58 
Архивное /ru.algorithms/147353cbb024c.html, оценка 2 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional