...
 
Commits (4)
......@@ -50,22 +50,38 @@ endif()
set(JOGL_VERSION "2.3.2")
option(VTK_JAVA_JOGL_COMPONENT "Need JOGL jar files" OFF)
if(VTK_JAVA_JOGL_COMPONENT)
if(VTK_JAVA_JOGL_COMPONENT OR VTK_JAVA_SWT_COMPONENT )
set(MAVEN_REPO "$ENV{HOME}/.m2/repository")
find_file(JOGL_LIB jogl-all-${JOGL_VERSION}.jar PATHS ${MAVEN_REPO}/org/jogamp/jogl/jogl-all/${JOGL_VERSION} )
find_file(JOGL_GLUE gluegen-rt-${JOGL_VERSION}.jar PATHS ${MAVEN_REPO}/org/jogamp/gluegen/gluegen-rt/${JOGL_VERSION})
mark_as_advanced(JOGL_LIB JOGL_GLUE)
if(NOT JOGL_LIB)
message(ERROR "
CMake can not build VTK with VTK_JAVA_JOGL_COMPONENT ON if the JOGL jar file is not provided.
Please set the JOGL_LIB variable.
")
if(VTK_JAVA_JOGL_COMPONENT)
if(NOT JOGL_LIB)
message(ERROR "
CMake can not build VTK with VTK_JAVA_JOGL_COMPONENT ON if the JOGL jar file is not provided.
Please set the JOGL_LIB variable.
")
endif()
if(NOT JOGL_GLUE)
message(ERROR "
CMake can not build VTK with VTK_JAVA_JOGL_COMPONENT ON if the GLUE jar file is not provided.
Please set the JOGL_GLUE variable.
")
endif()
endif()
if(NOT JOGL_GLUE)
message(ERROR "
CMake can not build VTK with VTK_JAVA_JOGL_COMPONENT ON if the GLUE jar file is not provided.
Please set the JOGL_GLUE variable.
")
if(VTK_JAVA_SWT_COMPONENT)
if(NOT JOGL_LIB)
message(ERROR "
CMake can not build VTK with VTK_JAVA_SWT_COMPONENT ON if the JOGL jar file is not provided.
Please set the JOGL_LIB variable.
")
endif()
if(NOT JOGL_GLUE)
message(ERROR "
CMake can not build VTK with VTK_JAVA_SWT_COMPONENT ON if the GLUE jar file is not provided.
Please set the JOGL_GLUE variable.
")
endif()
endif()
endif()
......
package vtk.rendering.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.GLRunnable;
import com.jogamp.opengl.swt.GLCanvas;
import vtk.vtkObject;
/**
* @author Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
* @author Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
* @copyright This work was supported by CEA/CESTA
* Commissariat a l'Energie Atomique et aux Energies Alternatives,
* 15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
......@@ -19,21 +23,24 @@ public class vtkInternalSwtComponent extends GLCanvas implements Listener {
private vtkSwtComponent parent;
public static GLData GetGLData() {
public static GLCapabilities GetGLCapabilities() {
GLCapabilities caps;
caps = new GLCapabilities(GLProfile.get(GLProfile.GL2GL3));
caps.setDoubleBuffered(true);
caps.setHardwareAccelerated(true);
caps.setSampleBuffers(false);
caps.setNumSamples(4);
GLData gl = new GLData();
gl.doubleBuffer = true;
gl.depthSize = 1; // must be set to something on Linux
return gl;
return caps;
}
public vtkInternalSwtComponent(vtkSwtComponent parent, Composite parentComposite) {
super(parentComposite, SWT.NO_BACKGROUND, GetGLData());
super(parentComposite, SWT.NO_BACKGROUND, GetGLCapabilities(), null);
this.parent = parent;
vtkSwtInteractorForwarderDecorator forwarder =
(vtkSwtInteractorForwarderDecorator)this.parent.getInteractorForwarder();
vtkSwtInteractorForwarderDecorator forwarder = (vtkSwtInteractorForwarderDecorator) this.parent
.getInteractorForwarder();
this.addMouseListener(forwarder);
this.addKeyListener(forwarder);
......@@ -51,26 +58,64 @@ public class vtkInternalSwtComponent extends GLCanvas implements Listener {
protected void IntializeRenderWindow() {
setCurrent(); // need to be done so SetWindowIdFromCurrentContext can get the current context!
parent.getRenderWindow().InitializeFromCurrentContext();
// setCurrent(); // need to be done so SetWindowIdFromCurrentContext can
// get the current context!
// Context is not created until the first draw call. The renderer isn't
// initialized until the context is
// present.
invoke(false, new GLRunnable() {
@Override
public boolean run(GLAutoDrawable arg0) {
// This makes this thread (should be the main thread) current
getContext().makeCurrent();
parent.getRenderWindow().InitializeFromCurrentContext();
// Swapping buffers is handled by the vtkSwtComponent
parent.getRenderWindow().SwapBuffersOff();
return false;
}
});
// Swap buffers to trigger context creation
swapBuffers();
setAutoSwapBufferMode(false);
}
@Override
public void update() {
super.update();
parent.Render();
if (isRealized()) {
parent.Render();
}
}
@Override
public void dispose() {
this.removeListener(SWT.Paint, this);
this.removeListener(SWT.Close, this);
this.removeListener(SWT.Dispose, this);
this.removeListener(SWT.Resize, this);
if (getContext().isCurrent()) {
getContext().release();
}
super.dispose();
}
@Override
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Paint:
parent.Render();
if (isRealized()) {
parent.Render();
}
break;
case SWT.Dispose:
parent.Delete();
vtkObject.JAVA_OBJECT_MANAGER.gc(false);
break;
case SWT.Close:
//System.out.println("closing");
// System.out.println("closing");
break;
case SWT.Resize:
parent.setSize(getClientArea().width, getClientArea().height);
......
package vtk.rendering.swt;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.widgets.Composite;
import com.jogamp.opengl.swt.GLCanvas;
import vtk.vtkRenderWindow;
import vtk.rendering.vtkAbstractComponent;
/**
* Provide SWT based vtk rendering component
*
* @author Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
* @author Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
* @copyright This work was supported by CEA/CESTA
* Commissariat a l'Energie Atomique et aux Energies Alternatives,
* 15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
......@@ -28,13 +29,18 @@ public class vtkSwtComponent extends vtkAbstractComponent<GLCanvas> {
this.eventForwarder = new vtkSwtInteractorForwarderDecorator(this, this.eventForwarder);
this.isWindowCreated = true;
this.uiComponent = new vtkInternalSwtComponent(this, parentComposite);
renderWindow.AddObserver("StartEvent", this, "startEvent");
renderWindow.AddObserver("EndEvent", this, "endEvent");
}
/**
* Set the size of the VTK component
*
* @param x width
* @param y height
*/
@Override
public void setSize(int x, int y) {
x = x < 1 ? 1 : x;
y = y < 1 ? 1 : y;
......@@ -48,6 +54,7 @@ public class vtkSwtComponent extends vtkAbstractComponent<GLCanvas> {
* Render the VTK component. Should not be called externally.
* Call update() to refresh the window content.
*/
@Override
public void Render() {
// Make sure we can render
if (inRenderCall || renderer == null || renderWindow == null) {
......@@ -80,10 +87,12 @@ public class vtkSwtComponent extends vtkAbstractComponent<GLCanvas> {
* @return the encapsulated SWT component (a GLCanvas instance)
* @see vtk.rendering.vtkAbstractComponent#getComponent()
*/
@Override
public GLCanvas getComponent() {
return this.uiComponent;
}
@Override
public void Delete() {
this.lock.lock();
// We prevent any further rendering
......@@ -109,4 +118,19 @@ public class vtkSwtComponent extends vtkAbstractComponent<GLCanvas> {
protected void updateInRenderCall(boolean value) {
this.inRenderCall = value;
}
/** This method is called by the VTK JNI code. Do not remove. */
void startEvent() {
if (!getComponent().getContext().isCurrent()) {
getComponent().getContext().makeCurrent();
}
}
/** This method is called by the VTK JNI code. Do not remove. */
void endEvent() {
if (getComponent().getContext().isCurrent()) {
getComponent().swapBuffers();
getComponent().getContext().release();
}
}
}
package vtk.sample.rendering;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import vtk.vtkActor;
import vtk.vtkConeSource;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.rendering.swt.vtkSwtComponent;
public class SwtConeRendering {
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static {
if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
if (!lib.IsLoaded()) {
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell(display);
// build VTK Pipeline
vtkConeSource cone = new vtkConeSource();
cone.SetResolution(8);
vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
coneMapper.SetInputConnection(cone.GetOutputPort());
vtkActor coneActor = new vtkActor();
coneActor.SetMapper(coneMapper);
// VTK rendering part
vtkSwtComponent swtWidget = new vtkSwtComponent(shell);
swtWidget.getRenderer().AddActor(coneActor);
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
package vtk.sample.rendering;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import vtk.vtkActor;
import vtk.vtkConeSource;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.rendering.swt.vtkSwtComponent;
public class SwtConeRendering {
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static {
if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
if (!lib.IsLoaded()) {
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// build VTK Pipeline
vtkConeSource cone = new vtkConeSource();
cone.SetResolution(8);
vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
coneMapper.SetInputConnection(cone.GetOutputPort());
vtkActor coneActor = new vtkActor();
coneActor.SetMapper(coneMapper);
// VTK rendering part
vtkSwtComponent swtWidget = new vtkSwtComponent(shell);
swtWidget.getRenderer().AddActor(coneActor);
shell.addControlListener(new ControlListener() {
@Override
public void controlResized(ControlEvent e) {
if (e.widget instanceof Shell) {
Shell s = (Shell) e.widget;
swtWidget.setSize(s.getClientArea().width, s.getClientArea().height);
}
}
@Override
public void controlMoved(ControlEvent e) {
// TODO Auto-generated method stub
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}