0

我正在使用 OpenTK 工具在 c# 中使用 OpenGL。

我的问题是:首先,我们想在 glwindows 中用线条绘制一些形状,我们开始绘制线条,使用按钮 MouseDown 事件并用 key(e) 完成它,当我们完成绘制线条时,接下来我们将要绘制新的行,windowsframe(ColorBufferBit)将清除,我不知道我必须做什么?

为了更多的解释,我必须说,当我们画线时,我们调用
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ,当我们完成它时,我们会想要绘制新的线,强制,我必须调用
GL.Clear(ClearBufferMask.AccumBufferBit | ClearBufferMask.DepthBufferBit);
我必须做什么?这是代码

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();
    }
}

}

4

1 回答 1

0

例如,您可以创建一个 Vector2 数组并保存它的起点和终点。然后在绘制线条的函数中创建一个 for 并绘制所有线条。

于 2012-07-11T07:20:13.710 回答