我试图按照本教程在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并使其在每次编译解决方案时都被发布(否则整个事情都不会运行)。
答案 0 :(得分:1)
请勿更改glBegin
和glEnd
内的状态(即不要调用glLineWidth
)。来自docs:
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
来调用。见评论。