/// <summary> /// Verifies that the tree at the given index is sufficiently spaced /// from the other trees. If trees are placed too closely, one tree /// can quickly pop in front of the other as the camera angle /// changes.</summary> /// <param name="treeIndex">index of tree we are testing</param> /// <returns>true if valid, false otherwise</returns> bool IsTreePositionValid(Vector3Fixed pos) { if (trees.Count < 1) { return(true); } float x = (float)pos.X; float z = (float)pos.Z; for (int i = 0; i < trees.Count; i++) { double fDeltaX = Math.Abs(x - (float)((Tree)trees[i]).Position.X); double fDeltaZ = Math.Abs(z - (float)((Tree)trees[i]).Position.Z); if (3.0 > Math.Pow(fDeltaX, 2) + Math.Pow(fDeltaZ, 2)) { return(false); } } return(true); }
/// <summary> /// Render the trees in the sample /// </summary> protected void DrawTrees() { Vector3Fixed eyePartFixed; eyePartFixed = new Vector3Fixed(); eyePartFixed.X = (FixedPoint)eyePart.X; eyePartFixed.Y = (FixedPoint)eyePart.Y; eyePartFixed.Z = (FixedPoint)eyePart.Z; // Set diffuse blending for alpha set in vertices. renderState.AlphaBlendEnable = true; renderState.SourceBlend = Blend.SourceAlpha; renderState.DestinationBlend = Blend.InvSourceAlpha; // Enable alpha testing (skips pixels with less than a // certain alpha.) if (device.DeviceCaps.AlphaCompareCaps.SupportsGreaterEqual) { device.RenderState.AlphaTestEnable = true; device.RenderState.ReferenceAlpha = 0x08; device.RenderState.AlphaFunction = Compare.GreaterEqual; } // Loop through and render all trees device.SetStreamSource(0, treeVertexBuffer, 0); foreach (Tree t in trees) { // Quick culling for trees behind the camera // This calculates the tree position relative to the // camera, and projects that vector against the camera's // direction vector. A negative dot product indicates a // non-visible tree. if (0 > (t.Position.X - eyePartFixed.X) * GlobalDirection.X + (t.Position.Z - eyePartFixed.Z) * GlobalDirection.Z) { break; } // Set the tree texture device.SetTexture(0, treeTextures[t.TreeTextureIndex]); // Translate the billboard into place billboardMatrix.M41 = t.Position.X; billboardMatrix.M42 = t.Position.Y; billboardMatrix.M43 = t.Position.Z; device.Transform.WorldFixed = billboardMatrix; // Render the billboard device.DrawPrimitives(PrimitiveType.TriangleStrip, t.OffsetIndex, 2); } // Restore state device.Transform.World = Matrix.Identity; device.RenderState.AlphaTestEnable = false; device.RenderState.AlphaBlendEnable = false; }