Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3507 Members - Latest Member: PienueDut

26. May 2012, 01:24:40 pm
Xith3D CommunityGeneral CategorySupport (Moderator: Marvin Fröhlich)Running Xith under Swt Application
Pages: [1]
Print
Author Topic: Running Xith under Swt Application  (Read 1159 times)
elijah
Enjoying the stay
*
Offline Offline

Posts: 72


View Profile
« on: 09. January 2010, 10:45:24 am »

I have been developing xith under RPC. It works prefectly. Now because of security reasons, I need to convert it to run on pure Swt Composite/Application. I attached code here. Hope somebody can help me.

Code:
package test;


import org.eclipse.swt.SWT;


public class PanelXithCanvas extends Composite {

private Xith3DEnvironment XITH3D_ENVIRONMENT = null;
private RenderLoop RENDER_LOOP;
private GLCanvas GL_CANVAS;

private int GL_WIDTH = 1000;
private int GL_HEIGHT = 1000;
private int GL_MARGIN = 2;

private Canvas3D CANVAS_3D;

private Composite XITH_COMPOSITE;
/**
* Create the composite.
* @param parent
* @param style
*/
public PanelXithCanvas(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout(SWT.HORIZONTAL));

XITH_COMPOSITE = new Composite(this, SWT.NONE);
// XITH_COMPOSITE.setLayout(new GridLayout(1, false));
XITH_COMPOSITE.setLayout(new FillLayout(SWT.HORIZONTAL));

                setSceneRecord();
}


private void ClearRenderer() {
CANVAS_3D.clear();
RENDER_LOOP.getXith3DEnvironment().getCanvas().clear();
RENDER_LOOP.getXith3DEnvironment().removeCanvas( CANVAS_3D );


RENDER_LOOP.getXith3DEnvironment().removeAllCanvas3Ds();
RENDER_LOOP.getXith3DEnvironment().removeAllBranchGraphs();
RENDER_LOOP.getXith3DEnvironment().destroy();
RENDER_LOOP.setStopOperation(StopOperation.DESTROY_AND_EXIT);
    RENDER_LOOP.end();
   
    GL_CANVAS.dispose();
}

public void setSceneRecord() {

initCommunicationToComposite();
addDisposeListener(new DisposeListener() {
public void widgetDisposed(final DisposeEvent e) {
System.out.println("PanelXithCanvas widgetDisposed");
ClearRenderer();
}
});

}

public void setSceneRecord(int glWidth, int glHeight ) {
GL_WIDTH  = glWidth;
GL_HEIGHT = glHeight;
setSceneRecord();
}

private void initCommunicationToComposite() {
//DisplayMode displayMode = DisplayModeSelector.getImplementation( OpenGLLayer.JOGL_SWT ).getBestMode( 800, 600 );
//CanvasConstructionInfo ConstructionInfo =  new CanvasConstructionInfo( displayMode, DisplayMode.WINDOWED, "TESTING" );
//CANVAS_3D = Canvas3DFactory.create( ConstructionInfo, PARENT );

        Colorf c = new Colorf( 38, 82, 245 );
CANVAS_3D = Canvas3DFactory.create(OpenGLLayer.JOGL_SWT, GL_WIDTH, GL_HEIGHT, FullscreenMode.WINDOWED_UNDECORATED, false, FSAA.OFF, XITH_COMPOSITE );
CANVAS_3D.setBackgroundColor(c);

InitGLCanvas();

XITH3D_ENVIRONMENT = createXithEnvironment();
RENDER_LOOP = new RenderLoop();
RENDER_LOOP.setMaxFPS(120f);
RENDER_LOOP.setXith3DEnvironment(XITH3D_ENVIRONMENT);
RENDER_LOOP.begin(RunMode.RUN_IN_SEPARATE_THREAD);


}

private Xith3DEnvironment createXithEnvironment() {
       
Tuple3f eyePosition = new Vector3f(0.0f, 0.0f, 10.0f);
Tuple3f viewFocus = new Vector3f(0.0f, 0.0f, 0.0f);
Tuple3f vecUp = new Vector3f(0.0f, 1.0f, 0.0f);

Xith3DEnvironment env = new Xith3DEnvironment();
env.getView().lookAt(eyePosition, viewFocus, vecUp);
env.addCanvas(CANVAS_3D);
env.checkRenderPreferences();
// env.addPerspectiveBranch(createScene());

return env;
}

private void InitGLCanvas(){
// Constraints on the GLCanvas that "holds" the Canvas3D
GridData gdcan = new GridData();
gdcan.widthHint = GL_WIDTH;
gdcan.heightHint = GL_WIDTH;
gdcan.grabExcessHorizontalSpace = true;
gdcan.grabExcessVerticalSpace = true;
gdcan.minimumHeight = GL_WIDTH ;
gdcan.minimumWidth = GL_WIDTH;

GL_CANVAS = ((CanvasPeerImplSWT) CANVAS_3D.getPeer()).getComponent();
GL_CANVAS.setLayoutData(gdcan);

GL_CANVAS.addControlListener(new ControlListener() {
public void controlMoved(ControlEvent e) {
System.out.println( " InitGLCanvas() Moved ");
}

public void controlResized(ControlEvent e) {
System.out.println( " InitGLCanvas() Resize = " + XITH_COMPOSITE.getSize() +
            " GL_CANVAS Size = " + GL_CANVAS.getSize() +
            " CANVAS_3D Size = " + CANVAS_3D.getSize());



}
});
}


@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}

The other problem I'm having is exiting the program. In eclipse after terminating the Swt app, the red button still active and I have to force an exit like System.exit(0);

Code:
package test;



import org.eclipse.swt.SWT;

public class gcEclipseTestMain {

protected Shell shell;
protected PanelXithCanvas xithCanvas;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
gcEclipseTestMain window = new gcEclipseTestMain();
window.open();
                         System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent arg0) {

}
});
shell.setSize(450, 300);
shell.setText("TESTING XITH COMPOSITE");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));

PanelXithCanvas xithCanvas = new PanelXithCanvas(shell, SWT.NONE);
}
}
« Last Edit: 09. January 2010, 10:47:47 am by elijah » Logged
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« Reply #1 on: 09. January 2010, 04:48:21 pm »

Yep, you can definitely get things running inside JFace/RCP.  You need to write a ViewPart (a JFace/RCP thing).  In http://xith.org/forum/index.php/topic,1435.0.html, you were subclassing ViewPart.  createPartControl IS the correct place to initialize the Xith3D environment and create your scenegraph.

You should not have any security issues with subclassing; however, you will have to do a lot thread control if you want your SWT controls to affect your Xith world.  Each has its own render thread which is unsynchronized for performance.  If the Xith world runs completely standalone, then you will be fine.  SWT knows how to forward all mouse/keyboard events to Xith who will take it from there.

If you want your own SWT buttons, trees, etc to cause changes inside Xith, then you will need to transfer instructions from your JFace code to your Xith code via a command pattern or something similar.  Likewise, if you want changes to your scenegraph to be reflected in SWT controls (such as those in other Views), then you'll need to transfer the instructions from the Xith thread to the SWT render thread.

Examples of how to accomplish this transfer can be found inside org.xith3d.render.jsr231.CanvasPeerImplSWT.

Marvin, can you sticky this thread?
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« Reply #2 on: 09. January 2010, 04:49:51 pm »

The reason that you are having problems with exit is that one of your threads is not terminating.  The JVM will not close on its own until all non-daemon threads have terminated gracefully.  You need to request that the Xith render loop close itself.
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic