首页 > 系统相关 >谷歌的 OpenGL ES 库 Angle PBuffer 测试代码(Windows 平台)

谷歌的 OpenGL ES 库 Angle PBuffer 测试代码(Windows 平台)

时间:2023-03-19 22:00:10浏览次数:39  
标签:Angle OpenGL Windows printf GLAD EGL eglDisplay ES

 

/* 这个例子是 Windows 平台测试谷歌的 Angle 库,PBuffer 的创建。用到了 GLAD 的 EGL 和 GLES 2.x、GLES 3.x 模块。
 *
 * 用到的 Angle 的动态链接库是:
 *
 *   d3dcompiler_47.dll
 *   libEGL.dll
 *   libGLESv2.dll
 *
 * 这三个 dll 很多软件带了,比如 vscode(64位),很多浏览器的目录,也可以自己下载 Angle 的源码编译。 
 */

/* HDC      可以是任意窗口的 HDC,相当于 Windows 平台的 display,这里可以设置成 NULL(无窗口依赖)
 *
 */
int test_pbuffer(HDC hdc)
{
    int n;

    #if defined(GLAD_EGL_H_) || defined(GLAD_EGL)

    // 先用 GLAD 加载 EGL 的函数
    n = gladLoaderLoadEGL(0);
    if (n == 0) {
        printf("egl> gladLoaderLoadEGL() = %d\n", n);
        return -1;
    }
    else {
        printf("egl> gladLoaderLoadEGL() = %d\n", n);
    }
    #endif

    // EGL 版本参数
    EGLint ai32ContextAttribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE, EGL_NONE
    };

    // 获取 EGL display
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType) hdc);

    // 初始化 EGL
    eglInitialize(eglDisplay, 0, 0);

    // 绑定 OpenGL ES API。这个函数在 Windows 平台是空,OpenGL ES 的函数稍后我们用 GLAD 加载
    if (eglBindAPI) {
        eglBindAPI(EGL_OPENGL_ES_API);
    }

    // 设置 OpenGL ES 初始化参数
    EGLint pi32ConfigAttribs[] = {
        //EGL_SURFACE_TYPE, EGL_WINDOW_BIT,         // 绘制到窗口,配合上面的 HDC 句柄
        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,          // 使用 PBuffer
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,    // OpenGL ES 版本
        EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,      // 颜色缓冲格式
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_DEPTH_SIZE, 24,
        EGL_STENCIL_SIZE, 8,
        EGL_NONE, EGL_NONE
    };

    // 选择 display 设置属性
    int iConfigs;
    EGLConfig eglConfig;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);

    if (iConfigs != EGL_TRUE) {
        printf("egl> Error: eglChooseConfig(): config not found.\n");
        return -1;
    }

    // 创建 PBuffer 表面
    EGLint pBufferAttribs[] = {
        EGL_WIDTH, 800,
        EGL_HEIGHT, 600,
        EGL_LARGEST_PBUFFER, EGL_TRUE,
        EGL_NONE, EGL_NONE
    };
    EGLSurface eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, pBufferAttribs);

    if (!eglSurface) {
        printf("egl> pbuffer surface create failed.\n");
        return -1;
    }

    // 创建 OpenGL ES 上下文
    EGLContext eglContext;
    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);

    // 绑定 OpenGL ES 上下文
    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    // 使用 GLAD 加载 GLES 的函数
    #if defined(GLAD_GLES2_H_) || defined(GLAD_GLES2)
    n = gladLoaderLoadGLES2();
    if (n == 0) {
        printf("egl> gladLoaderLoadGLES2() = %d\n", n);
        return -1;
    }
    else {
        printf("egl> gladLoaderLoadGLES2() = %d\n", n);
    }
    #endif

    //
    // 从这里开始,可以使用普通的 OpenGL (ES) 函数
    //

    // 清屏
    glClearColor(0.0, 0.5, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    // 读取颜色缓冲区
    uint8_t* buf = (uint8_t*) malloc(800 * 600 * 4);
    glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, buf);

    // 使用 stb_image_write 保存图片,如果程序目录看到一张蓝色背景的图片,说明测试成功。
    stbi_write_png("test.png", 800, 600, 4, buf, 0);
    printf("gles> save test.png ok.\n");

    delete buf;

    // EGL 的反转缓冲区
    eglSwapBuffers(eglDisplay, eglSurface);

    // 取消绑定上下文
    eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

    // 释放 EGL 环境
    eglDestroyContext(eglDisplay, eglContext);
    eglDestroySurface(eglDisplay, eglSurface);
    eglTerminate(eglDisplay);

    printf("egl> test ok.\n");
}

 

/* 这个例子是 Windows 平台测试谷歌的 Angle 库,PBuffer 的创建。用到了 GLAD 的 EGL 和 GLES 2.x、GLES 3.x 模块。 * * 用到的 Angle 的动态链接库是: * *   d3dcompiler_47.dll *   libEGL.dll *   libGLESv2.dll * * 这三个 dll 很多软件带了,比如 vscode(64位),很多浏览器的目录,也可以自己下载 Angle 的源码编译。  */
/* HDC      可以是任意窗口的 HDC,相当于 Windows 平台的 display,这里可以设置成 NULL(无窗口依赖) * */int test_pbuffer(HDC hdc){    int n;
    #if defined(GLAD_EGL_H_) || defined(GLAD_EGL)
    // 先用 GLAD 加载 EGL 的函数    n = gladLoaderLoadEGL(0);    if (n == 0) {        printf("egl> gladLoaderLoadEGL() = %d\n", n);        return -1;    }    else {        printf("egl> gladLoaderLoadEGL() = %d\n", n);    }    #endif
    // EGL 版本参数    EGLint ai32ContextAttribs[] = {        EGL_CONTEXT_CLIENT_VERSION, 2,        EGL_NONE, EGL_NONE    };
    // 获取 EGL display    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType) hdc);
    // 初始化 EGL    eglInitialize(eglDisplay, 0, 0);
    // 绑定 OpenGL ES API。这个函数在 Windows 平台是空,OpenGL ES 的函数稍后我们用 GLAD 加载    if (eglBindAPI) {        eglBindAPI(EGL_OPENGL_ES_API);    }
    // 设置 OpenGL ES 初始化参数    EGLint pi32ConfigAttribs[] = {        //EGL_SURFACE_TYPE, EGL_WINDOW_BIT,         // 绘制到窗口,配合上面的 HDC 句柄        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,          // 使用 PBuffer        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,    // OpenGL ES 版本        EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER,      // 颜色缓冲格式        EGL_BLUE_SIZE, 8,        EGL_GREEN_SIZE, 8,        EGL_RED_SIZE, 8,        EGL_ALPHA_SIZE, 8,        EGL_DEPTH_SIZE, 24,        EGL_STENCIL_SIZE, 8,        EGL_NONE, EGL_NONE    };
    // 选择 display 设置属性    int iConfigs;    EGLConfig eglConfig;    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);
    if (iConfigs != EGL_TRUE) {        printf("egl> Error: eglChooseConfig(): config not found.\n");        return -1;    }
    // 创建 PBuffer 表面    EGLint pBufferAttribs[] = {        EGL_WIDTH, 800,        EGL_HEIGHT, 600,        EGL_LARGEST_PBUFFER, EGL_TRUE,        EGL_NONE, EGL_NONE    };    EGLSurface eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, pBufferAttribs);
    if (!eglSurface) {        printf("egl> pbuffer surface create failed.\n");        return -1;    }
    // 创建 OpenGL ES 上下文    EGLContext eglContext;    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
    // 绑定 OpenGL ES 上下文    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
    // 使用 GLAD 加载 GLES 的函数    #if defined(GLAD_GLES2_H_) || defined(GLAD_GLES2)    n = gladLoaderLoadGLES2();    if (n == 0) {        printf("egl> gladLoaderLoadGLES2() = %d\n", n);        return -1;    }    else {        printf("egl> gladLoaderLoadGLES2() = %d\n", n);    }    #endif
    //    // 从这里开始,可以使用普通的 OpenGL (ES) 函数    //
    // 清屏    glClearColor(0.0, 0.5, 1.0, 1.0);    glClear(GL_COLOR_BUFFER_BIT);
    // 读取颜色缓冲区    uint8_t* buf = (uint8_t*) malloc(800 * 600 * 4);    glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, buf);
    // 使用 stb_image_write 保存图片,如果程序目录看到一张蓝色背景的图片,说明测试成功。    stbi_write_png("test.png", 800, 600, 4, buf, 0);    printf("gles> save test.png ok.\n");
    delete buf;
    // EGL 的反转缓冲区    eglSwapBuffers(eglDisplay, eglSurface);
    // 取消绑定上下文    eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    // 释放 EGL 环境    eglDestroyContext(eglDisplay, eglContext);    eglDestroySurface(eglDisplay, eglSurface);    eglTerminate(eglDisplay);
    printf("egl> test ok.\n");}

标签:Angle,OpenGL,Windows,printf,GLAD,EGL,eglDisplay,ES
From: https://www.cnblogs.com/sdragonx/p/17234500.html

相关文章

  • 可以建立TCP连接但是无法ping通?windows设置
    前提关闭防火墙可以ping通,开启防火墙发现无法ping通,但是TCP连接可达。做法启用入站规则中的文件和打印机共享(ipv4或者ipv6根据需要自己看)尝试ping。如果还不行执......
  • Windows系统常见问题(不断更新)
    1、恢复快捷方式的小箭头及“快捷方式”字样。导入如下注册表内容:WindowsRegistryEditorVersion5.00[HKEY_CLASSES_ROOT\lnkfile]"IsShortcut"=""[HKEY_CURRENT_USE......
  • Windows平台解压lz4格式文件的方法
    在GitHub上看到一个7-Zip的扩展项目,为7-Zip增加了一些格式和压缩算法的支持,也包括lz4的压缩/解压缩。还是熟悉的7z味道,可以当作是一个增强版的7z。https://github.com/mcm......
  • 调整Windows进入系统等待时间
    title:调整Windows进入系统等待时间date:2023-03-1914:41:00categories:小技能调整Windows进入系统等待时间此电脑右击》属性高级系统设置启动和故障修复......
  • Windows.edb占用空间太大,如何禁用或删除Windows 搜索索引( Windows Search Index)
    .原文:《https://zhuanlan.zhihu.com/p/507590692》最近电脑上C盘的空间不够了,发现主要是C盘腾讯的微信和QQ占用比较大的空间,这个将来想办法设置数据到其它目录,另发现系统有......
  • 【Android开发】经典范例1-实现仿Windows7图片预览窗格效果
    本实例将显示类似于windows7提供的图片预览窗格效果,单击任意一张图片,可以在右侧显示该图片的预览效果。效果如图所示:具体实现方法:res/layout/main.x......
  • windows备份文件
    背景:在windows 下进行文件夹备份,备份成些压缩包什么之类的,话不多说,直接上bat 脚本:@echooff@rem全局变量注意事项:C:\Users\Anita\Desktop\temp\,前面最后的“\”一定不要......
  • 二、什么时OpenGL
    *OpenGL即OpenGraphicsLibrary,它是一个由Khronos组织指定并维护的规范(Specification)*OpenGL核心是一个c库,同时也支持多种语言的派生 1、固定管线模式(3.2) 2......
  • .net7 AesCng 加密解密封装,仅支持Windows平台
    ///<summary>///AesCng加密///仅支持Windows平台///</summary>///<paramname="value">明文(待加密)</param>......
  • 在Windows中安装Jenkins
    Jenkins在自动化测试中的使用 下载安装Jenkins1、下载链接:https://www.jenkins.io/download/,要选择长期支持离线型的版本:  2、安装JDK:   安装Jenkins前,......