예제 #1
0
        private VertexAttributeBufferPtr GetPositionBufferPtr(string varNameInShader)
        {
            VertexAttributeBufferPtr ptr = null;

            using (var buffer = new VertexAttributeBuffer <HexahedronPosition>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
            {
                int dimSize = this.DataSource.DimenSize;
                buffer.Create(dimSize);
                unsafe
                {
                    var array = (HexahedronPosition *)buffer.Header.ToPointer();
                    int I, J, K;
                    for (int gridIndex = 0; gridIndex < dimSize; gridIndex++)
                    {
                        this.DataSource.InvertIJK(gridIndex, out I, out J, out K);
                        array[gridIndex].FLT = this.DataSource.Position + this.DataSource.PointFLT(I, J, K);
                        array[gridIndex].FRT = this.DataSource.Position + this.DataSource.PointFRT(I, J, K);
                        array[gridIndex].BRT = this.DataSource.Position + this.DataSource.PointBRT(I, J, K);
                        array[gridIndex].BLT = this.DataSource.Position + this.DataSource.PointBLT(I, J, K);
                        array[gridIndex].FLB = this.DataSource.Position + this.DataSource.PointFLB(I, J, K);
                        array[gridIndex].FRB = this.DataSource.Position + this.DataSource.PointFRB(I, J, K);
                        array[gridIndex].BRB = this.DataSource.Position + this.DataSource.PointBRB(I, J, K);
                        array[gridIndex].BLB = this.DataSource.Position + this.DataSource.PointBLB(I, J, K);
                    }
                }
                ptr = buffer.GetBufferPtr();
            }

            return(ptr);
        }
예제 #2
0
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(positions.Length);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < positions.Length; i++)
                            {
                                array[i] = positions[i];
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else
            {
                throw new ArgumentException();
            }
        }
예제 #3
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            {
                // velocity
                var buffer = new VertexAttributeBuffer <vec4>("empty", VertexAttributeConfig.Vec4, BufferUsage.DynamicCopy);
                buffer.Create(ParticleModel.particleCount);
                unsafe
                {
                    var random = new Random();
                    var array  = (vec4 *)buffer.Header.ToPointer();
                    for (int i = 0; i < ParticleModel.particleCount; i++)
                    {
                        array[i] = new vec4(
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            0);
                    }
                }
                var ptr = buffer.GetBufferPtr();
                this.VelocityBufferPtr = ptr;
            }

            this.PositionBufferPtr = this.Model.GetVertexAttributeBufferPtr(ParticleModel.strPosition, null);
        }
예제 #4
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            this.positionBufferPtr = this.Model.GetVertexAttributeBufferPtr(QuadStripColoredModel.position, null);
            this.colorBufferPtr    = this.Model.GetVertexAttributeBufferPtr(QuadStripColoredModel.color, null);
        }
예제 #5
0
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (positionBuffer == null)
         {
             using (var buffer = new VertexAttributeBuffer <vec4>(varNameInShader, VertexAttributeConfig.Vec4, BufferUsage.DynamicDraw))
             {
                 buffer.Create(this.SideLength * this.SideLength);
                 unsafe
                 {
                     var pointer = (vec4 *)buffer.Header.ToPointer();
                     for (int z = 0; z < this.SideLength; z++)
                     {
                         for (int x = 0; x < this.SideLength; x++)
                         {
                             pointer[x + z * this.SideLength] = new vec4(
                                 -(float)this.SideLength / 2.0f + 0.5f + (float)x,
                                 0.0f,
                                 (float)this.SideLength / 2.0f - 0.5f - (float)z,
                                 1.0f
                                 );
                         }
                     }
                 }
                 positionBuffer = buffer.GetBufferPtr();
             }
         }
         return(positionBuffer);
     }
     else
     {
         throw new System.ArgumentException();
     }
 }
예제 #6
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            this.positionBufferPtr = this.Model.GetVertexAttributeBufferPtr(QuadStripModel.position, null);
            this.texCoordBufferPtr = this.Model.GetVertexAttributeBufferPtr(QuadStripModel.texCoord, null);
        }
예제 #7
0
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (positionBuffer == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.DynamicDraw))
             {
                 buffer.Create(positions.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < positions.Length; i++)
                     {
                         array[i] = positions[i];
                     }
                 }
                 positionBuffer = buffer.GetBufferPtr();
             }
         }
         return(positionBuffer);
     }
     else
     {
         return(null);
     }
 }
            public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
            {
                if (bufferName == strPosition)
                {
                    if (positionBufferPtr == null)
                    {
                        using (var buffer = new VertexAttributeBuffer <vec3>(
                                   varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                        {
                            buffer.Create(particleCount);
                            unsafe
                            {
                                var array = (vec3 *)buffer.Header.ToPointer();
                                for (int i = 0; i < particleCount; i++)
                                {
                                    double beta  = random.NextDouble() * Math.PI;
                                    double theta = random.NextDouble() * Math.PI * 2;
                                    float  x     = (float)(a * Math.Sin(beta) * Math.Cos(theta));
                                    float  y     = (float)(b * Math.Sin(beta) * Math.Sin(theta));
                                    float  z     = (float)(c * Math.Cos(beta));
                                    array[i] = new vec3(x, y, z);
                                }
                            }

                            positionBufferPtr = buffer.GetBufferPtr();
                        }
                    }

                    return(positionBufferPtr);
                }
                else
                {
                    throw new ArgumentException();
                }
            }
예제 #9
0
        private VertexAttributeBufferPtr GetTexCoordBufferPtr(string varNameInShader)
        {
            VertexAttributeBufferPtr texCoordBufferPtr = null;

            using (var buffer = new VertexAttributeBuffer <float>(
                       varNameInShader, VertexAttributeConfig.Float, BufferUsage.StaticDraw))
            {
                int uCount = GetUCount(interval);
                int vCount = GetVCount(interval);
                buffer.Create(uCount * vCount);
                unsafe
                {
                    int index = 0;
                    var array = (float *)buffer.Header.ToPointer();
                    for (int uIndex = 0; uIndex < uCount; uIndex++)
                    {
                        for (int vIndex = 0; vIndex < vCount; vIndex++)
                        {
                            array[index++] = (float)uIndex / (float)uCount;
                        }
                    }
                }

                texCoordBufferPtr = buffer.GetBufferPtr();

                return(texCoordBufferPtr);
            }
        }
예제 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bufferName"></param>
        /// <param name="varNameInShader"></param>
        /// <returns></returns>
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == position)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(this.markerCount * 2);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < this.markerCount; i++)
                            {
                                array[i * 2 + 0] = new vec3(-0.5f + (float)i / (float)(this.markerCount - 1), 0.5f, 0);
                                array[i * 2 + 1] = new vec3(-0.5f + (float)i / (float)(this.markerCount - 1), -0.5f, 0);
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bufferName"></param>
        /// <param name="varNameInShader"></param>
        /// <returns></returns>
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == position)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create((this.quadCount + 1) * 2);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < (this.quadCount + 1); i++)
                            {
                                array[i * 2 + 0] = new vec3(-0.5f + (float)i / (float)(this.quadCount), 0.5f, 0);
                                array[i * 2 + 1] = new vec3(-0.5f + (float)i / (float)(this.quadCount), -0.5f, 0);
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else if (bufferName == color)
            {
                if (colorBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create((this.quadCount + 1) * 2);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < (this.quadCount + 1); i++)
                            {
                                int x = this.bitmap.Width * i / this.quadCount;
                                if (x == this.bitmap.Width)
                                {
                                    x = this.bitmap.Width - 1;
                                }
                                vec3 value = this.bitmap.GetPixel(x, 0).ToVec3();
                                array[i * 2 + 0] = value;
                                array[i * 2 + 1] = value;
                            }
                        }

                        colorBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(colorBufferPtr);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #12
0
        private VertexAttributeBufferPtr GetPositionBufferPtr(string varNameInShader)
        {
            VertexAttributeBufferPtr positionBufferPtr = null;

            using (var buffer = new VertexAttributeBuffer <vec3>(
                       varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
            {
                bool initialized = false;
                vec3 max         = new vec3();
                vec3 min         = new vec3();
                int  uCount      = GetUCount(interval);
                int  vCount      = GetVCount(interval);
                buffer.Create(uCount * vCount);
                unsafe
                {
                    int index = 0;
                    var array = (vec3 *)buffer.Header.ToPointer();
                    for (int uIndex = 0; uIndex < uCount; uIndex++)
                    {
                        for (int vIndex = 0; vIndex < vCount; vIndex++)
                        {
                            double u        = Math.PI * uIndex / uCount;
                            double v        = Math.PI * 2 * vIndex / vCount;
                            var    position = GetPosition(u, v);

                            if (!initialized)
                            {
                                max         = position;
                                min         = position;
                                initialized = true;
                            }
                            else
                            {
                                position.UpdateMax(ref max);
                                position.UpdateMin(ref min);
                            }
                            array[index++] = position;
                        }
                    }
                    //this.Lengths = max - min;
                    vec3 worldPosition = max / 2.0f + min / 2.0f;
                    for (int i = 0; i < index; i++)
                    {
                        array[i] = array[i] - worldPosition;
                    }
                }

                positionBufferPtr = buffer.GetBufferPtr();

                return(positionBufferPtr);
            }
        }
예제 #13
0
            public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
            {
                if (bufferName == strposition)
                {
                    if (positionBufferPtr == null)
                    {
                        using (var buffer = new VertexAttributeBuffer <vec3>(
                                   varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                        {
                            buffer.Create(particleCount);
                            unsafe
                            {
                                var array = (vec3 *)buffer.Header.ToPointer();
                                for (int i = 0; i < particleCount; i++)
                                {
                                    if (i % 2 == 0)
                                    {
                                        while (true)
                                        {
                                            var x = (float)(random.NextDouble() * 2 - 1) * factor;
                                            var y = (float)(random.NextDouble() * 2 - 1) * factor;
                                            var z = (float)(random.NextDouble() * 2 - 1) * factor;
                                            if (y < 0 && x * x + y * y + z * z >= factor * factor)
                                            {
                                                array[i] = new vec3(x, y, z);
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        double theta = random.NextDouble() * 2 * Math.PI - Math.PI;
                                        double alpha = random.NextDouble() * 2 * Math.PI - Math.PI;
                                        array[i] = new vec3(
                                            (float)(Math.Sin(theta) * Math.Cos(alpha)) * factor,
                                            (float)(Math.Sin(theta) * Math.Sin(alpha)) * factor,
                                            (float)(Math.Cos(theta)) * factor);
                                    }
                                }
                            }

                            positionBufferPtr = buffer.GetBufferPtr();
                        }
                    }

                    return(positionBufferPtr);
                }
                else
                {
                    throw new ArgumentException();
                }
            }
예제 #14
0
        /// <summary>
        /// 将此Buffer的数据上传到GPU内存,并获取在GPU上的指针。执行此方法后,此对象中的非托管内存即可释放掉,不再占用CPU内存。
        /// Uploads this buffer to GPU memory and gets its pointer.
        /// It's totally OK to free memory of unmanaged array stored in this buffer object after this method invoked.
        /// </summary>
        /// <returns></returns>
        protected VertexAttributeBufferPtr Upload2GPU()
        {
            uint[] buffers = new uint[1];
            glGenBuffers(1, buffers);
            const uint target = OpenGL.GL_ARRAY_BUFFER;

            glBindBuffer(target, buffers[0]);
            glBufferData(target, this.ByteLength, this.Header, (uint)this.Usage);
            glBindBuffer(target, 0);

            var bufferPtr = new VertexAttributeBufferPtr(
                this.VarNameInVertexShader, buffers[0], this.Config, this.Length, this.ByteLength, this.InstancedDivisor, this.PatchVertexes);

            return(bufferPtr);
        }
예제 #15
0
        /// <summary>
        /// 将此Buffer的数据上传到GPU内存,并获取在GPU上的指针。执行此方法后,此对象中的非托管内存即可释放掉,不再占用CPU内存。
        /// Uploads this buffer to GPU memory and gets its pointer.
        /// It's totally OK to free memory of unmanaged array stored in this buffer object after this method invoked.
        /// </summary>
        /// <returns></returns>
        public VertexAttributeBufferPtr GetBufferPtr()
        {
            if (bufferPtr == null)
            {
                if (glGenBuffers == null)
                {
                    glGenBuffers = OpenGL.GetDelegateFor <OpenGL.glGenBuffers>();
                    glBindBuffer = OpenGL.GetDelegateFor <OpenGL.glBindBuffer>();
                    glBufferData = OpenGL.GetDelegateFor <OpenGL.glBufferData>();
                }

                bufferPtr = Upload2GPU();
            }

            return(bufferPtr);
        }
예제 #16
0
파일: Square.cs 프로젝트: hhxx1314/CSharpGL
 /// <summary>
 ///
 /// </summary>
 /// <param name="bufferName"></param>
 /// <param name="varNameInShader"></param>
 /// <returns></returns>
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (positionBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 buffer.Create(model.positions.Length);
                 unsafe
                 {
                     var array = (vec3 *)buffer.Header.ToPointer();
                     for (int i = 0; i < model.positions.Length; i++)
                     {
                         array[i] = model.positions[i];
                     }
                 }
                 positionBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(positionBufferPtr);
     }
     else if (bufferName == strTexCoord)
     {
         if (uvBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <vec2>(varNameInShader, VertexAttributeConfig.Vec2, BufferUsage.StaticDraw))
             {
                 buffer.Create(model.texCoords.Length);
                 unsafe
                 {
                     var array = (vec2 *)buffer.Header.ToPointer();
                     for (int i = 0; i < model.texCoords.Length; i++)
                     {
                         array[i] = model.texCoords[i];
                     }
                 }
                 uvBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(uvBufferPtr);
     }
     else
     {
         return(null);
     }
 }
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strposition)
     {
         if (positionBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 buffer.Create(4);
                 unsafe
                 {
                     var array = (vec3 *)buffer.Header.ToPointer();
                     array[0] = new vec3(-1.0f, -1.0f, 0.5f);
                     array[1] = new vec3(1.0f, -1.0f, 0.5f);
                     array[2] = new vec3(1.0f, 1.0f, 0.5f);
                     array[3] = new vec3(-1.0f, 1.0f, 0.5f);
                 }
                 positionBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(positionBufferPtr);
     }
     else if (bufferName == struv)
     {
         if (uvBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <vec2>(varNameInShader, VertexAttributeConfig.Vec2, BufferUsage.StaticDraw))
             {
                 buffer.Create(4);
                 unsafe
                 {
                     var array = (vec2 *)buffer.Header.ToPointer();
                     array[0] = new vec2(1, 1);
                     array[1] = new vec2(0, 1);
                     array[2] = new vec2(0, 0);
                     array[3] = new vec2(1, 0);
                 }
                 uvBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(uvBufferPtr);
     }
     else
     {
         throw new NotImplementedException();
     }
 }
예제 #18
0
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strposition)
     {
         if (positionBuffer == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 buffer.Create(boundingBox.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < boundingBox.Length; i++)
                     {
                         array[i] = boundingBox[i] - 0.5f;
                     }
                 }
                 positionBuffer = buffer.GetBufferPtr();
             }
         }
         return(positionBuffer);
     }
     else if (bufferName == strcolor)
     {
         if (colorBuffer == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 buffer.Create(boundingBox.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < boundingBox.Length; i++)
                     {
                         array[i] = boundingBox[i];
                     }
                 }
                 colorBuffer = buffer.GetBufferPtr();
             }
         }
         return(colorBuffer);
     }
     else
     {
         return(null);
     }
 }
예제 #19
0
 /// <summary>
 /// 获取指定的顶点属性缓存。
 /// <para>Gets specified vertex buffer object.</para>
 /// </summary>
 /// <param name="bufferName">buffer name(Gets this name from 'strPosition' etc.</param>
 /// <param name="varNameInShader">name in vertex shader like `in vec3 in_Position;`.</param>
 /// <returns>Vertex Buffer Object.</returns>
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (this.positionBufferPtr == null)
         {
             this.positionBufferPtr = GetPositionBufferPtr(varNameInShader);
         }
         return(this.positionBufferPtr);
     }
     else if (bufferName == strTexCoord)
     {
         if (this.colorBufferPtr == null)
         {
             this.colorBufferPtr = GetTexCoordBufferPtr(varNameInShader);
         }
         return(this.colorBufferPtr);
     }
     else
     {
         throw new ArgumentException();
     }
 }
예제 #20
0
        private VertexAttributeBufferPtr GetColorBufferPtr(string varNameInShader)
        {
            VertexAttributeBufferPtr ptr = null;

            using (var buffer = new VertexAttributeBuffer <HexahedronTexCoord>(varNameInShader, VertexAttributeConfig.Float, BufferUsage.StaticDraw))
            {
                float[] textures = GetTextureCoords(this.GridBlockProperties[this.defaultBlockPropertyIndex]);

                int gridCellCount = this.DataSource.DimenSize;
                buffer.Create(gridCellCount);
                unsafe
                {
                    var array = (HexahedronTexCoord *)buffer.Header.ToPointer();
                    for (int gridIndex = 0; gridIndex < gridCellCount; gridIndex++)
                    {
                        array[gridIndex].SetCoord(textures[gridIndex]);
                    }
                }
                ptr = buffer.GetBufferPtr();
            }

            return(ptr);
        }
예제 #21
0
        private BoundingBox GetBoundingBox()
        {
            BoundingBox box  = null;
            var         self = this.BindingObject.Renderer as PickableRenderer;
            VertexAttributeBufferPtr positionPtr = self.PositionBufferPtr;
            IntPtr pointer = positionPtr.MapBuffer(MapBufferAccess.ReadOnly);

            unsafe
            {
                var  array = (vec3 *)pointer;
                vec3 max = array[0], min = array[0];
                for (int i = 1; i < positionPtr.ByteLength / sizeof(vec3); i++)
                {
                    vec3 position = array[i];
                    position.UpdateMax(ref max);
                    position.UpdateMin(ref min);
                }
                box = new BoundingBox(min, max);
            }
            positionPtr.UnmapBuffer();

            return(box);
        }
예제 #22
0
        public unsafe VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr != null)
                {
                    return(positionBufferPtr);
                }

                using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                {
                    int vertexCount = (faceCount * 2 + 2) * (pipeline.Count - 1);
                    buffer.Create(vertexCount);
                    var array = (vec3 *)buffer.Header.ToPointer();
                    int index = 0;
                    var max   = new vec3(float.MinValue, float.MinValue, float.MinValue);
                    var min   = new vec3(float.MaxValue, float.MaxValue, float.MaxValue);
                    for (int i = 1; i < pipeline.Count; i++)
                    {
                        vec3 p1     = pipeline[i - 1];
                        vec3 p2     = pipeline[i];
                        vec3 vector = p2 - p1;// 从p1到p2的向量
                        // 找到互相垂直的三个向量:vector, orthogontalVector1和orthogontalVector2
                        vec3 orthogontalVector1 = new vec3(vector.y - vector.z, vector.z - vector.x, vector.x - vector.y);
                        vec3 orthogontalVector2 = vector.cross(orthogontalVector1);
                        orthogontalVector1 = orthogontalVector1.normalize() * (float)Math.Sqrt(this.radius);
                        orthogontalVector2 = orthogontalVector2.normalize() * (float)Math.Sqrt(this.radius);
                        for (int faceIndex = 0; faceIndex < faceCount + 1; faceIndex++)
                        {
                            double angle = (Math.PI * 2 * faceIndex) / faceCount;
                            vec3   delta = orthogontalVector1 * (float)Math.Cos(angle) + orthogontalVector2 * (float)Math.Sin(angle);
                            vec3   tmp1 = p1 + delta, tmp2 = p2 + delta;
                            tmp1.UpdateMax(ref max); tmp1.UpdateMin(ref min);
                            tmp2.UpdateMax(ref max); tmp2.UpdateMin(ref min);
                            array[index++] = tmp1;
                            array[index++] = tmp2;
                        }
                    }
                    this.Lengths = max - min;

                    positionBufferPtr = buffer.GetBufferPtr();
                }

                return(positionBufferPtr);
            }
            else if (bufferName == strBrightness)
            {
                if (brightnessBufferPtr != null)
                {
                    return(brightnessBufferPtr);
                }

                using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                {
                    int vertexCount = (faceCount * 2 + 2) * (pipeline.Count - 1);
                    buffer.Create(vertexCount);
                    var array  = (vec3 *)buffer.Header.ToPointer();
                    var random = new Random();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        var x = (float)(random.NextDouble() * 0.5 + 0.5);
                        array[i] = new vec3(x, x, x);
                    }

                    brightnessBufferPtr = buffer.GetBufferPtr();
                }

                return(brightnessBufferPtr);
            }
            else
            {
                throw new ArgumentException();
            }
        }
예제 #23
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            this.positionBufferPtr = this.Model.GetVertexAttributeBufferPtr(LinesModel.position, null);
        }
예제 #24
0
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <float>(
                               varNameInShader, VertexAttributeConfig.Vec4, BufferUsage.StaticDraw))
                    {
                        buffer.Create(xy_vertices.Length);
                        unsafe
                        {
                            var array = (float *)buffer.Header.ToPointer();
                            for (int i = 0; i < xy_vertices.Length; i++)
                            {
                                array[i] = xy_vertices[i] * this.halfExtent;
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else if (bufferName == strNormal)
            {
                if (normalBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <float>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(xy_normals.Length);
                        unsafe
                        {
                            var array = (float *)buffer.Header.ToPointer();
                            for (int i = 0; i < xy_normals.Length; i++)
                            {
                                array[i] = xy_normals[i];
                            }
                        }

                        normalBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(normalBufferPtr);
            }
            else if (bufferName == strTangent)
            {
                if (tangentBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <float>(
                               varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(xy_tangents.Length);
                        unsafe
                        {
                            var array = (float *)buffer.Header.ToPointer();
                            for (int i = 0; i < xy_tangents.Length; i++)
                            {
                                array[i] = xy_tangents[i];
                            }
                        }

                        tangentBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(tangentBufferPtr);
            }
            else if (bufferName == strTexCoord)
            {
                if (texCoordBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <float>(
                               varNameInShader, VertexAttributeConfig.Vec2, BufferUsage.StaticDraw))
                    {
                        buffer.Create(xy_texCoords.Length);
                        unsafe
                        {
                            var array = (float *)buffer.Header.ToPointer();
                            for (int i = 0; i < xy_texCoords.Length; i++)
                            {
                                array[i] = xy_texCoords[i];
                            }
                        }

                        texCoordBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(texCoordBufferPtr);
            }
            else
            {
                throw new ArgumentException();
            }
        }
예제 #25
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bufferName"></param>
        /// <param name="varNameInShader"></param>
        /// <returns></returns>
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(TetrahedronModel.position.Length);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < TetrahedronModel.position.Length; i++)
                            {
                                array[i] = TetrahedronModel.position[i];
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else if (bufferName == strColor)
            {
                if (colorBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(TetrahedronModel.color.Length);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < TetrahedronModel.color.Length; i++)
                            {
                                array[i] = TetrahedronModel.color[i];
                            }
                        }

                        colorBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(colorBufferPtr);
            }
            else if (bufferName == strNormal)
            {
                if (normalBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec3>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(TetrahedronModel.normal.Length);
                        unsafe
                        {
                            var array = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < TetrahedronModel.normal.Length; i++)
                            {
                                array[i] = TetrahedronModel.normal[i];
                            }
                        }

                        normalBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(normalBufferPtr);
            }
            else
            {
                return(null);
            }
        }
예제 #26
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bufferName"></param>
        /// <param name="varNameInShader"></param>
        /// <returns></returns>
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <CubeModel.CubePosition>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(1);
                        unsafe
                        {
                            var positionArray = (CubeModel.CubePosition *)buffer.Header.ToPointer();
                            positionArray[0] = CubeModel.position;
                        }
                        unsafe
                        {
                            var positionArray = (vec3 *)buffer.Header.ToPointer();
                            for (int i = 0; i < 24; i++)
                            {
                                positionArray[i] = positionArray[i] / 2 * Lengths;
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(positionBufferPtr);
            }
            else if (bufferName == strColor)
            {
                if (colorBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <CubeModel.CubeColor>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(1);
                        unsafe
                        {
                            var colorArray = (CubeModel.CubeColor *)buffer.Header.ToPointer();
                            colorArray[0] = CubeModel.color;
                        }

                        colorBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(colorBufferPtr);
            }
            else if (bufferName == strNormal)
            {
                if (normalBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <CubeModel.CubeNormal>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
                    {
                        buffer.Create(1);
                        unsafe
                        {
                            var normalArray = (CubeModel.CubeNormal *)buffer.Header.ToPointer();
                            normalArray[0] = CubeModel.normal;
                        }

                        normalBufferPtr = buffer.GetBufferPtr();
                    }
                }
                return(normalBufferPtr);
            }
            else
            {
                return(null);
            }
        }
예제 #27
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="bufferName"></param>
 /// <param name="varNameInShader"></param>
 /// <returns></returns>
 public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
 {
     if (bufferName == strPosition)
     {
         if (positionBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 float[] positions = model.GetPositions();
                 //List<vec3> pos = new List<vec3>();
                 //for (int i = 0; i < positions.Length; i += 3)
                 //{
                 //    pos.Add(new vec3(positions[i], positions[i + 1], positions[i + 2]));
                 //}
                 //IBoundingBox box = pos.Move2Center();
                 //vec3 lengths = box.MaxPosition - box.MinPosition;
                 buffer.Create(positions.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < positions.Length; i++)
                     {
                         array[i] = positions[i];
                     }
                 }
                 positionBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(positionBufferPtr);
     }
     else if (bufferName == strColor)
     {
         if (colorBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 float[] normals = model.GetNormals();
                 buffer.Create(normals.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < normals.Length; i++)
                     {
                         array[i] = normals[i];
                     }
                 }
                 colorBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(colorBufferPtr);
     }
     else if (bufferName == strNormal)
     {
         if (normalBufferPtr == null)
         {
             using (var buffer = new VertexAttributeBuffer <float>(varNameInShader, VertexAttributeConfig.Vec3, BufferUsage.StaticDraw))
             {
                 float[] normals = model.GetNormals();
                 buffer.Create(normals.Length);
                 unsafe
                 {
                     var array = (float *)buffer.Header.ToPointer();
                     for (int i = 0; i < normals.Length; i++)
                     {
                         array[i] = normals[i];
                     }
                 }
                 normalBufferPtr = buffer.GetBufferPtr();
             }
         }
         return(normalBufferPtr);
     }
     else
     {
         return(null);
     }
 }
예제 #28
0
        public VertexAttributeBufferPtr GetVertexAttributeBufferPtr(string bufferName, string varNameInShader)
        {
            if (bufferName == strPosition)
            {
                if (positionBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec4>(
                               varNameInShader, VertexAttributeConfig.Vec4, BufferUsage.DynamicCopy))
                    {
                        buffer.Create(particleCount);
                        unsafe
                        {
                            var array = (vec4 *)buffer.Header.ToPointer();
                            for (int i = 0; i < particleCount; i++)
                            {
                                array[i] = new vec4(
                                    (float)(random.NextDouble() - 0.5) * 20,
                                    (float)(random.NextDouble() - 0.5) * 20,
                                    (float)(random.NextDouble() - 0.5) * 20,
                                    (float)(random.NextDouble())
                                    );
                            }
                        }

                        positionBufferPtr = buffer.GetBufferPtr();
                    }
                }

                return(positionBufferPtr);
            }
            else if (bufferName == strVelocity)
            {
                if (velocityBufferPtr == null)
                {
                    using (var buffer = new VertexAttributeBuffer <vec4>(
                               varNameInShader, VertexAttributeConfig.Vec4, BufferUsage.DynamicCopy))
                    {
                        buffer.Create(particleCount);
                        unsafe
                        {
                            var array = (vec4 *)buffer.Header.ToPointer();
                            for (int i = 0; i < particleCount; i++)
                            {
                                array[i] = new vec4(
                                    (float)(random.NextDouble() - 0.5) * 0.2f,
                                    (float)(random.NextDouble() - 0.5) * 0.2f,
                                    (float)(random.NextDouble() - 0.5) * 0.2f,
                                    0
                                    );
                            }
                        }

                        velocityBufferPtr = buffer.GetBufferPtr();
                    }
                }

                return(velocityBufferPtr);
            }
            else
            {
                throw new ArgumentException();
            }
        }