Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Texturing a polygon (Graphics 2d)  (Read 2929 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Posted 2011-09-04 04:10:22 »

Hi, I'm currently writing  a simple 3d rendering engine (I've got a depth system, billboards, and a rotatable camera working so far [the camera does not support a pitch change]).
What I need, is a way to draw a polygon, but texture it, is that possible? or at least, not too memory consuming?

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-09-04 06:27:52 »

I believe you need java.awt.TexturePaint.

1  
2  
g.setPaint(texturePaint);
g.fill(polygon);

Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #2 - Posted 2011-09-04 07:41:07 »

Thank you!
but, it didn't quite work yet Smiley

I believe it's only grabbing the top left pixel for the texture, at the moment, but I'm not sure.
Since Rectangle2D is abstract, this is a class I created:

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  
54  
55  
56  
57  
   class Rect2D extends Rectangle2D {

      @Override
      public Rectangle2D createIntersection(Rectangle2D arg0) {
         // TODO Auto-generated method stub
        return null;
      }

      @Override
      public Rectangle2D createUnion(Rectangle2D arg0) {
         // TODO Auto-generated method stub
        return null;
      }

      @Override
      public int outcode(double arg0, double arg1) {
         // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public void setRect(double arg0, double arg1, double arg2, double arg3) {
         // TODO Auto-generated method stub
       
      }

      @Override
      public double getHeight() {
         // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public double getWidth() {
         // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public double getX() {
         // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public double getY() {
         // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public boolean isEmpty() {
         // TODO Auto-generated method stub
        return false;
      }
     
   }


to create the TexturePaint, I did this:

1  
2  
3  
      Rectangle2D rect = new Rect2D();
      rect.add(sprite_width, sprite_height);
      tex = new TexturePaint(ImageHelper.toBufferedImage(sprite_index), rect);


sprite_width and sprite_height are the width/height of the image.

What could be the issue?

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #3 - Posted 2011-09-04 07:59:41 »

Never mind, I fixed the problem.
Though, after all this, I realized I'm going to have to make it work myself. What you gave me can't support camera rotations, as the texture inside the polygon is always flat and facing the camera Tongue
Thanks for your help though Smiley

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-09-04 08:30:28 »

You know there are 2 inner classes in Rectangle2D right? Rectangle2D.Double and Rectangle2D.Float Wink

If you need to rotate, shear, or just about do any transformation, use AffineTransform.
For the operation you want (rotating the image towards the screen, you can just simulate it by squeezing the image, or making the Rectangle2D's width smaller than the image's width.

Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #5 - Posted 2011-09-04 17:07:25 »

Can you please provide some kind of example/psuedo code on how to actually stretch the images to fit inside my polygon?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
            setDepth(depth - priority);
            Polygon poly1 = new Polygon();
            poly1.addPoint((int) Math.round(point1), (int) Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale1)));
            poly1.addPoint((int) Math.round(point2), (int) Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale2)));
            poly1.addPoint((int) Math.round(point2), (int) Math.round(tempy + ((newCamera.z-(bottom * 9.0)) * scale2)));
            poly1.addPoint((int) Math.round(point1), (int) Math.round(tempy + ((newCamera.z-(bottom * 9.0)) * scale1)));
           
            Rectangle2D rect = new Rectangle2D.Double(0, 0, sprite_width, sprite_height);
            tex = new TexturePaint(texture, rect);
           
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(tex);
            g2d.fill(poly1);


is what I've written so far.

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-09-04 17:42:51 »

Try using the Polygon's getBounds2D method Wink

Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #7 - Posted 2011-09-04 17:52:02 »

Rectangle2D rect = new Rectangle2D.Double(0, 0, poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());

That just makes it look even worse xD




[edit]
when I tried
rect.setFrame(point1, Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale1)), poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());
under defining rect, it fixed the width of the texture on the polygon, it's still repeating on the y axis when the camera turns though, it isn't stretching to fit into the polygon.

[edit again]
I changed
1  
rect.setFrame(point1, Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale1)), poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());

to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
         double p1 = Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale1));
         double p2 = Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale2));
         double hp = p1;
         if (p2 < p1) {
            hp = p2;
         }

         Rectangle2D rect = new Rectangle2D.Double(0, 0, poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());
         rect.setFrame(point1, hp, poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());
         tex = new TexturePaint(texture, rect);


this almost works, it correctly puts the texture at the top left part of the polygon. There's only 1 problem now, the texture doesn't get... pinched... towards the smaller parts of the polygon.

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-09-04 18:29:21 »

Well you are starting at (0,0). This will cause it to only draw a small part of the image if the poly is somewhere in the middle. The Javadocs say that the Rectangle2D will be replicated in all directions infinitely and then bind the image on each one. Try doing
1  
new TexturePaint(texture, polygon.getBounds2D());

Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #9 - Posted 2011-09-04 18:31:54 »

That does what the code I gave before did, just more efficient I guess Tongue

the texture still isn't pinched at the end...

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2011-09-04 18:40:33 »

IIRC you can't do arbitrary texture warping via java2d. You certainly can't do perspective correct texture mapping. Probably best to rasterise it yourself.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #11 - Posted 2011-09-04 22:44:16 »

So there's no way to UV map a texture to a polygon using Graphics2D?

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline orange451

JGO Coder


Medals: 8
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #12 - Posted 2011-09-05 02:42:34 »

Smiley I got it working

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  
         double ntop = top * 9.0;
         double nbottom = bottom * 9.0;
         
         int basey = (view_height/2);
         double top1=basey+(newCamera.z-ntop)*scale1;
         double top2=basey+(newCamera.z-ntop)*scale2;
         double bottom1=basey+(newCamera.z-nbottom)*scale1;
         double bottom2=basey+(newCamera.z-nbottom)*scale2;
         double difBot=bottom2-bottom1;
         double difTop=top2-top1;

         int current=0;
         double res=Math.floor((scale1+scale2)/4)+1;
         double number=point2-point1;
         
         for (int i = 0; i <= Math.floor((number+res)/res); i += 1) {
             double curTop=current/number*difTop+top1;
             double curBot=current/number*difBot+bottom1;
             
             int dx1 = (int) (point1 + current);
             int dy1 = (int) curTop;
             int dx2 = dx1 + (int)res;
             int dy2 = dy1 + (int) Math.ceil((curBot-curTop));
            int sx1 = (int) Math.round(Math.floor((double)current/number*((double)sprite_width-1.0)));
            int sy1 = 0;
            int sx2 = sx1 + 1;
            int sy2 = sprite_height;
            g.drawImage(sprite_index, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
             current+=res;
         }

if anyone wants to know

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-09-05 04:08:52 »

Oh yeah the 10-param drawImage method is perfect! Great find 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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (24 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!