Login  Register

Re: Java3D stereo

Posted by babor on Mar 07, 2014; 11:26am
URL: https://forum.jogamp.org/Java3D-stereo-tp4029914p4031792.html

Thanks for your comments. Surely the idea of different scenegraph is acceptable, but at the moment we'd like to keep Java3D API within our application. Changing the scenegraph will be quite a challenge for us.

In the meantime I have a kind of the solution - it works fine but is not very "graceful" :)
Following the path that AWT Component overrides GC of Canvas3D when it is added to a parent, it is possible to react on such event, find the topmost Component and reset the original Canvas3D GC to the whole hierarchy using reflection on setGraphicsConfiguration  (quite as Xerxes suggested).

A wrapped Canvas3D can look like this:
import java.awt.Component;
import java.awt.Container;
import java.awt.GraphicsConfiguration;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.lang.reflect.Method;
import javax.media.j3d.Canvas3D;

public class WrappedCanvas3D extends Canvas3D {
    
    private GraphicsConfiguration initialGC;
    
    public WrappedCanvas3D(GraphicsConfiguration graphicsConfiguration) {
        super(graphicsConfiguration);
        initialGC = this.getGraphicsConfiguration();        
        this.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if(e.getChangeFlags() == HierarchyEvent.PARENT_CHANGED) {
                    try {
                        Class c = java.awt.Component.class;
                        Method method = c.getDeclaredMethod("setGraphicsConfiguration", GraphicsConfiguration.class);
                        method.setAccessible(true);
                        Component topmostComponent = getTopmostComponent(e.getChangedParent());
                        if(topmostComponent != null && initialGC != null)
                             method.invoke(topmostComponent, initialGC);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex.getMessage());
                    }                    
                }                
            }
        });
    }
    
    private Component getTopmostComponent(Component component) {
        if(component == null)
            return null;        
        Container parent = component.getParent();
        if(parent == null || !(parent instanceof Component))
            return component;
        return getTopmostComponent((Component)parent);
    }
}