在c#中帮助opengl或opentk

时间:2012-05-12 15:51:45

标签: c# opengl opentk

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

我的问题是:首先,我们想在glwindows中绘制一些形状的线条,我们开始绘制线条,使用按钮MouseDown事件并使用键(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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

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