예제 #1
0
        public Plane(Context context)
        {
            Verify.ThrowIfNull(context);

            RenderState lineRS = new RenderState();

            lineRS.FacetCulling.Enabled = false;

            ShaderProgram lineSP = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Plane.Shaders.LineVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Plane.Shaders.LineGS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Plane.Shaders.LineFS.glsl"));

            _lineLogarithmicDepth         = (Uniform <bool>)lineSP.Uniforms["u_logarithmicDepth"];
            _lineLogarithmicDepthConstant = (Uniform <float>)lineSP.Uniforms["u_logarithmicDepthConstant"];
            _lineFillDistance             = (Uniform <float>)lineSP.Uniforms["u_fillDistance"];
            _lineColorUniform             = (Uniform <Vector3F>)lineSP.Uniforms["u_color"];

            OutlineWidth = 1;
            OutlineColor = Color.Gray;

            ///////////////////////////////////////////////////////////////////

            RenderState fillRS = new RenderState();

            fillRS.FacetCulling.Enabled            = false;
            fillRS.Blending.Enabled                = true;
            fillRS.Blending.SourceRGBFactor        = SourceBlendingFactor.SourceAlpha;
            fillRS.Blending.SourceAlphaFactor      = SourceBlendingFactor.SourceAlpha;
            fillRS.Blending.DestinationRGBFactor   = DestinationBlendingFactor.OneMinusSourceAlpha;
            fillRS.Blending.DestinationAlphaFactor = DestinationBlendingFactor.OneMinusSourceAlpha;

            ShaderProgram fillSP = Device.CreateShaderProgram(
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Plane.Shaders.FillVS.glsl"),
                EmbeddedResources.GetText("OpenGlobe.Scene.Renderables.Plane.Shaders.FillFS.glsl"));

            _fillLogarithmicDepth         = (Uniform <bool>)fillSP.Uniforms["u_logarithmicDepth"];
            _fillLogarithmicDepthConstant = (Uniform <float>)fillSP.Uniforms["u_logarithmicDepthConstant"];
            _fillColorUniform             = (Uniform <Vector3F>)fillSP.Uniforms["u_color"];
            _fillAlphaUniform             = (Uniform <float>)fillSP.Uniforms["u_alpha"];

            LogarithmicDepthConstant = 1;
            FillColor        = Color.Gray;
            FillTranslucency = 0.5f;

            ///////////////////////////////////////////////////////////////////

            _positionBuffer = Device.CreateVertexBuffer(BufferHint.StaticDraw, 2 * 4 * SizeInBytes <Vector3F> .Value);

            ushort[] indices = new ushort[]
            {
                0, 1, 2, 3,                             // Line loop
                0, 1, 2, 0, 2, 3                        // Triangles
            };
            IndexBuffer indexBuffer = Device.CreateIndexBuffer(BufferHint.StaticDraw, indices.Length * sizeof(ushort));

            indexBuffer.CopyFromSystemMemory(indices);

            int stride = 2 * SizeInBytes <Vector3F> .Value;

            _va = context.CreateVertexArray();
            _va.Attributes[VertexLocations.PositionHigh] =
                new VertexBufferAttribute(_positionBuffer, ComponentDatatype.Float, 3, false, 0, stride);
            _va.Attributes[VertexLocations.PositionLow] =
                new VertexBufferAttribute(_positionBuffer, ComponentDatatype.Float, 3, false, SizeInBytes <Vector3F> .Value, stride);
            _va.IndexBuffer = indexBuffer;

            Show        = true;
            ShowOutline = true;
            ShowFill    = true;

            ///////////////////////////////////////////////////////////////////

            _drawStateLine = new DrawState(lineRS, lineSP, _va);
            _drawStateFill = new DrawState(fillRS, fillSP, _va);

            Origin = Vector3D.Zero;
            XAxis  = Vector3D.UnitX;
            YAxis  = Vector3D.UnitY;
        }
예제 #2
0
        public void Set(Context context, IList <Vector3D> positions)
        {
            //
            // This method expects that the positions are ordered in a repeated pattern of
            // below terrain, above terrain pairs.
            //
            // Wall mesh
            //
            Mesh wallMesh = new Mesh();

            wallMesh.PrimitiveType = PrimitiveType.TriangleStrip;

            //
            // Positions
            //
            int numberOfLineSegments = (positions.Count / 2) - 1;
            int numberOfVertices     = 2 + numberOfLineSegments + numberOfLineSegments;
            VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfVertices);
            IList <Vector3D>             tempPositions      = positionsAttribute.Values;

            wallMesh.Attributes.Add(positionsAttribute);
            foreach (Vector3D v in positions)
            {
                tempPositions.Add(v);
            }

            //
            // Vertex array
            //
            _wallVA = context.CreateVertexArray(wallMesh, _wallSP.VertexAttributes, BufferHint.StaticDraw);

            //
            // Line mesh
            //
            Mesh lineMesh = new Mesh();

            lineMesh.PrimitiveType = PrimitiveType.LineStrip;

            //
            // Positions
            //
            positionsAttribute = new VertexAttributeDoubleVector3("position", numberOfVertices);
            tempPositions      = positionsAttribute.Values;
            lineMesh.Attributes.Add(positionsAttribute);
            foreach (Vector3D v in positions)
            {
                tempPositions.Add(v);
            }

            //
            // Indices
            //
            int numIndices = 4 * numberOfLineSegments;

            ushort[] indices   = new ushort[numIndices];
            int      baseIndex = 1;

            for (int i = 0; i < numIndices; i += 4, baseIndex += 2)
            {
                indices[i]     = (ushort)baseIndex;
                indices[i + 1] = (ushort)(baseIndex - 1);
                indices[i + 2] = (ushort)(baseIndex + 1);
                indices[i + 3] = (ushort)(baseIndex + 2);
            }
            IndexBuffer indexBuffer = Device.CreateIndexBuffer(BufferHint.StaticDraw, numIndices * sizeof(ushort));

            indexBuffer.CopyFromSystemMemory(indices);

            //
            // Vertex array
            //
            _lineVA             = context.CreateVertexArray(lineMesh, _wallSP.VertexAttributes, BufferHint.StaticDraw);
            _lineVA.IndexBuffer = indexBuffer;

            //
            // State
            //
            _wallDrawState = new DrawState();
            _wallDrawState.RenderState.FacetCulling.Enabled = false;
            _wallDrawState.RenderState.DepthTest.Enabled    = false;
            _wallDrawState.RenderState.DepthMask            = false;
            _wallDrawState.VertexArray   = _lineVA;
            _wallDrawState.ShaderProgram = _wallSP;

            _shadowVolumePassOne               = new DrawState();
            _shadowVolumePassOne.VertexArray   = _lineVA;
            _shadowVolumePassOne.ShaderProgram = _shadowVolumeSP;
            _shadowVolumePassOne.RenderState.FacetCulling.Enabled = false;
            _shadowVolumePassOne.RenderState.DepthMask            = false;
            _shadowVolumePassOne.RenderState.ColorMask            = new ColorMask(false, false, false, false);
            StencilTest stOne = _shadowVolumePassOne.RenderState.StencilTest;

            stOne.Enabled = true;
            stOne.FrontFace.DepthFailStencilPassOperation = StencilOperation.Decrement;
            stOne.BackFace.DepthFailStencilPassOperation  = StencilOperation.Increment;

            _shadowVolumePassTwo                       = new DrawState();
            _shadowVolumePassTwo.VertexArray           = _lineVA;
            _shadowVolumePassTwo.ShaderProgram         = _shadowVolumeSP;
            _shadowVolumePassTwo.RenderState.DepthMask = false;
            StencilTest stTwo = _shadowVolumePassTwo.RenderState.StencilTest;

            stTwo.Enabled = true;
            stTwo.FrontFace.DepthFailStencilPassOperation = StencilOperation.Zero;
            stTwo.FrontFace.DepthPassStencilPassOperation = StencilOperation.Zero;
            stTwo.FrontFace.Function = StencilTestFunction.NotEqual;
            stTwo.BackFace.DepthFailStencilPassOperation = StencilOperation.Zero;
            stTwo.BackFace.DepthPassStencilPassOperation = StencilOperation.Zero;
            stTwo.BackFace.Function = StencilTestFunction.NotEqual;
        }