Texturing a polygon (Graphics 2d)

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?

I believe you need java.awt.TexturePaint.


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

Thank you!
but, it didn’t quite work yet :slight_smile:

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:

	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:

		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?

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 :stuck_out_tongue:
Thanks for your help though :slight_smile:

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.

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

				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.

Try using the Polygon’s getBounds2D method :wink:

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

rect.setFrame(point1, Math.round(tempy + ((newCamera.z-(top * 9.0)) * scale1)), poly1.getBounds2D().getWidth(), poly1.getBounds2D().getHeight());

to:


			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.

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


new TexturePaint(texture, polygon.getBounds2D());

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

the texture still isn’t pinched at the end…

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.

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

:slight_smile: I got it working

			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

Oh yeah the 10-param drawImage method is perfect! Great find :slight_smile: