[cairo] [patch] enable projective transformations

Bill Spitzak spitzak at gmail.com
Tue Aug 17 12:08:54 PDT 2010

The only 3D thing that Cairo literally cannot do that people want is a 
perspective transform of an image. Users certainly want this and it is 
supported by certain other graphics api's because it allows a 3D 
texture-mapped object to be rendered.


If you really wanted the minimum necessary addition, a 
"cairo_set_surface_source_with_3x3_matrix" call would suffice. This 3x3 
matrix would be used to translate x,y positions in the source surface to 
the output surface by multiplying [x,y,1] by the matrix to get [X*w, 
Y*w, w], then dividing by w to get X,Y.

The actual 3x3 matrix used for the projection is some concatenation of 
the CTM with this matrix. This is so Cairo's design can be followed in 
that a user can make an arbitrary transform, then call a "subroutine" 
that draws something, and that something is transformed uniformly. This 
is probably really easy.

If I understand Maarten Bosmans correctly, pixman is already capable of 
this. Also only 3x3 matricies are needed for this.


The more questionable part is whether Cairo should provide more 
user-friendly api for this. Cairo does not need to provide a "rotate" 
function because the user could just specify the matrix, but it does. So 
perhaps it should provide some perspective functions.

The problem is that any simple API to specify a perspecive projection is 
not going to produce all possibilities (it would need 8 numbers in the 
api for that). You need to concatenate it with some other 
transformations, both before and after the perspective call. All the 
transforms already in cairo, plus rotation in axis other than z, and 
translation in z, are needed.

It would make sense to reuse the transformation functions cairo already 
has, so the result is that the 2D objects cairo can draw can be 
projection transformed.

My scribbles seem to show that you cannot get useful concatenations 
without a full 4x4 matrix intermediate.


To achieve useful results that cannot be gotten by just drawing to a 
temporary surface and rendering that with perspective, this also 
requires line width locking and font transform locking, so that the z 
has no effect on line thickness or font size. This will allow 
axis-aligned labels to be drawn and 3d renderings that look like line 
drawings with fixed line widths.

More information about the cairo mailing list