Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Minimal GLX OpenGL3.0 example

  1. #1
    Junior Member Regular Contributor oc2k1's Avatar
    Join Date
    Jan 2007
    Location
    germany
    Posts
    170

    Minimal GLX OpenGL3.0 example

    New drivers OpenGl 3.0 capable drivers are out, but nowhere is an example to test it or start own experiments. Here is my minimal c++ example:

    Code :
    #include <GL/glx.h>
    #include <GL/gl.h>
    #include <unistd.h>
    #include <iostream>
     
    #define GLX_CONTEXT_MAJOR_VERSION_ARB		0x2091
    #define GLX_CONTEXT_MINOR_VERSION_ARB		0x2092
    typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
     
     
    int main (int argc, char ** argv){
    	Display *dpy = XOpenDisplay(0);
     
    	int nelements;
    	GLXFBConfig *fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), 0, &amp;nelements);
     
    	static int attributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None };
    	XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy),attributeList);
     
    	XSetWindowAttributes swa;
    	swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
    	swa.border_pixel = 0;
    	swa.event_mask = StructureNotifyMask;
    	Window win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 100, 100, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &amp;swa);
     
    	XMapWindow (dpy, win);
     
    	//oldstyle context:
    	//	GLXContext ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
     
    	std::cout << "glXCreateContextAttribsARB " << (void*) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB") << std::endl;
    	GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
     
    	int attribs[] = {
    		GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
    		GLX_CONTEXT_MINOR_VERSION_ARB, 0,
    		0};
     
    	GLXContext ctx = glXCreateContextAttribsARB(dpy, *fbc, 0, true, attribs);
     
    	glXMakeCurrent (dpy, win, ctx);
     
    		glClearColor (0, 0.5, 1, 1);
    		glClear (GL_COLOR_BUFFER_BIT);
    		glXSwapBuffers (dpy, win);
     
    		sleep(1);
     
    		glClearColor (1, 0.5, 0, 1);
    		glClear (GL_COLOR_BUFFER_BIT);
    		glXSwapBuffers (dpy, win);
     
    		sleep(1);
     
    	ctx = glXGetCurrentContext(); 
    	glXDestroyContext(dpy, ctx); 
    	}
    g++ gl3example.cpp -o gl3example -lX11 -lGL

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2007
    Location
    Italy
    Posts
    15

    Re: Minimal GLX OpenGL3.0 example

    Thanks a lot.
    Best regards,

    Manuel

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    1

    Re: Minimal GLX OpenGL3.0 example

    one issue:

    it should be

    static int attributeList[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None };

    and then

    GLXFBConfig *fbc = glXChooseFBConfig(dpy, vi->screen, attributeList, &nelements);

    and somewhere after that:

    XVisualInfo *vi = glXGetVisualFromFBConfig(dpy, fbc[0]);

    otherwise you get a badmatch when calling MakeCurrent().

  4. #4
    Member Regular Contributor
    Join Date
    Oct 2006
    Posts
    352

    Re: Minimal GLX OpenGL3.0 example

    I am bumping this thread to post some updated code for OpenGL 3.0 on GLX. I wouldn't normally do that, but this thread is showing up prominently in search results and the current code is flawed (works on nvidia but not on ati drivers).

    As before, save as gl3.cpp and compile with "g++ gl3.cpp -o gl3 -lX11 -lGL".
    Code :
    #include <GL/glx.h>
    #include <GL/gl.h>
    #include <unistd.h>
    #include <iostream>
     
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
     
    #define GLX_CONTEXT_MAJOR_VERSION_ARB       0x2091
    #define GLX_CONTEXT_MINOR_VERSION_ARB       0x2092
    typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
     
    int main (int argc, char ** argv)
    {
        Display *display = XOpenDisplay(0);
     
        glXCreateContextAttribsARBProc glXCreateContextAttribsARB = NULL;
     
        const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
        std::cout << extensions << std::endl;
     
        static int visual_attribs[] =
        {
            GLX_RENDER_TYPE, GLX_RGBA_BIT,
            GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
            GLX_DOUBLEBUFFER, true,
            GLX_RED_SIZE, 1,
            GLX_GREEN_SIZE, 1,
            GLX_BLUE_SIZE, 1,
            None
         };
     
        std::cout << "Getting framebuffer config" << std::endl;
        int fbcount;
        GLXFBConfig *fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &amp;fbcount);
        if (!fbc)
        {
            std::cout << "Failed to retrieve a framebuffer config" << std::endl;
            return 1;
        }
     
        std::cout << "Getting XVisualInfo" << std::endl;
        XVisualInfo *vi = glXGetVisualFromFBConfig(display, fbc[0]);
     
        XSetWindowAttributes swa;
        std::cout << "Creating colormap" << std::endl;
        swa.colormap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone);
        swa.border_pixel = 0;
        swa.event_mask = StructureNotifyMask;
     
        std::cout << "Creating window" << std::endl;
        Window win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, 100, 100, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &amp;swa);
        if (!win)
        {
            std::cout << "Failed to create window." << std::endl;
            return 1;
        }
     
        std::cout << "Mapping window" << std::endl;
        XMapWindow(display, win);
     
        // Create an oldstyle context first, to get the correct function pointer for glXCreateContextAttribsARB
        GLXContext ctx_old = glXCreateContext(display, vi, 0, GL_TRUE);
        glXCreateContextAttribsARB =  (glXCreateContextAttribsARBProc)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
        glXMakeCurrent(display, 0, 0);
        glXDestroyContext(display, ctx_old);
     
        if (glXCreateContextAttribsARB == NULL)
        {
            std::cout << "glXCreateContextAttribsARB entry point not found. Aborting." << std::endl;
            return false;
        }
     
        static int context_attribs[] =
        {
            GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
            GLX_CONTEXT_MINOR_VERSION_ARB, 0,
            None
        };
     
        std::cout << "Creating context" << std::endl;
        GLXContext ctx = glXCreateContextAttribsARB(display, fbc[0], NULL, true, context_attribs);
        if (!ctx)
        {
            std::cout << "Failed to create GL3 context." << std::endl;
            return 1;
        }
     
        std::cout << "Making context current" << std::endl;
        glXMakeCurrent(display, win, ctx);
     
            glClearColor (0, 0.5, 1, 1);
            glClear (GL_COLOR_BUFFER_BIT);
            glXSwapBuffers (display, win);
     
            sleep(1);
     
            glClearColor (1, 0.5, 0, 1);
            glClear (GL_COLOR_BUFFER_BIT);
            glXSwapBuffers (display, win);
     
            sleep(1);
     
        ctx = glXGetCurrentContext();
        glXMakeCurrent(display, 0, 0);
        glXDestroyContext(display, ctx);
    }
    [The Open Toolkit library: C# OpenGL 4.4, OpenGL ES 3.1, OpenAL 1.1 for Mono/.Net]

  5. #5
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,213

    Re: Minimal GLX OpenGL3.0 example

    Quote Originally Posted by Stephen A
    OpenGL 3.0 on GLX....the current code is flawed (works on nvidia but not on ati drivers)...As before, save as gl3.cpp and compile with "g++ gl3.cpp -o gl3 -lX11 -lGL".
    On GL2.1 (NVidia GL3 capable drivers, but GL2.1-capable hardware (GeForce 6800 Ultra)), I get a fatal on X error:

    Code :
    > ./gl3
    GLX_EXT_visual_info GLX_EXT_visual_rating GLX_SGIX_fbconfig GLX_SGIX_pbuffer GLX_SGI_video_sync GLX_SGI_swap_control GLX_EXT_texture_from_pixmap GLX_ARB_create_context GLX_ARB_multisample GLX_NV_float_buffer GLX_ARB_fbconfig_float GLX_ARB_get_proc_address
    Getting framebuffer config
    Getting XVisualInfo
    Creating colormap
    Creating window
    Mapping window
    Creating context
    Making context current
    X Error of failed request:  BadAlloc (insufficient resources for operation)
      Major opcode of failed request:  144 (GLX)
      Minor opcode of failed request:  34 ()
      Serial number of failed request:  30
      Current serial number in output stream:  31
    libxcb: WARNING! Program tries to lock an already locked connection,
            which indicates a programming error.
            There will be no further warnings about this issue.

    GL_VERSION is "2.1.2 NVIDIA 180.29".

  6. #6
    Junior Member Regular Contributor
    Join Date
    Feb 2006
    Location
    greece
    Posts
    181

    Re: Minimal GLX OpenGL3.0 example

    That's normal behavior I think
    while(1){keyboardsolo(FORTE, BPM_190);}

  7. #7
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,213

    Re: Minimal GLX OpenGL3.0 example

    Quote Originally Posted by Y-tension
    That's normal behavior I think
    Well, I guess my implicit point was there's no reason to check or successful creation if the code is just going to core dump on a non-GL3 box. Therefore either the current checks are insufficent, or there's a bug in NVidia's GL3 API implementation.

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    3

    Re: Minimal GLX OpenGL3.0 example

    Okay. I get an Segmentation Fault from the source of the first posting:

    rain@rain:~/Dokumente/OpenGL/Minimal Example$ ./gl3example.cpp
    glXCreateContextAttribsARB 0
    Segmentation fault

    An this is the output i get from Stephan A code:

    rain@rain:~/Dokumente/OpenGL/Minimal Example$ ./gl3exa
    GLX_ARB_get_proc_address GLX_ARB_multisample GLX_EXT_import_context GLX_EXT_visual_info GLX_EXT_visual_rating GLX_MESA_copy_sub_buffer GLX_MESA_swap_control GLX_MESA_swap_frame_usage GLX_OML_swap_method GLX_SGI_make_current_read GLX_SGI_swap_control GLX_SGI_video_sync GLX_SGIS_multisample GLX_SGIX_fbconfig GLX_SGIX_visual_select_group
    Getting framebuffer config
    Getting XVisualInfo
    Creating colormap
    Creating window
    Mapping window
    glXCreateContextAttribsARB entry point not found. Aborting.


    Can you tell me what i'm doing wrong?

  9. #9
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    3

    Re: Minimal GLX OpenGL3.0 example

    I'm getting the same problem as TheRain.It compiles fine, but running it with ./<filename> I get the same problem...

  10. #10
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    3

    Re: Minimal GLX OpenGL3.0 example

    I did some more research on the issue, though, and it is because the variable glXCreateContextAttribsARB is NULL. The only point where it can go from NULL to a value is when it is set to equal the return value of the function:

    glXCreateContextAttribsARBProc)glXGetProcAddress(( const GLubyte*)"glXCreateContextAttribsARB".
    This command returns NULL when:
    "function requested [glXCreateContextAttribsARB?] is not supported in the implementation being queried."
    I found this at http://www.opengl.org/sdk/docs/man/, however, it doesn't specify: implementation of what? Is it a graphics card issue (I have Intel, not ATI) or just my freeglut version of glx?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •