Back to My Programs Textures of Wood

 

1. Task definition

2. Solving the problem
    2.1 Tree growth description
    2.2 Tree growth simulation

3. Screen capture

4. Program management
    4.1 Saving picture

5. Program details
    5.1 Graphics initialization
    5.2 User-computer communication
    5.3 Setting color palette, screen capture, picture transfer

6. Conclusions

7. References


Task definition

Make program for wood textures generation with valid output picture file format.

Solving the problem

Tree growth description

Desired to make wood texture generator, we must first simulate 3D growth of tree, with correct parameters. I decided to generate pine-tree textures, because of its geometrical simplicity.

Pine-tree has conical shape with height-to-radius ratio of 80:1. Radius of first annual ring or thickness of any annual ring in the future is dependent on tree food supply. Based on observations we can set ring thickness between 1.5 in 3.5 mm. Part of annual ring growing in spring, when tree has enough food, is light-colored. Oppositely, the part growing in the fall, quickly changes to dark colored. Every year new annual ring envelopes te rest of the tree, and generates numerous branch sources, deviating in direction and height. As said, tree trunk grows in ratio 80:1, on the other way branches do in ratio of  approximately 32:1. In addition we must take into consideration the lower fifth of trunk, where there branches with the thickening of the trunk, die away. Tree cones are not perfect. Their radius deviates around their theoretical radius with sine function, amplitude 3 mm, wave length 0.5 m. Because of obstacles that appear in the vicinity of the tree (walls, other trees), thickness of annual ring also changes tangentially. According to this, we can expect maximum of  15% eccentricity of trunk. On the lower part of trunk we also detect tangential deformation of raduis because of  root effect.
 

Tree growth simulation

Program for wood textures generation needs an input datum, tree age, with maximum alowable of 127 years. Program reserves array for 127 annual-ring thicknesses, 127 radii, 127 heights of annual cones, 127 * 7 direction angles and heights of branches. Tree is generated to grow along X coordiante axis, units in meters.

Primarily we generate a random set of annual-ring thicknesses, which influent set of radii and annual-cone heights. On the top of every annual-cone we select a random number of branches that will grow there (0-7). Full circle is then divided into pies depending on branch-number. Every pie (angle) is then altered by up to +/- 20° randomly. Branche origins are also moved along the height of the tree by up to +/- 5 cm.
Finally we declare the trunk eccentricity, up to 15 %. Tree is now defined.
 

Screen capture

Picture of texture is captured on the cutting layer (screen layer). Screen layer is defined with the position of its center [XYZ], and with the normal vector of screen. This one is defined with angles (horisontal angle) [j] (angle between the projection of normal vector to layer XY and X axis) and (elevation angle) [J] (angle between layer XY and normal vector of screen).
Size of screen rectangle is defined with [Zoom] factor.

Screen is captured separately for every pixel of screen. We must scan screen rows from top to bottom, and columns from left to right. Coordiantes of every pixel [ij] are transformed to coordinate system of  tree [xyz]:

I = (i-320) J = (j-240)

x = X + (sin(j)*I + sin(J)*J* cos(j))*Zoom/320

y = Y - (cos(j)*I - sin(J)*J* sin(j))*Zoom/320

z = Z - (cos(J)*J)*Zoom/320
 


Screen Layer presentation


Numbers 320 and 240 are halves of SVGA mode 640x480.

When [xyz] coordinates of pixel are calculated, we must determine the color of this pixel.
Color palette of pine-tree is found to be a line between RGB points [68 3C 00] and [D4 A4 2C] hex. I`ve determined these two border colors by observation (valid only for palette file TreePal0.COL). Considering the most accurate graphical modes work in 8:8:8 direct color modes, the Red, Green and Blue axes can be divided up to maximum of  256 integer values. If we place our line into such a RGB cube, then the line will have it`s diferentials Dr, Dg and Db. Only in lonsam cases, one or more diferentials will stretch up to 256 units. These are mainly palettes of clean colors of red, green, blue, yellow, turquoise, violet and gray. In our case the span Dr Dg Db is [+108 +104 +44] dec. Because the RGB coordinate system is integer-based, our line can provide us with maximum of the longes diferenial plus 1 diferent colors (our case 109 colors). Writing a picture file in direct color mode would be because of the above reasons wasteful in size, speed and compressibility. That`s why we chose 256 fixed colors mode 640x480x256.
 


RGB space presentation


Color of every pixes is defined by its coordinates [xyz]. Coordinates y and z are transformed into radius from X axis, [r 2]= y 2 + z 2, next to it to the angle y = arctg(y/z) on layer YZ. Based on coordinate x and angle [y] we seek for that last cone, whose radius does not exceed the radius of our pixel from X axis. Dependent on the ratio of distance of pixel from the last cone with respect to the thickness of annual-ring here, we get the percentage of pixel position within annual-ring thickness, where from we derive color index (0% = 255, 100% = 1).

Radius of cone depends on coordinate x: R(x) = (R[i] - (x/80))

Then modified with sine wave: R(x) = R(x) * (1 + (sin(4*P*x)*0.033))

and eccentricity: R(x) = R(x) * (1 + sin(y)*Exc)

Considering the effect of roots dependent on height: R(x) = R(x) * (1 + root*0.3*xx55)

xx = (1 - x/H[max]) where H[max] is the height of the outermost cone.

R(x) = R(x) * [mul]     is the last factor to be considered - effect of branch. I`ve simulated branches with local multiplication of all cones around the branch. Multiplication factor depends on modified angle [j] between vector of branch and vector between two points (pixel to branch origin point on X axis). A starting factor of 1 is multiplied with factors of all neighboring branches. These are:
   if angle [j] is smaller than 0.65 rad (1 + 32*e-26*j), else is 1

With all these multiplication factors, we deformed-streched the basic tree-cones to a shape of tree. The last multiplication factor influences the color of pixel. Annual-rings inside branches are much more thinner than in the trunk [0.4 .. 1.2] mm, therefore they are darker. Their color span is no longer full, so we add color multiplier
Cmul = mul16 , which assures darkening of branches.
 

Program management

Program works in DOS and needs two parameters: The color palette is considered to be a 6-bit depth, ordered RGB, program initializes graphics 640x480x256 on standard SVGA 6-bit depth.

Possible problems at run-time are written to a file TREE.ERR. During run-time parameters of intersection layer can be manipulated:

To calculate a 640x480 picture, program running on 80486 33MHz needs cca. 13 mins, on Pentium 300 MHz cca. 1 minute. Because of this, after each change of parameters mentioned above, a preliminary drawing is done at resolution 80x60 pixles smudged over 8x8 pixels (just to see where the knots are).
Therefore this kind of picture is rough and ugly, but the calculation on Pentium is done in a split second. When we feel we found what we were looking for, we smoothe the picture up. When the picture is smoothed, we save it into a TGA 640x480x256 format under specified name following by a picture index. As said, smoothing needs about 1 minute. While not smoothing, processor is idling and checking input keys. We can exit the program then by the regular way. But while smoothing, the only way to exit a program under DOS is

Saving the picture

I`ve decided to export picture in TarGA format, because I knew how to create it. TGA can be compressed or uncompressed. I`ve chosen the uncompressed 256 color one.

TGA format:
 
Size
Number
Descr.
byte
12
head
word
1
width X
word
1
height Y
byte
2
head
3byte RGB
256
palette
byte
X * Y
picture

Program details

Graphics initialization

We initialize graphics through VESA BIOS interrupt. Concerning the advance of computer technology I consider every computer has that ability, therefore I did not perform any VESA capability check.
 

User - computer communication

Communication is done through keyboard. To simplify key detection, I wrote a keyboard interrupt handler. While a key is pressed, its modified scan code will be stored into Command.Keys[0/1]. Two keys can be registered that way. The status of keys Ctrl, Alt and Del is detected separately, and stored into Commands.Release[2]. Bit indexed 0 is set when Ctrl is pressed, bit 1 when Alt and bit 2 when Del is pressed. So, when Commands.Release[2] is equal to 7, we have am Ctrl-Alt-Del event.

Setting up color palette

We set up color palette throug VGA ports 0x3C8 and 0x3C9. To avoid unnecessary VGA bank switching, I decided to store the picture into a virtual screen (VGA.Virtual[y][x]), from where the picture is copied to VGA card after calculation is done.

Conclusion

Program was compiled by Watcom C/C++/Asm compiler. It needs DOS4GW extender, which switches the processor from the real-mode to protected-mode. Effect of this is, that processor starts using 32-bit registers instead of 16-bit ones. To access memory we no longer need a 20-bit address in combination of segment and offset registers, but only a 32-bit offset register, with which we can access up to 4 Gb of physical memory.


References:
 

Miran Klemenc                                                                                                                           Ljubljana, Aug. 21st 1998
  1