Example #1
0
        /// <summary>
        /// 创建相关资源
        /// </summary>
        /// <param name="gd"></param>
        /// <param name="factory"></param>
        public void CreateDeviceResources(GraphicsDevice gd, ResourceFactory factory)
        {
            List <Vector3> positions = new List <Vector3>();

            //记录其indices
            // List<ushort> indics = new List<ushort>();
            //填充顶点跟索引
            //详细流程,如果是投影坐标,将其转换成wgs84的经纬度坐标,再使用参考系计算出其真实的地理坐标
            foreach (var coord in _feature.Geometry.Coordinates)
            {
                //将其转换成弧度制,自动贴地
                positions.Add(_shape.ToVector3(new Geodetic3D(MathExtension.ToRadius(coord.X), MathExtension.ToRadius(coord.Y))));
            }
            //三角网化
            var indices = EarClippingOnEllipsoid.Triangulate(positions);

            //三角细分,细分精度为1度
            _mesh         = TriangleMeshSubdivision.Compute(positions, indices.ToArray(), Math.PI / 180);
            _vertexBuffer = factory.CreateBuffer(new BufferDescription((uint)(12 * _mesh.Positions.Count()), BufferUsage.VertexBuffer));
            gd.UpdateBuffer(_vertexBuffer, 0, _mesh.Positions);
            _indexBuffer = factory.CreateBuffer(new BufferDescription((uint)(sizeof(ushort) * _mesh.Indices.Length), BufferUsage.IndexBuffer));
            gd.UpdateBuffer(_indexBuffer, 0, _mesh.Indices);
            ShaderSetDescription shaderSet = new ShaderSetDescription(
                new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3))
            },
                new[]
            {
                ResourceHelper.LoadEmbbedShader(ShaderStages.Vertex, "GlobeVS.spv", gd),
                ResourceHelper.LoadEmbbedShader(ShaderStages.Fragment, "GlobeFS.spv", gd)
            });

            _pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                           BlendStateDescription.SingleOverrideBlend,
                                                           DepthStencilStateDescription.DepthOnlyLessEqual,
                                                           RasterizerStateDescription.Default,
                                                           PrimitiveTopology.TriangleList,
                                                           shaderSet,
                                                           //共享View和prj的buffer
                                                           new ResourceLayout[] { ShareResource.ProjectionResourceLoyout },
                                                           gd.MainSwapchain.Framebuffer.OutputDescription));
            _cl = factory.CreateCommandList();
        }
Example #2
0
        public Polygon(Context context, Ellipsoid globeShape, IEnumerable <Vector3D> positions)
        {
            Verify.ThrowIfNull(context);
            Verify.ThrowIfNull(globeShape);

            //
            // Pipeline Stage 1a:  Clean up - Remove duplicate positions
            //
            List <Vector3D> cleanPositions = (List <Vector3D>)SimplePolygonAlgorithms.Cleanup(positions);

            //
            // Pipeline Stage 1b:  Clean up - Swap winding order
            //
            EllipsoidTangentPlane  plane            = new EllipsoidTangentPlane(globeShape, cleanPositions);
            ICollection <Vector2D> positionsOnPlane = plane.ComputePositionsOnPlane(cleanPositions);

            if (SimplePolygonAlgorithms.ComputeWindingOrder(positionsOnPlane) == PolygonWindingOrder.Clockwise)
            {
                cleanPositions.Reverse();
                //((List<Vector2D>)positionsOnPlane).Reverse();
            }

            //
            // Pipeline Stage 2:  Triangulate
            //
            IndicesUnsignedInt indices = EarClippingOnEllipsoid.Triangulate(cleanPositions);
            //IndicesInt32 indices = EarClipping.Triangulate(positionsOnPlane);

            //
            // Pipeline Stage 3:  Subdivide
            //
            TriangleMeshSubdivisionResult result = TriangleMeshSubdivision.Compute(cleanPositions, indices, Trig.ToRadians(1));

            //
            // Pipeline Stage 4:  Set height
            //
            VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3(
                "position", (result.Indices.Values.Count / 3) + 2);

            foreach (Vector3D position in result.Positions)
            {
                positionsAttribute.Values.Add(globeShape.ScaleToGeocentricSurface(position));
            }

            Mesh mesh = new Mesh();

            mesh.PrimitiveType         = PrimitiveType.Triangles;
            mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise;
            mesh.Attributes.Add(positionsAttribute);
            mesh.Indices = result.Indices;

            ShaderProgram sp = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Polygon.Shaders.PolygonVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Polygon.Shaders.PolygonFS.glsl"));

            ((Uniform <Vector3F>)sp.Uniforms["u_globeOneOverRadiiSquared"]).Value = globeShape.OneOverRadiiSquared.ToVector3F();
            _colorUniform = (Uniform <Vector4F>)sp.Uniforms["u_color"];

            _drawState = new DrawState();
            _drawState.RenderState.Blending.Enabled                = true;
            _drawState.RenderState.Blending.SourceRGBFactor        = SourceBlendingFactor.SourceAlpha;
            _drawState.RenderState.Blending.SourceAlphaFactor      = SourceBlendingFactor.SourceAlpha;
            _drawState.RenderState.Blending.DestinationRGBFactor   = DestinationBlendingFactor.OneMinusSourceAlpha;
            _drawState.RenderState.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha;
            _drawState.ShaderProgram = sp;
            _meshBuffers             = Device.CreateMeshBuffers(mesh, _drawState.ShaderProgram.VertexAttributes, BufferHint.StaticDraw);

            _primitiveType = mesh.PrimitiveType;

            Color = Color.White;
        }