/// <summary> /// Sets the currently selected Texture from the list of textures used by the TerrainPatch. /// </summary> /// <param name="tex">The texture to store in the TerrainPatch.</param> public void SetTexture(DataCore.Texture tex) { if (tex != null) { Textures[_selectedTexture] = tex; } }
/// <summary> /// Applies the snowfall generator to the TerrainPage. /// </summary> /// <param name="level">The snow level (elevation).</param> /// <param name="blend">The snow blending distance.</param> private void CreateSnow(float level, float blend) { DataCore.Texture tex = _page.TerrainPatch.GetTexture(); DataCore.Texture newTex = new Voyage.Terraingine.DataCore.Texture(); Bitmap image = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format32bppArgb); float xScale = _page.TerrainPatch.Width / image.Width; float yScale = _page.TerrainPatch.Height / image.Height; string filename; Vector2 origin; Vector3 point; int v1, v2, v3; float alpha; int fileCount = -1; for (int j = 0; j < image.Height; j++) { for (int i = 0; i < image.Width; i++) { origin = new Vector2((float)i * xScale, (float)j * yScale); _page.GetPlane(origin, out v1, out v2, out v3, out point); if (point.Y >= level) { if (point.Y >= level + blend) { image.SetPixel(i, j, Color.FromArgb(0, Color.White)); } else { alpha = 255f - (point.Y - level) / blend * 255f; image.SetPixel(i, j, Color.FromArgb((int)alpha, Color.White)); } } else { image.SetPixel(i, j, Color.FromArgb(255, Color.White)); } } } do { fileCount++; filename = Path.GetDirectoryName(tex.FileName) + "\\" + Path.GetFileNameWithoutExtension(tex.FileName) + "_snowfall" + fileCount + ".bmp"; } while (File.Exists(filename)); image.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp); newTex.FileName = filename; newTex.Name = "Snowfall"; newTex.Operation = TextureOperation.BlendTextureAlpha; newTex.OperationText = "Use Texture Alpha"; _page.TerrainPatch.AddTexture(newTex); _textures = _page.TerrainPatch.Textures; }
/// <summary> /// Returns the specified Texture from the list of textures used by the TerrainPatch. /// </summary> /// <param name="index">The index of the Texture to return.</param> /// <returns>The indicated texture returned (null if an invalid index is specified).</returns> public DataCore.Texture GetTexture(int index) { DataCore.Texture texture = null; if (index > -1 && index < _textures.Count) { texture = (DataCore.Texture)Textures[index]; } return(texture); }
/// <summary> /// Stores data about a texture into the XML document. /// </summary> /// <param name="xmlParent">The XML node to insert data at.</param> /// <param name="tex">The DataCore.Texture to get data from.</param> private void WriteTexture(XmlElement xmlParent, DataCore.Texture tex) { XmlElement xmlTPPar; string filepath, filename = null; // Create the texture element xmlTPPar = _xmlDoc.CreateElement("", "Texture", ""); // Store the texture name xmlTPPar.AppendChild(CreateTextNode("Name", Path.GetFileName(tex.Name))); // Store the texture filename if (_fileExists) { filepath = Path.GetDirectoryName(_dlgSave.FileName); filename = filepath + "\\Textures\\"; filename += Path.GetFileNameWithoutExtension(tex.Name) + Path.GetExtension(tex.FileName); } else { filepath = Path.GetDirectoryName(_dlgSave.FileName); filepath += "\\" + Path.GetFileNameWithoutExtension(_dlgSave.FileName); filename = filepath + "\\Textures\\"; filename += Path.GetFileNameWithoutExtension(tex.Name) + Path.GetExtension(tex.FileName); } xmlTPPar.AppendChild(CreateTextNode("FileName", filename)); // Store the texture texture operation xmlTPPar.AppendChild(CreateTextNode("TextureOperation", tex.OperationText)); // Store if the texture is a mask xmlTPPar.AppendChild(CreateTextNode("IsMask", tex.Mask.ToString())); // Store the texture scale values xmlTPPar.AppendChild(CreateVector2Node("Scale", tex.Scale)); // Store the texture shift values xmlTPPar.AppendChild(CreateVector2Node("Shift", tex.Shift)); // Store the texture element xmlParent.AppendChild(xmlTPPar); }
/// <summary> /// Sets the texture drawing operation for the specified texture state in the DirectX device. /// </summary> /// <param name="texture">Texture state to update.</param> private void SetTextureOperation(int texture) { DataCore.Texture tex = (DataCore.Texture)_page.TerrainPatch.Textures[texture]; D3D.TextureOperation operation = tex.Operation; switch (operation) { case D3D.TextureOperation.Add: case D3D.TextureOperation.AddSigned: case D3D.TextureOperation.AddSmooth: case D3D.TextureOperation.BlendTextureAlpha: case D3D.TextureOperation.DotProduct3: case D3D.TextureOperation.Modulate: case D3D.TextureOperation.Modulate2X: case D3D.TextureOperation.Subtract: _viewport.Device.TextureState[texture].ColorArgument1 = D3D.TextureArgument.Current; _viewport.Device.TextureState[texture].ColorArgument2 = D3D.TextureArgument.TextureColor; _viewport.Device.TextureState[texture].ColorOperation = operation; break; case D3D.TextureOperation.BlendFactorAlpha: _viewport.Device.RenderState.BlendOperation = D3D.BlendOperation.Subtract; _viewport.Device.RenderState.SourceBlend = D3D.Blend.SourceAlpha; _viewport.Device.RenderState.DestinationBlend = D3D.Blend.DestinationAlpha; _viewport.Device.RenderState.AlphaBlendEnable = true; _viewport.Device.RenderState.BlendFactor = Color.FromArgb(50, Color.Black); _viewport.Device.TextureState[texture].ColorArgument1 = D3D.TextureArgument.Current; _viewport.Device.TextureState[texture].ColorArgument2 = D3D.TextureArgument.TextureColor; _viewport.Device.TextureState[texture].ColorOperation = operation; break; case D3D.TextureOperation.SelectArg1: default: _viewport.Device.TextureState[texture].ColorArgument1 = D3D.TextureArgument.TextureColor; _viewport.Device.TextureState[texture].ColorOperation = D3D.TextureOperation.SelectArg1; break; } }
/// <summary> /// Adds a texture to the list of textures used by the TerrainPatch. /// </summary> /// <param name="tex">The Texture to add to the TerrainPatch.</param> public void AddTexture(DataCore.Texture tex) { _textures.Add(tex); AddTextureCoordinates(); }
/// <summary> /// Applies the snowmelt generator to the TerrainPage. /// </summary> private void MeltSnow() { DataCore.Texture oldTex = _page.TerrainPatch.GetTexture(); DataCore.Texture tex = new Voyage.Terraingine.DataCore.Texture(); Bitmap oldImage = oldTex.GetImage(); Bitmap image = new Bitmap(oldImage.Width, oldImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); float xScale = _page.TerrainPatch.Width / image.Width; float yScale = _page.TerrainPatch.Height / image.Height; string filename; Vector2 origin; Vector3 point, normal; Vector3 n1, n2, n3; int v1, v2, v3; Vector3 light = (Vector3)_receivedData[0]; float alpha, dot; Color color; int fileCount = -1; float dist1, dist2, dist3; for (int j = 0; j < image.Height; j++) { for (int i = 0; i < image.Width; i++) { // Get original pixel color color = oldImage.GetPixel(i, j); // Find point on terrain origin = new Vector2((float)i * xScale, (float)j * yScale); _page.GetPlane(origin, out v1, out v2, out v3, out point); // Determine weighted distance from each vertex dist1 = ((Vector3)_page.TerrainPatch.Vertices[v1].Position - point).Length(); dist2 = ((Vector3)_page.TerrainPatch.Vertices[v2].Position - point).Length(); dist3 = ((Vector3)_page.TerrainPatch.Vertices[v3].Position - point).Length(); // Determine weighted normal n1 = (dist1 / (dist1 + dist2 + dist3)) * _page.TerrainPatch.Vertices[v1].Normal; n2 = (dist2 / (dist1 + dist2 + dist3)) * _page.TerrainPatch.Vertices[v2].Normal; n3 = (dist3 / (dist1 + dist2 + dist3)) * _page.TerrainPatch.Vertices[v3].Normal; normal = n1 + n2 + n3; normal.Normalize(); // Determine angle of light against pixel position dot = Vector3.Dot(normal, -light); // Determine alpha of new pixel if (dot >= 0f) { alpha = oldImage.GetPixel(i, j).A - 255f * dot; } else { alpha = oldImage.GetPixel(i, j).A; } if (alpha < 0f) { alpha = 0f; } else if (alpha > 255f) { alpha = 255f; } color = Color.FromArgb(color.ToArgb()); image.SetPixel(i, j, color); } } do { fileCount++; filename = Path.GetDirectoryName(oldTex.FileName) + "\\" + Path.GetFileNameWithoutExtension(oldTex.FileName) + "_snowmelt" + fileCount + ".bmp"; } while (File.Exists(filename)); image.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp); tex.FileName = filename; tex.Name = "Snowmelt"; tex.Operation = TextureOperation.BlendTextureAlpha; tex.OperationText = "Use Texture Alpha"; _page.TerrainPatch.AddTexture(tex); _textures = _page.TerrainPatch.Textures; }