[cairo] Cairo, Glitz, etc. again...

David Reveman davidr at novell.com
Wed Jun 21 11:06:43 PDT 2006


On Wed, 2006-06-21 at 12:58 -0400, Jeremy L. Moles wrote: 
> I wrote a small cairo_example.c file a while back thinking I had a good
> grasp on how glitz and cairo work but I've realized these last few days
> upon revisiting it that this simply isn't the case. It works in that
> very contrived, very unrealistic example, but it does not intuitively
> fall into other uses. I'm going to delete that stuff for now since it's
> practically useless.
> 
> I just had a few questions I hoped Reveman or someone else familiar w/
> Glitz (MacSlow/Behdad?) could answer...
> 
> If possible, I want to use Glitz "texture objects" as a kind of "canvas"
> in an OpenGL game. More specifically, texture object will represent a UI
> element in the game which I will draw on (irregularly, and certainly not
> every frame) using Cairo.
> 
> I have some code that does this to some degree, but I'm not entirely
> sure I'm following the right procedure. For one, no matter how hard I
> try, I can't get alpha transparency to work like it should at all. Take
> the following:
> 
> void render_cairo(cairo_t* cr, int w, int h) {
> 	// Fill the surface with transparency...
> 	// This acts as a kind of "clearing" mechanism, right???
>         cairo_set_source_rgba(cr, 0.0f, 0.0f, 0.0f, 0.0f);
>         cairo_rectangle(cr, 0.0f, 0.0f, 1.0f, 1.0f);
>         cairo_fill(cr);
> 
> 	// Draw a 1/2 transparent "X".
>         cairo_set_source_rgba(cr, red, green, blue, 0.5f);
>         cairo_set_line_width(cr, 0.05f);
>         cairo_move_to(cr, 0.0f, 0.0f);
>         cairo_line_to(cr, 1.0f, 1.0f);
>         cairo_move_to(cr, 1.0f, 0.0f);
>         cairo_line_to(cr, 0.0f, 1.0f);
> 
>         cairo_stroke(cr);
> }
> 
> If I call this function only once, before the game's "main loop", the
> texture is okay and seems to honor the alpha values I give it. (That is,
> you can see the rest of the OpenGL scene underneath the Glitz texture).
> However, if I add calls to render_cairo() in the game's main loop, the
> texture does update itself but the alpha values in the "path" aren't
> honored, although the places I haven't "pathed" yet still seem to be
> okay (until I draw something on them). I'm assuming that's just
> indicative of a bigger misunderstanding on my part, but any help (or
> example working code of something similar) would be immensely
> appreciated. I've include some screenshots below...
> 
> http://cherustone.com/good.png <-- The result of only calling
> render_cairo() once, BEFORE the mainloop.
> 
> http://cherustone.com/bad.png <-- The result of calling render_cairo()
> during every frame.

My guess is that the second time you call render_cairo, operator isn't
OVER. Either way, you shouldn't clear the surface using OVER.

I would make render_cairo look like this:

void render_cairo(cairo_t* cr, int w, int h) {
    cairo_save (cr);

    // Fill the surface with transparency...
    cairo_operator (cr, CAIRO_OPERATOR_SOURCE); 
    cairo_set_source_rgba (cr, 0.0f, 0.0f, 0.0f, 0.0f);
    cairo_paint (cr);

    // Draw a 1/2 transparent "X".
    cairo_operator (cr, CAIRO_OPERATOR_OVER);
    cairo_set_source_rgba (cr, red, green, blue, 0.5f); 
    cairo_set_line_width (cr, 0.05f);
    cairo_move_to (cr, 0.0f, 0.0f);
    cairo_line_to (cr, 1.0f, 1.0f);
    cairo_move_to (cr, 1.0f, 0.0f);
    cairo_line_to (cr, 0.0f, 1.0f);

    cairo_stroke (cr);

    cairo_restore (cr); 
}

-David



More information about the cairo mailing list