SWT binding

i’m trying to use SWT as the rendering context for a GL binding and there were some posts in the archive that suggested lwjgl provided this. however, the latest cvs pull doesn’t contain anything.

but maybe someone can help me. i’ve been trying to modify the bindings i’ve found here

http://dev.eclipse.org/viewcvs/index.cgi/platform-swt-home/opengl/opengl.html?rev=1.6

and this derivative

http://www.realityinteractive.com/software/oss/

my plan was to get this working with the provided (basic) opengl binding, then modify it to use lwjgl or jogl, but this window system binding is not my cup of tea and going through lwjgl/jogl source today was not illuminating.

i’m able to create a shell and apparently attach an opengl context, but nothing happens when i call opengl commands. what is required to bind the opengl library to a rendering context?

any help would be appreciated.

sean

-------- attached test files -----------

my modified SwtGLContext file:

public class SwtGLContext
{
private int mContextHandle;
private Control mControl;
private int mGcHandle;
private GCData mGcData;

private int mXDisplayHandle;
private int mXDrawableHandle;

/**
 * Constructs a SwtGLContext object.
 * @param pControl
 */
public SwtGLContext(Control pControl)
{
    if (pControl == null)
    {
        SWT.error(SWT.ERROR_NULL_ARGUMENT);
    }

    mControl = pControl;

    mGcData = new GCData();
    mGcHandle = pControl.internal_new_GC(mGcData);
    if (mGcHandle == 0)
    {
        SWT.error(SWT.ERROR_NO_HANDLES);
    }

    mXDisplayHandle = OS.gdk_x11_drawable_get_xdisplay(mGcData.drawable);
    mXDrawableHandle = OS.gdk_x11_drawable_get_xid(mGcData.drawable);

    int vAttributes[] = {
        XGL.GLX_RGBA,
        XGL.GLX_DOUBLEBUFFER,
        0 };

    int vScreen = OS.XDefaultScreen(mXDisplayHandle);
    int vVisualInfoHandle =
        XGL.glXChooseVisual(mXDisplayHandle, vScreen, vAttributes);
    if (vVisualInfoHandle == 0)
    {
        SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
    }

    XVisualInfo vVisualInfo = new XVisualInfo();
    XGL.memmove(vVisualInfo, vVisualInfoHandle, XVisualInfo.sizeof);
    //OS.XFree(vVisualInfoHandle);  // no longer provided


    mContextHandle = XGL.glXCreateContext(mXDisplayHandle, vVisualInfo, 0, true);
    if (mContextHandle == 0)
    {
        SWT.error(SWT.ERROR_NO_HANDLES);
    }
}

public void makeCurrent()
{
    int vCurrent = XGL.glXGetCurrentContext();
    // Check to see if context is already current
    if (vCurrent == mContextHandle)
    {
        return;
    }

    XGL.glXMakeCurrent(mXDisplayHandle, mXDrawableHandle, mContextHandle);
}

public void swapBuffers()
{
    XGL.glXSwapBuffers(mXDisplayHandle, mXDrawableHandle);
}

public void dispose()
{
    // If the context is current, disable
    if (mContextHandle != 0)
    {
        if (XGL.glXGetCurrentContext() == mContextHandle)
        {
            XGL.glXMakeCurrent(mXDisplayHandle, 0, 0);
        }

        // Destroy context
        XGL.glXDestroyContext(mXDisplayHandle, mContextHandle);

        mContextHandle = 0;
    }

    if (!mControl.isDisposed())
    {
        // Dispose of GC
        mControl.internal_dispose_GC(mGcHandle, mGcData);
    }

    mControl = null;
    mGcHandle = 0;
    mGcData = null;
}

}

and the test class:

public class SwtTest
{
/**
* Constructs a SwtTest object.
*/
public SwtTest()
{
Display vDisplay = new Display();
Shell vShell = new Shell(vDisplay);
vShell.setText(“SwtTest”);

    // create the OpenGL context with the shell as its parent
    SwtGLContext vContext = new SwtGLContext(vShell);
    vContext.makeCurrent();

    vShell.open();

    while (!vShell.isDisposed())
    {
        if (!vDisplay.readAndDispatch())
        {
            vDisplay.sleep();
        }

        if (vShell.isDisposed())
        {
            break;
        }

        GL.glClearColor(1,1,1,1);
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        GL.glMatrixMode(GL.GL_MODELVIEW);
        GL.glLoadIdentity();
        GL.glBegin(GL.GL_TRIANGLE_STRIP);
        GL.glVertex3d(0, 0, 0);
        GL.glVertex3d(0, 1, 0);
        GL.glVertex3d(1, 0, 0);
        GL.glEnd();
        System.out.println("swap");
        vContext.swapBuffers();
    }

    vContext.dispose();
    vShell.dispose();
    vDisplay.dispose();
}

public static void main(String[] pArguments)
{
    new SwtTest();
}

}

The SWT stuff for SWT is not in cvs since it hasn’t been completed. I will try to get some info on the current status.

You might want to contact:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1095168790

for some info.

Right after vContext.makeCurrent(), you need to do this:


org.lwjgl.opengl.GLContext.useContext(vContext);

Cas :slight_smile:

thanks.

i’m curious as to what this is doing and why it is necessary to call this before calling opengl commands. it looks like this function checks to see if the opengl library has been loaded and, if not, loads it.

so, in order to call opengl commands, do you need to load the opengl library. can the library be loaded at any point by any thread or only the thread that will be calling the opengl commands? i’m trying to understand, generally, what is necessary in order to use the opengl library – i.e. a context must be created to draw into using glx/wgl/agl, the context must be made current, ???

thanks for your help.

sean

The short answer is - this call simply works out what extensions are available and sets the boolean values in GLContext appropriately. You don’t necessarily have to call it but if you don’t your code will likely fall over in unexpected and pesky ways.

Cas :slight_smile:

so i figured out why nothing was being displayed. the x server couldn’t find a visual that matched the attributes i provided (no depth buffer size was specified and the default bit size is zero). so it works if i change


int vAttributes[] = {
    XGL.GLX_RGBA, XGL.GLX_DOUBLEBUFFER,
    0 };

to


int vAttributes[] = {
    XGL.GLX_RGBA, XGL.GLX_DOUBLEBUFFER,
    XGL.GLX_DEPTH_SIZE, 24,
    0 };

sean