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 (408)
games submitted by our members
Games in WIP (293)
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  
  [Solved]Saving GLCanvas into a BufferedImage  (Read 1749 times)
0 Members and 1 Guest are viewing this topic.
Offline GLDavid

Senior Newbie




Java games rock!


« Posted 2004-03-23 13: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

Senior Newbie




Java games rock!


« Reply #1 - Posted 2004-03-23 18: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

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-03-24 20:05:37 »

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

Bye bye !
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline chris25

Senior Newbie




Java games rock!


« Reply #3 - Posted 2004-04-25 22: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

Senior Newbie




Java games rock!


« Reply #4 - Posted 2004-04-27 21: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]
  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!
 
Try the Free Demo of Revenge of the Titans

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 (130 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (229 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.075 seconds with 22 queries.