trenmost's picture

Error with GraphicsContext

Hi!
I'm developong an application in C#, but i have some issues.
I use openTK for two
i have multiple formes, each of them has a OpenTK control on it, on which i draw by setting the glcontrol.MakeCurrent(), GL.Viewport(0,0,resx,resy), and using GL.Lines.

I have though an other method, which uses OpenTK for creating a really hi-res (4000x4000 or so) image of a shape (made of lines), and then copy the color data to a byte array.
For this method i use the following code:

 GL.Viewport(0, 0, resX, resY);
            OpenTK.GLControl glcontrol = new OpenTK.GLControl();
            glcontrol.SetBounds(0, 0, resX, resY);
            glcontrol.MakeCurrent();
            glcontrol.Select();
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.Color3(1.0f, 1.0f, 0.0f);
            GL.LineWidth(1.0f);
 
            GL.Begin(BeginMode.Lines);
            {
                for (int i = 0; i < A.getPointCount() - 1; i++)
                {
                    GL.Vertex2(A.getPoint(0, i) * tmp[0], A.getPoint(1, i) * tmp[1]);
                    GL.Vertex2(A.getPoint(0, i + 1) * tmp[0], A.getPoint(1, i + 1) * tmp[1]);
                }
            }
            GL.End();
 
            byte[] image = new byte[resX * resY]; // 0=> üres; 127=> Eredeti shape; 1=> megtalált kontúr; 2=>végső (külső) kontúr
            GL.ReadPixels(0, 0, resX, resY, PixelFormat.Red, PixelType.Byte, image);

now there are absolutely no problems, as long i run the application in Visual Studio. But as i run the application by clicking on the exe, strange errors occur when i after i use that copying method, and then i open a new form with a GLControl on it. The error message is:

System.NullReferenceException: Object reference not set to an instance of an object.
   at OpenTK.Graphics.GraphicsContext.<.cctor>b__0()
   at OpenTK.Graphics.GraphicsContext.get_CurrentContext()
   at OpenTK.Graphics.GraphicsContext.LoadAll()
   at OpenTK.GLControl.OnHandleCreated(EventArgs e)

the sad thing is that i cant debug where the error happens exactly, because the error only happens outside Visual studio.

does anyone has any idea about this issue? :)


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

There is a good chance this is a bug in OpenTK. It would really help if you could run your application with a debug version of OpenTK.dll, and direct the System.Diagnostics.Debug output to a logfile or the console. (Use OpenTK.sln to build the debug dll and copy OpenTK.dll and OpenTK.pdb to your application directory).

This output should display more information on what and where is going wrong.

I would also suggest giving a try to the latest nightly build, as it contains a couple of GLControl-related fixes.

Finally, make sure you call Dispose() on all relevant GLControls whenever a Form is destroyed, otherwise you will leak OpenGL contexts (which use quite a lot of memory).

trenmost's picture

thanks i'll try those, and ill post back :)

trenmost's picture

By doing a Release build with Disabled optimization in visual studio (unchecked Optimize code), then it works without this error...
andg GL.getError() reported no errors also.

now im going to generate a debug file as you said!

trenmost's picture

Good news!
It seems that with the latest nightly build (opentk-2011-11-11.zip), the application works as intended, even with optimized code!

thank you! :)

and thanks for this truly awesome project you are doing with OpenTK!

trenmost's picture

that problem is gone now but i have an other one:

GL.ReadPixels(0, 0, resX, resY, PixelFormat.Red, PixelType.Byte, image);

the code randomly freezes at this point without throwing any exceptions or anything. does anyone know how to resolve this?

note: I use high resolutions (as high as 8192x8192).

code fragments:

 
GL.Viewport(0, 0, resX, resY);
            OpenTK.GLControl glcontrol = new OpenTK.GLControl(new GraphicsMode(new ColorFormat(1,0,0,0)));
            glcontrol.SetBounds(0, 0, resX, resY);
            glcontrol.MakeCurrent();
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.Color3(1.0f, 1.0f, 0.0f);
            GL.LineWidth(1.0f);
 
            GL.Begin(BeginMode.Lines);
            {
                for (int i = 0; i < A.getPointCount() - 1; i++)
                {
                    GL.Vertex2(A.getPoint(0, i) * tmp[0], A.getPoint(1, i) * tmp[1]);
                    GL.Vertex2(A.getPoint(0, i + 1) * tmp[0], A.getPoint(1, i + 1) * tmp[1]);
                }
            }
            GL.End();
  byte[] image = new byte[resX * resY]; 
            GL.ReadPixels(0, 0, resX, resY, PixelFormat.Red, PixelType.Byte, image);

resX usually 8192, resY usually 8192. My video card can handle this large textures/viewports, and the freeze happens fairly rare.