JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Surface2File.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.jogl.offscreen;
30
31import com.jogamp.opengl.*;
32
33import com.jogamp.opengl.util.GLReadBufferUtil;
34
35import java.io.File;
36import java.io.IOException;
37import java.io.PrintWriter;
38import java.io.StringWriter;
39
40import com.jogamp.nativewindow.*;
41
42public class Surface2File implements SurfaceUpdatedListener {
43
44 final String filename;
45 final boolean alpha;
46 final GLReadBufferUtil readBufferUtil;
47 int shotNum = 0;
48
49 public Surface2File(final String filename, final boolean alpha) {
50 this.filename = filename;
51 this.alpha = alpha;
52 this.readBufferUtil = new GLReadBufferUtil(alpha, false);
53 }
54
55 public void dispose(final GL gl) {
56 readBufferUtil.dispose(gl);
57 }
58
59 public void surfaceUpdated(final Object updater, final NativeSurface ns, final long when) {
60 if (updater instanceof GLDrawable) {
61 final GLDrawable drawable = (GLDrawable) updater;
62 final GLContext ctx = GLContext.getCurrent();
63 if (null != ctx && ctx.getGLDrawable() == drawable) {
64 final GL gl = ctx.getGL();
65 // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec
66 gl.glFinish();
67 if(readBufferUtil.readPixels(gl, false)) {
68 gl.glFinish();
69 try {
71 } catch (final IOException ex) {
72 throw new RuntimeException("can not write survace to file", ex);
73 }
74 }
75 }
76 }
77 }
78
79 public void surface2File() throws IOException {
80 if (!readBufferUtil.isValid()) {
81 return;
82 }
83 final StringWriter sw = new StringWriter();
84 {
85 final String pfmt = alpha ? "rgba" : "rgb_" ;
86 new PrintWriter(sw).printf("%s-I_%s-%04d.png", filename, pfmt, shotNum);
87 }
88 final File file = new File(sw.toString());
89 readBufferUtil.write(file);
90 shotNum++;
91 }
92}
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
static GLContext getCurrent()
Returns this thread current context.
Definition: GLContext.java:515
abstract GL getGL()
Returns the GL pipeline object for this GLContext.
Surface2File(final String filename, final boolean alpha)
void surfaceUpdated(final Object updater, final NativeSurface ns, final long when)
Notification of a surface update event, eg.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
boolean readPixels(final GL gl, final boolean mustFlipVertically)
Read the drawable's pixels to TextureData and Texture, if requested at construction.
Provides low-level information required for hardware-accelerated rendering using a surface in a platf...
Clients may add their SurfaceUpdateListener implementation to a com.jogamp.nativewindow....
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
void glFinish()
Entry point to C language function: void {@native glFinish}() Part of GL_ES_VERSION_2_0,...