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_OpenGL
The run method in the JavaDia class has been changed to this:
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.
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, // GLint x
0, // GLint y
640, // GLsizei width
480, // GLsizei height
GL.GL_RGB, // GLenum format
GL.GL_UNSIGNED_BYTE, // GLenum type
pixelsRGB); // GLvoid *pixels
int[] pixelInts = new int[640 * 480];
// Convert RGB bytes to ARGB ints with no transparency. Flip image vertically by reading the
// rows of pixels in the byte buffer in reverse - (0,0) is at bottom left in OpenGL.
int p = width * height * 3; // Points to first byte (red) in each row.
int q; // Index into ByteBuffer
int i = 0; // Index into target int[]
int w3 = width * 3; // Number of bytes in each row
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);
//rotateT += 0.2f;
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 