-1

我试图按照本教程在 C# 上制作一个 OpenGL 应用程序。http://www.developerfusion.com/article/3823/opengl-in-c/2/

后来,我尝试进行以下更改:

class DrawObject : OpenGLControl
{
    public override void glDraw()
    {
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        GL.glLoadIdentity();
        GL.glBegin(GL.GL_LINE);
        GL.glLineWidth(10.0f);
        GL.glVertex3f(-3.0f, 0.0f, 0.0f);
        GL.glVertex3f(3.0f, 0.0f, 0.0f);
        GL.glEnd();
        GL.glFlush();
    }

    protected override void InitGLContext()
    {
        GL.glShadeModel(GL.GL_SMOOTH);
        GL.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        GL.glClearDepth(1.0f);
        GL.glEnable(GL.GL_DEPTH_TEST);
        GL.glDepthFunc(GL.GL_LEQUAL);
        GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    }

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

        Size s = Size;
        double aspect_ratio = (double)s.Width / (double)s.Height;
        GL.glMatrixMode(GL.GL_PROJECTION);
        GL.glLoadIdentity();

        GL.gluPerspective(45.0f, aspect_ratio, 0.1f, 100.0f);

        GL.glMatrixMode(GL.GL_MODELVIEW);
        GL.glLoadIdentity();
    }
}

public class DrawLine : Form
{
    DrawObject newObject = new DrawObject();

    public DrawLine()
    {
        Text = "Draw a line";
        newObject.Dock = DockStyle.Fill;
        Controls.Add(newObject);
    }

    public static void Main()
    {
        DrawLine drawLine = new DrawLine();
        Application.Run(drawLine);
    }
}

出于某种原因,我将在 Application.Run(drawLine) 处收到“无效枚举数”错误。基本上我想要做的是将它在 3 个指定顶点上渲染一个点的部分替换为它在给定 2 个顶点的情况下渲染一条线的部分。我不知道为什么点版本不会抛出这个异常,但行版本会。我在引用中引用了 csgl.dll,并添加了 csgl.native.dll 并使其在每次编译解决方案时发布(否则整个事情根本不会运行)。

4

1 回答 1

1

不要更改内部的状态glBeginand glEnd(即不要调用glLineWidth)。从文档

GL_INVALID_OPERATION is generated if glLineWidth is executed between 
the execution of glBegin and the corresponding execution of glEnd.


JulianLee 的回答:glBegin必须用 调用GL_LINES,而不是GL_LINE。看评论。

于 2012-04-11T13:57:04.920 回答