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 (416)
games submitted by our members
Games in WIP (307)
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  
  Split a TextureImage2D  (Read 1678 times)
0 Members and 1 Guest are viewing this topic.
Offline davicente

Senior Newbie





« Posted 2010-04-05 10:32:19 »

Hi everyone:

I have a difficult problem to solve.
I have a Shape3D object (with his TextureImage2D and his Geometry). The TextureImage2D is loaded from an image file. I need to get a BufferedImage to each part of the Shape3D object or to each Triangle of the shape. I only can get the BufferedImage from all the image file.
May be some option to get differents images from the original image file using the texture coordinates?

Thanks
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #1 - Posted 2010-04-05 13:40:39 »

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  
/**
 * Clips the input image to the specified shape
 *
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return An image, the same size as the input, but only containing
 *         those pixels that fall inside the clip shape
 */

public static BufferedImage clip( BufferedImage image, float... clipVerts )
{
   assert clipVerts.length >= 6;
   assert clipVerts.length % 2 == 0;

   int[] xp = new int[ clipVerts.length / 2 ];
   int[] yp = new int[ xp.length ];

   for( int j = 0; j < xp.length; j++ )
   {
      xp[ j ] = Math.round( clipVerts[ 2 * j ] * image.getWidth() );
      yp[ j ] = Math.round( clipVerts[ 2 * j + 1 ] * image.getHeight() );
   }

   Polygon clip = new Polygon( xp, yp, xp.length );
   BufferedImage out = new BufferedImage( image.getWidth(), image.getHeight(), image.getType() );
   Graphics g = out.getGraphics();
   g.setClip( clip );

   g.drawImage( image, 0, 0, null );
   g.dispose();

   return out;
}




1  
2  
3  
BufferedImage bi = ImageIO.read( new File( "kitten.jpg" ) );
BufferedImage clipped = clip( bi, 0.2f, 0.2f, 0.05f, 0.8f, 0.6f, 0.6f );
ImageIO.write( clipped, "PNG", new File( "clipped.png" ) );



Offline davicente

Senior Newbie





« Reply #2 - Posted 2010-04-05 14:17:21 »

Thanks a lot for the answer. I don´t have clear one thing. In this example the object BufferedImage out what you return has all image or only the part that you clipped. If it is only the part that you clipped it is what I need.

Thanks again.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #3 - Posted 2010-04-05 14:35:58 »

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  
/**
 * Clips the input image to the specified shape
 *
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */

public static BufferedImage clip( BufferedImage image, float... clipVerts )
{
   assert clipVerts.length >= 6;
   assert clipVerts.length % 2 == 0;

   int[] xp = new int[ clipVerts.length / 2 ];
   int[] yp = new int[ xp.length ];
   int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

   for( int j = 0; j < xp.length; j++ )
   {
      xp[ j ] = Math.round( clipVerts[ 2 * j ] * image.getWidth() );
      yp[ j ] = Math.round( clipVerts[ 2 * j + 1 ] * image.getHeight() );

      minX = Math.min( minX, xp[ j ] );
      minY = Math.min( minY, yp[ j ] );
      maxX = Math.max( maxX, xp[ j ] );
      maxY = Math.max( maxY, yp[ j ] );
   }

   for( int i = 0; i < xp.length; i++ )
   {
      xp[ i ] -= minX;
      yp[ i ] -= minY;
   }

   Polygon clip = new Polygon( xp, yp, xp.length );
   BufferedImage out = new BufferedImage( maxX - minX, maxY - minY, image.getType() );
   Graphics g = out.getGraphics();
   g.setClip( clip );

   g.drawImage( image, -minX, -minY, null );
   g.dispose();

   return out;
}


Offline davicente

Senior Newbie





« Reply #4 - Posted 2010-04-05 15:33:54 »

Thanks again. I´m going to try it Wink.
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!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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!
mrbenebob (17 views)
2013-06-19 14:55:23

BrassApparatus (27 views)
2013-06-19 08:52:37

NegativeZero (30 views)
2013-06-19 03:31:52

NegativeZero (33 views)
2013-06-19 03:24:09

Jesse_Attard (35 views)
2013-06-18 22:03:02

HeroesGraveDev (72 views)
2013-06-15 23:35:23

Vermeer (73 views)
2013-06-14 20:08:06

davedes (74 views)
2013-06-14 16:03:55

alaslipknot (66 views)
2013-06-13 07:56:31

Roquen (90 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

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
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!