views:

54

answers:

1

Given that I do something like this:

void glOrtho(   GLdouble    left, 
    GLdouble    right, 
    GLdouble    bottom, 
    GLdouble    top, 
    GLdouble    nearVal, 
    GLdouble    farVal);

and the result is: http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xmlw could I achieve a matrix like this:

http://cairographics.org/manual/cairo-matrix.html

I tried this:

cairo_matrix_t mat;
            mat.xx = 2 / (right - left);
            mat.yx = 0;
            mat.xy =  2 / (top - bottom);
            mat.yy = 0;
            mat.x0 = 0;
            mat.y0 = 0;

cairo_set_matrix(cr,&mat);

But it did not work. How could I acheive the same matrix that GlOrtho makes in Cairo? Thanks

A: 

I don't know Cairo so I'll delete my answer if a better one comes.

According to the docs of Cairo:

x_new = xx * x + xy * y + x0;
y_new = yx * x + yy * y + y0;

When you use OpenGL, the formula is like: (m being the matrix)

x_new = m(1,1) * x + m(1,2) * y + m(1,3) * z + m(1,4)
y_new = m(2,1) * x + m(2,2) * y + m(2,3) * z + m(2,4)
z_new = m(3,1) * x + m(3,2) * y + m(3,3) * z + m(3,4)

(note that for the sake of simplicity I did not mention the fourth coordinate)

So what you have to do is simply match the two formulas:

mat.xx = 2 / (right - left);
mat.yy = 2 / (top - bottom);
mat.xy = 0;
mat.yx = 0;
mat.x0 = -(right + left) / (right - left);
mat.y0 = -(top + bottom) / (top - bottom);

Please try this

Tomaka17
I dont think it worked, I'm drawing and never seeing anything,
Milo
The problem is also that this OpenGL matrix is built for surfaces whose coords are going from (-1,1) to (1,-1). I don't know if it is the case for Cairo
Tomaka17
float left; left = cameraX - ((float)engineGL.controls.MainGlFrame.Dimensions.x) * scalediv; float right; right = cameraX + ((float)engineGL.controls.MainGlFrame.Dimensions.x) * scalediv; float bottom; bottom = cameraY - ((float)engineGL.controls.MainGlFrame.Dimensions.y) * scalediv; float top; top = cameraY + ((float)engineGL.controls.MainGlFrame.Dimensions.y) * scalediv;
Milo
Try `left = cameraX - 1; right = cameraX + 1; top = cameraY + 1; bottom = cameraY - 1;` this is kind of a 'hack' but it will maybe work (again sorry but I can't test). The best solution would be to modify the matrix
Tomaka17
Is there an alternative way to achieve this by doing translate and scale?
Milo
If you want to use translate and scale, simply translate by `-cameraX, -cameraY` and then scale by `scalediv, scalediv`. Now that you talk about it, it's certainly easier :)
Tomaka17
Okay the +- one worked but scaling was wrong
Milo
Yeah I made a mistake, try `left = cameraX - scalediv; right = cameraX + scalediv; top = cameraY + scalediv; bottom = cameraY - scalediv;`. Note that this is a hack, you'd better modify the matrix (unfortunatly I have to leave home now, I'll come back in ~10 hours)
Tomaka17