Search code examples
c#linuxopenglx11glx

glXChooseVisual fails when using Mono-C# on linux


When I call "glXChooseVisual" (aka GLX.ChooseVisual in C#) it returns a null IntPtr. Now what confuses me is when I build the NeHe lesson02 basically using the same code it works (the only difference it being written in C).

Also when I step into code from the OpenTK and find when it calls "glXChooseVisual" it returns a valid Visual ptr, and I can't as of yet find anything im missing.

Also what bugs me is I have had this working in the past like a year ago. The reason i'm doing this is for a cross platform API thats not just limited to OpenGL so any help would be great.

I've tried all this on [Ubuntu 11.10 Nvidia 5700] and [Fedora 16 Nvidia 6100] and they both fail. You can copy and past this code in a Mono-C# console App to test it out.

using System;
using System.Runtime.InteropServices;

namespace TestGL
{
    static class GLX
    {
        [DllImport("libX11", EntryPoint = "XOpenDisplay", ExactSpelling = true)]
        public static extern IntPtr XOpenDisplay(IntPtr display_name);

        [DllImport("libX11", EntryPoint = "XDefaultScreen", ExactSpelling = true)]
        public static extern int XDefaultScreen(IntPtr dpy);

        [DllImport("libGL", EntryPoint = "glXChooseVisual", ExactSpelling = true)]
        public static extern IntPtr ChooseVisual(IntPtr dpy, int screen, int[] attribList);

        public const int RGBA   =   4;
        public const int DOUBLEBUFFER   =5;
        public const int RED_SIZE   =   8;
        public const int GREEN_SIZE =   9;
        public const int BLUE_SIZE  =   10;
        public const int ALPHA_SIZE =   11;
        public const int DEPTH_SIZE =   12;
        public const int None = 0x8000;
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Hope this works!");

            //Get DC
            IntPtr dc = GLX.XOpenDisplay(new IntPtr(0));
            int screen = GLX.XDefaultScreen(dc);

            //Set BackBuffer format
            int[] attrListDbl =
            {
                GLX.RGBA,
                GLX.DOUBLEBUFFER,
                GLX.RED_SIZE, 8,
                GLX.GREEN_SIZE, 8,
                GLX.BLUE_SIZE, 8,
                GLX.DEPTH_SIZE, 16,
                0
            };

            IntPtr visual = GLX.ChooseVisual(dc, screen, attrListDbl);
            if (visual == IntPtr.Zero)
            {
                int[] attrListSgl =
                {
                    GLX.RGBA,
                    GLX.RED_SIZE, 8,
                    GLX.GREEN_SIZE, 8,
                    GLX.BLUE_SIZE, 8,
                    GLX.DEPTH_SIZE, 16,
                    0
                };

                visual = GLX.ChooseVisual(dc, screen, attrListSgl);
            }

            if (visual == IntPtr.Zero) 
            {
                Console.WriteLine("Failed to get visual."); 
            }
            else
            {
                Console.WriteLine("Yahoo.");    
            }

            //ctx = GLX.CreateContext(dc, visual, new IntPtr(0), true);
            //GLX.MakeCurrent(dc, handle, ctx);
        }
    }
}

Solution

  • I found the issue. Don't know why I didnt have this issue a year ago but I just had to change "libGL" to "libgl.so.1" and it works.