Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Offscreen rendering to an image.  (Read 730 times)
0 Members and 1 Guest are viewing this topic.
Offline olavgg

JGO Visitor




« Posted 2008-05-02 19:03:30 »

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:
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,                   // 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 Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 21:29:12

alaslipknot (69 views)
2013-05-16 21:24:48

gouessej (99 views)
2013-05-16 00:53:38

gouessej (98 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.173 seconds with 20 queries.