PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Stretching a Surface

Is it possible to stretch a Surface so that it behaves like a parallelogram? What I mean is that I don't want the corners of the new Surface to be perpendicular.
No. You can rotate and size (stretch etc) but not slope / /. What is it you want to achieve as there may be some other way to get the result.

Also there are PF demos out there of surfaces distorted by effects and one of these might be easily adapted.( I'm thinking of Jason's melt demo) Do a search on this forum to find.
The actual low-level code of PF supports it, the API simply isn't exposed. This is certainly not a task for a beginner, but the underlying blitter can use any 2x3 transform matrix when blitting.

I wouldn't mind adding the API myself... I am simply unsure what would be the way to expose the functionality in the API?

BlitStretchedX( rect, rect, ... ??? )
How about something like BlitTransform() that accepts the transformation matrix to use? That still exposes the details of the matrix for someone to understand, but it's a start.

Maybe there's a simlper way to represent the matrix? I doubt it, but maybe?
fzammetti wrote:How about something like BlitTransform() that accepts the transformation matrix to use? That still exposes the details of the matrix for someone to understand, but it's a start.


That's a really good idea. I could then add functions to initialize the matrix depending on what you want to do.

fzammetti wrote:Maybe there's a simlper way to represent the matrix? I doubt it, but maybe?


I don't see what can be simplier then a matrix. A matrix is the ultimate explicit representation of math equations. Changing it would mean losing functionality and obscuring what the data representation means.
Yeah, I didn't see any way either... I suppose you could come up with some sort of "transformation language", but I think that's well beyond the point where it's not worth the effort. Just being able to pass in the matrix would probably be a boon to many people.
Conan wrote:I'm thinking of Jason's melt demo) Do a search on this forum to find.


The melt was actually splitting up an image into vertical slices - not really a single blit.

Thierry, although it would definitely be good to allow the use of any transformation matrix - it would also be nice to have a helper function for common things. Like to map into an arbitrary 4 point box (not a rect struct, but any 4 cornered shape).

I think that allegro does this, but it may have only dealt with triangles. That wouldn't be bad either. It also allows for passing colors for each of the vertices - which would be really great to have if it was fast enough.

Just some ideas...

Jason
fast_rx, would it be enough to expose the BlitTransformation() method, and then just document a number of generically useful matrixes? In my mind that would be a pretty good deal, even without Thierry (or anyone else) implementing extra functions...
I guess if you or someone else can explain how to tweak those numbers into what they need to be for skewed quadrilaterals, or whatever you'd want to call them.
I have to be honest and say I haven't the foggiest idea how the matrix works :) I think it's clear that we would all need Thierry to write up some documentation on it if this were to be useful to anyone.

However, as Thierry said previously, it's just a methematical structure, so if we get the basic idea, it shouldn't be much of a leap to do a great many of the things we want, including your skewed blit.
Thanks for replying guys.

Conan wrote:No. You can rotate and size (stretch etc) but not slope / /. What is it you want to achieve as there may be some other way to get the result.


I want to draw my surface defined by four corners. These four corners is not always shaping a perpendicular square.
I looked at the code for the transformation matrix this weekend. I was making one with anti-aliasing (or oversampling).

While looking at the code, I realised that it's extremely similar in what I wrote for my tile blitter. Even the same notations... du's and dv's.

While it appears that you can distort along the vertical and horizontal of the source image, I don't see how it could be used to blit to a four sided poly. Please prove me wrong, but I don't see it happening. I think you would need to calculate a du/dv for each scan line for that to work.

I may look at the code for allegro later to see how they did it.
You cannot speicify 4 points with this transform matrix, it just wont work. The reason is that you would be trying to solve a non-linear problem with a matrix. You will have to either:

1) Split the quad into 2 triangles
2) Have the user specify 3 points and calculate the 4th
3) Do perspective correction

#2 is my favorite, by far.
Yes, it's a good idea to specify three corners and calculate the fourth.

The problem is that I don't want to mess around with the matrix without guidance (I actually don't understand how it works).

Let's say I want to use BlitStretch and create a surface with corners on (0,0) (100,0) (10,100) (110,100). The source is a Rect(0,0,100,100).

What should the matrix look like? Today the matrix in BlitStretch(...):
T[0][0] = rx;
T[0][1] = 0;
T[1][0] = 0;
T[1][1] = ry;
T[2][0] = -destRect.left * rx + (srcRect.left << 10);
T[2][1] = -destRect.top * ry + (srcRect.top << 10);


How should each element in the matrix in my case be calculated? It should be a general solution to it, shouldn't it?

Once again thanks for replying and sorry for causing so much trouble about this stretching-thing.
For your example, T[1][0] will be the 'change in x vesus y' which is your skew amount.

Just taking a guess, I think it would be 10/100 (or -10/100) using the fixed float format of course.

But you'll have to get your 4 points into the function somehow.



On the subject, a very simple modification to the BlitRotate code will aleviate the need for the temp surface used in BlitRotateStretch...

change
Code:

int c = int(cosf( -angle ) * 1024.0f);
int s = int(sinf( -angle ) * 1024.0f);

to
Code:

float iscale = 1.0f/scale;

int c = int(iscale * cosf( -angle ) * 1024.0f);
int s = int(iscale * sinf( -angle ) * 1024.0f);


Also, the comment up there at the top that says
// Calculate destination rectangle
//todo: find better fit


I think the best fit would be
Code:

int diag = (int)( (fabs(cos(angle)*source->GetWidth()) + fabs(sin(angle)*source->GetHeight()) )*(scale/2.0f) + 1.0f);