Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 810 times)
0 Members and 1 Guest are viewing this topic.
Offline olavgg

Innocent Bystander





« 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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (94 views)
2014-04-10 04:04:31

BurntPizza (154 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!