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

Senior Newbie





« Posted 2010-04-05 08: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

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2010-04-05 11: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 12: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

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2010-04-05 12: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 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57:14
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!