Location: PHPKode > projects > PHP Cad and Drawing Program > phpcad/povray/grass.html
<!doctype HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<title>POV-Ray Cyclopedia: grasspatch.inc Read Me</title>


<body TEXT="#000000" LINK="#0000FF" VLINK="#FF0000" ALINK="#00BB00" BGCOLOR="#FFFFFF">
<img SRC="images/cyclopedia_header.gif" ALT="The POV-Ray Cyclopedia" WIDTH="590" HEIGHT="63" BORDER="0" HSPACE="0" VSPACE="0">
<p>When I was working on an entry for the IRTC topic Ruins, I decided I needed some grass. So I spent a couple of days playing with my bezier.inc file and came up with this.</p>
<p>Note 3-13-03: Updated Grasspatch to work with POV-Ray 3.5.</p>
<p><div align=center>
Download <A HREF="include/grasspatch.inc">grasspatch.inc</A> here<br>
Download <A HREF="include/bezier.inc">bezier.inc</A> here<br>
Download the Grasspatch Template file for <A HREF="include/macgrasstemplate.text">Mac</A><br>
<small><A HREF="mac.html">Click here for instructions</A></small><br>
Download the Grasspatch Template file for <A HREF="include/grasspatch.zip">Windows</A><br>

<table width=640>
<th valign=top BGCOLOR=silver COLSPAN=2>Part Zero<br>Setting up the basics</th></tr>
<tr><td valign=top colspan=2><p>You will need bezier.inc to use grasspatch.inc., just a warning</p>
<p>You can include grasspatch.inc at the beginning of a file, and if you have already included bezier.inc, all the better. If you haven't it will include bezier.inc for you. A standard I use is to declare HelloWhatever at the beginning of an include file. When you include grasspatch, it checks if HelloBezier is defined, and if it is, it move on, otherwise it will include the file. Maybe this is paranoia on my part, but including some files multiple times can ask for trouble as far as I know, or at least make you use more memory than you should during parsing of a scene.</p>

<p>Now there are three basic Patch types available:
    <LI>0: Circle 
    <LI>1: Square
    <LI>2: Polygon 
    <LI>3: The Curving Path Patch
<p>Lets play with the settings for the blades of grass.</p>
<font COLOR="#0000FF"><code><pre>
// Blade_Heights are in standard POV units
#declare Blade_Height_Minimum = 1.75;
#declare Blade_Height_Maximum = 3;

// Some random number generators, you may change these if you wish
#declare someseed = seed(2345);
#declare xseed = seed(42);
#declare zseed = seed(369);

#declare Patch_Tranlation = <0,0,0>;
// Sometimes it's handy just to see where the blades will be
// so we set PlotPoints to true, by default it is
#declare PlotPoints = false;

// per unit length. Square this to know how many will be in each
// unit squared of landscape
#declare Blade_Density = 2; 

// And of course the texture. This is the default:
#declare Blade_Tex = 
  texture { pigment { rgb <0,0.3,0> } 
            finish { phong 0.25 ambient 0.2 } }
<p>Some of these should be self_explanatory, the hieght of each blade will range from Blade_Height_Minimum to Blade_Height_Maximum, and if you want to change the random number seeds, feel free to. These are the three I use here.</p>
<p>Patch_Translation is a vector that will move every blade in a uniform manner. Plot Points replaces blades of grass with small spheres, just so you can get a feel of where the patch is on the ground. By default this is off. Blade_Density control how many blades appear in one POV unit along each axis, so if your Blade_Density is two, you will have four blades on each unit squared (this is the average amount, of course). And the texture is a simple dark green with some ambient applied to keep the shadows from overwhelming the grass. This is the only way POV-Ray can counteract a shadow, but it would mess up radiosity quite a bit, I'm sure</p>

<th valign=top BGCOLOR=silver COLSPAN=2>Part One<br>Detailing circular patches</th></tr>
<tr><td valign=top>
<p>To create a circular clump of grass there isn't much that we have to do.</p>
<font color="#0000FF"><code><pre>
#declare Patch_Shape = 0; // Circular

#declare Clump_Radius_Minimum = 0;
#declare Clump_Radius_Maximum = 2;
<p>All that is left is to run the macro, which I have called<br>
<font color="#0000FF"><code><pre>
And this is all it took to make image 1.

<p>You may notice that the blades are concentrated in the center and this can be corrected by adjusting Spread_Correction:
<font color="#0000FF"><code><pre>
#declare Spread_Correction = 0.5;
The closer to zero, the greater the tendency to place the blades close to Clump_Radius_Maximum. The higher it is, the greater the tendency to place the blades cose to Clump_Radius_Minimum. As you can see, setting it to 0.5 gives us better distribution, as in image 2.
<p>And of course to make a ring we simply declare Clump_Radius_Minimum to be something greater than zero:

<font color="#0000FF"><code><pre>
#declare Clump_Radius_Minimum = 1;
Which gave us image 3.</p>
<td valign=top align=left BGCOLOR=lightsteelblue>
<IMG SRC="images/grass001.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<IMG SRC="images/grass002.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<IMG SRC="images/grass003.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<th valign=top BGCOLOR=silver COLSPAN=2>Part Two<br>The Square Patch</th></tr>
<tr><td valign=top>
<p>The square field has a few different properties. It will lie on a grid on the x-z plane.
<font color="#0000FF"><code><pre>
#declare Patch_Shape = 1; // Square
#declare Patch_X_Minimum = -2;
#declare Patch_X_Maximum = 2;

#declare Patch_Z_Minimum = -2;
#declare Patch_Z_Maximum = 2;

And we have image 4. This distribution method should prevent large areas from being empty and it covers the ground quite nicely.</p>

<td valign=top BGCOLOR=lightsteelblue>
<IMG SRC="images/grass004.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<th valign=top BGCOLOR=silver COLSPAN=2>Part Three<br>The Polygon Patch</th></tr>
<tr><td valign=top>
The Polygon patch is loads of fun, but version 1.0 of grasspatch.inc doesn't do the greatest job of making it look like a rigid polygon. In fact, the shapes aren't rigid polygons, but because ofthat we can make slightly more complex shapes:

<font color="#0000FF"><code><pre>
#declare Patch_Shape = 2; // Polygon
#declare Clump_Radius_Minimum = 0;
#declare Clump_Radius_Maximum = 2;
#declare Patch_Sides = 3;
#declare Poly_Radius_Range = 0.333;

This is the code for image 5. Poly_Radius_Range works by reducing the radius of the patch by a certain amount, dependant on the angle. If it is set to 1 the sides will be sucked into the center of the polygon. If it is set to 0 you will end up with a circular patch. Setting it  to a negative number will create cloverleaf type shapes as well. This is adjusted by Clump_Radius_Maximum.<p>

<p>Some recomendations for "straight" edges:
<UL><LI>triangle : 0.333
    <LI>square   : 0.25
    <LI>pentagon : 0.15
    <LI>hexagon  : 0.1
    <LI>heptagon : 0.08
    <LI>octagon  : 0.075
    <LI>enneagon : 0.065

Of course, with semi-regular polygons you will want to, on occaison, to rotate the patch, so declare
<font color="#0000FF"><code><pre>
#declare RadialOffset = 0;
The default is zero and this is strict rotation around the y axis.</p>

<p>Just for fun, lets play with some settings:

<font color="#0000FF"><code><pre>
#declare someseed = seed(1653);
#declare Patch_Sides = 5;
#declare Blade_Density = 3;
#declare Spread_Correction = 0.25;
#declare Poly_Radius_Range = 0.75;
And we have image 6.
<td valign=top BGCOLOR=lightsteelblue>
<IMG SRC="images/grass005.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<IMG SRC="images/grass006.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<tr><th valign=top BGCOLOR=silver COLSPAN=2>Part Four<br>The Curving Path Patch</th></tr>
<tr><td valign=top>
<p>The Curving Path Patch relies on declaring a spline with bezier.inc. It ignores the y component of the path, and deals only with the x and y components. For the image I used a smaller Clump_Radius_Maximum to focus the blades closer to the spline.

<font color="#0000FF"><code><pre>
#declare Patch_Shape = 3;

#declare Patch_Path = array[4] { <-3,0,0>, <-1,0,5>, <1,0,-5> <3,0,0> }
#declare Path_Density = 6;
#declare Path_Points = 20;

#declare Clump_Radius_Maximum = 0.75;

And we have image 7. The id of the spline has to 'Patch_Path.' You can preview the spline with :
<font color="#0000FF"><code><pre>
Path_Density works differently than Blade_Density. Path_Density tells grasspatch.inc to find that many points at each point along the path. The number of points is determined by Path_Points.</p>
<td valign=top BGCOLOR=lightsteelblue>
<IMG SRC="images/grass007.jpg" ALT="Circular clump" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>

<tr><th valign=top BGCOLOR=silver COLSPAN=2>Part Five<br>Blade Details</th></tr>
<tr><td valign=top>
<p>The blades of grass can now be customized to some extent. The curve has been made more natural and the width is now customizable. There file will also allow you to reduce the detail of the blades as they get further away from the camera.</p>

<font color="#0000FF"><code><pre>
#declare Use_Blade_Distance = true;
#declare Blade_Detail = 10;
#declare Max_Blade_Angle = 45;
#declare Min_Blade_Angle = 10;

#declare Blade_Scale = 1;
#declare Blade_Width = 0.075;

#declare Use_Blade_Distance = true;
#declare Camera_Position = &lt;0,0,-4&gt;;
#declare Max_Blade_Detail = 20;
#declare Min_Blade_Detail = 5;
#declare Max_Detail_Distance = 1;
#declare Min_Detail_Distance = 10;
<p>Use_Blade_Distance toggles whether or not you use detail loss at a greater distance or not. If Use_Blade_Distance is true, then Max_Blade_Angle and Min_Blade_Angle are used. If Use_Blade_Distance is false, then Blade_Detail is used uniformly on all the blades. The blades are contrained in a cone_shape, the angle from the central height to the side are dictated by Max_Blade_Angle and Min_Blade_Angle. Since this is hard to describe, image 8 hopefully tells the story well.</p>
<p>Blade_Scale allows you to alter the entire scale of the blade. It's a global change since grasspatch.inc assumes 1 unit = 1 inch. You'll want to change that to 1/12 if 1 unit = 1 foot in your scene. Blade_Width allows you to alter the width of the blade to some extent. The default is 0.075 which can make a thinner blade. Image 9 shows two blades, one with width 0.075 and one with width 0.25.</p>
<p>The next few declarations involve a potentially time saving device. Blades of grass that have a lot of detail to the curve are useful when they are close to the camera, but when they are far away they don't need to be as detailed, so if we calculate fewer points, we get fewer triangles, less parse time, and a faster render. You don't have to use this feature, so you can turn it off by declaring Use_Blade_Distance = false. You will need to declare Camera_Position to be the camera location point. (It doesn't really have to be, but the effect is more practical when they are the same.) and Max_Blade_Detail and Min_Blade_Detail, as well as Max_Detail_Distance and Min_Detail_Distance. At Max_Detail_Distance the blades will have Max_Detail_Distance. The detail will decrease until the distance between the blade and the camera position is equal to Min_Detail_Distance. At this point and whenever the distance is greater than that, the blade will have Min_Blade_Detail.</p>

<td valign=top BGCOLOR=lightsteelblue>
<IMG SRC="images/grass008.jpg" ALT="blade angles illustrated" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<DIV BGCOLOR="#CCCCCC" ALIGN=CENTER>8. Blade Angles Illustrated</SMALL></DIV>
<IMG SRC="images/grass009.jpg" ALT="thin blade fat blade" WIDTH="240" HEIGHT="180" HSPACE="0" VSPACE="0"><br>
<hr width=640>
Send <a HREF="mailto:hide@address.com">feedback</a>, or head back to the <a HREF="index.html">Cyclopedia</a>.<p>

Thanks for watching.<p>
document.write("<small>This page was last updated on " + document.lastModified + "</small>")
<!------- Webstat counter code ------------->
   <script language="JavaScript"> var code = " ";var page = "Grasspatch Read Me";</script>
   <script src="http://java.webstat.net/cgi-bin/hitc?name=pov-ray"></script>
   <script language="JavaScript">document.write(code);</script>
   <noscript><a href="http://www.webstat.net" target="_top">
   <img src="http://webstat.net/cgi-bin/statc?name=pov-ray&j=n" 
    alt="WebStat Counter" width=88 height=31 border=0></a></noscript>
   <!--------- End webstat code --------------->

Return current item: PHP Cad and Drawing Program