Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [Solved]Saving GLCanvas into a BufferedImage  (Read 1418 times)
0 Members and 2 Guests are viewing this topic.
Offline GLDavid

JGO n00b
*

Posts: 10


Java games rock!


« on: 2004-03-23 07:55:21 »

Hello !

Is it possible to save the GLCanvas into a BufferedImage ?
If yes, what are the methods ?

Thank you very much.

Offline Sylvie

JGO n00b
*

Posts: 34


Java games rock!


« Reply #1 on: 2004-03-23 12:15:44 »

I think this has been discussed before but this is what I have done:

float[] vPixelBuffer = new float[pWidth * pHeight * 3];

// First read the frame buffer
gl.glReadPixels(
   0,
   0,
   pWidth,
   pHeight,
   GL.GL_RGB,
   GL.GL_FLOAT,
   vPixelBuffer);

BufferedImage vBufferedImage =
   new BufferedImage(pWidth, pHeight,
       BufferedImage.TYPE_INT_RGB);

// Copy pixel data into buffered image
for (int vRow = 0; vRow < pHeight; vRow++)
{
   for (int vColumn = 0; vColumn < pWidth; vColumn++)
   {
       // Get separate color components
       int vIndex = ((vRow * pWidth) + vColumn) * 3;
       int r = (int) (vPixelBuffer[vIndex] * 255);
       int g = (int) (vPixelBuffer[vIndex + 1] * 255);
       int b = (int) (vPixelBuffer[vIndex + 2] * 255);

       /*
        * Set rgb color by shifting components into corresponding
        * integer bits.
        */
       int vRgb = (r << 16) + (g << 8) + b;

       // Set buffer image pixel -- flip y coordinate
       vBufferedImage.setRGB(
           vColumn,
           pHeight - vRow - 1,
           vRgb);
   }
}

// do something with vBufferedImage


Also, I would interested if there was a more efficient way of converting the float pixel buffer into a BufferedImage. This is the first thing I could think of and I have since moved on to more pressing things.

Sean
Offline GLDavid

JGO n00b
*

Posts: 10


Java games rock!


« Reply #2 on: 2004-03-24 14:05:37 »

Very well Sylvie !  Grin
Thank you very much, your code is working very well !

Bye bye !
Games published by our own members! Go get 'em!
Offline chris25

JGO n00b
*

Posts: 9


Java games rock!


« Reply #3 on: 2004-04-25 16:36:50 »

Hello everyone,
I am dealing with the same problem, but somehow i can't manage to get the code running right. When i create a picture from the BufferedImage, i only get a screen cotaining
the backgroundcolor from the glcanvas. It looks like the buffer is not read properly. I tried to copy the image several times while moving the object on the canvas. When the objects move outside the visible screen, somthing is showing up inside the bufferedimage, but it's only a square containing colors of the original image. Are there any parameters in the code i possibly have to adjust?

Thank you very much in advance,
Chris
Offline jan7da

JGO n00b
*

Posts: 9


Java games rock!


« Reply #4 on: 2004-04-27 15:59:25 »


I am not sure much,
but i had the same problem, where in glReadPixels was returnning all 'zeros'.

so I put glReadPixels in display() immediately after glFlush(). and things seem to work properly.

-j
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.074 seconds with 20 queries.