为cairo图形创建openGL wgl上下文

时间:2017-06-24 13:44:33

标签: c opengl cairo

我正在尝试创建虚拟上下文,以便我可以使用cairo-gl进行渲染,然后我可以将该表面复制到表面图像缓冲区。

以下代码只是一个测试,因此我可以评估openGL支持是否有效。

所以这就是我正在做的事情,但是在创建cairo_device时我总是遇到错误。 _cairo_gl_dispatch_init_buffers会抛出错误。

编辑:实际上,cairo无法获得_cairo_gl_get_version和_cairo_gl_get_flavor。

HDC hdc = GetDC((HWND)pGraphics->GetWindow());

PIXELFORMATDESCRIPTOR pfd = {
    sizeof(PIXELFORMATDESCRIPTOR),   // size of this pfd  
    1,                     // version number  
    PFD_DRAW_TO_WINDOW |   // support window  
    PFD_SUPPORT_OPENGL |   // support OpenGL  
    PFD_DOUBLEBUFFER,      // double buffered  
    PFD_TYPE_RGBA,         // RGBA type  
    24,                    // 24-bit color depth  
    0, 0, 0, 0, 0, 0,      // color bits ignored  
    0,                     // no alpha buffer  
    0,                     // shift bit ignored  
    0,                     // no accumulation buffer  
    0, 0, 0, 0,            // accum bits ignored  
    32,                    // 32-bit z-buffer  
    0,                     // no stencil buffer  
    0,                     // no auxiliary buffer  
    PFD_MAIN_PLANE,        // main layer  
    0,                     // reserved  
    0, 0, 0                // layer masks ignored  
};
//HDC  hdc;
int  iPixelFormat;

// get the best available match of pixel format for the device context   
iPixelFormat = ChoosePixelFormat(hdc, &pfd);

// make that the pixel format of the device context  
SetPixelFormat(hdc, iPixelFormat, &pfd);


////// create a rendering context  
HGLRC hglrc = wglCreateContext(hdc);

// Test openGL

cairo_surface_t *surface_gl;
cairo_t *cr_gl;
cairo_device_t *cairo_device = cairo_wgl_device_create(hglrc);

surface_gl = cairo_gl_surface_create_for_dc(cairo_device, hdc, 500, 500);
cr_gl = cairo_create(surface_gl);

cairo_set_source_rgb(cr_gl, 1, 0, 0);
cairo_paint(cr_gl);

cairo_set_source_rgb(cr_gl, 0, 0, 0);
cairo_select_font_face(cr_gl, "Sans", CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr_gl, 40.0);

cairo_move_to(cr_gl, 10.0, 50.0);
cairo_show_text(cr_gl, "openGL test");

cairo_surface_write_to_png(surface_gl, "C:/Users/Youlean/Desktop/imageGL.png");

cairo_destroy(cr_gl);
cairo_surface_destroy(surface_gl);

0 个答案:

没有答案
相关问题