protected void Blit(GLTextuer2D output, int width, int height) { if (temp == null) { temp = new GLFrameBuffer(); } temp.Bind(); temp.BindRead(); temp.AttachColor(colorBuff, 0); IGL.Primary.ReadBuffer((int)ReadBufferMode.ColorAttachment0); temp.AttachColor(output, 1); IGL.Primary.DrawBuffers(new int[] { (int)DrawBufferMode.ColorAttachment1 }); if (!temp.IsValid) { Log.Error("Frame buff is invalid on blit:\r\n" + Environment.StackTrace); } IGL.Primary.BlitFramebuffer(0, 0, width, height, 0, 0, width, height, (int)ClearBufferMask.ColorBufferBit, (int)BlitFramebufferFilter.Linear); GLFrameBuffer.UnbindRead(); GLFrameBuffer.Unbind(); frameBuff.Bind(); }
public BasePass(MeshRenderer[] m, int w, int h) { Meshes = m; color = new GLTextuer2D[2]; width = w; height = h; for (int i = 0; i < color.Length; i++) { color[i] = new GLTextuer2D(PixelInternalFormat.Rgba16f); color[i].Bind(); color[i].SetData(IntPtr.Zero, PixelFormat.Rgba, w, h); color[i].Linear(); color[i].ClampToEdge(); GLTextuer2D.Unbind(); } depth = new GLRenderBuffer(); depth.Bind(); depth.SetBufferStorageAsDepth(w, h); GLRenderBuffer.Unbind(); frame = new GLFrameBuffer(); frame.Bind(); frame.AttachColor(color[0], 0); frame.AttachColor(color[1], 1); frame.AttachDepth(depth); if (!frame.IsValid) { Log.Error("Invalid frame buffer"); } GLFrameBuffer.Unbind(); }
protected void CreateBuffersIfNeeded() { if (resizeProcessor == null) { resizeProcessor = new PreviewProcessor(); } if (renderBuff == null) { renderBuff = new GLRenderBuffer(); renderBuff.Bind(); renderBuff.SetBufferStorageAsDepth(4096, 4096); Console.WriteLine("render buff id: " + renderBuff.Id); GLRenderBuffer.Unbind(); } if (colorBuff == null) { //colorbuff part of the framebuffer is always Rgba32f to support all texture formats //that could be rendered into it colorBuff = new GLTextuer2D(PixelInternalFormat.Rgba32f); colorBuff.Bind(); colorBuff.SetData(new float[0], PixelFormat.Rgba, 4096, 4096); colorBuff.SetFilter((int)TextureMinFilter.Linear, (int)TextureMagFilter.Linear); Console.WriteLine("color buff id: " + colorBuff.Id); GLTextuer2D.Unbind(); } if (frameBuff == null) { frameBuff = new GLFrameBuffer(); Console.WriteLine("frame buff id: " + frameBuff.Id); frameBuff.Bind(); frameBuff.AttachColor(colorBuff); frameBuff.AttachDepth(renderBuff); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); GL.ReadBuffer(ReadBufferMode.ColorAttachment0); if (!frameBuff.IsValid) { var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); Console.WriteLine("Framebuffer not complete!!! with status: " + status); GLFrameBuffer.Unbind(); return; } GLFrameBuffer.Unbind(); } }
protected void CreateBuffersIfNeeded() { if (resizeProcessor == null) { resizeProcessor = new PreviewProcessor(); } if (renderBuff == null) { renderBuff = new GLRenderBuffer(); renderBuff.Bind(); renderBuff.SetBufferStorageAsDepth(4096, 4096); GLRenderBuffer.Unbind(); } if (colorBuff == null) { //colorbuff part of the framebuffer is always Rgba32f to support all texture formats //that could be rendered into it colorBuff = new GLTextuer2D(PixelInternalFormat.Rgba32f); colorBuff.Bind(); colorBuff.SetData(IntPtr.Zero, PixelFormat.Rgba, 4096, 4096); colorBuff.Nearest(); colorBuff.Repeat(); GLTextuer2D.Unbind(); } if (frameBuff == null) { frameBuff = new GLFrameBuffer(); frameBuff.Bind(); frameBuff.AttachColor(colorBuff); frameBuff.AttachDepth(renderBuff); IGL.Primary.DrawBuffers(new int[] { (int)DrawBufferMode.ColorAttachment0 }); IGL.Primary.ReadBuffer((int)ReadBufferMode.ColorAttachment0); if (!frameBuff.IsValid) { var status = IGL.Primary.CheckFramebufferStatus((int)FramebufferTarget.Framebuffer); GLFrameBuffer.Unbind(); return; } GLFrameBuffer.Unbind(); } }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); Closed += ShaderTest_Closed; var ext = GLStatics.Extensions(); gl3dcontroller = new Controller3D(); gl3dcontroller.PaintObjects = ControllerDraw; gl3dcontroller.MatrixCalc.PerspectiveNearZDistance = 1f; gl3dcontroller.MatrixCalc.PerspectiveFarZDistance = 1000f; gl3dcontroller.ZoomDistance = 20F; gl3dcontroller.Start(glwfc, new Vector3(0, 0, 0), new Vector3(110f, 0, 0f), 1F); gl3dcontroller.KeyboardTravelSpeed = (ms, eyedist) => { return((float)ms / 100.0f); }; items.Add(new GLColorShaderWorld(), "COSW"); items.Add(new GLColorShaderObjectTranslation(), "COSOT"); items.Add(new GLTexturedShaderObjectTranslation(), "TEXOT"); { Bitmap bmp = new Bitmap(Properties.Resources.dotted2); // demo argb copy byte[] argbbytes = bmp.GetARGBBytes(); Bitmap copy = GLOFC.Utils.BitMapHelpers.CreateBitmapFromARGBBytes(bmp.Width, bmp.Height, argbbytes); var tex = new GLTexture2D(copy, SizedInternalFormat.Rgba8); items.Add(tex, "dotted2"); Bitmap bmp2 = tex.GetBitmap(inverty: false); bmp2.Save(@"c:\code\dotted2.bmp"); } #region coloured lines if (true) { GLRenderState lines = GLRenderState.Lines(5); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(-100, -0, 100), new Vector3(10, 0, 0), 21), new Color4[] { Color.White, Color.Red, Color.DarkRed, Color.DarkRed }) ); GLRenderState lines2 = GLRenderState.Lines(1); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines2, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(100, -0, -100), new Vector3(0, 0, 10), 21), new Color4[] { Color.Orange, Color.Blue, Color.DarkRed, Color.DarkRed })); } if (true) { GLRenderState lines = GLRenderState.Lines(1); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, 10, -100), new Vector3(-100, 10, 100), new Vector3(10, 0, 0), 21), new Color4[] { Color.Yellow, Color.Orange, Color.Yellow, Color.Orange }) ); rObjects.Add(items.Shader("COSW"), GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, 10, -100), new Vector3(100, 10, -100), new Vector3(0, 0, 10), 21), new Color4[] { Color.Yellow, Color.Orange, Color.Yellow, Color.Orange }) ); } #endregion #region Coloured triangles if (true) { GLRenderState rc = GLRenderState.Tri(); rc.CullFace = false; rObjects.Add(items.Shader("COSOT"), "scopen", GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Triangles, rc, GLCubeObjectFactory.CreateSolidCubeFromTriangles(5f), new Color4[] { Color4.Red, Color4.Green, Color4.Blue, Color4.White, Color4.Cyan, Color4.Orange }, new GLRenderDataTranslationRotation(new Vector3(10, 3, 20)) )); } #endregion #region Matrix Calc Uniform items.Add(new GLMatrixCalcUniformBlock(), "MCUB"); // def binding of 0 #endregion #region FB GLTexture2D ctex = new GLTexture2D(); items.Add(ctex, "drawnbitmap"); if (true) { int width = 1024, height = 768; // set up FB // NOTE: things end up inverted in Y in the texture, this is because textures are bottom up structures - seems the internet agrees GLFrameBuffer fb = new GLFrameBuffer(); // attach a texture to draw to ctex.CreateOrUpdateTexture(width, height, OpenTK.Graphics.OpenGL4.SizedInternalFormat.Rgba8, 1); ctex.SetMinMagLinear(); fb.AttachColor(ctex, 0, 0); GLRenderBuffer rb = new GLRenderBuffer(); items.Add(rb); rb.Allocate(RenderbufferStorage.DepthComponent32f, ctex.Width, ctex.Height); fb.AttachDepth(rb); // bind Framebuffer to system for it to be the target to draw to, with a default back colour fb.BindColor(new OpenTK.Graphics.Color4(40, 40, 40, 255)); GLMatrixCalc mc = new GLMatrixCalc(); mc.PerspectiveNearZDistance = 1f; mc.PerspectiveFarZDistance = 1000f; mc.ResizeViewPort(this, new Size(ctex.Width, ctex.Height)); Vector3 lookat = new Vector3(0, 0, 0); Vector2 camerapos = new Vector2(110f, 0); mc.CalculateModelMatrix(lookat, camerapos, 20F, 0); mc.CalculateProjectionMatrix(); ((GLMatrixCalcUniformBlock)items.UB("MCUB")).SetFull(mc); var renderState = GLRenderState.Start(); Vector4[] p = new Vector4[4]; int size = 64; int offset = 10; p[0] = new Vector4(offset, offset, 0, 1); // topleft - correct winding for our system. For dotted, red/blue at top as dots p[1] = new Vector4(offset, offset + size, 0, 1); // bottomleft p[2] = new Vector4(offset + size, offset, 0, 1); // topright p[3] = new Vector4(offset + size, offset + size, 0, 1); // botright items.Add(new GLDirect(), "fbds1"); GLRenderState rts = GLRenderState.Tri(); GLRenderDataTexture rdt = new GLRenderDataTexture(items.Tex("dotted2")); var ri = GLRenderableItem.CreateVector4(items, PrimitiveType.TriangleStrip, rts, p, rdt); ri.Execute(items.Shader("fbds1"), renderState, mc); GLRenderState lines = GLRenderState.Lines(1); var l1 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(-100, -0, 100), new Vector3(10, 0, 0), 21), new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed }); l1.Execute(items.Shader("COSW"), renderState, mc); var l2 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(100, -0, -100), new Vector3(0, 0, 10), 21), new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed }); l2.Execute(items.Shader("COSW"), renderState, mc); var l3 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, 10, -100), new Vector3(-100, 10, 100), new Vector3(10, 0, 0), 21), new Color4[] { Color.Yellow, Color.Orange, Color.Yellow, Color.Orange }); l3.Execute(items.Shader("COSW"), renderState, mc); var l4 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, GLShapeObjectFactory.CreateLines(new Vector3(-100, 10, -100), new Vector3(100, 10, -100), new Vector3(0, 0, 10), 21), new Color4[] { Color.Yellow, Color.Orange, Color.Yellow, Color.Orange }); l4.Execute(items.Shader("COSW"), renderState, mc); GLRenderState rc = GLRenderState.Tri(); rc.CullFace = false; var ri2 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Triangles, rc, GLCubeObjectFactory.CreateSolidCubeFromTriangles(5f), new Color4[] { Color4.Red, Color4.Green, Color4.Blue, Color4.White, Color4.Cyan, Color4.Orange }, new GLRenderDataTranslationRotation(new Vector3(10, 3, 20))); ri2.Execute(items.Shader("COSOT"), renderState, mc); GLRenderState rq = GLRenderState.Quads(); var ri3 = GLRenderableItem.CreateVector4Vector2(items, PrimitiveType.Triangles, rq, GLShapeObjectFactory.CreateQuad(5f, 5f, new Vector3(-90F.Radians(), 0, 0)), GLShapeObjectFactory.TexQuadCW, new GLRenderDataTranslationRotationTexture(items.Tex("dotted2"), new Vector3(10, 0, 0))); ri3.Execute(items.Shader("TEXOT"), renderState, mc); GLFrameBuffer.UnBind(); gl3dcontroller.MatrixCalc.SetViewPort(); // restore the view port byte[] texdatab = ctex.GetTextureImageAs <byte>(OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, 0, true); Bitmap bmp = GLOFC.Utils.BitMapHelpers.CreateBitmapFromARGBBytes(ctex.Width, ctex.Height, texdatab); bmp.Save(@"c:\code\out.bmp"); } #endregion if (true) { Vector4[] p = new Vector4[4]; int size = 128; int offset = 10; p[0] = new Vector4(offset, offset, 0, 1); // topleft - correct winding for our system. For dotted, red/blue at top as dots p[1] = new Vector4(offset, offset + size, 0, 1); // bottomleft p[2] = new Vector4(offset + size, offset, 0, 1); // topright p[3] = new Vector4(offset + size, offset + size, 0, 1); // botright items.Add(new GLDirect(), "ds1"); GLRenderState rts = GLRenderState.Tri(); GLRenderDataTexture rdt = new GLRenderDataTexture(items.Tex("dotted2")); rObjects.Add(items.Shader("ds1"), "ds1", GLRenderableItem.CreateVector4(items, PrimitiveType.TriangleStrip, rts, p, rdt)); } if (true) { GLRenderState rq = GLRenderState.Quads(); float width = 20F; float height = 20F / ctex.Width * ctex.Height; // TexQuadInv corrects for the inverted FB texture rObjects.Add(items.Shader("TEXOT"), GLRenderableItem.CreateVector4Vector2(items, PrimitiveType.Quads, rq, GLShapeObjectFactory.CreateQuad(width, height, new Vector3(-90F.Radians(), 0, 0)), GLShapeObjectFactory.TexQuadCCW, new GLRenderDataTranslationRotationTexture(ctex, new Vector3(-15, 0, 10)) )); } if (true) { GLRenderState rq = GLRenderState.Quads(); rObjects.Add(items.Shader("TEXOT"), GLRenderableItem.CreateVector4Vector2(items, PrimitiveType.Quads, rq, GLShapeObjectFactory.CreateQuad(5f, 5f, new Vector3(-90F.Radians(), 0, 0)), GLShapeObjectFactory.TexQuadCW, new GLRenderDataTranslationRotationTexture(items.Tex("dotted2"), new Vector3(10, 0, 0)) )); } dataoutbuffer = items.NewStorageBlock(5); dataoutbuffer.AllocateBytes(sizeof(float) * 4 * 32, OpenTK.Graphics.OpenGL4.BufferUsageHint.DynamicRead); // 32 vec4 back }