Thursday, April 12, 2012

C# - CsGL invalid enumerant

Good day to all.


I was trying to follow this tutorial to make an OpenGL application on C#. http://www.developerfusion.com/article/3823/opengl-in-c/2/


Later on, I tried to make the following changes:


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


For some reason, I will then get an "invalid enumerant" error, at Application.Run(drawLine). Basically what I'm trying to do is to replace the part where it renders a dot on 3 specified vertices, with a part where it renders a line given 2 vertices. I do not know why the dot version does not throw this exception, but the line version does. I have referenced csgl.dll in my references, and added csgl.native.dll and made it to be published every time the solution is compiled (otherwise the whole thing wouldn't run at all).


Any help will be much appreciated, as I am a beginner at this.


Answer:


Don't change state inside glBegin and glEnd (that is, don't call glLineWidth). From the docs:
GL_INVALID_OPERATION is generated if glLineWidth is executed between 
the execution of glBegin and the corresponding execution of glEnd.

Answer from JulianLee: glBegin must be called with GL_LINES, not GL_LINE. See comments.

No comments:

Post a Comment