简体   繁体   中英

helping for opengl or opentk in c#

I'm working with OpenGL in c# with OpenTK tools.

My problem is :first, We want to draw some shape with lines in glwindows , we start drawing line, with button MouseDown event and finish it with key(e), when we finished drawin lines and in the next we will want to draw new lines ,the windowsframe (ColorBufferBit) will clear and i dont know what i must to do??

for more explanation , i must say that when we are drawing line , we call
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); and when we finishid it and we will want to darwing new lines,Forcibly ,i must call
GL.Clear(ClearBufferMask.AccumBufferBit | ClearBufferMask.DepthBufferBit);
what must i do? here is the code

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;
using System.Drawing;
using System.Drawing.Imaging;

namespace FarmApp
{
class openGLfarm : GameWindow
{
    bool flage, chechFirst;
    float XMouseBegin_first, YMouseBegin_first;
    //-----------------------------
    public openGLfarm(GraphicsMode mode)
        : base(700, 700, GraphicsMode.Default, "O Sample")
    {
        VSync = VSyncMode.On;
        chechFirst = true;
        Mouse.ButtonDown += new EventHandler<MouseButtonEventArgs>(Mouse_ButtonDown);
        Mouse.ButtonUp += new EventHandler<MouseButtonEventArgs>(Mouse_ButtonUp);
        Mouse.Move += new EventHandler<MouseMoveEventArgs>(Mouse_Move);

        Mouse.ButtonDown += new EventHandler<MouseButtonEventArgs>(Mouse_ButtonDown);
        Mouse.ButtonUp += new EventHandler<MouseButtonEventArgs>(Mouse_ButtonUp);
        Mouse.Move += new EventHandler<MouseMoveEventArgs>(Mouse_Move);


    }
    //-------------------------------
         public openGLfarm()
    {
        // TODO: Complete member initialization
    }


    void Mouse_Move(object sender, MouseMoveEventArgs e)
    {
        // Now use mouse_delta to move the camera
    }

    void Mouse_ButtonUp(object sender, MouseButtonEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButton.Left:
                {
                    flage = true;
                    if (chechFirst)
                    {
                        chechFirst = false;
                        XMouseBegin_first = Mouse.X;
                        YMouseBegin_first = Mouse.Y;

                    }
                    break;
                }
            case MouseButton.Middle:
                ///camera_mode = ECameraMode.CAMERA_DOLLY;
                break;
            case MouseButton.Right:
                //camera_mode = ECameraMode.CAMERA_ORBIT;
                break;
        }

    }

    void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButton.Left:
                {
                    break;
                }
            case MouseButton.Middle:
                ///camera_mode = ECameraMode.CAMERA_DOLLY;
                break;
            case MouseButton.Right:
                //camera_mode = ECameraMode.CAMERA_ORBIT;
                break;
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

    }

    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);
        if (Keyboard[Key.Escape])
            Exit();
        if (Keyboard[Key.E])
        {
            flage = false;
            chechFirst = true;

       }
    }



    void table(float count)
    {
        float x, y;
        GL.Color4(Color4.Wheat);

        float z = (2f) / (count);
        x = -1;
        y = -1;
        for (double i = 0; i < count; i++)
        {
            GL.Begin(BeginMode.Lines);

            GL.Vertex2(x + (z), -1);
            GL.Vertex2(x + (z), 1);

            GL.End();
            x = x + (z);
        }

        for (double i = 0; i < count; i++)
        {
            GL.Begin(BeginMode.Lines);

            GL.Vertex2(-1, y + (z));
            GL.Vertex2(1, y + (z));

            GL.End();
            y = y + (z);
        }


    }
    public void DrawLine(float Xbegin, float Ybegin, float Xend, float Yend)
    {
        GL.Begin(BeginMode.Lines);
        GL.Vertex2(Xbegin, Ybegin);
        GL.Vertex2(Xend, Yend);
        GL.End();

    }
    protected override void OnRenderFrame(FrameEventArgs e)
    {
        //base.OnRenderFrame(e);
        if (chechFirst)
            GL.Clear(ClearBufferMask.AccumBufferBit | ClearBufferMask.DepthBufferBit);

        else
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();
        table(Form1.co);
        if (flage)
        {
            //flage = false;
            DrawLine((XMouseBegin_first / 350f - 1f), -(YMouseBegin_first / 350f - 1f), (Mouse.X / 350f - 1f), -(Mouse.Y / 350f - 1f));
        }
        SwapBuffers();
    }
}

}

you could foor example create an Array of Vector2 and go saving the start and end points of it. Then in the function that draws the lines make a for and draw all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM