public void surfaceUpdated(Object updater, NativeWindow window, long when) { if (updater instanceof GLDrawable) { GLDrawable drawable = (GLDrawable) updater; GLContext ctx = GLContext.getCurrent(); if (null != ctx && ctx.getGLDrawable() == drawable) { GL gl = ctx.getGL(); // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec gl.glFinish(); readBufferUtil.fetchOffscreenTexture(drawable, gl); gl.glFinish(); try { surface2File("shot"); } catch (IOException ex) { throw new RuntimeException("can not write survace to file", ex); } } } }
public void snapshot(int sn, GL gl, String fileSuffix, String destPath) { final GLDrawable drawable = gl.getContext().getGLReadDrawable(); final String postSNDetail = String.format("awt-usr%03d", textRendererGLEL.userCounter); final String filenameAWT = getSnapshotFilename( sn, postSNDetail, drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(), glReadBufferUtil.hasAlpha(), fileSuffix, destPath); if (swapBuffersBeforeRead) { drawable.swapBuffers(); // Just to test whether we use the right buffer, // i.e. back-buffer shall no more be required .. gl.glClear(GL.GL_COLOR_BUFFER_BIT); } else { gl.glFinish(); // just make sure rendering finished .. } final boolean awtOrientation = drawable.isGLOriented(); System.err.println( Thread.currentThread().getName() + ": ** screenshot: awtOrient/v-flip " + awtOrientation + ", swapBuffersBeforeRead " + swapBuffersBeforeRead + ", " + filenameAWT); final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(gl, awtOrientation); final File fout = new File(filenameAWT); try { ImageIO.write(image, "png", fout); } catch (IOException e) { e.printStackTrace(); } /** * final String filenameJGL = getSnapshotFilename(sn, "jgl", * drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(), * glReadBufferUtil.hasAlpha(), fileSuffix, destPath); glReadBufferUtil.write(new * File(filenameJGL)); */ }
/** * Takes a snapshot of the drawable's current front framebuffer. Example filenames: * * <pre> * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testES2OffScreenFBOSglBuf____-n0001-msaa0-GLES2_-sw-fbobject-Bdbl-Frgb__Irgb_-S00_default-0400x0300.png * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testES2OffScreenPbufferDblBuf-n0003-msaa0-GLES2_-sw-pbuffer_-Bdbl-Frgb__Irgb_-S00_default-0200x0150.png * TestGLDrawableAutoDelegateOnOffscrnCapsNEWT.testGL2OffScreenPbufferSglBuf-n0003-msaa0-GL2___-hw-pbuffer_-Bone-Frgb__Irgb_-S00_default-0200x0150.png * </pre> * * @param sn sequential number * @param postSNDetail optional detail to be added to the filename after <code>sn</code> * @param gl the current GL context object. It's read drawable is being used as the pixel source * and to gather some details which will end up in the filename. * @param readBufferUtil the {@link GLReadBufferUtil} to be used to read the pixels for the * screenshot. * @param fileSuffix Optional file suffix without a <i>dot</i> defining the file type, i.e. <code> * "png"</code>. If <code>null</code> the <code>"png"</code> as defined in {@link * TextureIO#PNG} is being used. * @param destPath Optional platform dependent file path. It shall use {@link File#separatorChar} * as is directory separator. It shall not end with a directory separator, {@link * File#separatorChar}. If <code>null</code> the current working directory is being used. */ public void snapshot( int sn, String postSNDetail, GL gl, GLReadBufferUtil readBufferUtil, String fileSuffix, String destPath) { if (null == fileSuffix) { fileSuffix = TextureIO.PNG; } final int maxSimpleTestNameLen = getMaxTestNameLen() + getClass().getSimpleName().length() + 1; final String simpleTestName = this.getSimpleTestName("."); final String filenameBaseName; { final GLDrawable drawable = gl.getContext().getGLReadDrawable(); final GLCapabilitiesImmutable caps = drawable.getChosenGLCapabilities(); final String accel = caps.getHardwareAccelerated() ? "hw" : "sw"; final String scrnm; if (caps.isOnscreen()) { scrnm = "onscreen"; } else if (caps.isFBO()) { scrnm = "fbobject"; } else if (caps.isPBuffer()) { scrnm = "pbuffer_"; } else if (caps.isBitmap()) { scrnm = "bitmap__"; } else { scrnm = "unknown_"; } final String dblb = caps.getDoubleBuffered() ? "dbl" : "one"; final String F_pfmt = readBufferUtil.hasAlpha() ? "rgba" : "rgb_"; final String pfmt = caps.getAlphaBits() > 0 ? "rgba" : "rgb_"; final int samples = caps.getNumSamples(); final String aaext = caps.getSampleExtension(); postSNDetail = null != postSNDetail ? "-" + postSNDetail : ""; filenameBaseName = String.format( "%-" + maxSimpleTestNameLen + "s-n%04d%s-%-6s-%s-%s-B%s-F%s_I%s-S%02d_%s-%04dx%04d.%s", simpleTestName, sn, postSNDetail, drawable.getGLProfile().getName(), accel, scrnm, dblb, F_pfmt, pfmt, samples, aaext, drawable.getWidth(), drawable.getHeight(), fileSuffix) .replace(' ', '_'); } final String filename = null != destPath ? destPath + File.separator + filenameBaseName : filenameBaseName; System.err.println( Thread.currentThread().getName() + ": ** screenshot: " + filename + ", maxTestNameLen " + maxSimpleTestNameLen + ", <" + simpleTestName + ">"); gl.glFinish(); // just make sure rendering finished .. if (readBufferUtil.readPixels(gl, false)) { readBufferUtil.write(new File(filename)); } }
/** * Draws the five sides of the hemicube into the provided drawable * * @param drawable * @param which * @param near * @param far * @param bCollect */ public void drawHemiCube( GLAutoDrawable drawable, int which, double near, double far, boolean bCollect) { // TODO - PART 1 - DRAW HEMICUBE! GL gl = drawable.getGL(); GLU glu = new GLU(); /* Return the face based on which parameter */ int[] vertices = scene.getObject().getFace(which); /* Find the center of the face */ /* Center of the face is the average of the three vertices */ /* Using returned list of vertex indices, find the vertices * corresponding to a particular face */ Vertex v1 = scene.getObject().vertexList.get(vertices[0]); Vertex v2 = scene.getObject().vertexList.get(vertices[1]); Vertex v3 = scene.getObject().vertexList.get(vertices[2]); /* Locate center of face */ /* Average of three vertices */ Point3d centerPoint = new Point3d(); centerPoint = new Point3d(v1.p); centerPoint.add(v2.p); centerPoint.add(v3.p); centerPoint.scale(0.33333); /* Set up camera frame */ /* --- Surface normal --- */ /* Declare points of vertex for face */ Point3d p1 = new Point3d(v1.p); Point3d p2 = new Point3d(v2.p); Point3d p3 = new Point3d(v3.p); /* Declare vector u as p2-p1 */ Point3d uVec = new Point3d(p2); uVec.sub(p1); Vector3d u = new Vector3d(uVec); /* Declare vector v as p3-p1 */ Point3d vVec = new Point3d(p3); vVec.sub(p1); Vector3d v = new Vector3d(vVec); /* Make normal vector */ Vector3d norm = new Vector3d(); norm.cross(u, v); /* --- Vectors Orthonormal to Normal --- */ Point3d vec1pt = new Point3d(p1); vec1pt.sub(p2); Vector3d vec1 = new Vector3d(vec1pt); vec1.cross(vec1, norm); // Cross surface normal with vec1 to get orthogonal vector Vector3d vec2 = new Vector3d(); vec2.cross( norm, vec1); // Cross product of surface normal with new vector vec1 to get 2nd orthogonal vector /* Make unit vectors */ norm.normalize(); vec1.normalize(); vec2.normalize(); /* Set up the five different frustums, and stitch together using viewPort */ /* Viewport to set up the view of the scene */ /* ----- FRONT FACE ----- */ gl.glPushMatrix(); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight() / 3); /* Set up frustums for this face */ gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(-near, near, -near, near, near, far); /* Position camera at center of specified patch (which) */ gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt( centerPoint.x, centerPoint.y, centerPoint.z, centerPoint.x + norm.x, centerPoint.y + norm.y, centerPoint.z + norm.z, centerPoint.x + vec1.x, centerPoint.y + vec1.y, centerPoint.z + vec1.z); /* Draw the frustum to screen */ draw(drawable, scene.drawStyle.IDENT); gl.glPopMatrix(); /* ----- BOTTOM FACE ----- */ gl.glPushMatrix(); gl.glViewport(0, drawable.getHeight() / 3, drawable.getWidth(), drawable.getHeight() / 6); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(-near, near, 0, near, near, far); /* Position camera at center of specified patch (which) */ gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt( centerPoint.x, centerPoint.y, centerPoint.z, centerPoint.x + (-vec1.x), centerPoint.y + (-vec1.y), centerPoint.z + (-vec1.z), centerPoint.x + norm.x, centerPoint.y + norm.y, centerPoint.z + norm.z); /* Draw the frustum to screen */ draw(drawable, scene.drawStyle.IDENT); gl.glPopMatrix(); /* ----- TOP FACE ----- */ gl.glPushMatrix(); gl.glViewport( 0, (drawable.getHeight() / 3) + (drawable.getHeight() / 6), drawable.getWidth(), drawable.getHeight() / 6); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(-near, near, -near, 0, near, far); /* Position camera at center of specified patch (which) */ gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt( centerPoint.x, centerPoint.y, centerPoint.z, centerPoint.x + vec1.x, centerPoint.y + vec1.y, centerPoint.z + vec1.z, centerPoint.x - norm.x, centerPoint.y - norm.y, centerPoint.z - norm.z); /* Draw the frustum to screen */ draw(drawable, scene.drawStyle.IDENT); gl.glPopMatrix(); /* ----- LEFT FACE ----- */ gl.glPushMatrix(); gl.glViewport( 0, (drawable.getHeight() / 3) + 2 * (drawable.getHeight() / 6), drawable.getWidth(), drawable.getHeight() / 6); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(0, near, -near, near, near, far); /* Position camera at center of specified patch (which) */ gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt( centerPoint.x, centerPoint.y, centerPoint.z, centerPoint.x + vec2.x, centerPoint.y + vec2.y, centerPoint.z + vec2.z, centerPoint.x + vec1.x, centerPoint.y + vec1.y, centerPoint.z + vec1.z); /* Draw the frustum to screen */ draw(drawable, scene.drawStyle.IDENT); gl.glPopMatrix(); /* ----- RIGHT FACE ----- */ gl.glPushMatrix(); gl.glViewport( 0, (drawable.getHeight() / 3) + 3 * (drawable.getHeight() / 6), drawable.getWidth(), drawable.getHeight() / 6); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(near, 0, -near, near, near, far); /* Position camera at center of specified patch (which) */ gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt( centerPoint.x, centerPoint.y, centerPoint.z, centerPoint.x + (-vec2.x), centerPoint.y + (-vec2.y), centerPoint.z + (-vec2.z), centerPoint.x + vec1.x, centerPoint.y + vec1.y, centerPoint.z + vec1.z); /* Draw the frustum to screen */ draw(drawable, scene.drawStyle.IDENT); gl.glPopMatrix(); /* ---- End Frustums ---- */ // if collecting the form factors, then read back and process the data if (bCollect) { gl.glFlush(); gl.glFinish(); gl.glReadPixels( 0, 0, divisions * 2, divisions * 6, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixelDataBuffer); collectData(which); } }