Commit 640180a1 authored by Sebastien Jourdain's avatar Sebastien Jourdain

Add support for Java/JOGL graphical rendering component

This component rely on JOGL to do the proper graphical integration with the Java environment.
For instance Java7 on Mac broke VTK rendering mechanism. This type of component,
should prevent such issue by relying on a dedicated external library that just focus on OpenGL in Java context.

Change-Id: Ib0cb6ef0b04b5f6be311bc34e3565e779b65fc6b
parent 4eb4a238
......@@ -48,7 +48,10 @@ set(Module_SRCS
vtkDepthPeelingPass.cxx
vtkFrameBufferObject.cxx
vtkFrameBufferObject2.cxx
vtkRenderbuffer.cxx
vtkGLSLShader.cxx
vtkGLSLShaderDeviceAdapter.cxx
vtkGLSLShaderDeviceAdapter2.cxx
vtkGLSLShaderProgram.cxx
vtkGaussianBlurPass.cxx
vtkGenericOpenGLRenderWindow.cxx
vtkImageProcessingPass.cxx
......@@ -71,24 +74,25 @@ set(Module_SRCS
vtkOpenGLLight.cxx
vtkOpenGLLightingPainter.cxx
vtkOpenGLPainterDeviceAdapter.cxx
vtkOpenGLPolyDataMapper2D.cxx
vtkOpenGLPolyDataMapper.cxx
vtkOpenGLPolyDataMapper2D.cxx
vtkOpenGLProperty.cxx
vtkOpenGLRenderer.cxx
vtkOpenGLRenderWindow.cxx
vtkOpenGLRenderer.cxx
vtkOpenGLRepresentationPainter.cxx
vtkOpenGLScalarsToColorsPainter.cxx
vtkOpenGLState.cxx
vtkOpenGLTexture.cxx
vtkOverlayPass.cxx
vtkPixelBufferObject.cxx
vtkRenderbuffer.cxx
vtkRenderPassCollection.cxx
vtkRenderPass.cxx
vtkRenderPassCollection.cxx
vtkRenderState.cxx
vtkRenderbuffer.cxx
vtkRenderbuffer.cxx
vtkSequencePass.cxx
vtkShader2Collection.cxx
vtkShader2.cxx
vtkShader2Collection.cxx
vtkShaderProgram2.cxx
vtkShadowMapBakerPass.cxx
vtkShadowMapPass.cxx
......@@ -99,10 +103,6 @@ set(Module_SRCS
vtkUniformVariables.cxx
vtkVolumetricPass.cxx
${CMAKE_CURRENT_BINARY_DIR}/vtkgl.cxx
vtkGLSLShaderDeviceAdapter.cxx
vtkGLSLShaderDeviceAdapter2.cxx
vtkGLSLShader.cxx
vtkGLSLShaderProgram.cxx
${CMAKE_CURRENT_BINARY_DIR}/vtkRenderingOpenGLObjectFactory.cxx
)
......
......@@ -25,6 +25,9 @@ vtkStandardNewMacro(vtkGenericOpenGLRenderWindow);
vtkGenericOpenGLRenderWindow::vtkGenericOpenGLRenderWindow()
{
this->DirectStatus = 0;
this->CurrentStatus = false;
this->SupportsOpenGLStatus = 0;
}
vtkGenericOpenGLRenderWindow::~vtkGenericOpenGLRenderWindow()
......@@ -103,23 +106,20 @@ void vtkGenericOpenGLRenderWindow::MakeCurrent()
bool vtkGenericOpenGLRenderWindow::IsCurrent()
{
bool current = 0;
this->InvokeEvent(vtkCommand::WindowIsCurrentEvent, &current);
return current;
this->InvokeEvent(vtkCommand::WindowIsCurrentEvent, &this->CurrentStatus);
return this->CurrentStatus;
}
int vtkGenericOpenGLRenderWindow::SupportsOpenGL()
{
int supports_ogl = 0;
this->InvokeEvent(vtkCommand::WindowSupportsOpenGLEvent, &supports_ogl);
return supports_ogl;
this->InvokeEvent(vtkCommand::WindowSupportsOpenGLEvent, &this->SupportsOpenGLStatus);
return this->SupportsOpenGLStatus;
}
int vtkGenericOpenGLRenderWindow::IsDirect()
{
int is_direct = 0;
this->InvokeEvent(vtkCommand::WindowIsDirectEvent, &is_direct);
return is_direct;
this->InvokeEvent(vtkCommand::WindowIsDirectEvent, &this->DirectStatus);
return this->DirectStatus;
}
void vtkGenericOpenGLRenderWindow::PushState()
......@@ -246,3 +246,18 @@ void vtkGenericOpenGLRenderWindow::CreateAWindow()
void vtkGenericOpenGLRenderWindow::DestroyWindow()
{
}
void vtkGenericOpenGLRenderWindow::SetIsDirect(int newValue)
{
this->DirectStatus = newValue;
}
void vtkGenericOpenGLRenderWindow::SetSupportsOpenGL(int newValue)
{
this->SupportsOpenGLStatus = newValue;
}
void vtkGenericOpenGLRenderWindow::SetIsCurrent(bool newValue)
{
this->CurrentStatus = newValue;
}
......@@ -105,7 +105,17 @@ public:
void DestroyWindow();
// }@
// Description:
// Allow to update state within observer callback without changing
// data argument and MTime.
void SetIsDirect(int newValue);
void SetSupportsOpenGL(int newValue);
void SetIsCurrent(bool newValue);
protected:
int DirectStatus;
int SupportsOpenGLStatus;
bool CurrentStatus;
private:
vtkGenericOpenGLRenderWindow(const vtkGenericOpenGLRenderWindow&); // Not implemented.
......
......@@ -10,6 +10,15 @@ if(VTK_JAVA_SWT_COMPONENT)
find_package(SWT REQUIRED)
endif()
# Add the option for build the JOGL component to VTK.
option(VTK_JAVA_JOGL_COMPONENT "Need JOGL jar files" OFF)
if(VTK_JAVA_JOGL_COMPONENT)
set(MAVEN_REPO "$ENV{HOME}/.m2/repository")
find_file(JOGL_LIB jogl-all-2.0.2.jar PATHS ${MAVEN_REPO}/org/jogamp/jogl/jogl-all/2.0.2 )
find_file(JOGL_GLUE gluegen-rt-2.0.2.jar PATHS ${MAVEN_REPO}/org/jogamp/gluegen/gluegen-rt/2.0.2 )
endif()
set(WrappingJava_SRCS
vtkJavaUtil.cxx
)
......@@ -18,7 +27,7 @@ vtk_module_library(vtkWrappingJava ${WrappingJava_SRCS})
# javac runs out memory on Mac so increase heapsize
if(APPLE)
set(JAVAC_OPTIONS -J-Xmx256m)
set(JAVAC_OPTIONS -J-Xmx512m)
endif()
foreach(module ${VTK_MODULES_ENABLED})
......@@ -175,6 +184,12 @@ if(vtkRenderingCore_ENABLED AND vtkIOImage_ENABLED)
rendering/swt/vtkSwtInteractorForwarderDecorator
)
endif()
if(VTK_JAVA_JOGL_COMPONENT)
set(VTK_JAVA_DEPENDENCIES ${VTK_JAVA_DEPENDENCIES}
rendering/jogl/vtkJoglComponent
sample/rendering/JoglConeRendering
)
endif()
set(VTK_JAVA_SAMPLE_DEPENDENCIES ${VTK_JAVA_SAMPLE_DEPENDENCIES}
InternalFrames
Demo
......@@ -232,13 +247,29 @@ if(VTK_JAVA_SWT_COMPONENT)
set(SWT_FILES "${VTK_BINARY_DIR}/java/vtk/rendering/swt/*.java")
endif()
if(VTK_JAVA_JOGL_COMPONENT)
set(JOGL_FILES "${VTK_BINARY_DIR}/java/vtk/rendering/jogl/*.java" "${VTK_BINARY_DIR}/java/vtk/sample/rendering/JoglConeRendering.java")
endif()
# Handle Java class path
set(JAVA_COMPILATION_CLASSPATH "${VTK_JAVA_HOME}/..")
if(ECLIPSE_SWT_LIBRARIES)
set(JAVA_COMPILATION_CLASSPATH "${JAVA_COMPILATION_CLASSPATH}${SEPARATOR}${ECLIPSE_SWT_LIBRARIES}")
endif()
if(JOGL_LIB)
set(JAVA_COMPILATION_CLASSPATH "${JAVA_COMPILATION_CLASSPATH}${SEPARATOR}${JOGL_LIB}")
endif()
if(JOGL_GLUE)
set(JAVA_COMPILATION_CLASSPATH "${JAVA_COMPILATION_CLASSPATH}${SEPARATOR}${JOGL_GLUE}")
endif()
# Compile the Java classes
add_custom_command(
OUTPUT ${VTK_BINARY_DIR}/java/javac_stamp.txt
DEPENDS ${VTK_JAVA_SOURCE_FILES}
COMMAND ${JAVA_COMPILE} ${JAVAC_OPTIONS}
-source 1.5 -classpath ${VTK_JAVA_HOME}/..${SEPARATOR}${ECLIPSE_SWT_LIBRARIES} -sourcepath ${VTK_SOURCE_DIR}/Wrapping/Java/ -d ${VTK_BINARY_DIR}/java
${VTK_BINARY_DIR}/java/vtk/*.java ${VTK_BINARY_DIR}/java/vtk/rendering/*.java ${VTK_BINARY_DIR}/java/vtk/rendering/awt/*.java ${SWT_FILES}
-source 1.5 -target 1.5 -classpath ${JAVA_COMPILATION_CLASSPATH} -sourcepath ${VTK_SOURCE_DIR}/Wrapping/Java/ -d ${VTK_BINARY_DIR}/java
${VTK_BINARY_DIR}/java/vtk/*.java ${VTK_BINARY_DIR}/java/vtk/rendering/*.java ${VTK_BINARY_DIR}/java/vtk/rendering/awt/*.java ${VTK_BINARY_DIR}/java/vtk/sample/*.java ${SWT_FILES} ${JOGL_FILES}
COMMAND ${CMAKE_COMMAND} -E touch ${VTK_BINARY_DIR}/java/javac_stamp.txt
COMMENT "Compiling Java Classes"
)
......
package vtk.rendering.jogl;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import vtk.vtkGenericOpenGLRenderWindow;
import vtk.vtkObject;
import vtk.vtkOpenGLRenderWindow;
import vtk.vtkRenderWindow;
import vtk.rendering.vtkAbstractComponent;
import vtk.rendering.vtkInteractorForwarder;
/**
* Provide JOGL based rendering component for VTK
*
* @author Sebastien Jourdain - sebastien.jourdain@kitware.com
*/
public class vtkJoglComponent extends vtkAbstractComponent<GLCanvas> {
protected GLCanvas uiComponent;
protected boolean isWindowCreated;
protected GLEventListener glEventListener;
protected vtkGenericOpenGLRenderWindow glRenderWindow;
public vtkJoglComponent() {
this(new vtkGenericOpenGLRenderWindow());
}
public vtkJoglComponent(vtkRenderWindow renderWindowToUse) {
super(renderWindowToUse);
this.isWindowCreated = false;
this.uiComponent = new GLCanvas(new GLCapabilities(GLProfile.getDefault()));
this.glRenderWindow = (vtkGenericOpenGLRenderWindow) renderWindowToUse;
this.glRenderWindow.SetIsDirect(1);
this.glRenderWindow.SetSupportsOpenGL(1);
this.glRenderWindow.SetIsCurrent(true);
// Create the JOGL Event Listener
this.glEventListener = new GLEventListener() {
public void init(GLAutoDrawable drawable) {
vtkJoglComponent.this.isWindowCreated = true;
// Make sure the JOGL Context is current
GLContext ctx = drawable.getContext();
if (!ctx.isCurrent()) {
ctx.makeCurrent();
}
// Init VTK OpenGL RenderWindow
vtkJoglComponent.this.glRenderWindow.SetMapped(1);
vtkJoglComponent.this.glRenderWindow.SetPosition(0, 0);
vtkJoglComponent.this.setSize(drawable.getWidth(), drawable.getHeight());
vtkJoglComponent.this.glRenderWindow.OpenGLInit();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
vtkJoglComponent.this.setSize(width, height);
}
public void display(GLAutoDrawable drawable) {
vtkJoglComponent.this.inRenderCall = true;
vtkJoglComponent.this.glRenderWindow.Render();
vtkJoglComponent.this.inRenderCall = false;
}
public void dispose(GLAutoDrawable drawable) {
vtkJoglComponent.this.Delete();
vtkObject.JAVA_OBJECT_MANAGER.gc(false);
}
};
this.uiComponent.addGLEventListener(this.glEventListener);
// Bind the interactor forwarder
vtkInteractorForwarder forwarder = this.getInteractorForwarder();
this.uiComponent.addMouseListener(forwarder);
this.uiComponent.addMouseMotionListener(forwarder);
this.uiComponent.addKeyListener(forwarder);
// Make sure when VTK internaly request a Render, the Render get
// properly triggered
renderWindowToUse.AddObserver("WindowFrameEvent", this, "Render");
}
public GLCanvas getComponent() {
return this.uiComponent;
}
/**
* Render the internal component
*/
public void Render() {
// Make sure we can render
if (!inRenderCall) {
this.uiComponent.repaint();
}
}
/**
* @return true if the graphical component has been properly set and
* operation can be performed on it.
*/
public boolean isWindowSet() {
return this.isWindowCreated;
}
}
package vtk.sample.rendering;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import vtk.vtkActor;
import vtk.vtkConeSource;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.rendering.jogl.vtkJoglComponent;
public class JoglConeRendering {
// -----------------------------------------------------------------
// 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) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// 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
vtkJoglComponent joglWidget = new vtkJoglComponent();
joglWidget.getRenderer().AddActor(coneActor);
// UI part
JFrame frame = new JFrame("SimpleVTK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(joglWidget.getComponent(),
BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
package vtk.sample.rendering.annotation;
import javax.swing.JFrame;
import vtk.vtkActor;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment