public GlobeClipmapTerrain(Context context, RasterSource terrainSource, EsriRestImagery imagery, Ellipsoid ellipsoid, int clipmapPosts) { _terrainSource = terrainSource; _ellipsoid = ellipsoid; _clipmapPosts = clipmapPosts; _clipmapSegments = _clipmapPosts - 1; int clipmapLevels = _terrainSource.Levels.Count; _clipmapLevels = new ClipmapLevel[clipmapLevels]; for (int i = 0; i < _clipmapLevels.Length; ++i) { _clipmapLevels[i] = new ClipmapLevel(); } for (int i = 0; i < _clipmapLevels.Length; ++i) { RasterLevel terrainLevel = _terrainSource.Levels[i]; _clipmapLevels[i].Terrain = terrainLevel; _clipmapLevels[i].HeightTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapPosts, _clipmapPosts, TextureFormat.Red32f)); _clipmapLevels[i].NormalTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapPosts, _clipmapPosts, TextureFormat.RedGreenBlue32f)); _clipmapLevels[i].CoarserLevel = i == 0 ? null : _clipmapLevels[i - 1]; _clipmapLevels[i].FinerLevel = i == _clipmapLevels.Length - 1 ? null : _clipmapLevels[i + 1]; // Aim for roughly one imagery texel per geometry texel. // Find the first imagery level that meets our resolution needs. double longitudeResRequired = terrainLevel.PostDeltaLongitude; double latitudeResRequired = terrainLevel.PostDeltaLatitude; RasterLevel imageryLevel = null; for (int j = 0; j < imagery.Levels.Count; ++j) { imageryLevel = imagery.Levels[j]; if (imageryLevel.PostDeltaLongitude <= longitudeResRequired && imageryLevel.PostDeltaLatitude <= latitudeResRequired) { break; } } _clipmapLevels[i].Imagery = imageryLevel; _clipmapLevels[i].ImageryWidth = (int)Math.Ceiling(_clipmapPosts * terrainLevel.PostDeltaLongitude / imageryLevel.PostDeltaLongitude); _clipmapLevels[i].ImageryHeight = (int)Math.Ceiling(_clipmapPosts * terrainLevel.PostDeltaLatitude / imageryLevel.PostDeltaLatitude); _clipmapLevels[i].ImageryTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapLevels[i].ImageryWidth, _clipmapLevels[i].ImageryHeight, TextureFormat.RedGreenBlue8, false)); } _shaderProgram = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.ClipmapTerrain.GlobeClipmapVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.ClipmapTerrain.GlobeClipmapFS.glsl")); _fillPatchPosts = (clipmapPosts + 1) / 4; // M _fillPatchSegments = _fillPatchPosts - 1; // Create the MxM block used to fill the ring and the field. Mesh fieldBlockMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(_fillPatchSegments, _fillPatchSegments)), _fillPatchSegments, _fillPatchSegments); _fillPatch = context.CreateVertexArray(fieldBlockMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); // Create the Mx3 block used to fill the space between the MxM blocks in the ring Mesh ringFixupHorizontalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(_fillPatchSegments, 2.0)), _fillPatchSegments, 2); _horizontalFixupPatch = context.CreateVertexArray(ringFixupHorizontalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); // Create the 3xM block used to fill the space between the MxM blocks in the ring Mesh ringFixupVerticalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2.0, _fillPatchSegments)), 2, _fillPatchSegments); _verticalFixupPatch = context.CreateVertexArray(ringFixupVerticalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh offsetStripHorizontalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2 * _fillPatchPosts, 1.0)), 2 * _fillPatchPosts, 1); _horizontalOffsetPatch = context.CreateVertexArray(offsetStripHorizontalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh offsetStripVerticalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(1.0, 2 * _fillPatchPosts - 1)), 1, 2 * _fillPatchPosts - 1); _verticalOffsetPatch = context.CreateVertexArray(offsetStripVerticalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh centerMesh = RectangleTessellator.Compute(new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2.0, 2.0)), 2, 2); _centerPatch = context.CreateVertexArray(centerMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh degenerateTriangleMesh = CreateDegenerateTriangleMesh(); _degenerateTrianglePatch = context.CreateVertexArray(degenerateTriangleMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); _patchOriginInClippedLevel = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_patchOriginInClippedLevel"]; _levelScaleFactor = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_levelScaleFactor"]; _levelZeroWorldScaleFactor = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_levelZeroWorldScaleFactor"]; _levelOffsetFromWorldOrigin = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_levelOffsetFromWorldOrigin"]; _heightExaggeration = (Uniform<float>)_shaderProgram.Uniforms["u_heightExaggeration"]; _viewPosInClippedLevel = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_viewPosInClippedLevel"]; _fineLevelOriginInCoarse = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_fineLevelOriginInCoarse"]; _unblendedRegionSize = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_unblendedRegionSize"]; _oneOverBlendedRegionSize = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_oneOverBlendedRegionSize"]; _fineTextureOrigin = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_fineTextureOrigin"]; _showBlendRegions = (Uniform<bool>)_shaderProgram.Uniforms["u_showBlendRegions"]; _useBlendRegions = (Uniform<bool>)_shaderProgram.Uniforms["u_useBlendRegions"]; _oneOverClipmapSize = (Uniform<float>)_shaderProgram.Uniforms["u_oneOverClipmapSize"]; _color = (Uniform<Vector3F>)_shaderProgram.Uniforms["u_color"]; _blendRegionColor = (Uniform<Vector3F>)_shaderProgram.Uniforms["u_blendRegionColor"]; _terrainToImageryResolutionRatio = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_terrainToImageryResolutionRatio"]; _terrainOffsetInImagery = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_terrainOffsetInImagery"]; _oneOverImagerySize = (Uniform<Vector2F>)_shaderProgram.Uniforms["u_oneOverImagerySize"]; _showImagery = (Uniform<bool>)_shaderProgram.Uniforms["u_showImagery"]; _lighting = (Uniform<bool>)_shaderProgram.Uniforms["u_shade"]; ((Uniform<Vector3F>)_shaderProgram.Uniforms["u_globeRadiiSquared"]).Value = ellipsoid.RadiiSquared.ToVector3F(); _renderState = new RenderState(); _renderState.FacetCulling.FrontFaceWindingOrder = fieldBlockMesh.FrontFaceWindingOrder; _primitiveType = fieldBlockMesh.PrimitiveType; float oneOverBlendedRegionSize = (float)(10.0 / _clipmapPosts); _oneOverBlendedRegionSize.Value = new Vector2F(oneOverBlendedRegionSize, oneOverBlendedRegionSize); float unblendedRegionSize = (float)(_clipmapSegments / 2 - _clipmapPosts / 10.0 - 1); _unblendedRegionSize.Value = new Vector2F(unblendedRegionSize, unblendedRegionSize); _useBlendRegions.Value = true; _showImagery.Value = true; _oneOverClipmapSize.Value = 1.0f / clipmapPosts; _updater = new ClipmapUpdater(context, _clipmapLevels); HeightExaggeration = 0.00001f; }
public GlobeClipmapTerrain(Context context, RasterSource terrainSource, EsriRestImagery imagery, Ellipsoid ellipsoid, int clipmapPosts) { _terrainSource = terrainSource; _ellipsoid = ellipsoid; _clipmapPosts = clipmapPosts; _clipmapSegments = _clipmapPosts - 1; int clipmapLevels = _terrainSource.Levels.Count; _clipmapLevels = new ClipmapLevel[clipmapLevels]; for (int i = 0; i < _clipmapLevels.Length; ++i) { _clipmapLevels[i] = new ClipmapLevel(); } for (int i = 0; i < _clipmapLevels.Length; ++i) { RasterLevel terrainLevel = _terrainSource.Levels[i]; _clipmapLevels[i].Terrain = terrainLevel; _clipmapLevels[i].HeightTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapPosts, _clipmapPosts, TextureFormat.Red32f)); _clipmapLevels[i].NormalTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapPosts, _clipmapPosts, TextureFormat.RedGreenBlue32f)); _clipmapLevels[i].CoarserLevel = i == 0 ? null : _clipmapLevels[i - 1]; _clipmapLevels[i].FinerLevel = i == _clipmapLevels.Length - 1 ? null : _clipmapLevels[i + 1]; // Aim for roughly one imagery texel per geometry texel. // Find the first imagery level that meets our resolution needs. double longitudeResRequired = terrainLevel.PostDeltaLongitude; double latitudeResRequired = terrainLevel.PostDeltaLatitude; RasterLevel imageryLevel = null; for (int j = 0; j < imagery.Levels.Count; ++j) { imageryLevel = imagery.Levels[j]; if (imageryLevel.PostDeltaLongitude <= longitudeResRequired && imageryLevel.PostDeltaLatitude <= latitudeResRequired) { break; } } _clipmapLevels[i].Imagery = imageryLevel; _clipmapLevels[i].ImageryWidth = (int)Math.Ceiling(_clipmapPosts * terrainLevel.PostDeltaLongitude / imageryLevel.PostDeltaLongitude); _clipmapLevels[i].ImageryHeight = (int)Math.Ceiling(_clipmapPosts * terrainLevel.PostDeltaLatitude / imageryLevel.PostDeltaLatitude); _clipmapLevels[i].ImageryTexture = Device.CreateTexture2D(new Texture2DDescription(_clipmapLevels[i].ImageryWidth, _clipmapLevels[i].ImageryHeight, TextureFormat.RedGreenBlue8, false)); } _shaderProgram = Device.CreateShaderProgram( EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.ClipmapTerrain.GlobeClipmapVS.glsl"), EmbeddedResources.GetText("OpenGlobe.Scene.Terrain.ClipmapTerrain.GlobeClipmapFS.glsl")); _fillPatchPosts = (clipmapPosts + 1) / 4; // M _fillPatchSegments = _fillPatchPosts - 1; // Create the MxM block used to fill the ring and the field. Mesh fieldBlockMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(_fillPatchSegments, _fillPatchSegments)), _fillPatchSegments, _fillPatchSegments); _fillPatch = context.CreateVertexArray(fieldBlockMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); // Create the Mx3 block used to fill the space between the MxM blocks in the ring Mesh ringFixupHorizontalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(_fillPatchSegments, 2.0)), _fillPatchSegments, 2); _horizontalFixupPatch = context.CreateVertexArray(ringFixupHorizontalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); // Create the 3xM block used to fill the space between the MxM blocks in the ring Mesh ringFixupVerticalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2.0, _fillPatchSegments)), 2, _fillPatchSegments); _verticalFixupPatch = context.CreateVertexArray(ringFixupVerticalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh offsetStripHorizontalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2 * _fillPatchPosts, 1.0)), 2 * _fillPatchPosts, 1); _horizontalOffsetPatch = context.CreateVertexArray(offsetStripHorizontalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh offsetStripVerticalMesh = RectangleTessellator.Compute( new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(1.0, 2 * _fillPatchPosts - 1)), 1, 2 * _fillPatchPosts - 1); _verticalOffsetPatch = context.CreateVertexArray(offsetStripVerticalMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh centerMesh = RectangleTessellator.Compute(new RectangleD(new Vector2D(0.0, 0.0), new Vector2D(2.0, 2.0)), 2, 2); _centerPatch = context.CreateVertexArray(centerMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); Mesh degenerateTriangleMesh = CreateDegenerateTriangleMesh(); _degenerateTrianglePatch = context.CreateVertexArray(degenerateTriangleMesh, _shaderProgram.VertexAttributes, BufferHint.StaticDraw); _patchOriginInClippedLevel = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_patchOriginInClippedLevel"]; _levelScaleFactor = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_levelScaleFactor"]; _levelZeroWorldScaleFactor = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_levelZeroWorldScaleFactor"]; _levelOffsetFromWorldOrigin = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_levelOffsetFromWorldOrigin"]; _heightExaggeration = (Uniform <float>)_shaderProgram.Uniforms["u_heightExaggeration"]; _viewPosInClippedLevel = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_viewPosInClippedLevel"]; _fineLevelOriginInCoarse = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_fineLevelOriginInCoarse"]; _unblendedRegionSize = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_unblendedRegionSize"]; _oneOverBlendedRegionSize = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_oneOverBlendedRegionSize"]; _fineTextureOrigin = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_fineTextureOrigin"]; _showBlendRegions = (Uniform <bool>)_shaderProgram.Uniforms["u_showBlendRegions"]; _useBlendRegions = (Uniform <bool>)_shaderProgram.Uniforms["u_useBlendRegions"]; _oneOverClipmapSize = (Uniform <float>)_shaderProgram.Uniforms["u_oneOverClipmapSize"]; _color = (Uniform <Vector3F>)_shaderProgram.Uniforms["u_color"]; _blendRegionColor = (Uniform <Vector3F>)_shaderProgram.Uniforms["u_blendRegionColor"]; _terrainToImageryResolutionRatio = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_terrainToImageryResolutionRatio"]; _terrainOffsetInImagery = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_terrainOffsetInImagery"]; _oneOverImagerySize = (Uniform <Vector2F>)_shaderProgram.Uniforms["u_oneOverImagerySize"]; _showImagery = (Uniform <bool>)_shaderProgram.Uniforms["u_showImagery"]; _lighting = (Uniform <bool>)_shaderProgram.Uniforms["u_shade"]; ((Uniform <Vector3F>)_shaderProgram.Uniforms["u_globeRadiiSquared"]).Value = ellipsoid.RadiiSquared.ToVector3F(); _renderState = new RenderState(); _renderState.FacetCulling.FrontFaceWindingOrder = fieldBlockMesh.FrontFaceWindingOrder; _primitiveType = fieldBlockMesh.PrimitiveType; float oneOverBlendedRegionSize = (float)(10.0 / _clipmapPosts); _oneOverBlendedRegionSize.Value = new Vector2F(oneOverBlendedRegionSize, oneOverBlendedRegionSize); float unblendedRegionSize = (float)(_clipmapSegments / 2 - _clipmapPosts / 10.0 - 1); _unblendedRegionSize.Value = new Vector2F(unblendedRegionSize, unblendedRegionSize); _useBlendRegions.Value = true; _showImagery.Value = true; _oneOverClipmapSize.Value = 1.0f / clipmapPosts; _updater = new ClipmapUpdater(context, _clipmapLevels); HeightExaggeration = 0.00001f; }