]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk.rendering/src/vtk/rendering/swt/vtkInternalSwtComponent.java
VTK.Rendering plug-in + win64 fragment
[simantics/3d.git] / vtk.rendering / src / vtk / rendering / swt / vtkInternalSwtComponent.java
1 package vtk.rendering.swt;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.widgets.Composite;
5 import org.eclipse.swt.widgets.Event;
6 import org.eclipse.swt.widgets.Listener;
7
8 import com.jogamp.opengl.GLAutoDrawable;
9 import com.jogamp.opengl.GLCapabilities;
10 import com.jogamp.opengl.GLProfile;
11 import com.jogamp.opengl.GLRunnable;
12 import com.jogamp.opengl.swt.GLCanvas;
13
14 import vtk.vtkObject;
15
16 /**
17  * @author    Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
18  * @copyright This work was supported by CEA/CESTA
19  *            Commissariat a l'Energie Atomique et aux Energies Alternatives,
20  *            15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
21  */
22 public class vtkInternalSwtComponent extends GLCanvas implements Listener {
23
24   private vtkSwtComponent parent;
25
26   public static GLCapabilities GetGLCapabilities() {
27     GLCapabilities caps;
28     caps = new GLCapabilities(GLProfile.get(GLProfile.GL2GL3));
29     caps.setDoubleBuffered(true);
30     caps.setHardwareAccelerated(true);
31     caps.setSampleBuffers(false);
32     caps.setNumSamples(4);
33     
34     caps.setAlphaBits(8);
35
36     return caps;
37   }
38
39   public vtkInternalSwtComponent(vtkSwtComponent parent, Composite parentComposite) {
40
41     super(parentComposite, SWT.NO_BACKGROUND, GetGLCapabilities(), null);
42     this.parent = parent;
43
44     vtkSwtInteractorForwarderDecorator forwarder = (vtkSwtInteractorForwarderDecorator) this.parent
45       .getInteractorForwarder();
46
47     this.addMouseListener(forwarder);
48     this.addKeyListener(forwarder);
49     this.addMouseMoveListener(forwarder);
50     this.addMouseTrackListener(forwarder);
51     this.addMouseWheelListener(forwarder);
52
53     this.addListener(SWT.Paint, this);
54     this.addListener(SWT.Close, this);
55     this.addListener(SWT.Dispose, this);
56     this.addListener(SWT.Resize, this);
57
58     this.IntializeRenderWindow();
59   }
60
61   protected void IntializeRenderWindow() {
62
63     // setCurrent(); // need to be done so SetWindowIdFromCurrentContext can
64     // get the current context!
65     // Context is not created until the first draw call. The renderer isn't
66     // initialized until the context is
67     // present.
68     invoke(false, new GLRunnable() {
69
70       @Override
71       public boolean run(GLAutoDrawable arg0) {
72         // This makes this thread (should be the main thread) current
73         getContext().makeCurrent();
74         parent.getRenderWindow().InitializeFromCurrentContext();
75         // Swapping buffers is handled by the vtkSwtComponent
76         parent.getRenderWindow().SwapBuffersOff();
77         return false;
78       }
79     });
80
81     // Swap buffers to trigger context creation
82     swapBuffers();
83     setAutoSwapBufferMode(false);
84   }
85
86   @Override
87   public void update() {
88     super.update();
89     if (isRealized()) {
90       parent.Render();
91     }
92   }
93
94   @Override
95   public void dispose() {
96     this.removeListener(SWT.Paint, this);
97     this.removeListener(SWT.Close, this);
98     this.removeListener(SWT.Dispose, this);
99     this.removeListener(SWT.Resize, this);
100
101     if (getContext().isCurrent()) {
102       getContext().release();
103     }
104     super.dispose();
105   }
106
107   @Override
108   public void handleEvent(Event event) {
109     switch (event.type) {
110     case SWT.Paint:
111       if (isRealized()) {
112         parent.Render();
113       }
114       break;
115     case SWT.Dispose:
116       parent.Delete();
117       vtkObject.JAVA_OBJECT_MANAGER.gc(false);
118       break;
119     case SWT.Close:
120       // System.out.println("closing");
121       break;
122     case SWT.Resize:
123       parent.setSize(getClientArea().width, getClientArea().height);
124       break;
125     }
126   }
127 }