当前位置: 首页>>代码示例>>C#>>正文


C# Vector3f.Normalize方法代码示例

本文整理汇总了C#中Vector3f.Normalize方法的典型用法代码示例。如果您正苦于以下问题:C# Vector3f.Normalize方法的具体用法?C# Vector3f.Normalize怎么用?C# Vector3f.Normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Vector3f的用法示例。


在下文中一共展示了Vector3f.Normalize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RotateBetween

        public static Matrix3f RotateBetween( Vector3f from, Vector3f to )
        {
            from.Normalize();
            to.Normalize();            

            var crossProduct = Vector3f.Cross( from, to );
            var normalizedAxis = crossProduct.Normalized();
            float sinTheta = crossProduct.Norm();
            float cosTheta = Vector3f.Dot( from, to );
            
            float x = normalizedAxis.x;
            float y = normalizedAxis.y;
            float z = normalizedAxis.z;
            
            var m = new Matrix3f();

            m[ 0, 0 ] = x * x * ( 1.0f - cosTheta ) + cosTheta;
            m[ 0, 1 ] = y * x * ( 1.0f - cosTheta ) - z * sinTheta;
            m[ 0, 2 ] = z * x * ( 1.0f - cosTheta ) + y * sinTheta;

            m[ 1, 0 ] = x * y * ( 1.0f - cosTheta ) + z * sinTheta;
            m[ 1, 1 ] = y * y * ( 1.0f - cosTheta ) + cosTheta;
            m[ 1, 2 ] = z * y * ( 1.0f - cosTheta ) - x * sinTheta;

            m[ 2, 0 ] = x * z * ( 1.0f - cosTheta ) - y * sinTheta;
            m[ 2, 1 ] = y * z * ( 1.0f - cosTheta ) + x * sinTheta;
            m[ 2, 2 ] = z * z * ( 1.0f - cosTheta ) + cosTheta;

            return m;
        }
开发者ID:jiawen,项目名称:libcgt.net,代码行数:30,代码来源:Matrix3f.cs

示例2: ConvertClickToLine

        /// <summary>
        /// Converts a pixel into a ray for object intersection.
        /// pixel (0,0) is the top left corner
        /// </summary>
        /// <param name="pixel"></param>
        /// <param name="origin"></param>
        /// <param name="direction"></param>
        public void ConvertClickToLine( Vector2i pixel, out Vector3f origin, out Vector3f direction )
        {
            origin = Vector3f.Zero;
            direction = Vector3f.Zero;

            switch( CameraType )
            {
                case CameraType.PERSPECTIVE:

                    origin = Position;
                    direction = new Vector3f
                        (
                        ( float )
                        ( ( ( 2.0 * pixel.x / ScreenSize.x ) - 1.0 ) * Math.Tan( 0.5 * FieldOfView ) * AspectRatio ),
                        ( float )
                        ( ( ( 2.0 * ( ScreenSize.y - pixel.y ) / ScreenSize.y ) - 1.0 ) * Math.Tan( 0.5 * FieldOfView ) ),
                        -1.0f
                        );
                    direction = WorldCoordinatesOf( direction ) - origin;
                    direction.Normalize();
                    break;

                case CameraType.ORTHOGRAPHIC:

                    Vector2f wh = GetOrthoWidthHeight();
                    origin = new Vector3f
                        (
                        ( float ) ( ( 2.0 * pixel.x / ScreenSize.x - 1.0 ) * wh.x ),
                        ( float ) ( -( 2.0 * pixel.y / ScreenSize.y - 1.0 ) * wh.y ),
                        0.0f
                        );
                    origin = WorldCoordinatesOf( origin );
                    direction = ViewDirection;
                    break;
            }
        }
开发者ID:jiawen,项目名称:libcgt.net,代码行数:43,代码来源:Camera.cs

示例3: Build

        public static Terrain Build(IOpenGL33 gl, int columns, int rows, float width, float depth)
        {
            var noise = new PerlinNoise();
            var noise2 = new PerlinNoise();
            
            var vertexBuffer = new VertexBuffer<PositionNormalTexCoord>((columns + 1) * (rows + 1), gl);
            var indexBuffer = new ElementBuffer<uint>(columns * rows * 6 , gl);

            float fx = - width / 2;
            float fy = -depth / 2;

            float dx = width / columns;
            float dy = depth / rows;

            int i = 0;

            for (int y = 0; y < (rows + 1); y++)
            {
                for (int x = 0; x < (columns + 1); x++, i++)
                {
                    float ix = fx + x * dx;
                    float iy = fy + y * dy;

                    const float detail = 0.25f;
                    const float detailScale = 0.2f;
                    const float macro = 0.008f;
                    const float macroScale = 4f;
                    var fUp = (float)((noise.Noise(x * detail, 0, (y + 1) * detail) - noise2.Noise(x * detail, 0, (y + 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, (y + 1) * macro) * macroScale) ;
                    var fDown = (float)((noise.Noise(x * detail, 0,  (y - 1) * detail) - noise2.Noise(x * detail, 0,  (y - 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0,  (y - 1) * macro) * macroScale) ;
                    var fLeft = (float)((noise.Noise((x - 1) * detail, 0, y * detail) - noise2.Noise((x - 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x - 1) * macro, 0, y * macro) * macroScale);
                    var fRight = (float)((noise.Noise((x + 1) * detail, 0, y * detail) - noise2.Noise((x + 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x + 1) * macro, 0, y * macro) * macroScale);
                    var fThis = (float)((noise.Noise(x * detail, 0, y * detail) - noise2.Noise(x * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, y * macro) * macroScale);

                    var vUp = new Vector3f(ix, fUp, iy - dy);
                    var vDown = new Vector3f(ix, fDown, iy + dy);
                    var vLeft = new Vector3f(ix - 1, fLeft, iy);
                    var vRight = new Vector3f(ix + 1, fRight, iy);
                    var vThis = new Vector3f(ix, fThis, iy);

                    var upperLeft = CalculateNormal(vLeft, vUp, vThis);
                    var upperRight = CalculateNormal(vUp, vRight, vThis);
                    var lowerRight = CalculateNormal(vRight, vDown, vThis);
                    var lowerLeft = CalculateNormal(vDown, vLeft, vThis);

                    var normal = upperLeft + upperRight + lowerLeft + lowerRight;
                    normal = new Vector3f(normal.X * 0.25f, normal.Y * 0.25f, normal.Z * 0.25f);

                    vertexBuffer[i] = new PositionNormalTexCoord { Normal = normal.Normalize(), Position = vThis, TexCoord = new Vector2f(vThis.X, vThis.Z)};
                }
            }
            int offset = 0;
            foreach(var index in Enumerable.Range(0, columns * rows + columns).Where(index => (index % (columns + 1)) != columns))
            {
                indexBuffer[offset] = (uint)index;
                indexBuffer[offset + 1] = (uint)(index + (columns + 1) + 1); 
                indexBuffer[offset + 2] = (uint)index + 1;

                indexBuffer[offset + 3] = (uint)index;                
                indexBuffer[offset + 4] = (uint)(index + (columns + 1));
                indexBuffer[offset + 5] = (uint)(index + (columns + 1) + 1);
                offset += 6;
            }

            using(vertexBuffer.Bind())
                vertexBuffer.BufferData(BufferUsage.StaticDraw);

            using(indexBuffer.Bind())
                indexBuffer.BufferData(BufferUsage.StaticDraw);

            var vertexArray = new VertexArray(gl, new[] {vertexBuffer}, new[] {PositionNormalTexCoord.Descriptor});

            var shader = new TerrainShader(gl);

            return new Terrain(gl, vertexArray, vertexBuffer, indexBuffer, shader);
        }
开发者ID:GeirGrusom,项目名称:ModGL,代码行数:75,代码来源:Terrain.cs


注:本文中的Vector3f.Normalize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。