Hello everyone!
I've just jumped on the java 3d programming train and so far I'm having a pleasant ride!
The first program I'm trying to make is some kind of a column chart program. So far I'm doing pretty good with rendering the 3D stuff, however capturing the screen without having to display anything on screen is extremly annoying to get right. I just want to make a java program which renders 3d stuff in the background, the user shouldn't see anything on his screen, there should only be created a new image file.
I've googled a bit a searched around this forum for some advice, and I found something, however I still just get a blank screenshot.
I've used the sample code which is found here:
http://en.wikipedia.org/wiki/Java_OpenGLThe run method in the JavaDia class has been changed to this:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public void run() { GLCapabilities caps = new GLCapabilities(); caps.setHardwareAccelerated(true);
GLPbuffer offscreenPBuffer = GLDrawableFactory.getFactory().createGLPbuffer(caps, new DefaultGLCapabilitiesChooser(), 640, 480, null); offscreenPBuffer.addGLEventListener( new JavaRenderer() ); offscreenPBuffer.display();
offscreenPBuffer.removeGLEventListener(new JavaRenderer()); offscreenPBuffer.destroy(); System.exit(0); } |
And after the gl.glEnd(); in the display method in the JavaRenderer Class I've added this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| gl.glReadBuffer(GL.GL_BACK); gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); int width = 640; int height = 480; ByteBuffer pixelsRGB = BufferUtil.newByteBuffer(640 * 480 * 3);
gl.glReadPixels(0, 0, 640, 480, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixelsRGB); int[] pixelInts = new int[640 * 480];
int p = width * height * 3; int q; int i = 0; int w3 = width * 3; for (int row = 0; row < height; row++) { p -= w3; q = p; for (int col = 0; col < width; col++) { int iR = pixelsRGB.get(q++); int iG = pixelsRGB.get(q++); int iB = pixelsRGB.get(q++);
pixelInts[i++] = 0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF); } }
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
bufferedImage.setRGB(0, 0, width, height, pixelInts, 0, width);
try { File file =new File("test.png"); BufferedImage img= Screenshot.readToBufferedImage(640, 480); javax.imageio.ImageIO.write(bufferedImage, "PNG", file); } catch (Exception e) {} |
What am I doing wrong? I guess it something very simple
