Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » Draw, Display, Images, Bitmaps, Icons » Is 'texture mapping' possible on Painter?
Is 'texture mapping' possible on Painter? [message #52982] Tue, 28 January 2020 15:32 Go to next message
Tom1
Messages: 674
Registered: March 2007
Contributor
Hi,

I would like to 'texture map' an image in a shape of a sector or fan. Is this possible in Painter, like it is in OpenGL using a triangle fan?

(I.e. by defining the center, radius and starting and ending angles of a sector and then fill the area with an Image having its top edge touching the center point and its lower edge touching the arc defined by the radius, stretching out between the starting and ending angles.)

I would like to avoid using OpenGL in this project, partly because this is "just a simple" 2D mapping. However, rendering speed may be a factor here as the data is updated quite frequently.

Best regards,

Tom
Re: Is 'texture mapping' possible on Painter? [message #52995 is a reply to message #52982] Fri, 31 January 2020 17:30 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12179
Registered: November 2005
Ultimate Member
Tom1 wrote on Tue, 28 January 2020 15:32
Hi,

I would like to 'texture map' an image in a shape of a sector or fan. Is this possible in Painter, like it is in OpenGL using a triangle fan?

(I.e. by defining the center, radius and starting and ending angles of a sector and then fill the area with an Image having its top edge touching the center point and its lower edge touching the arc defined by the radius, stretching out between the starting and ending angles.)

I would like to avoid using OpenGL in this project, partly because this is "just a simple" 2D mapping. However, rendering speed may be a factor here as the data is updated quite frequently.

Best regards,

Tom


Now that is an interesting task... Smile I think individual triagles are still affine transformations, so IMO should be possible to do this.

EDIT:

Well, not affine after all, but IMO neither this it possible in OpenGL with simple texture mapping. IMO you need to add some advanced non-affine shaders to do this 100% correctly... Probably easier to do without the fan at that point, just pixel shader that maps the texture.

Mirek

[Updated on: Fri, 31 January 2020 17:38]

Report message to a moderator

Re: Is 'texture mapping' possible on Painter? [message #53003 is a reply to message #52995] Mon, 03 February 2020 10:33 Go to previous messageGo to next message
Tom1
Messages: 674
Registered: March 2007
Contributor
Hi Mirek,

Thanks for the suggestion, but I would really like to do this with Painter only. (Unfortunately, my brain does not have an OpenGL shaders -module installed. Wink)

As the sectors in the fan are really less than one degree wide each, it would not hurt to visualize them as a set of narrow triangular wedges, each showing one pixel wide stripe from the original image. So the pixels in the image would be represented as parallel lines with their length increasing while getting further from the center.

Is there a function inside the Painter that can render an Image in a way that each one of the Image corners will land in independently specified coordinates and everything in between simply gets interpolated evenly therein?

Best regards,

Tom

Re: Is 'texture mapping' possible on Painter? [message #53004 is a reply to message #53003] Tue, 04 February 2020 11:33 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12179
Registered: November 2005
Ultimate Member
You have to compute the transformation matrix correctly.

Mirek
Re: Is 'texture mapping' possible on Painter? [message #53005 is a reply to message #53004] Tue, 04 February 2020 13:28 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12179
Registered: November 2005
Ultimate Member
#include <CtrlLib/CtrlLib.h>
#include <plugin/jpg/jpg.h>

using namespace Upp;

Xform2D MapTriangle(Pointf s1, Pointf s2, Pointf s3)
{ // maps 0,0 -> s3, 1,0 -> s1, 0,1 -> s2
	Xform2D s;
	s.x.x = s1.x - s3.x;
	s.y.x = s1.y - s3.y;
	s.x.y = s2.x - s3.x;
	s.y.y = s2.y - s3.y;
	s.t = s3;
	return s;
}

Xform2D MapTriangles(Pointf s1, Pointf s2, Pointf s3, Pointf t1, Pointf t2, Pointf t3)
{
	return Inverse(MapTriangle(s1, s2, s3)) * MapTriangle(t1, t2, t3);
}

struct MyApp : TopWindow {
	Image img;

	virtual void Paint(Draw& dw)
	{
		Size sz = GetSize();
		DrawPainter w(dw, sz);
		w.Co();
		w.DrawRect(sz, White());
		Pointf center = (Point)sz / 2;
		double radius = 0.95 * min(center.x, center.y);

		Size isz = img.GetSize();

		int steps = 200;
		for(double i = 0; i < steps; i++) {
			Pointf q = Polar(i * M_2PI / steps);
			Pointf p1 = radius * q + center;
			Pointf p2 = radius * Polar((i + 1) * M_2PI / steps + 0.4 / steps) + center;

			Xform2D m = Xform2D::Scale(Distance(p1, p2) * steps / isz.cx, radius / isz.cy);
			m = m * Xform2D::Rotation(Bearing(p2 - p1));
			Pointf h = p1 - i * isz.cx / steps * Orthogonal(q);
			m = m * Xform2D::Translation(h.x, h.y);
			
			double x1 = i * isz.cx / steps;
			double x2 = (i + 1) * isz.cx / steps;
			double xc = (x1 + x2) / 2;
			w.Move(center).Line(p1).Line(p2).Fill(img,
				MapTriangles(Pointf(xc, isz.cy), Pointf(x1, 0), Pointf(x2, 0),
				             center, p1, p2));
		}
		
		w.Finish();
	}
};

GUI_APP_MAIN
{
	MyApp app;
	app.img = StreamRaster::LoadFileAny("C:/xxx/aukro.jpg");
	app.Run();
}


(Adding MapTriangles as Xform2D static method...)
Re: Is 'texture mapping' possible on Painter? [message #53006 is a reply to message #53005] Tue, 04 February 2020 16:11 Go to previous messageGo to next message
Tom1
Messages: 674
Registered: March 2007
Contributor
Hi Mirek!

Absolutely perfect!!! This is an incredibly elegant solution and runs so fast!

Thank you so very much! Smile

Best regards,

Tom
Re: Is 'texture mapping' possible on Painter? [message #53007 is a reply to message #53006] Wed, 05 February 2020 21:18 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 491
Registered: November 2008
Location: France
Senior Member
Hi Tom,

Could it be possible for you to post a sample image of what the result looks like: original image and the mapped image

[edit] Forget about it, I'm compiling the sample Smile

Thank you

[Updated on: Wed, 05 February 2020 21:22]

Report message to a moderator

Re: Is 'texture mapping' possible on Painter? [message #53010 is a reply to message #53007] Thu, 06 February 2020 20:40 Go to previous message
Didier is currently offline  Didier
Messages: 491
Registered: November 2008
Location: France
Senior Member
This sample code is very interesting,

I never took time to look at what Xform2D was used for (I never needed it) and when I see this example, it looks very powerful and easy to use

Maybe it could be included in the examples
Previous Topic: Remove the transparent area around the image (sprite optimization)
Next Topic: Drawing / Painter Difference,Usage,Manual
Goto Forum:
  


Current Time: Mon Feb 17 08:31:23 CET 2020

Total time taken to generate the page: 0.01030 seconds