enablerbr's picture

Leadwerks integration (was: I need help converting method from Tao to OpenTK.)

this is the method i'm trying to convert from Tao to OpenTK.

public static void MakeCurrent()
        {
            Wgl.wglMakeCurrent(Wgl.wglGetCurrentDC(), Wgl.wglGetCurrentContext());
        }

what is the OpenTK versions of the above Tao Wgl methods? if it helps i'm using a gamewindow.

forgot to mention i'm using svn version of OpenTK and coding in c#.


Comments

Comment viewing options

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

"'CallingConvention' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static."

the CallingConvention word in the delegates before the =. is being underlined.

changed it to [UnmanagedFunctionPointer(CallingConvention.StdCall)]. however everything still hangs/freezes.

so the code looks like this now.

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public unsafe delegate void GetSize(int* width, int* height);
        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
        public delegate void MakeCurrent();
 
        public IntPtr CreateCustom(TBuffer.GetSize getsize, TBuffer.MakeCurrent makecurrent)
        {
            return this.Buffer = SafeNativeMethods.TCreateCustomBuffer(getsize, makecurrent);
        }
 
        [SuppressUnmanagedCodeSecurityAttribute]
        [DllImport(ENGINE_DLL, EntryPoint = "CreateCustomBuffer", CallingConvention = CallingConvention.StdCall)]
        internal unsafe static extern IntPtr TCreateCustomBuffer(TBuffer.GetSize getsize, TBuffer.MakeCurrent makecurrent);
 
 
        private static void MakeCurrent()
        {
            if (Window != null)
                Window.Context.MakeCurrent(Window.WindowInfo);
        }
 
        private unsafe static void GetSize(int* width, int* height)
        {
            if (Window != null)
            {
                width = (int*)Window.Width;
                height = (int*)Window.Height;
            }
        }
 
        private unsafe static TBuffer.GetSize DGetSize = new TBuffer.GetSize(GetSize);
        private static TBuffer.MakeCurrent DMakeCurrent = new TBuffer.MakeCurrent(MakeCurrent);
 
        unsafe { buff.CreateCustom(DGetSize, DMakeCurrent); }
the Fiddler's picture

A very good suggestion in the leadwerks forum is to add a call to Console.WriteLine in the callbacks. This way you can see whether the callback is actually called, or if the process hangs before getting to that point.

enablerbr's picture

pushedx from the Leadwerks forums figured it out. i should have been doing this.

        private unsafe static void GetSize(int* width, int* height)
        {
            if (Window != null)
            {
                *width = Window.Width;
                *height = Window.Height;
            }
        }

instead of this.

        private unsafe static void GetSize(int* width, int* height)
        {
            if (Window != null)
            {
                width = (int*)Window.Width;
                height = (int*)Window.Height;
            }
        }
enablerbr's picture

now it leave the RenderLights command of the Leadwerks engine. the Leadwerks engine uses Deferred Lighting/Shader techique.

"RenderLights
C: void RenderLights( TBuffer buffer )
C++: void Engine::RenderLights( TBuffer buffer )
BlitzMax: RenderLights( buffer:TBuffer )
Pascal: procedure RenderLights ( buffer:THandle );
Renders all lights of the current world into the current buffer, using the given buffer for geometry data. The current buffer is usually set before the RenderLights() command as BackBuffer(). Usually the given buffer was previously rendered using RenderWorld(). When using post-shaders, the current buffer is set before the RenderLights() command to a postbuffer instead of the BackBuffer(). "

pushedx and Josh(Leadwerks engine author) have gotten renderlights working. i still have my issues :) with my wrapper. yet as far as OpenTK and Leadwerks engine is concerned. they are happily working together without issues.

so thanks again (hope i'm not overdoing it with the thank yous) The Fiddler for helping me with my problem and posting comments in the Leadwerks Forum. so once i get my wrappers issues sorted. i can then move on to using .Net for creating my engine tools. hopefully prove C# is not just for WEB apps or office apps.

the Fiddler's picture

Glad you got it working. It's usually small oversights like this that take the longest to solve.

Two more small things:

  1. You can avoid unsafe code by using GetSize(out int width, out int height). The p/invoke layer is smart enough to turn these into pointers implicitly.
  2. For tools, you'd probably want to use Windows.Forms with OpenTK.GLControl. This way, you can use the Visual Studio designer to create your interface. GameWindow, on the other hand, is more suited to games rather than GUIs.
enablerbr's picture

is there more to it than just doing GetSize(out int width, out int height). as when i do it complains about width and height needing to be assigned.

private unsafe static void GetSize(out int width, out int height)
        {
            if (Window != null)
            {
                width = Window.Width;
                height = Window.Height;
            }
        }<code>
the Fiddler's picture

When you have an out variable, the compiler forces you to assign a value before you leave the method. Simple solution: initialize width and height to 0.

private static void GetSize(out int width, out int height)
{
    width = height = 0;
 
    if (Window != null)
    {
        width = Window.Width;
        height = Window.Height;
    }
}

No need to use 'unsafe' either.

enablerbr's picture

thanks for clearing that up.

something has come to light with OpenTK and Leadwerks engine. for some reason release versions of the OpenTK dlls are not working for some of the commands. yet the debug versions of the OpenTK dlls work just fine.

the Fiddler's picture

Possible stack corruption, caused either by OpenTK or by the engine wrapper. Made a lengthy post in the leadwerks forums, the gist is to test your code against the "gw-next2" OpenTK branch.

This branch contains a massive update to the p/invoke layer in OpenTK and there's a chance it will fix the issue (if the issue is caused by OpenTK, which is possible but not certain).

enablerbr's picture

tested against "gw_next2" but it's the same as previous. debug works no problems. release still only partially works. that was with the offical Leadwerks wrapper and example code provided by pushedx.