/// <summary>
        /// 设置要高亮显示的图元。
        /// </summary>
        /// <param name="mode">要高亮显示的图元类型</param>
        /// <param name="indexes">要高亮显示的图元的索引。</param>
        public void SetHighlightIndexes(DrawMode mode, params uint[] indexes)
        {
            int indexesLength = indexes.Length;

            if (indexesLength > this.maxElementCount)
            {
                IndexBufferPtr original = this.indexBufferPtr;
                using (var buffer = new OneIndexBuffer(IndexElementType.UInt,
                                                       mode, BufferUsage.DynamicDraw))
                {
                    buffer.Create(indexesLength);
                    this.indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
                }
                this.maxElementCount = indexesLength;
                original.Dispose();
            }

            var    indexBufferPtr = this.indexBufferPtr as OneIndexBufferPtr;
            IntPtr pointer        = indexBufferPtr.MapBuffer(MapBufferAccess.WriteOnly);

            unsafe
            {
                var array = (uint *)pointer.ToPointer();
                for (int i = 0; i < indexesLength; i++)
                {
                    array[i] = indexes[i];
                }
            }
            indexBufferPtr.UnmapBuffer();

            indexBufferPtr.Mode         = mode;
            indexBufferPtr.ElementCount = indexesLength;
        }
Esempio n. 2
0
        private void Dispose(bool disposing)
        {
            if (this.disposedValue == false)
            {
                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unmanaged resources.
                uint[] arrays = new uint[] { this.ID };
                this.ID = 0;
                IntPtr ptr = Win32.wglGetCurrentContext();
                if (ptr != IntPtr.Zero)
                {
                    glDeleteVertexArrays(1, new uint[] { this.ID });
                }
                {
                    BufferPtr[] propertyBufferPtrs = this.propertyBufferPtrs;
                    foreach (var item in propertyBufferPtrs)
                    {
                        item.Dispose();
                    }
                }
                {
                    IndexBufferPtr indexBufferPtr = this.indexBufferPtr;
                    indexBufferPtr.Dispose();
                }
            }

            this.disposedValue = true;
        }
Esempio n. 3
0
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new ZeroIndexBuffer(
                    DrawMode.LineStrip, 0, BigDipperModel.positions.Length))
                {
                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
                //using (var buffer = new OneIndexBuffer<uint>(
                //    DrawMode.Lines, BufferUsage.StaticDraw))
                //{
                //    buffer.Alloc(BigDipperModel.positions.Length);
                //    unsafe
                //    {
                //        var array = (uint*)buffer.Header.ToPointer();
                //        for (uint i = 0; i < BigDipperModel.positions.Length; i++)
                //        {
                //            array[i] = i;
                //        }
                //        array[0] = uint.MaxValue;
                //        array[4] = uint.MaxValue;
                //        array[10] = uint.MaxValue;
                //    }
                //    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                //}
            }

            return indexBufferPtr;
        }
        /// <summary>
        /// 根据<see cref="IndexBufferPtr"/>的具体类型获取一个<see cref="PickableRenderer"/>
        /// </summary>
        /// <param name="bufferable"></param>
        /// <param name="propertyNameMap"></param>
        /// <param name="positionNameInIBufferable"></param>
        /// <param name="switches"></param>
        /// <returns></returns>
        public static InnerPickableRenderer GetRenderer(
            this IBufferable bufferable,
            PropertyNameMap propertyNameMap,
            string positionNameInIBufferable,
            params GLSwitch[] switches)
        {
            if (bufferable == null || propertyNameMap == null || string.IsNullOrEmpty(positionNameInIBufferable))
            {
                throw new ArgumentNullException();
            }

            IndexBufferPtr indexBufferPtr = bufferable.GetIndex();

            if (indexBufferPtr is ZeroIndexBufferPtr)
            {
                return(new ZeroIndexRenderer(bufferable, PickingShaderHelper.GetShaderCodes(), propertyNameMap, positionNameInIBufferable, switches));
            }
            else if (indexBufferPtr is OneIndexBufferPtr)
            {
                return(new OneIndexRenderer(bufferable, PickingShaderHelper.GetShaderCodes(), propertyNameMap, positionNameInIBufferable, switches));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Esempio n. 5
0
        private void Dispose(bool disposing)
        {
            if (this.disposedValue == false)
            {
                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unmanaged resources.
                uint[] arrays = new uint[] { this.ID };
                this.ID = 0;
                IntPtr ptr = Win32.wglGetCurrentContext();
                if (ptr != IntPtr.Zero)
                {
                    GL.GetDelegateFor <GL.glDeleteVertexArrays>()(1, new uint[] { this.ID });
                }
                foreach (var item in this.bufferRenderers)
                {
                    item.Dispose();
                }
                this.indexBufferRenderer.Dispose();
                this.bufferRenderers     = null;
                this.indexBufferRenderer = null;
            }

            this.disposedValue = true;
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="indexBufferPtr"></param>
        public FormIndexBufferPtrBoard(IndexBufferPtr indexBufferPtr = null)
        {
            InitializeComponent();

            if (indexBufferPtr != null)
            {
                this.SetTarget(indexBufferPtr);
            }
        }
Esempio n. 7
0
        private void ColorCodedRender(RenderEventArgs arg, IndexBufferPtr temporaryIndexBufferPtr = null)
        {
            UpdatePolygonMode(arg.PickingGeometryType);

            ShaderProgram program = this.Program;

            // 绑定shader
            program.Bind();
            program.SetUniform("pickingBaseId",
                               temporaryIndexBufferPtr == null ? (int)this.PickingBaseId : 0);
            UniformMat4 uniformmMVP4Picking = this.uniformmMVP4Picking;
            bool        mvpUpdated          = uniformmMVP4Picking.Updated;

            if (mvpUpdated)
            {
                uniformmMVP4Picking.SetUniform(program);
            }

            PickingSwitchesOn();
            GLSwitch primitiveRestartIndexSwitch = null;
            var      oneIndexBufferPtr           = temporaryIndexBufferPtr as OneIndexBufferPtr;

            if (oneIndexBufferPtr != null)
            {
                primitiveRestartIndexSwitch = new PrimitiveRestartSwitch(oneIndexBufferPtr);
                primitiveRestartIndexSwitch.On();
            }

            if (this.vertexArrayObject == null)
            {
                var vertexArrayObject = new VertexArrayObject(
                    this.indexBufferPtr, this.positionBufferPtr);
                vertexArrayObject.Create(arg, program);

                this.vertexArrayObject = vertexArrayObject;
            }
            //else
            {
                this.vertexArrayObject.Render(arg, program, temporaryIndexBufferPtr);
            }

            if (oneIndexBufferPtr != null)
            {
                primitiveRestartIndexSwitch.Off();
            }
            PickingSwitchesOff();

            if (mvpUpdated)
            {
                uniformmMVP4Picking.ResetUniform(program);
            }


            // 解绑shader
            program.Unbind();
        }
Esempio n. 8
0
 public IndexBufferPtr GetIndex()
 {
     if (this.indexBufferPtr == null)
     {
         using (var buffer = new ZeroIndexBuffer(DrawMode.Lines, 0, codedColors.Length * 2 + 4))
         {
             this.indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
         }
     }
     return this.indexBufferPtr;
 }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new ZeroIndexBuffer(this.model.GetDrawModel(), 0, this.model.positions.Length))
                {
                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
Esempio n. 10
0
        //private Stack<UniformArrayVariable> uniformArrayVariableStack = new Stack<UniformArrayVariable>();
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        protected override void DoRender(RenderEventArgs arg)
        {
            ShaderProgram program = this.Program;

            if (program == null)
            {
                return;
            }

            // 绑定shader
            program.Bind();

            SetUniformValues(program);

            SwitchesOn();

            IndexBufferPtr indexBufferPtr = this.indexBufferPtr;

            if (this.vertexArrayObject == null)
            {
                PropertyBufferPtr[] propertyBufferPtrs = this.propertyBufferPtrs;
                if (indexBufferPtr != null && propertyBufferPtrs != null)
                {
                    var vertexArrayObject = new VertexArrayObject(
                        indexBufferPtr, propertyBufferPtrs);
                    vertexArrayObject.Create(arg, program);

                    this.vertexArrayObject = vertexArrayObject;
                }
            }
            {
                VertexArrayObject vertexArrayObject = this.vertexArrayObject;
                if (vertexArrayObject != null)
                {
                    if (vertexArrayObject.IndexBufferPtr != indexBufferPtr)
                    {
                        vertexArrayObject.Render(arg, program, indexBufferPtr);
                    }
                    else
                    {
                        vertexArrayObject.Render(arg, program);
                    }
                }
            }

            SwithesOff();

            ResetUniformValues(program);

            // 解绑shader
            program.Unbind();
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new ZeroIndexBuffer(
                           DrawMode.QuadStrip, 0, positions.Length))
                {
                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
Esempio n. 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="indexBufferPtr"></param>
        public void SetTarget(IndexBufferPtr indexBufferPtr)
        {
            if (indexBufferPtr == null)
            {
                throw new ArgumentNullException();
            }

            this.controller = indexBufferPtr.CreateController();

            UpdateUI(this.controller);

            this.Text = string.Format("{0}", this.controller);
        }
Esempio n. 13
0
        /// <summary>
        /// 一个vertex array object。(即VAO)
        /// <para>VAO是用来管理VBO的。可以进一步减少DrawCall。</para>
        /// </summary>
        /// <param name="propertyBufferRenderers">给出此VAO要管理的所有VBO。</param>
        public VertexArrayObject(IndexBufferPtr indexBufferRenderer, params VertexBufferPtr[] propertyBufferRenderers)
        {
            if (indexBufferRenderer == null)
            {
                throw new ArgumentNullException("indexBufferRenderer");
            }
            if (propertyBufferRenderers == null || propertyBufferRenderers.Length == 0)
            {
                throw new ArgumentNullException("propertyBuffers");
            }

            this.indexBufferRenderer = indexBufferRenderer;
            this.bufferRenderers     = propertyBufferRenderers;
        }
Esempio n. 14
0
        /// <summary>
        /// VAO是用来管理VBO的。可以进一步减少DrawCall。
        /// <para>VAO is used to reduce draw-call.</para>
        /// </summary>
        /// <param name="indexBufferPtr">index buffer pointer that used to invoke draw command.</param>
        /// <param name="vertexAttributeBufferPtrs">给出此VAO要管理的所有VBO。<para>All VBOs that are managed by this VAO.</para></param>
        public VertexArrayObject(IndexBufferPtr indexBufferPtr, params VertexAttributeBufferPtr[] vertexAttributeBufferPtrs)
        {
            if (indexBufferPtr == null)
            {
                throw new ArgumentNullException("indexBufferPtr");
            }
            // Zero vertex attribute is allowed in GLSL.
            //if (vertexAttributeBufferPtrs == null || vertexAttributeBufferPtrs.Length == 0)
            //{
            //    throw new ArgumentNullException("vertexAttributeBufferPtrs");
            //}

            this.IndexBufferPtr            = indexBufferPtr;
            this.VertexAttributeBufferPtrs = vertexAttributeBufferPtrs;
        }
Esempio n. 15
0
 /// <summary>
 /// 执行一次渲染的过程。
 /// <para>Execute rendering command.</para>
 /// </summary>
 /// <param name="arg"></param>
 /// <param name="shaderProgram"></param>
 /// <param name="temporaryIndexBufferPtr">render by a temporary index buffer</param>
 public void Render(RenderEventArgs arg, ShaderProgram shaderProgram, IndexBufferPtr temporaryIndexBufferPtr = null)
 {
     if (temporaryIndexBufferPtr != null)
     {
         this.Bind();
         temporaryIndexBufferPtr.Render(arg);
         this.Unbind();
     }
     else
     {
         this.Bind();
         this.IndexBufferPtr.Render(arg);
         this.Unbind();
     }
 }
Esempio n. 16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="indexBufferPtr"></param>
        public FormIndexBufferPtrBoard(IndexBufferPtr indexBufferPtr = null)
        {
            InitializeComponent();

            this.cmbDrawMode.Items.Clear();
            foreach (var item in Enum.GetValues(typeof(CSharpGL.DrawMode)))
            {
                this.cmbDrawMode.Items.Add((CSharpGL.DrawMode)item);
            }

            if (indexBufferPtr != null)
            {
                this.SetTarget(indexBufferPtr);
            }
        }
Esempio n. 17
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            // init shader program.
            ShaderProgram program = this.shaderCodes.CreateProgram();

            // init vertex attribute buffer objects.
            IBufferable model = this.Model;

            VertexAttributeBufferPtr[] vertexAttributeBufferPtrs;
            {
                var list = new List <VertexAttributeBufferPtr>();
                foreach (var item in this.attributeMap)
                {
                    VertexAttributeBufferPtr bufferPtr = model.GetVertexAttributeBufferPtr(
                        item.NameInIBufferable, item.VarNameInShader);
                    if (bufferPtr == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }
                    list.Add(bufferPtr);
                }
                vertexAttributeBufferPtrs = list.ToArray();
            }

            // init index buffer.
            IndexBufferPtr indexBufferPtr = model.GetIndexBufferPtr();

            // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care this rule when designing a model's index buffer.
            var ptr = indexBufferPtr as OneIndexBufferPtr;

            if (ptr != null)
            {
                GLSwitch glSwitch = new PrimitiveRestartSwitch(ptr);
                this.switchList.Add(glSwitch);
            }

            // init VAO.
            var vertexArrayObject = new VertexArrayObject(indexBufferPtr, vertexAttributeBufferPtrs);

            vertexArrayObject.Initialize(program);

            // sets fields.
            this.Program = program;
            this.vertexAttributeBufferPtrs = vertexAttributeBufferPtrs;
            this.indexBufferPtr            = indexBufferPtr;
            this.vertexArrayObject         = vertexArrayObject;
        }
        private void ColorCodedRender(RenderEventArgs arg, IndexBufferPtr temporaryIndexBufferPtr = null)
        {
            UpdatePolygonMode(arg.PickingGeometryType);

            ShaderProgram program = this.Program;

            // 绑定shader
            program.Bind();
            program.SetUniform("pickingBaseId",
                               temporaryIndexBufferPtr == null ? (int)this.PickingBaseId : 0);
            UniformMat4 uniformmMVP4Picking = this.uniformmMVP4Picking;

            {
                mat4 projection = arg.Camera.GetProjectionMatrix();
                mat4 view       = arg.Camera.GetViewMatrix();
                mat4 model      = this.GetModelMatrix();
                uniformmMVP4Picking.Value = projection * view * model;
            }
            uniformmMVP4Picking.SetUniform(program);

            PickingSwitchesOn();
            GLSwitch primitiveRestartIndexSwitch = null;
            var      oneIndexBufferPtr           = temporaryIndexBufferPtr as OneIndexBufferPtr;

            if (oneIndexBufferPtr != null)
            {
                primitiveRestartIndexSwitch = new PrimitiveRestartSwitch(oneIndexBufferPtr);
                primitiveRestartIndexSwitch.On();
            }

            {
                this.vertexArrayObject.Render(arg, program, temporaryIndexBufferPtr);
            }

            if (oneIndexBufferPtr != null)
            {
                primitiveRestartIndexSwitch.Off();
            }
            PickingSwitchesOff();

            //if (mvpUpdated) { uniformmMVP4Picking.ResetUniform(program); }

            // 解绑shader
            program.Unbind();
        }
Esempio n. 19
0
 public static IndexBufferPtrController CreateController(this IndexBufferPtr indexBufferPtr)
 {
     {
         var ptr = indexBufferPtr as ZeroIndexBufferPtr;
         if (ptr != null)
         {
             return(new ZeroIndexBufferPtrController(ptr));
         }
     }
     {
         var ptr = indexBufferPtr as OneIndexBufferPtr;
         if (ptr != null)
         {
             return(new OneIndexBufferPtrController(ptr));
         }
     }
     {
         throw new NotImplementedException();
     }
 }
Esempio n. 20
0
        /// <summary>
        /// 一个vertex array object。(即VAO)
        /// <para>VAO是用来管理VBO的。可以进一步减少DrawCall。</para>
        /// </summary>
        /// <param name="propertyBufferPtrs">给出此VAO要管理的所有VBO。</param>
        public VertexArrayObject(IndexBufferPtr indexBufferPtr, params BufferPtr[] propertyBufferPtrs)
        {
            if (indexBufferPtr == null)
            {
                throw new ArgumentNullException("indexBufferRenderer");
            }
            if (propertyBufferPtrs == null || propertyBufferPtrs.Length == 0)
            {
                throw new ArgumentNullException("propertyBuffers");
            }

            if (glGenVertexArrays == null)
            {
                glGenVertexArrays    = OpenGL.GetDelegateFor <OpenGL.glGenVertexArrays>();
                glBindVertexArray    = OpenGL.GetDelegateFor <OpenGL.glBindVertexArray>();
                glDeleteVertexArrays = OpenGL.GetDelegateFor <OpenGL.glDeleteVertexArrays>();
            }

            this.indexBufferPtr     = indexBufferPtr;
            this.propertyBufferPtrs = propertyBufferPtrs;
        }
Esempio n. 21
0
        /// <summary>
        /// 一个vertex array object。(即VAO)
        /// <para>VAO是用来管理VBO的。可以进一步减少DrawCall。</para>
        /// </summary>
        /// <param name="propertyBufferPtrs">给出此VAO要管理的所有VBO。</param>
        public VertexArrayObject(IndexBufferPtr indexBufferPtr, params BufferPtr[] propertyBufferPtrs)
        {
            if (indexBufferPtr == null)
            {
                throw new ArgumentNullException("indexBufferRenderer");
            }
            if (propertyBufferPtrs == null || propertyBufferPtrs.Length == 0)
            {
                throw new ArgumentNullException("propertyBuffers");
            }

            if (glGenVertexArrays == null)
            {
                glGenVertexArrays = OpenGL.GetDelegateFor<OpenGL.glGenVertexArrays>();
                glBindVertexArray = OpenGL.GetDelegateFor<OpenGL.glBindVertexArray>();
                glDeleteVertexArrays = OpenGL.GetDelegateFor<OpenGL.glDeleteVertexArrays>();
            }

            this.IndexBufferPtr = indexBufferPtr;
            this.propertyBufferPtrs = propertyBufferPtrs;
        }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndexBufferPtr()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer(IndexElementType.UByte, DrawMode.Quads, BufferUsage.StaticDraw))
                {
                    buffer.Create(indexes.Length);
                    unsafe
                    {
                        var array = (byte *)buffer.Header.ToPointer();
                        for (int i = 0; i < indexes.Length; i++)
                        {
                            array[i] = indexes[i];
                        }
                    }
                    indexBufferPtr = buffer.GetBufferPtr();
                }
            }

            return(indexBufferPtr);
        }
        /// <summary>
        /// 在此Buffer中的图元进行N选1
        /// select a line from triangle/quad/polygon in this renderer.
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="indexBufferPtr">indicates the primitive to pick a line from.</param>
        internal void Render4InnerPicking(RenderEventArgs arg, IndexBufferPtr indexBufferPtr)
        {
            // record clear color
            var originalClearColor = new float[4];

            OpenGL.GetFloat(GetTarget.ColorClearValue, originalClearColor);

            // 白色意味着没有拾取到任何对象
            // white color: nothing picked.
            OpenGL.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            OpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT);

            // restore clear color
            OpenGL.ClearColor(originalClearColor[0], originalClearColor[1], originalClearColor[2], originalClearColor[3]);

            this.ColorCodedRender(arg, indexBufferPtr);

            OpenGL.Flush();

            //var filename = string.Format("Render4InnerPicking{0:yyyy-MM-dd_HH-mm-ss.ff}.png", DateTime.Now);
            //Save2PictureHelper.Save2Picture(0, 0,
            //    e.CanvasRect.Width, e.CanvasRect.Height, filename);
        }
Esempio n. 24
0
        private void Dispose(bool disposing)
        {
            if (this.disposedValue == false)
            {
                if (disposing)
                {
                    // Dispose managed resources.
                }

                // Dispose unmanaged resources.
                IntPtr ptr = Win32.wglGetCurrentContext();
                if (ptr != IntPtr.Zero)
                {
                    {
                        glDeleteVertexArrays(1, this.ids);
                        this.ids[0] = 0;
                    }
                    {
                        VertexAttributeBufferPtr[] vertexAttributeBufferPtrs = this.VertexAttributeBufferPtrs;
                        if (vertexAttributeBufferPtrs != null)
                        {
                            foreach (var item in vertexAttributeBufferPtrs)
                            {
                                item.Dispose();
                            }
                        }
                    }
                    {
                        IndexBufferPtr indexBufferPtr = this.IndexBufferPtr;
                        indexBufferPtr.Dispose();
                    }
                }
            }

            this.disposedValue = true;
        }
Esempio n. 25
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                if (model.positions.Length < byte.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer <byte>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Create(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (byte *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                {
                                    indexArray[i] = byte.MaxValue;
                                }
                                else
                                {
                                    indexArray[i] = (byte)model.indexes[i];
                                }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else if (model.positions.Length < ushort.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer <ushort>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Create(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (ushort *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                {
                                    indexArray[i] = ushort.MaxValue;
                                }
                                else
                                {
                                    indexArray[i] = (ushort)model.indexes[i];
                                }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else
                {
                    using (var buffer = new OneIndexBuffer <uint>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Create(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (uint *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                indexArray[i] = model.indexes[i];
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
            }

            return(indexBufferPtr);
        }
Esempio n. 26
0
 /// <summary>
 /// 执行一次渲染的过程。
 /// </summary>
 /// <param name="arg"></param>
 /// <param name="shaderProgram"></param>
 /// <param name="temporaryIndexBufferPtr">render by a temporary index buffer</param>
 public void Render(RenderEventArg arg, ShaderProgram shaderProgram, IndexBufferPtr temporaryIndexBufferPtr = null)
 {
     if (temporaryIndexBufferPtr != null)
     {
         this.Bind();
         temporaryIndexBufferPtr.Render(arg, shaderProgram);
         this.Unbind();
     }
     else
     {
         IndexBufferPtr indexBufferPtr = this.IndexBufferPtr;
         if (indexBufferPtr != null)
         {
             this.Bind();
             indexBufferPtr.Render(arg, shaderProgram);
             this.Unbind();
         }
     }
 }
        protected override void DoInitialize()
        {
            // init shader program.
            ShaderProgram program = this.shaderCodes.CreateProgram();

            VertexAttributeBufferPtr positionBufferPtr = null;
            IBufferable model = this.Model;

            VertexAttributeBufferPtr[] vertexAttributeBufferPtrs;
            {
                var list = new List <VertexAttributeBufferPtr>();
                foreach (var item in this.attributeMap)
                {
                    VertexAttributeBufferPtr bufferPtr = model.GetVertexAttributeBufferPtr(
                        item.NameInIBufferable, item.VarNameInShader);
                    if (bufferPtr == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }

                    if (item.NameInIBufferable == this.PositionNameInIBufferable)
                    {
                        positionBufferPtr = new VertexAttributeBufferPtr(
                            "in_Position",// in_Postion same with in the PickingShader.vert shader
                            bufferPtr.BufferId,
                            bufferPtr.Config,
                            bufferPtr.Length,
                            bufferPtr.ByteLength);
                        break;
                    }
                    list.Add(bufferPtr);
                }
                vertexAttributeBufferPtrs = list.ToArray();
            }

            // 由于picking.vert/frag只支持vec3的position buffer,所以有此硬性规定。
            if (positionBufferPtr == null || positionBufferPtr.Config != VertexAttributeConfig.Vec3)
            {
                throw new Exception(string.Format("Position buffer must use a type composed of 3 float as PropertyBuffer<T>'s T!"));
            }

            // init index buffer.
            IndexBufferPtr indexBufferPtr = model.GetIndexBufferPtr();

            // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care this rule when designing a model's index buffer.
            var ptr = indexBufferPtr as OneIndexBufferPtr;

            if (ptr != null)
            {
                GLSwitch glSwitch = new PrimitiveRestartSwitch(ptr);
                this.switchList.Add(glSwitch);
            }

            // init VAO.
            var vertexArrayObject = new VertexArrayObject(indexBufferPtr, positionBufferPtr);

            vertexArrayObject.Initialize(program);

            // sets fields.
            this.Program = program;
            this.vertexAttributeBufferPtrs = new VertexAttributeBufferPtr[] { positionBufferPtr };
            this.indexBufferPtr            = indexBufferPtr;
            this.vertexArrayObject         = vertexArrayObject;
        }
Esempio n. 28
0
        protected override void DoRender(RenderEventArgs arg)
        {
            ShaderProgram program = this.shaderProgram;

            if (program == null)
            {
                return;
            }

            // 绑定shader
            program.Bind();

            var updatedUniforms = (from item in this.uniformVariables where item.Updated select item).ToArray();

            foreach (var item in updatedUniforms)
            {
                item.SetUniform(program);
            }

            int count = this.switchList.Count;

            for (int i = 0; i < count; i++)
            {
                this.switchList[i].On();
            }

            IndexBufferPtr indexBufferPtr = this.indexBufferPtr;

            if (this.vertexArrayObject == null)
            {
                PropertyBufferPtr[] propertyBufferPtrs = this.propertyBufferPtrs;
                if (indexBufferPtr != null && propertyBufferPtrs != null)
                {
                    var vertexArrayObject = new VertexArrayObject(
                        indexBufferPtr, propertyBufferPtrs);
                    vertexArrayObject.Create(arg, program);

                    this.vertexArrayObject = vertexArrayObject;
                }
            }
            {
                VertexArrayObject vertexArrayObject = this.vertexArrayObject;
                if (vertexArrayObject != null)
                {
                    if (vertexArrayObject.IndexBufferPtr != indexBufferPtr)
                    {
                        vertexArrayObject.IndexBufferPtr = indexBufferPtr;
                    }
                    vertexArrayObject.Render(arg, program);
                }
            }

            for (int i = count - 1; i >= 0; i--)
            {
                this.switchList[i].Off();
            }

            foreach (var item in updatedUniforms)
            {
                item.ResetUniform(program);
            }

            // 解绑shader
            program.Unbind();
        }
Esempio n. 29
0
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                if (model.positions.Length < byte.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer<byte>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (byte*)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                { indexArray[i] = byte.MaxValue; }
                                else
                                { indexArray[i] = (byte)model.indexes[i]; }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else if (model.positions.Length < ushort.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer<ushort>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (ushort*)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                { indexArray[i] = ushort.MaxValue; }
                                else
                                { indexArray[i] = (ushort)model.indexes[i]; }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else
                {
                    using (var buffer = new OneIndexBuffer<uint>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (uint*)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                indexArray[i] = model.indexes[i];
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
            }

            return indexBufferPtr;
        }