如何使用 CSGL 旋转三角形(顺时针)?

发布于 2024-10-08 20:10:29 字数 1277 浏览 2 评论 0原文

我试图制作一个顺时针旋转的三角形,但我做不到。我做了一个定时器控件,但没有定时器结果是一样的。因此,下面的代码不会显示旋转三角形。 如何使用 CSGL 旋转三角形?

namespace WinDrawCoordinate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private float a = 0.0f;
        private void Form1_Load(object sender, EventArgs e)
        {

            timer1.Start();
        }

        protected void Gosterim()
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT);
            GL.glLoadIdentity();
            Hesapla();
            Ucgen();

        }
        protected void ayarlar()
        {
            GL.glClearColor(1, 1, 1, 1);
            GL.glShadeModel(GL.GL_FLAT);
        }
        protected void Hesapla()
        {
            a += 0.5f;
            this.Refresh();
        }

        protected void Ucgen()
        {
            GL.glColor3f(0, 1, 1);
            GL.glRotatef(a, 0, 0, -1);
            GL.glBegin(GL.GL_TRIANGLES);
            GL.glVertex2f(-0.2f, -0.2f);
            GL.glVertex2f(0.2f, -0.2f);
            GL.glVertex2f(0.0f, 0.2f);
            GL.glEnd();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ayarlar();

            Gosterim();
        }
    }

I'm trying to make a clock-wise rotating triangle, but I can not. I made a timer control but the result is the same without the timer. As a result, the below code does not show rotating triangle.

How can i rotate triangle with CSGL?

namespace WinDrawCoordinate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private float a = 0.0f;
        private void Form1_Load(object sender, EventArgs e)
        {

            timer1.Start();
        }

        protected void Gosterim()
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT);
            GL.glLoadIdentity();
            Hesapla();
            Ucgen();

        }
        protected void ayarlar()
        {
            GL.glClearColor(1, 1, 1, 1);
            GL.glShadeModel(GL.GL_FLAT);
        }
        protected void Hesapla()
        {
            a += 0.5f;
            this.Refresh();
        }

        protected void Ucgen()
        {
            GL.glColor3f(0, 1, 1);
            GL.glRotatef(a, 0, 0, -1);
            GL.glBegin(GL.GL_TRIANGLES);
            GL.glVertex2f(-0.2f, -0.2f);
            GL.glVertex2f(0.2f, -0.2f);
            GL.glVertex2f(0.0f, 0.2f);
            GL.glEnd();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ayarlar();

            Gosterim();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2024-10-15 20:10:30

仔细检查这一行:

GL.glRotatef(a, 0, 0, -1);

不应该吗?

GL.glRotatef(a, 0, 0, 1);

看起来旋转角度 (a) 的增量应该没问题……但是计时器“滴答作响”并触发 ayarlarGosterim 的速度有多快?

另外,您可能想要添加与旋转角度相关的检查,如果它循环很多并且超过 359.5(因为您每次都添加 0.5),那么您实际上正在“完整循环”

Double check this line:

GL.glRotatef(a, 0, 0, -1);

Shouldn't it be?

GL.glRotatef(a, 0, 0, 1);

Looks like your increment to the rotation angle (a) should be OK....but how fast is the timer "ticking" and firing off ayarlar and Gosterim?

Also you may want to add a check in relation to the rotation angle, if it loops alot and goes over 359.5 (Since you are adding 0.5 each time through), then you are going "full circle" literally

薯片软お妹 2024-10-15 20:10:29

从发布的代码中我看到了一些事情:

  1. 您没有任何矩阵设置。虽然您可以使用默认矩阵,但您几乎肯定应该指定自己的矩阵,以确保三角形位于您期望的位置。默认矩阵应该适合您,因为它们指定恒等模型视图和投影。

  2. 您没有调用交换缓冲区。完成绘制后需要调用来交换前后缓冲区,以便显示三角形。对于 CsGL,我认为您可以使用 SwapBuffer() 方法。

  3. 不要使用计时器来加速重画。计时器工作不可靠,因为它们使用 Windows 消息泵,并且很难获得良好的结果。相反,您应该使用渲染循环——一个在程序运行的整个过程中运行的循环,并且不断刷新屏幕。不过,您必须确保给操作系统时间来处理您的消息。一个非常简单的渲染循环可能是:

    void RenderLoop() {
      而(真){
        设置相机();
        创建几何体();
        交换缓冲区();
        应用程序.DoEvents(); // 这让 Windows 处理消息
      }
    }
    

    请注意,有更好的方法可以在 C# 中获得良好的消息循环,但这很容易设置和推理。

From the code posted I see a couple things:

  1. You don't have any matrix set-up. While you can use the default matrices you should almost certainly specify your own to ensure the triangle is where you expect. The default matrices should work for you here as they specify an identity modelview and projection.

  2. You don't have a call to swap buffers. You need a call after you're done drawing to swap the front and back buffers so that the triangle is displayed. For CsGL I think there is a SwapBuffer() method you can use.

  3. Don't use a timer to pump your redrawing. Timers work unreliably as they use the windows message pump and they are hard to get good results from. Instead you should use a render loop--a loop that runs the entire time your program runs and just keeps refreshing the screen. You have to make sure you give the operating system time to handle your messages though. A very simple render loop might be:

    void RenderLoop() {
      while (true) {
        SetUpCamera();
        CreateGeometry();
        SwapBuffers();
        Application.DoEvents(); // this lets windows process messages
      }
    }
    

    note that there are better ways to get a good message loop in C# but this is easy to set up and reason about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文