Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 846 times)
0 Members and 1 Guest are viewing this topic.
Offline olavgg

Innocent Bystander





« Posted 2008-05-02 17: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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!