Inheritance: IDisposable
Exemplo n.º 1
0
        public void swapColorAttachment(Framebuffer other, int index)
        {
            FramebufferTexture tmp = m_color[index];
            m_color[index] = other.m_color[index];
            other.m_color[index] = tmp;

            glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, framebufferId[0]);
            glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER,
                attachment_id[index], m_color[index].TextureTarget, m_color[index].TextureId, 0);
            glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, 0);

            glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, other.framebufferId[0]);
            glFramebufferTexture2D(OpenGL.GL_FRAMEBUFFER,
                attachment_id[index], other.m_color[index].TextureTarget, other.m_color[index].TextureId, 0);
            glBindFramebuffer(OpenGL.GL_FRAMEBUFFER, 0);

            ErrorCode error = (ErrorCode)OpenGL.GetError();
            if (error != ErrorCode.NoError)
            {
                throw new Exception(string.Format("OpenGL Error: {0}", error));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Pick geometry at specified positon.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="pickTriangle"></param>
        /// <param name="pickQuad"></param>
        /// <param name="pickPolygon"></param>
        /// <returns></returns>
        public PickedGeometry Pick(int x, int y, bool pickTriangle, bool pickQuad, bool pickPolygon)
        {
            int width = this.Scene.Canvas.Width, height = this.Scene.Canvas.Height;

            if (x < 0 || width <= x)
            {
                return(null);
            }
            if (y < 0 || height <= y)
            {
                return(null);
            }

            PickingGeometryTypes geometryTypes = 0;

            if (pickTriangle)
            {
                geometryTypes |= PickingGeometryTypes.Triangle;
            }
            if (pickQuad)
            {
                geometryTypes |= PickingGeometryTypes.Quad;
            }
            if (pickPolygon)
            {
                geometryTypes |= PickingGeometryTypes.Polygon;
            }
            if (geometryTypes == 0)
            {
                return(null);
            }

            PickedGeometry pickedGeometry = null;

            Framebuffer framebuffer = GetPickingFramebuffer();

            framebuffer.Bind();
            {
                const float one = 1.0f;
                GL.Instance.ClearColor(one, one, one, one);
                GL.Instance.Clear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);

                var arg = new PickingEventArgs(this.Scene, x, y, geometryTypes);
                this.RenderForPicking(this.Scene.RootElement, arg);

                uint stageVertexId = ColorCodedPicking.ReadStageVertexId(x, y);

                pickedGeometry = Pick(stageVertexId, arg, this.Scene.RootElement);

                if (pickedGeometry != null)
                {
                    var      depth  = new float[1];
                    GCHandle pinned = GCHandle.Alloc(depth, GCHandleType.Pinned);
                    IntPtr   header = pinned.AddrOfPinnedObject();
                    // same with: IntPtr header = Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
                    GL.Instance.ReadPixels(x, y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, header);
                    pinned.Free();

                    pickedGeometry.PickedPosition = new vec3(x, y, depth[0]);
                }
            }
            framebuffer.Unbind();

            return(pickedGeometry);
        }
Exemplo n.º 3
0
        private static Framebuffer CreateFramebuffer(int width, int height)
        {
            Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, OpenGL.GL_RGBA);
            Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24);
            var framebuffer = new Framebuffer();
            framebuffer.Bind();
            framebuffer.Attach(colorBuffer);
            framebuffer.Attach(depthBuffer);

            framebuffer.CheckCompleteness();

            return framebuffer;
        }
Exemplo n.º 4
0
        //static List<WeakReference<FBORenderContext>> renderContextList = new List<WeakReference<FBORenderContext>>();
        //public FBORenderContext()
        //{
        //    renderContextList.Add(new WeakReference<FBORenderContext>(this));
        //}
        //public override void Destroy()
        //{
        //    bool found = false;
        //    WeakReference<FBORenderContext> target = null;
        //    for (int index = 0; index < renderContextList.Count; index++)
        //    {
        //        target = renderContextList[index];
        //        FBORenderContext context;
        //        if (target.TryGetTarget(out context))
        //        {
        //            if (context == this)
        //            { break; }
        //        }
        //    }
        //    if (found)
        //    {
        //        renderContextList.Remove(target);
        //    }
        //    base.Destroy();
        //}
        //public static FBORenderContext GetCurrentRenderContext()
        //{
        //    IntPtr renderContext = Win32.wglGetCurrentContext();
        //}
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="bitDepth"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public override bool Create(int width, int height, int bitDepth, object parameter)
        {
            //  Call the base class.
            base.Create(width, height, bitDepth, parameter);

            // Create frame buffer object.
            Framebuffer framebuffer = CreateFramebuffer(width, height);
            this.framebuffer = framebuffer;

            //  Create the DIB section.
            var dibSection = new DIBSection();
            dibSection.Create(this.DeviceContextHandle, width, height, bitDepth);
            this.dibSection = dibSection;

            return true;
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public override void SetDimensions(int width, int height)
        {
            //  Call the base.
            base.SetDimensions(width, height);

            //	Resize dib section.
            this.dibSection.Resize(width, height, this.BitDepth);

            //  TODO: We should be able to just use the code below - however we
            //  get invalid dimension issues at the moment, so recreate for now.
            ////  Resize the render buffer storage.
            //this.framebuffer.Resize(width, height);

            this.framebuffer.Dispose();
            Framebuffer framebuffer = CreateFramebuffer(width, height);
            this.framebuffer = framebuffer;
        }