private Framebuffer CreateFramebuffer(int width, int height) { //Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, GL.GL_RGBA); //Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24); var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); //framebuffer.Attach(colorBuffer);//0 //this.BindingTexture = framebuffer.Attach(TextureAttachment.DepthAttachment);//1 var texture = new Texture(new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_DEPTH_COMPONENT32, width, height, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT), // 设置默认滤波模式 new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR), new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR), // 设置深度比较模式 new TexParameteri(TexParameter.PropertyName.TextureCompareMode, (int)GL.GL_COMPARE_REF_TO_TEXTURE), new TexParameteri(TexParameter.PropertyName.TextureCompareFunc, (int)GL.GL_LEQUAL), // 设置边界截取模式 new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE), new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE)); texture.Initialize(); framebuffer.Attach(FramebufferTarget.Framebuffer, texture, AttachmentLocation.Depth); this.BindingTexture = texture; //framebuffer.Attach(depthBuffer);// special //framebuffer.SetDrawBuffers(GL.GL_COLOR_ATTACHMENT0 + 1);// as in 1 in framebuffer.Attach(texture);//1 //framebuffer.SetDrawBuffers(GL.GL_NONE); framebuffer.SetDrawBuffer(GL.GL_NONE); //framebuffer.SetReadBuffer(GL.GL_NONE); this.BindingTexture.Bind(); framebuffer.CheckCompleteness(); this.BindingTexture.Unbind(); framebuffer.Unbind(); return(framebuffer); }
private Framebuffer CreateFramebuffer(int width, int height) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); { var texture = new Texture(new TexImageBitmap(width, height), new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_REPEAT), new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_REPEAT), new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_REPEAT), new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR), new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR)); texture.Initialize(); framebuffer.Attach(FramebufferTarget.Framebuffer, texture, 0u);// 1 this.BindingTexture = texture; } { var renderbuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Depth);// special } //framebuffer.SetDrawBuffer(GL.GL_COLOR_ATTACHMENT0 + 1);// as in 1 in framebuffer.Attach(FramebufferTarget.Framebuffer, texture, 1u);// 1 framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }
private Framebuffer CreateFramebuffer(int width, int height) { var texture = new Texture(TextureTarget.Texture2D, new NullImageFiller(width, height, GL.GL_RGBA, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE), new SamplerParameters( TextureWrapping.Repeat, TextureWrapping.Repeat, TextureWrapping.Repeat, TextureFilter.Linear, TextureFilter.Linear)); texture.Initialize(); this.BindingTexture = texture; Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, GL.GL_RGBA); Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24); var framebuffer = new Framebuffer(); framebuffer.Bind(); framebuffer.Attach(colorBuffer); //0 framebuffer.Attach(texture); //1 framebuffer.Attach(depthBuffer); // special framebuffer.SetDrawBuffers(GL.GL_COLOR_ATTACHMENT0 + 1); // as in 1 in framebuffer.Attach(texture);//1 framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }
/// <summary> /// Pick geometry at specified positon. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="geometryTypes"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public PickedGeometry Pick(int x, int y, PickingGeometryTypes geometryTypes, int width, int height) { if (x < 0 || width <= x) { return(null); } if (y < 0 || height <= y) { return(null); } if (geometryTypes == 0) { return(null); } PickedGeometry pickedGeometry = null; Framebuffer framebuffer = GetPickingFramebuffer(width, height); 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.RootNode, arg); bool dump = false; if (dump) { var final = new Bitmap(width, height); var data = final.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //glGetTexImage((uint)texture.Target, 0, GL_BGRA, GL_UNSIGNED_BYTE, data.Scan0); GL.Instance.ReadPixels(0, 0, width, height, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, data.Scan0); final.UnlockBits(data); final.RotateFlip(RotateFlipType.Rotate180FlipX); final.Save(string.Format("picking.dump.png")); } uint stageVertexId = ColorCodedPicking.ReadStageVertexId(x, y); pickedGeometry = SearchGeometry(stageVertexId, arg, this.Scene.RootNode); 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); }
/// <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) { 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); }
private Framebuffer CreatePickFramebuffer(int width, int height) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); framebuffer.Attach(RenderbufferType.ColorBuffer); framebuffer.Attach(RenderbufferType.DepthBuffer); framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }
private Framebuffer CreatePickFramebuffer(int width, int height) { Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, GL.GL_RGBA); Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24); var framebuffer = new Framebuffer(); framebuffer.Bind(); framebuffer.Attach(colorBuffer); framebuffer.Attach(depthBuffer); framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }
private Framebuffer CreateFramebuffer(int width, int height) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); Renderbuffer colorbuffer = framebuffer.Attach(RenderbufferType.ColorBuffer); //0 Texture texture = framebuffer.Attach(TextureAttachment.ColorAttachment); //1 Renderbuffer depthbuffer = framebuffer.Attach(RenderbufferType.DepthBuffer); // special framebuffer.SetDrawBuffer(GL.GL_COLOR_ATTACHMENT0 + 1); // as in 1 in framebuffer.Attach(TextureAttachment.ColorAttachment);//1 framebuffer.CheckCompleteness(); framebuffer.Unbind(); this.BindingTexture = texture; return(framebuffer); }
private static Framebuffer CreateFramebuffer(int width, int height) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); { var renderbuffer = new Renderbuffer(width, height, GL.GL_RGBA, RenderbufferType.ColorBuffer); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, 0u);// 0 } { var renderbuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24, RenderbufferType.DepthBuffer); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Depth);// special } framebuffer.CheckCompleteness(); framebuffer.Unbind(); return framebuffer; }
/// <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) { 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); } framebuffer.Unbind(); return(pickedGeometry); }
private Framebuffer CreatePickFramebuffer(int width, int height) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); { var renderbuffer = new Renderbuffer(width, height, GL.GL_RGBA); // attach the renderbufer as first color buffer attachment. const uint colorAttachmentLocation = 0; framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, colorAttachmentLocation); } { var renderbuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Depth);// special } framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }
private Framebuffer CreateFramebuffer(int width, int height) { //Renderbuffer colorBuffer = Renderbuffer.CreateColorbuffer(width, height, GL.GL_RGBA); //Renderbuffer depthBuffer = Renderbuffer.CreateDepthbuffer(width, height, DepthComponentType.DepthComponent24); var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); //framebuffer.Attach(colorBuffer);//0 this.BindingTexture = framebuffer.Attach(TextureAttachment.DepthAttachment);//1 //framebuffer.Attach(depthBuffer);// special //framebuffer.SetDrawBuffers(GL.GL_COLOR_ATTACHMENT0 + 1);// as in 1 in framebuffer.Attach(texture);//1 //framebuffer.SetDrawBuffers(GL.GL_NONE); framebuffer.SetDrawBuffer(GL.GL_NONE); //framebuffer.SetReadBuffer(GL.GL_NONE); this.BindingTexture.Bind(); framebuffer.CheckCompleteness(); this.BindingTexture.Unbind(); framebuffer.Unbind(); return(framebuffer); }
/// <summary> /// Pick geometry at specified positon. /// </summary> /// <param name="x">Left Down is (0, 0)</param> /// <param name="y">Left Down is (0, 0)</param> /// <param name="geometryType"></param> /// <returns></returns> public PickedGeometry Pick(int x, int y, GeometryType geometryType) { 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, geometryType.ToFlags()); this.RenderForPicking(this.Scene.RootElement, arg); uint stageVertexId = ColorCodedPicking.ReadStageVertexId(x, y); pickedGeometry = Pick(stageVertexId, arg, this.Scene.RootElement); } framebuffer.Unbind(); return(pickedGeometry); }
private static Framebuffer CreateFramebuffer(int width, int height, ContextGenerationParams parameters) { var framebuffer = new Framebuffer(width, height); framebuffer.Bind(); { var renderbuffer = new Renderbuffer(width, height, GL.GL_RGBA); uint colorAttachmentLocation = 0; framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, colorAttachmentLocation);// 0 } { var renderbuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Depth);// special } if (parameters.StencilBits > 0) { var renderbuffer = new Renderbuffer(width, height, GL.GL_STENCIL_INDEX8); framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Stencil); } framebuffer.CheckCompleteness(); framebuffer.Unbind(); return(framebuffer); }