Render cairo surface directly to OpenGL texture
Asked Answered
H

3

17

I'm using cairo (http://cairographics.org) in combination with an OpenGL based 3D graphics library.
I'm currently using the 3D library on Windows, but I'm hoping to receive an answer that is platform independent. This is all done in c++.

I've got the straight forward approach working which is to use cairo_image_surface_create in combination with glTexImage2D to get an OpenGL texture.

However, from what I've been able to gather from the documentation cairo_image_surface_create uses a CPU-based renderer and writes the output to main memory.

I've come to understand cairo has a new OpenGL based renderer which renders its output directly on the GPU, but I'm unable to find concrete details on how to use it.
(I've found some details on the glitz-renderer, but it seems to be deprecated and removed).

I've checked the surface list at: http://www.cairographics.org/manual/cairo-surfaces.html, but I feel I'm missing the obvious.

My question is: How do I create a cairo surface that renders directly to an OpenGL texture?
Do note: I'll need to be able to use the texture directly (without copying) to display the cairo output on screen.

Harim answered 24/5, 2011 at 9:5 Comment(0)
T
3

As of 2015, Cairo GL SDL2 is probably the best way to use Cairo GL

https://github.com/cubicool/cairo-gl-sdl2

If you are on an OS like Ubuntu where cairo is not compiled with GL you will need to compile your own copy and let cairo-gl-sdl2 know where it is.

Tarp answered 11/11, 2015 at 11:55 Comment(0)
L
1

The glitz renderer has been replaced by the experimental cairo-gl backend.

You'll find a mention of it in: http://cairographics.org/OpenGL/

Can't say if it's stable enough to be used though.

Once you have a gl backend working, you can render into a Framebuffer Object to render directly in a given texture.

Latrishalatry answered 26/5, 2011 at 6:24 Comment(3)
I'm aware the glitz renderer has been replaced, I mentioned this in the original question. But that page doesn't seem to list any instructions on how to actually use the cairo-gl backend, I'm quite specifically asking how to rendering directly to an OpenGL texture without using glTexImage2D. Do you have more precise instructions?Harim
@Vhab - sorry I don't know how to use cairo-gl myself. But without it, I believed you're due to use glTexImage (or equivalent). With cairo-gl, you can bind a framebuffer object to enable the redirection of cairo draw calls to a texture. I can hardly be more specific, sorryLatrishalatry
I'm unable to find concrete information on how to use cairo-gl, which is what lead me to asking the question on stackoverflow. I'll gladly accept any answer that shows me how to actually use cairo-gl and obtain a valid OpenGL texture with the cairo output.Harim
B
0

I did it using GL_BGRA.

int tex_w = cairo_image_surface_get_width(surface);
int tex_h = cairo_image_surface_get_height(surface);
unsigned char* data = cairo_image_surface_get_data(surface);

then do

glTexImage2D(GL_TEXTURE_2D, 0, 4, tex_w,tex_h, 0,GL_BGRA, GL_UNSIGNED_BYTE, data);

when you create the texture. Use glTexSubImage2D(...) to update th texture when the image content changes. For speed set the filters for the texture to GL_NEAREST

Berton answered 23/2, 2012 at 9:45 Comment(1)
Unfortunately, if you would have read my question that exactly is what I'm not asking.Harim

© 2022 - 2024 — McMap. All rights reserved.