Beispiel #1
0
 /// <summary> Mouse Wheel handler - hook to GLWindowControl</summary>
 public void MouseWheel(object sender, GLMouseEventArgs e)
 {
     if (e.Delta != 0)
     {
         if (keyboard.Ctrl)
         {
             if (MatrixCalc.FovScale(e.Delta < 0))
             {
                 MatrixCalc.CalculateProjectionMatrix();
                 Invalidate();
             }
         }
         else
         {
             ZoomScale(e.Delta > 0);
         }
     }
 }
Beispiel #2
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Closed += ShaderTest_Closed;

            // operate matrixcalc in +Z away mode (default), in perspective mode (default)

            matrixcalc = new GLMatrixCalc();                                                   // must make after GL window is made
            matrixcalc.ResizeViewPort(this, glwfc.Size);                                       // inform matrix calc of window size
            matrixcalc.CalculateModelMatrix(new Vector3(0, 0, 0), new Vector2(135, 0), 50, 0); // set up the lookat position, the camera direction, the distance and rotation
            matrixcalc.CalculateProjectionMatrix();                                            // and set the project matrix

            glwfc.Paint += Draw;                                                               // register for draw

            // disposable items are stored in GLItemsList, so they can be cleanly disposed of at the end

            // make three stock shaders with names

            items.Add(new GLColorShaderWorld(), "COSW");
            items.Add(new GLColorShaderObjectTranslation(), "COSOT");
            items.Add(new GLTexturedShaderObjectTranslation(), "TEXOT");

            // make a texture from resources called dotted2

            items.Add(new GLTexture2D(Properties.Resources.dotted2, SizedInternalFormat.Rgba8), "dotted2");

            // render state for lines
            GLRenderState lines = GLRenderState.Lines(1);

            // make a set of vertices from the shape factory
            var rs1 = GLShapeObjectFactory.CreateLines(new Vector3(-100, -0, -100), new Vector3(-100, -0, 100), new Vector3(10, 0, 0), 21);

            // make a array of colours for the vertexes (note do not need a full set, the render creator will repeat them automatically)
            var rc1 = new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed };

            // make a render item, indicating type (Lines), vertexes (rs1) and colours (rc1)
            var ri1 = GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Lines, lines, rs1, rc1);

            // add to render list - paint with this shader, and this render
            rObjects.Add(items.Shader("COSW"), ri1);

            // do more..

            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(0, 0, 10), 21),
                                                              new Color4[] { Color.Red, Color.Red, Color.DarkRed, Color.DarkRed }));

            // render state for triangles

            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, 0, -10))
                                                              ));

            rObjects.Add(items.Shader("COSOT"), "scopen2",
                         GLRenderableItem.CreateVector4Color4(items, PrimitiveType.Triangles, rc,
                                                              GLCubeObjectFactory.CreateSolidCubeFromTriangles(5f),
                                                              new Color4[] { Color4.Red, Color4.Red, Color4.Red, Color4.Red, Color4.Red, Color4.Red },
                                                              new GLRenderDataTranslationRotation(new Vector3(10, 0, 10))
                                                              ));

            // render state for quads

            GLRenderState rq = GLRenderState.Quads();

            rObjects.Add(items.Shader("TEXOT"),
                         GLRenderableItem.CreateVector4Vector2(items, PrimitiveType.Quads, rq,
                                                               GLShapeObjectFactory.CreateQuad(5.0f, 5.0f, new Vector3(0f.Radians(), 0, 0)), GLShapeObjectFactory.TexQuadCW,
                                                               new GLRenderDataTranslationRotationTexture(items.Tex("dotted2"), new Vector3(0, 0, 0))
                                                               ));

            // make a Uniformblock to hold matrix info

            items.Add(new GLMatrixCalcUniformBlock(), "MCUB");     // def binding of 0
        }
        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
        }