glXMakeCurrent & XMapWindow - Xwindows

This is a discussion on glXMakeCurrent & XMapWindow - Xwindows ; Hi guys I just copied-pasted some code that I found on the net to create a XWindow & I also compared it with the code which is given the OpenGl/XWindow green book. Basically the code compiled fine but the widnow ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: glXMakeCurrent & XMapWindow

  1. glXMakeCurrent & XMapWindow

    Hi guys

    I just copied-pasted some code that I found on the net to create a
    XWindow & I also compared it with the code which is given the
    OpenGl/XWindow green book. Basically the code compiled fine but the
    widnow would be displayed to the screen. I tried 2 things, adding a
    XFlush call which actually causes the window to be displayed but I also
    swapped the positions of glXMakeCurrent & XMapWindow and then the
    window got displayed too... So I am too sure why the example that I
    found on the next call glXMakeContext first and XMapWindow second and
    still probably managed somehow to get a Window displayed (when I don't)
    ?

    Here is the code that won't work but if you swap the pos of
    glXMakeCurrent & XMapWindow then it does work for me. Any idea ? Thank
    you very much, mark

    #include
    #include // xgl
    #include // openGl include filess

    //#include // openGl include files

    #include
    #include

    class XGlException
    {
    public:
    XGlException( const char* exceptionMessage ) :
    m_exceptionMessage( exceptionMessage )
    {}
    const char* what() const
    {
    return m_exceptionMessage;
    }
    private:
    const char* m_exceptionMessage;
    };

    class GlXWindow
    {
    public:
    GlXWindow( unsigned int width, unsigned int height, const char*
    windowName ) :
    m_width( width ),
    m_height( height ),
    m_windowName( windowName )
    {
    try
    {
    // Open a connection to the X server. If an unexpected condition
    // occurs print an explanation an exit
    if ( ! ( m_display = XOpenDisplay ( "" ) ) )
    {
    throw( XGlException( "can't create display" ) );
    }

    // Make sure OpenGl's GLX extension is supported. The glXQuery-
    // Extension also return the GLX extension's error base and event
    base.
    // For almost all openGl programs, this information is
    irrelevant.
    int temp;
    if ( ! glXQueryExtension( m_display, &temp, &temp ) )
    {
    throw( XGlException( "X server has no openGL GLX extensions" )
    );
    }

    int screenNumber ;
    Window root;

    screenNumber = DefaultScreen( m_display );
    root = RootWindow( m_display, screenNumber );

    // Find an appropriate openGl capable visual. Look for a double
    // buffering first. If not found, settle for a single buffered
    visual.
    int visualAttributes[] =
    {
    GLX_RGBA,
    GLX_RED_SIZE, 8,
    GLX_GREEN_SIZE, 8,
    GLX_BLUE_SIZE, 8,
    GLX_DOUBLEBUFFER,
    GLX_DEPTH_SIZE, 1,
    None
    };

    XVisualInfo* visualInfo;

    visualInfo = glXChooseVisual( m_display, screenNumber,
    visualAttributes );

    if ( visualInfo == NULL )
    {
    throw( XGlException( "couldn't get an RGBA, double-buffered
    visual" ) );
    }

    // Create a X window with the selected visual. Since the visual
    selected
    // is likely not to be the default, create a X colormap for use.
    XSetWindowAttributes windowAttributes;

    windowAttributes.background_pixel = 0;
    windowAttributes.border_pixel = 0;
    windowAttributes.colormap = XCreateColormap ( m_display, root,
    visualInfo->visual, AllocNone );
    windowAttributes.event_mask = StructureNotifyMask |
    ExposureMask |
    KeyPressMask;

    unsigned long mask;

    mask = CWBackPixel |
    CWBorderPixel |
    CWColormap |
    CWEventMask;

    m_window = XCreateWindow( m_display, root, 0, 0, m_width,
    m_height,
    0, visualInfo->depth, InputOutput, visualInfo->visual, mask,
    &windowAttributes );

    // Set hints & properties
    XSizeHints sizeHints;
    sizeHints.x = 0;
    sizeHints.y = 0;
    sizeHints.width = m_width;
    sizeHints.height = m_height;
    sizeHints.flags = USSize | USPosition;
    XSetNormalHints( m_display, m_window, &sizeHints );
    XSetStandardProperties( m_display, m_window, m_windowName,
    m_windowName,
    None, (char **)NULL, 0, &sizeHints );

    // create an openGl rendering context
    m_windowGlXContext = glXCreateContext( m_display, visualInfo,
    NULL,
    GL_TRUE );

    if ( m_windowGlXContext == NULL)
    {
    throw( XGlException( "glXCreateContext failed") );
    }

    XFree( visualInfo );

    // Bind the rendering context to the window
    if ( ! glXMakeCurrent( m_display, m_window, m_windowGlXContext )
    )
    {
    throw( XGlException( "glXMakeCurrent failed") );
    }

    // Request that the X window be displayed on the screen
    XMapWindow( m_display, m_window );

    //XFlush( m_display );

    std::cerr << "just created the window" << std::endl;

    // Step 8: configure the openGL context for rendering
    //glEnable( GL_DEPTH_TEST );
    //glMatrixMode( GL_PROJECTION );
    //glLoadIdentity();
    //glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
    }
    catch( const XGlException& e )
    {
    std::cerr << e.what() << std::endl;
    exit( 0 );
    }
    }

    ~GlXWindow()
    {
    glXDestroyContext( m_display, m_windowGlXContext );
    XDestroyWindow( m_display, m_window );

    //glXDestroyContext(display, pbuffer_context);
    //glXDestroyPbuffer(display, pbuffer);

    XCloseDisplay( m_display );
    }

    private:
    unsigned int m_width;
    unsigned int m_height;
    const char* m_windowName;

    Window m_window;
    Display* m_display;
    GLXContext m_windowGlXContext;
    };

    int main()
    {
    GlXWindow window( 200, 200, "test" );
    while( 1 )
    {

    }
    return 0;
    }


  2. Re: glXMakeCurrent & XMapWindow

    mast2as@yahoo.com wrote:
    > Hi guys
    >
    > I just copied-pasted some code that I found on the net to create a
    > XWindow & I also compared it with the code which is given the
    > OpenGl/XWindow green book. Basically the code compiled fine but the
    > widnow would be displayed to the screen. I tried 2 things, adding a
    > XFlush call which actually causes the window to be displayed but I also
    > swapped the positions of glXMakeCurrent & XMapWindow and then the
    > window got displayed too... So I am too sure why the example that I
    > found on the next call glXMakeContext first and XMapWindow second and
    > still probably managed somehow to get a Window displayed (when I don't)
    > ?



    I've seen code like that work on some adapters/X servers and fail on
    others. The OpenGL rendering doesn't happen unless the window is
    mapped. In some cases the window mapping happens before the OpenGL
    data is sent to the adapter. Other times, the mapping comes later.

    We found that we needed to do things like:

    glXMakeCurrent(m_display, m_window, m_windowGlXContext);
    XMapWindow(m_display, m_window);
    XIfEvent(m_display, &event, WaitForMapNotify, m_window);
    XSync(m_display, 1);

    where

    Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
    { return (e->type == MapNotify) &&
    (e->xmap.window == (Window)arg); }


    in order to guarantee that the window is mapped before the program
    continues on to do the OpenGL rendering.

    Jim Lahue

  3. Re: glXMakeCurrent & XMapWindow


    Great! Thank you.

    Jim Lahue wrote:
    adapter. Other times, the mapping comes later.
    >
    > We found that we needed to do things like:
    >
    > glXMakeCurrent(m_display, m_window, m_windowGlXContext);
    > XMapWindow(m_display, m_window);
    > XIfEvent(m_display, &event, WaitForMapNotify, m_window);
    > XSync(m_display, 1);
    >
    > where
    >
    > Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
    > { return (e->type == MapNotify) &&
    > (e->xmap.window == (Window)arg); }
    >
    >
    > in order to guarantee that the window is mapped before the program
    > continues on to do the OpenGL rendering.
    >
    > Jim Lahue



+ Reply to Thread