public RayCastedTerrainTile(TerrainTile tile) { ShaderProgram sp = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.RayCastedTerrainTile.TerrainVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.RayCastedTerrainTile.TerrainFS.glsl")); _tileResolution = tile.Resolution; _tileMinimumHeight = tile.MinimumHeight; _tileMaximumHeight = tile.MaximumHeight; _tileAABBLowerLeft = Vector3D.Zero; // TEXEL_SPACE_TODO _tileAABBUpperRight = new Vector3D(tile.Resolution.X, tile.Resolution.Y, tile.MaximumHeight - tile.MinimumHeight); _heightExaggeration = (Uniform<float>)sp.Uniforms["u_heightExaggeration"]; _minimumHeight = (Uniform<float>)sp.Uniforms["u_minimumHeight"]; _maximumHeight = (Uniform<float>)sp.Uniforms["u_maximumHeight"]; _aabbLowerLeft = (Uniform<Vector3F>)sp.Uniforms["u_aabbLowerLeft"]; _aabbUpperRight = (Uniform<Vector3F>)sp.Uniforms["u_aabbUpperRight"]; _shadingAlgorithm = (Uniform<int>)sp.Uniforms["u_shadingAlgorithm"]; HeightExaggeration = 1; /////////////////////////////////////////////////////////////////// _drawState = new DrawState(); _drawState.RenderState.FacetCulling.Face = CullFace.Front; _drawState.ShaderProgram = sp; // // Upload height map as a one channel floating point texture // WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeof(float) * tile.Heights.Length); pixelBuffer.CopyFromSystemMemory(tile.Heights); _texture = Device.CreateTexture2DRectangle(new Texture2DDescription( tile.Resolution.X, tile.Resolution.Y, TextureFormat.Red32f)); _texture.CopyFromBuffer(pixelBuffer, ImageFormat.Red, ImageDatatype.Float); ShowTerrain = true; }
public TriangleMeshTerrainTile(Context context, TerrainTile tile) { ShaderProgram silhouetteSP = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteFS.glsl")); Uniform <float> fillDistance = (Uniform <float>)silhouetteSP.Uniforms["u_fillDistance"]; fillDistance.Value = 1.5f; _silhouetteHeightExaggeration = (Uniform <float>)silhouetteSP.Uniforms["u_heightExaggeration"]; ShaderProgram sp = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.TerrainVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.TerrainFS.glsl")); _tileMinimumHeight = tile.MinimumHeight; _tileMaximumHeight = tile.MaximumHeight; _heightExaggeration = (Uniform <float>)sp.Uniforms["u_heightExaggeration"]; _minimumHeight = (Uniform <float>)sp.Uniforms["u_minimumHeight"]; _maximumHeight = (Uniform <float>)sp.Uniforms["u_maximumHeight"]; HeightExaggeration = 1; /////////////////////////////////////////////////////////////////// Mesh mesh = new Mesh(); mesh.PrimitiveType = PrimitiveType.Triangles; mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise; int numberOfPositions = tile.Resolution.X * tile.Resolution.Y; VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfPositions); IList <Vector3D> positions = positionsAttribute.Values; mesh.Attributes.Add(positionsAttribute); int numberOfPartitionsX = tile.Resolution.X - 1; int numberOfPartitionsY = tile.Resolution.Y - 1; int numberOfIndices = (numberOfPartitionsX * numberOfPartitionsY) * 6; IndicesUnsignedInt indices = new IndicesUnsignedInt(numberOfIndices); mesh.Indices = indices; // // Positions // Vector2D lowerLeft = tile.Extent.LowerLeft; Vector2D toUpperRight = tile.Extent.UpperRight - lowerLeft; int heightIndex = 0; for (int y = 0; y <= numberOfPartitionsY; ++y) { double deltaY = y / (double)numberOfPartitionsY; double currentY = lowerLeft.Y + (deltaY * toUpperRight.Y); for (int x = 0; x <= numberOfPartitionsX; ++x) { double deltaX = x / (double)numberOfPartitionsX; double currentX = lowerLeft.X + (deltaX * toUpperRight.X); positions.Add(new Vector3D(currentX, currentY, tile.Heights[heightIndex++])); } } // // Indices // int rowDelta = numberOfPartitionsX + 1; int i = 0; for (int y = 0; y < numberOfPartitionsY; ++y) { for (int x = 0; x < numberOfPartitionsX; ++x) { indices.AddTriangle(new TriangleIndicesUnsignedInt(i, i + 1, rowDelta + (i + 1))); indices.AddTriangle(new TriangleIndicesUnsignedInt(i, rowDelta + (i + 1), rowDelta + i)); i += 1; } i += 1; } _drawState = new DrawState(); _drawState.RenderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; _drawState.ShaderProgram = sp; _drawState.VertexArray = context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw); _silhouetteDrawState = new DrawState(); _silhouetteDrawState.RenderState.FacetCulling.Enabled = false; _silhouetteDrawState.RenderState.DepthMask = false; _silhouetteDrawState.VertexArray = _drawState.VertexArray; _silhouetteDrawState.ShaderProgram = silhouetteSP; _primitiveType = mesh.PrimitiveType; _clearColor = new ClearState(); _clearColor.Buffers = ClearBuffers.ColorBuffer; // // Only depth needs to be cleared but include stencil for speed. // _clearDepthStencil = new ClearState(); _clearDepthStencil.Buffers = ClearBuffers.DepthBuffer | ClearBuffers.StencilBuffer; }
public VertexDisplacementMapTerrainTile(Context context, TerrainTile tile) { // // Upload height map as a one channel floating point texture // WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeof(float) * tile.Heights.Length); pixelBuffer.CopyFromSystemMemory(tile.Heights); _texture = Device.CreateTexture2DRectangle(new Texture2DDescription( tile.Resolution.X, tile.Resolution.Y, TextureFormat.Red32f)); _texture.CopyFromBuffer(pixelBuffer, ImageFormat.Red, ImageDatatype.Float); /////////////////////////////////////////////////////////////////// ShaderProgram spTerrain = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainFS.glsl")); _heightExaggerationUniform = (Uniform<float>)spTerrain.Uniforms["u_heightExaggeration"]; ((Uniform<Vector2F>)spTerrain.Uniforms["u_positionToTextureCoordinate"]).Value = new Vector2F( (float)(1.0 / (double)(tile.Resolution.X)), (float)( 1.0 / (double)(tile.Resolution.Y))); ((Uniform<Vector2F>)spTerrain.Uniforms["u_positionToRepeatTextureCoordinate"]).Value = new Vector2F( (float)(4.0 / (double)tile.Resolution.X), (float)(4.0 / (double)tile.Resolution.Y)); /////////////////////////////////////////////////////////////////// ShaderProgram spNormals = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsFS.glsl")); _heightExaggerationNormals = (Uniform<float>)spNormals.Uniforms["u_heightExaggeration"]; _fillDistanceNormals = (Uniform<float>)spNormals.Uniforms["u_fillDistance"]; ((Uniform<Vector3F>)spNormals.Uniforms["u_color"]).Value = Vector3F.Zero; /////////////////////////////////////////////////////////////////// ShaderProgram spWireframe = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeFS.glsl")); _lineWidthWireframe = (Uniform<float>)spWireframe.Uniforms["u_halfLineWidth"]; _heightExaggerationWireframe = (Uniform<float>)spWireframe.Uniforms["u_heightExaggeration"]; ((Uniform<Vector3F>)spWireframe.Uniforms["u_color"]).Value = Vector3F.Zero; /////////////////////////////////////////////////////////////////// Mesh mesh = RectangleTessellator.Compute(new RectangleD(new Vector2D(0.5, 0.5), new Vector2D((double)tile.Resolution.X - 0.5, (double)tile.Resolution.Y - 0.5)), tile.Resolution.X - 1, tile.Resolution.Y - 1); _va = context.CreateVertexArray(mesh, spWireframe.VertexAttributes, BufferHint.StaticDraw); _primitiveType = mesh.PrimitiveType; /////////////////////////////////////////////////////////////////// RenderState rsTerrain = new RenderState(); rsTerrain.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; RenderState rsWireframe = new RenderState(); rsWireframe.Blending.Enabled = true; rsWireframe.Blending.SourceRGBFactor = SourceBlendingFactor.SourceAlpha; rsWireframe.Blending.SourceAlphaFactor = SourceBlendingFactor.SourceAlpha; rsWireframe.Blending.DestinationRGBFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsWireframe.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsWireframe.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; rsWireframe.DepthTest.Function = DepthTestFunction.LessThanOrEqual; RenderState rsNormals = new RenderState(); rsNormals.FacetCulling.Enabled = false; rsNormals.Blending.Enabled = true; rsNormals.Blending.SourceRGBFactor = SourceBlendingFactor.SourceAlpha; rsNormals.Blending.SourceAlphaFactor = SourceBlendingFactor.SourceAlpha; rsNormals.Blending.DestinationRGBFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsNormals.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha; /////////////////////////////////////////////////////////////////// _drawStateTerrain = new DrawState(rsTerrain, spTerrain, _va); _drawStateWireframe = new DrawState(rsWireframe, spWireframe, _va); _drawStateNormals = new DrawState(rsNormals, spNormals, _va); _tileMinimumHeight = tile.MinimumHeight; _tileMaximumHeight = tile.MaximumHeight; _heightExaggeration = 1; _normalsAlgorithm = TerrainNormalsAlgorithm.ForwardDifference; _showTerrain = true; _dirty = true; }
public VertexDisplacementMapTerrainTile(Context context, TerrainTile tile) { // // Upload height map as a one channel floating point texture // WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeof(float) * tile.Heights.Length); pixelBuffer.CopyFromSystemMemory(tile.Heights); _texture = Device.CreateTexture2DRectangle(new Texture2DDescription( tile.Resolution.X, tile.Resolution.Y, TextureFormat.Red32f)); _texture.CopyFromBuffer(pixelBuffer, ImageFormat.Red, ImageDatatype.Float); /////////////////////////////////////////////////////////////////// ShaderProgram spTerrain = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.TerrainFS.glsl")); _heightExaggerationUniform = (Uniform <float>)spTerrain.Uniforms["u_heightExaggeration"]; ((Uniform <Vector2F>)spTerrain.Uniforms["u_positionToTextureCoordinate"]).Value = new Vector2F( (float)(1.0 / (double)(tile.Resolution.X)), (float)(1.0 / (double)(tile.Resolution.Y))); ((Uniform <Vector2F>)spTerrain.Uniforms["u_positionToRepeatTextureCoordinate"]).Value = new Vector2F( (float)(4.0 / (double)tile.Resolution.X), (float)(4.0 / (double)tile.Resolution.Y)); /////////////////////////////////////////////////////////////////// ShaderProgram spNormals = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.NormalsFS.glsl")); _heightExaggerationNormals = (Uniform <float>)spNormals.Uniforms["u_heightExaggeration"]; _fillDistanceNormals = (Uniform <float>)spNormals.Uniforms["u_fillDistance"]; ((Uniform <Vector3F>)spNormals.Uniforms["u_color"]).Value = Vector3F.Zero; /////////////////////////////////////////////////////////////////// ShaderProgram spWireframe = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.VertexDisplacementMapTerrainTile.WireframeFS.glsl")); _lineWidthWireframe = (Uniform <float>)spWireframe.Uniforms["u_halfLineWidth"]; _heightExaggerationWireframe = (Uniform <float>)spWireframe.Uniforms["u_heightExaggeration"]; ((Uniform <Vector3F>)spWireframe.Uniforms["u_color"]).Value = Vector3F.Zero; /////////////////////////////////////////////////////////////////// Mesh mesh = RectangleTessellator.Compute(new RectangleD(new Vector2D(0.5, 0.5), new Vector2D((double)tile.Resolution.X - 0.5, (double)tile.Resolution.Y - 0.5)), tile.Resolution.X - 1, tile.Resolution.Y - 1); _va = context.CreateVertexArray(mesh, spWireframe.VertexAttributes, BufferHint.StaticDraw); _primitiveType = mesh.PrimitiveType; /////////////////////////////////////////////////////////////////// RenderState rsTerrain = new RenderState(); rsTerrain.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; RenderState rsWireframe = new RenderState(); rsWireframe.Blending.Enabled = true; rsWireframe.Blending.SourceRGBFactor = SourceBlendingFactor.SourceAlpha; rsWireframe.Blending.SourceAlphaFactor = SourceBlendingFactor.SourceAlpha; rsWireframe.Blending.DestinationRGBFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsWireframe.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsWireframe.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; rsWireframe.DepthTest.Function = DepthTestFunction.LessThanOrEqual; RenderState rsNormals = new RenderState(); rsNormals.FacetCulling.Enabled = false; rsNormals.Blending.Enabled = true; rsNormals.Blending.SourceRGBFactor = SourceBlendingFactor.SourceAlpha; rsNormals.Blending.SourceAlphaFactor = SourceBlendingFactor.SourceAlpha; rsNormals.Blending.DestinationRGBFactor = DestinationBlendingFactor.OneMinusSourceAlpha; rsNormals.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha; /////////////////////////////////////////////////////////////////// _drawStateTerrain = new DrawState(rsTerrain, spTerrain, _va); _drawStateWireframe = new DrawState(rsWireframe, spWireframe, _va); _drawStateNormals = new DrawState(rsNormals, spNormals, _va); _tileMinimumHeight = tile.MinimumHeight; _tileMaximumHeight = tile.MaximumHeight; _heightExaggeration = 1; _normalsAlgorithm = TerrainNormalsAlgorithm.ForwardDifference; _showTerrain = true; _dirty = true; }
public TriangleMeshTerrainTile(Context context, TerrainTile tile) { ShaderProgram silhouetteSP = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteGS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.SilhouetteFS.glsl")); Uniform<float> fillDistance = (Uniform<float>)silhouetteSP.Uniforms["u_fillDistance"]; fillDistance.Value = 1.5f; _silhouetteHeightExaggeration = (Uniform<float>)silhouetteSP.Uniforms["u_heightExaggeration"]; ShaderProgram sp = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.TerrainVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.TriangleMeshTerrainTile.TerrainFS.glsl")); _tileMinimumHeight = tile.MinimumHeight; _tileMaximumHeight = tile.MaximumHeight; _heightExaggeration = (Uniform<float>)sp.Uniforms["u_heightExaggeration"]; _minimumHeight = (Uniform<float>)sp.Uniforms["u_minimumHeight"]; _maximumHeight = (Uniform<float>)sp.Uniforms["u_maximumHeight"]; HeightExaggeration = 1; /////////////////////////////////////////////////////////////////// Mesh mesh = new Mesh(); mesh.PrimitiveType = PrimitiveType.Triangles; mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise; int numberOfPositions = tile.Resolution.X * tile.Resolution.Y; VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfPositions); IList<Vector3D> positions = positionsAttribute.Values; mesh.Attributes.Add(positionsAttribute); int numberOfPartitionsX = tile.Resolution.X - 1; int numberOfPartitionsY = tile.Resolution.Y - 1; int numberOfIndices = (numberOfPartitionsX * numberOfPartitionsY) * 6; IndicesUnsignedInt indices = new IndicesUnsignedInt(numberOfIndices); mesh.Indices = indices; // // Positions // Vector2D lowerLeft = tile.Extent.LowerLeft; Vector2D toUpperRight = tile.Extent.UpperRight - lowerLeft; int heightIndex = 0; for (int y = 0; y <= numberOfPartitionsY; ++y) { double deltaY = y / (double)numberOfPartitionsY; double currentY = lowerLeft.Y + (deltaY * toUpperRight.Y); for (int x = 0; x <= numberOfPartitionsX; ++x) { double deltaX = x / (double)numberOfPartitionsX; double currentX = lowerLeft.X + (deltaX * toUpperRight.X); positions.Add(new Vector3D(currentX, currentY, tile.Heights[heightIndex++])); } } // // Indices // int rowDelta = numberOfPartitionsX + 1; int i = 0; for (int y = 0; y < numberOfPartitionsY; ++y) { for (int x = 0; x < numberOfPartitionsX; ++x) { indices.AddTriangle(new TriangleIndicesUnsignedInt(i, i + 1, rowDelta + (i + 1))); indices.AddTriangle(new TriangleIndicesUnsignedInt(i, rowDelta + (i + 1), rowDelta + i)); i += 1; } i += 1; } _drawState = new DrawState(); _drawState.RenderState.FacetCulling.FrontFaceWindingOrder = mesh.FrontFaceWindingOrder; _drawState.ShaderProgram = sp; _drawState.VertexArray = context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw); _silhouetteDrawState = new DrawState(); _silhouetteDrawState.RenderState.FacetCulling.Enabled = false; _silhouetteDrawState.RenderState.DepthMask = false; _silhouetteDrawState.VertexArray = _drawState.VertexArray; _silhouetteDrawState.ShaderProgram = silhouetteSP; _primitiveType = mesh.PrimitiveType; _clearColor = new ClearState(); _clearColor.Buffers = ClearBuffers.ColorBuffer; // // Only depth needs to be cleared but include stencil for speed. // _clearDepthStencil = new ClearState(); _clearDepthStencil.Buffers = ClearBuffers.DepthBuffer | ClearBuffers.StencilBuffer; }