Search in sources :

Example 1 with GLAutoDrawable

use of com.jogamp.opengl.GLAutoDrawable in project gephi by gephi.

the class ScreenshotMaker method run.

@Override
public void run() {
    beforeTaking();
    try {
        // Stop display
        engine.stopDisplay();
        // Start progress
        Progress.start(progressTicket);
        Thread.sleep(100);
        final OffscreenCanvas drawable = new OffscreenCanvas(tileWidth, tileHeight, transparentBackground, antiAliasing);
        GLAutoDrawable autoDrawable = drawable.getGLAutoDrawable();
        //Tile rendering
        final TileRenderer renderer = new TileRenderer();
        renderer.setImageSize(width, height);
        renderer.setTileSize(tileWidth, tileHeight, 0);
        renderer.attachAutoDrawable(autoDrawable);
        final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider = GLPixelBuffer.defaultProviderWithRowStride;
        final boolean[] flipVertically = { false };
        final GLEventListener preTileGLEL = new GLEventListener() {

            @Override
            public void init(final GLAutoDrawable drawable) {
                final GL2 gl = drawable.getGL().getGL2();
                final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, transparentBackground ? 4 : 3, true);
                final GLPixelBuffer imageBuffer = pixelBufferProvider.allocate(gl, null, pixelAttribs, true, width, height, 1, 0);
                renderer.setImageBuffer(imageBuffer);
                flipVertically[0] = !drawable.isGLOriented();
            }

            @Override
            public void dispose(final GLAutoDrawable drawable) {
            }

            @Override
            public void display(final GLAutoDrawable drawable) {
            }

            @Override
            public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
            }
        };
        renderer.setGLEventListener(preTileGLEL, null);
        vizConfig.setDisableLOD(true);
        engine.updateLOD();
        // Render tiles
        int tiles = renderer.getParam(TileRenderer.TR_COLUMNS) * renderer.getParam(TileRenderer.TR_ROWS);
        Progress.switchToDeterminate(progressTicket, tiles);
        while (!renderer.eot() && !cancel) {
            renderer.display();
            Progress.progress(progressTicket);
        }
        Progress.switchToIndeterminate(progressTicket);
        renderer.detachAutoDrawable();
        autoDrawable.invoke(true, new GLRunnable() {

            @Override
            public boolean run(final GLAutoDrawable drawable) {
                drawable.getGL().glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
                //                drawable.reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
                return false;
            }
        });
        vizConfig.setDisableLOD(false);
        engine.updateLOD();
        if (!cancel) {
            final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
            final TextureData textureData = new TextureData(autoDrawable.getChosenGLCapabilities().getGLProfile(), transparentBackground ? GL.GL_RGBA : GL.GL_RGB, width, height, 0, imageBuffer.pixelAttributes, false, false, flipVertically[0], imageBuffer.buffer, null);
            // Get File
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    if (!autoSave) {
                        //Get last directory
                        String lastPathDefault = NbPreferences.forModule(ScreenshotMaker.class).get(LAST_PATH_DEFAULT, null);
                        String lastPath = NbPreferences.forModule(ScreenshotMaker.class).get(LAST_PATH, lastPathDefault);
                        final JFileChooser chooser = new JFileChooser(lastPath);
                        chooser.setAcceptAllFileFilterUsed(false);
                        chooser.setDialogTitle(NbBundle.getMessage(ScreenshotMaker.class, "ScreenshotMaker.filechooser.title"));
                        DialogFileFilter dialogFileFilter = new DialogFileFilter(NbBundle.getMessage(ScreenshotMaker.class, "ScreenshotMaker.filechooser.pngDescription"));
                        dialogFileFilter.addExtension("png");
                        chooser.addChoosableFileFilter(dialogFileFilter);
                        File selectedFile = new File(chooser.getCurrentDirectory(), getDefaultFileName() + ".png");
                        chooser.setSelectedFile(selectedFile);
                        int returnFile = chooser.showSaveDialog(null);
                        if (returnFile == JFileChooser.APPROVE_OPTION) {
                            ScreenshotMaker.this.file = chooser.getSelectedFile();
                            if (!ScreenshotMaker.this.file.getPath().endsWith(".png")) {
                                ScreenshotMaker.this.file = new File(ScreenshotMaker.this.file.getPath() + ".png");
                            }
                            //Save last path
                            defaultDirectory = ScreenshotMaker.this.file.getParentFile().getAbsolutePath();
                            NbPreferences.forModule(ScreenshotMaker.class).put(LAST_PATH, defaultDirectory);
                        } else {
                            ScreenshotMaker.this.file = null;
                        }
                    } else {
                        ScreenshotMaker.this.file = new File(defaultDirectory, getDefaultFileName() + ".png");
                    }
                }
            });
            // Write file
            if (file != null) {
                TextureIO.write(textureData, file);
            }
        }
        autoDrawable.destroy();
        //Reinit text renderer
        textManager.reinitRenderers();
        engine.startDisplay();
        //Progress finish
        Progress.finish(progressTicket);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    // After taking
    afterTaking();
}
Also used : GLAutoDrawable(com.jogamp.opengl.GLAutoDrawable) GL2(com.jogamp.opengl.GL2) GLEventListener(com.jogamp.opengl.GLEventListener) TextureData(com.jogamp.opengl.util.texture.TextureData) JFileChooser(javax.swing.JFileChooser) GLPixelBuffer(com.jogamp.opengl.util.GLPixelBuffer) GLPixelAttributes(com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes) GLRunnable(com.jogamp.opengl.GLRunnable) GLRunnable(com.jogamp.opengl.GLRunnable) DialogFileFilter(org.gephi.ui.utils.DialogFileFilter) File(java.io.File) TileRenderer(com.jogamp.opengl.util.TileRenderer)

Example 2 with GLAutoDrawable

use of com.jogamp.opengl.GLAutoDrawable in project jmonkeyengine by jMonkeyEngine.

the class JoglNewtAbstractDisplay method initGLCanvas.

protected void initGLCanvas() {
    GLCapabilities caps;
    if (settings.getRenderer().equals(AppSettings.JOGL_OPENGL_FORWARD_COMPATIBLE)) {
        caps = new GLCapabilities(GLProfile.getMaxProgrammable(true));
    } else {
        caps = new GLCapabilities(GLProfile.getMaxFixedFunc(true));
    }
    caps.setHardwareAccelerated(true);
    caps.setDoubleBuffered(true);
    caps.setStencilBits(settings.getStencilBits());
    caps.setDepthBits(settings.getDepthBits());
    if (settings.getSamples() > 1) {
        caps.setSampleBuffers(true);
        caps.setNumSamples(settings.getSamples());
    }
    canvas = GLWindow.create(caps);
    canvas.invoke(false, new GLRunnable() {

        public boolean run(GLAutoDrawable glad) {
            canvas.getGL().setSwapInterval(settings.isVSync() ? 1 : 0);
            return true;
        }
    });
    canvas.requestFocus();
    canvas.setSize(settings.getWidth(), settings.getHeight());
    canvas.addGLEventListener(this);
    //FIXME not sure it is the best place to do that
    renderable.set(true);
}
Also used : GLAutoDrawable(com.jogamp.opengl.GLAutoDrawable) GLCapabilities(com.jogamp.opengl.GLCapabilities) GLRunnable(com.jogamp.opengl.GLRunnable)

Example 3 with GLAutoDrawable

use of com.jogamp.opengl.GLAutoDrawable in project jmonkeyengine by jMonkeyEngine.

the class JoglAbstractDisplay method initGLCanvas.

protected void initGLCanvas() {
    device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    GLCapabilities caps;
    if (settings.getRenderer().equals(AppSettings.JOGL_OPENGL_FORWARD_COMPATIBLE)) {
        caps = new GLCapabilities(GLProfile.getMaxProgrammable(true));
    } else {
        caps = new GLCapabilities(GLProfile.getMaxFixedFunc(true));
    }
    caps.setHardwareAccelerated(true);
    caps.setDoubleBuffered(true);
    caps.setStencilBits(settings.getStencilBits());
    caps.setDepthBits(settings.getDepthBits());
    if (settings.getSamples() > 1) {
        caps.setSampleBuffers(true);
        caps.setNumSamples(settings.getSamples());
    }
    canvas = new GLCanvas(caps) {

        @Override
        public void addNotify() {
            super.addNotify();
            onCanvasAdded();
        }

        @Override
        public void removeNotify() {
            onCanvasRemoved();
            super.removeNotify();
        }
    };
    canvas.invoke(false, new GLRunnable() {

        public boolean run(GLAutoDrawable glad) {
            canvas.getGL().setSwapInterval(settings.isVSync() ? 1 : 0);
            return true;
        }
    });
    canvas.setFocusable(true);
    canvas.requestFocus();
    canvas.setSize(settings.getWidth(), settings.getHeight());
    canvas.setIgnoreRepaint(true);
    canvas.addGLEventListener(this);
    //FIXME not sure it is the best place to do that
    renderable.set(true);
}
Also used : GLCanvas(com.jogamp.opengl.awt.GLCanvas) GLAutoDrawable(com.jogamp.opengl.GLAutoDrawable) GLCapabilities(com.jogamp.opengl.GLCapabilities) GLRunnable(com.jogamp.opengl.GLRunnable)

Example 4 with GLAutoDrawable

use of com.jogamp.opengl.GLAutoDrawable in project processing by processing.

the class PSurfaceJOGL method initAnimator.

protected void initAnimator() {
    if (PApplet.platform == PConstants.WINDOWS) {
        // Force Windows to keep timer resolution high by
        // sleeping for time which is not a multiple of 10 ms.
        // See section "Clocks and Timers on Windows":
        //   https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
        Thread highResTimerThread = new Thread(() -> {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException ignore) {
            }
        }, "HighResTimerThread");
        highResTimerThread.setDaemon(true);
        highResTimerThread.start();
    }
    animator = new FPSAnimator(window, 60);
    drawException = null;
    animator.setUncaughtExceptionHandler(new GLAnimatorControl.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(final GLAnimatorControl animator, final GLAutoDrawable drawable, final Throwable cause) {
            synchronized (drawExceptionMutex) {
                drawException = cause;
                drawExceptionMutex.notify();
            }
        }
    });
    drawExceptionHandler = new Thread(new Runnable() {

        public void run() {
            synchronized (drawExceptionMutex) {
                try {
                    while (drawException == null) {
                        drawExceptionMutex.wait();
                    }
                    // System.err.println("Caught exception: " + drawException.getMessage());
                    if (drawException != null) {
                        Throwable cause = drawException.getCause();
                        if (cause instanceof ThreadDeath) {
                        // System.out.println("caught ThreadDeath");
                        // throw (ThreadDeath)cause;
                        } else if (cause instanceof RuntimeException) {
                            throw (RuntimeException) cause;
                        } else if (cause instanceof UnsatisfiedLinkError) {
                            throw new UnsatisfiedLinkError(cause.getMessage());
                        } else if (cause == null) {
                            throw new RuntimeException(drawException.getMessage());
                        } else {
                            throw new RuntimeException(cause);
                        }
                    }
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    });
    drawExceptionHandler.start();
}
Also used : FPSAnimator(com.jogamp.opengl.util.FPSAnimator) GLAutoDrawable(com.jogamp.opengl.GLAutoDrawable) GLAnimatorControl(com.jogamp.opengl.GLAnimatorControl)

Aggregations

GLAutoDrawable (com.jogamp.opengl.GLAutoDrawable)4 GLRunnable (com.jogamp.opengl.GLRunnable)3 GLCapabilities (com.jogamp.opengl.GLCapabilities)2 GL2 (com.jogamp.opengl.GL2)1 GLAnimatorControl (com.jogamp.opengl.GLAnimatorControl)1 GLEventListener (com.jogamp.opengl.GLEventListener)1 GLCanvas (com.jogamp.opengl.awt.GLCanvas)1 FPSAnimator (com.jogamp.opengl.util.FPSAnimator)1 GLPixelBuffer (com.jogamp.opengl.util.GLPixelBuffer)1 GLPixelAttributes (com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes)1 TileRenderer (com.jogamp.opengl.util.TileRenderer)1 TextureData (com.jogamp.opengl.util.texture.TextureData)1 File (java.io.File)1 JFileChooser (javax.swing.JFileChooser)1 DialogFileFilter (org.gephi.ui.utils.DialogFileFilter)1