avogatro's picture

<solved>GLControl + thread

Data:
hardware: Geforce8600GT + Athlon X2 5000
OS: Archlinux i686 32 bit
driver: NVIDIA beta 190.18
opentk: 0.9.8.2
mono:
Mono JIT compiler version 2.4.2.3
TLS: __thread
GC: Included Boehm (with typed GC)
SIGSEGV: altstack
Notifications: epoll
Architecture: x86
Disabled: none

HI:
I use GLcontrol to print moving text on a Windows Form(ontop, with fix location).
I use the Application_Idle methode. but the speed is not very constant .
A normal loop without checking "glcontrol.isIdle" is much more smooth.
Without threading, it works.
But I need to run the loop in a thread becauseI I have to be able to kill the the run loop at any time.

        public void Run()
        {
            long timestamp;
            long timedelta;
            bool should_render = false;
            long delta = 320000;
            int tempconter = 0 ;
            while (!this.exiting){
                timestamp = Stopwatch.GetTimestamp();
                if (last_timestamp == 0){
                    last_timestamp = Stopwatch.GetTimestamp();
                    continue;
                }
                timedelta = timestamp-last_timestamp;
 
                should_render = false;
                if (timedelta > delta)
                {
                    posX-=speed;
                    if (posX<=-fontWidth){
                    posX= this.Width;
                    }
                    timedelta -= delta;
                    should_render = true;
                }
                //posX-=tempconter;
 
                if (should_render == true)
                {
                    this.render(); 
                    last_timestamp = Stopwatch.GetTimestamp(); 
                }
                else
                Thread.Sleep(2);
            }
        private void render()
        {
 
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable( EnableCap.DepthTest );
 
            GL.Enable(EnableCap.LineSmooth);
            GL.ClearDepth(1.0);
            GL.ClearColor(0.0F,0.0F,0.0F,0.0F);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest);
            GL.Enable(EnableCap.CullFace);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            this.glControl.MakeCurrent();
            //GraphicsContext.CurrentContext().
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
 
            this.printer.Begin();
 
            this.printer.Print(text, the_font, fontColor,
                          new RectangleF(posX,0,fontWidth,this.Height*0.9F),
                          TextPrinterOptions.Default,TextAlignment.Center);
 
            this.printer.End();
            this.glControl.SwapBuffers();
 
 
        }
        }

the Problem:
function glcontrol.MakeCurrent(), don't work in a thread
Error:

Unhandled Exception: OpenTK.Graphics.GraphicsContextException: Failed to make context current.
  at OpenTK.Platform.X11.X11GLContext.MakeCurrent (IWindowInfo window) [0x000e3] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Platform/X11/X11GLContext.cs:219 
  at OpenTK.Graphics.GraphicsContext.MakeCurrent (IWindowInfo info) [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Graphics/GraphicsContext.cs:387 
  at OpenTK.GLControl.MakeCurrent () [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/GLControl.cs:191 
  at (wrapper remoting-invoke-with-check) OpenTK.GLControl:MakeCurrent ()
  at RollingText.RollingText.render () [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:144 
  at RollingText.RollingText.Run () [0x00087] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:238

I checkt the documentation http://www.opentk.com/doc/graphics/graphicscontext :
However function GraphicsContext.MakeCurrent(null, null) don't exist.
so i can't make the context non-current on the main thread.

Can someone help me?


Comments

Comment viewing options

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

solved.
I deleted my onload handler ...

(my_form).base.OnLoad(e);
glcontrol.load()
...
thx for all your help.

the Fiddler's picture

The issue is that you are making the context non-current on the main thread, but then try to call OpenGL commands from that thread. This will not work.

Make sure your Paint and Resize events are not calling OpenGL directly (rather, set a flag and call the necessary OpenGL commands from the background thread). You also need to make the context current on the background thread by calling glControl.MakeCurrent() before calling any OpenGL commands on that thread.

I have confirmed that this works as expected on Ubuntu Jaunty with OpenTK 0.9.9-2b.

Edit: ninja post, glad you solved it! :)

Edit 2: will add a code sample to the documentation, this is useful.

Edit 3: you can still use the TextPrinter with 0.9.9-2. Just add a reference to OpenTK.Compatibility.

avogatro's picture

desoleted TextPrinter works fine
under my 32bit archlinux. :-)
5% cpu.

But I want to run this code in windows too. ( I know this sucks, but i have to... )
I can see a flashing blackwindow, without textprinter. and 100%cpu ...

why?

well maybe this is stuff for another thread ...

the Fiddler's picture

That doesn't sound right. If you think its the TextPrinter, better create a new thread.

Also, try debug OpenTK.dll, maybe it manages to uncover something.

avogatro's picture

well the flashing windows comes from some transparent setting.
they don't work under xfce4, so i haven't deleted them.

i still can't see the text.

the Fiddler's picture

The TextPrinter is identical to the one found in 0.9.8.

Check whether the text examples that come with OpenTK work.

avogatro's picture

i found the problem, but not the source of the problem:
GL.translate(x,y,z); don't work.
if x!=0 or y!=0, i can't see my font.
z is not important..

If i use a rectangle in the functioncall:
this.printer.Print(), the same thng happens...

GL.MatrixMode ( MatrixMode.ModelView);
works

I'm done, and happy :D

avogatro's picture

well GL.translate works now, but
TextPrinter.Print with new Rectangle don't.