terinfire's picture

Tao.SDL or CS-SDL with OpenTK?

Hi all! Another Tao Framework developer moving over to OpenTK (if possible).

I'm having some issues, and maybe I've done something funny.

I'd like to make OpenGL calls and use OpenGL, but have SDL handle the Window, etc. Ported my Tao code over and everything compiles. My initial stuff with the Compatibility compiled/ran/worked. Tried moving it over to the native OpenTK framework without using the compatibility and I'm having a lot of problems.

On the first OpenGL call, it blows up with an NullReferenceException...

The following code used to work (again, until I ported over to OpenTK):

            if ( Sdl.SDL_InitSubSystem( Sdl.SDL_INIT_VIDEO | Sdl.SDL_OPENGL ) != 0 )
            {
                Debug.WriteLine( "Couldn't initialize video subsystem." );
                return;
            }
 
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_DOUBLEBUFFER, 1 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_BUFFER_SIZE, ColorDepth );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_RED_SIZE, 8 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_GREEN_SIZE, 8 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_BLUE_SIZE, 8 );
 
            m_Video = Sdl.SDL_SetVideoMode( Width, Height, ColorDepth, Flags );
 
            if ( m_Video == IntPtr.Zero )
            {
                Debug.WriteLine( "Couldn't start video mode." );
                return;
            }
 
            GL.Enable( EnableCap.Texture2D );  // NullRefException right here

I have a lot of sub-systems that spin-up. My guess is that OpenTK hasn't necessarily spun-up like it should. A lot of the examples I've seen actually have OpenTK manage the window, and that's what I'm trying to avoid doing. No inner exception is available either...

Any thoughts? Hopefully I'm not going to have to roll back to Tao's framework... Oy.


Comments

Comment viewing options

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

You need to inform OpenTK that you manage your own OpenGL context, like this:

GraphicsContext context = GraphicsContext.CreateDummyContext();

This will use the existing context to initialize the OpenGL bindings and everything should work fine afterwards (please post if it doesn't!) See the GraphicsContext documentation for more details.

Tao initializes itself implicitly at the first OpenGL function call. While this is more convenient when using different systems like Tao.SDL/GLUT/etc, it is very easy to initialize the bindings without a context (causing random crashes). Back when the Tao forums existed, every second post was about this issue - which is why OpenTK requires explicit initialization when using an external window library.

terinfire's picture

Hey, thank you for the response. Unfortunately, I'm still having the same error.

            if ( Sdl.SDL_InitSubSystem( Sdl.SDL_INIT_VIDEO | Sdl.SDL_OPENGL ) != 0 )
            {
                Debug.WriteLine( "Couldn't initialize video subsystem." );
                return;
            }
 
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_DOUBLEBUFFER, 1 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_BUFFER_SIZE, ColorDepth );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_RED_SIZE, 8 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_GREEN_SIZE, 8 );
            Sdl.SDL_GL_SetAttribute( Sdl.SDL_GL_BLUE_SIZE, 8 );
 
            m_Video = Sdl.SDL_SetVideoMode( Width, Height, ColorDepth, Flags );
 
            if ( m_Video == IntPtr.Zero )
            {
                Debug.WriteLine( "Couldn't start video mode." );
                return;
            }
 
            OpenTK.Graphics.GraphicsContext context = OpenTK.Graphics.GraphicsContext.CreateDummyContext( new OpenTK.ContextHandle( m_Video ) );
 
            GL.Enable( EnableCap.Texture2D );
            GL.Enable( EnableCap.AlphaTest );
            GL.AlphaFunc( AlphaFunction.Gequal, 0.1f );

I supplied the ContextHandle to OpenTK when creating the context. If I don't do the above code and just let it do a CreateDummyContext parameterless, I get the following error:

No GraphicsContext is current on the calling thread.

Not sure what's going on. I tried some of this last night after seeing it. Just wondering if maybe there are some dependency DLL's that aren't in my bin folder or what...

terinfire's picture

> OpenTK.dll!OpenTK.Platform.Windows.Wgl.GetCurrentContext() Line 26 C#
OpenTK.dll!OpenTK.Platform.Windows.WinFactory.CreateGetCurrentGraphicsContext.AnonymousMethod__0() Line 68 + 0x5 bytes C#
OpenTK.dll!OpenTK.Graphics.GraphicsContext.CreateDummyContext() Line 282 + 0xe bytes C#

Delegates' member variables are all null.

I'm getting this error from the call:

OpenTK.Graphics.GraphicsContext.CreateDummyContext();