【问题标题】:How to create an OpenGL context on an NodeJS native addon on MacOS?如何在 MacOS 上的 NodeJS 本机插件上创建 OpenGL 上下文?
【发布时间】:2020-07-17 00:52:21
【问题描述】:

跟进this 问题。

我正在尝试创建一个使用 OpenGL 的 NodeJS 原生插件。

我无法使用 OpenGL 函数,因为 CGLGetCurrentContext() 总是返回 NULL

当尝试创建一个新的上下文来绘制时,CGLChoosePixelFormat 总是返回错误kCGLBadConnection invalid CoreGraphics connection

让我烦恼的是,当我将创建 OpenGL 上下文的代码隔离到一个独立的 CPP 项目中时,它就可以工作了!当我在 NodeJS 插件中运行它时,它只会报错!

我创建了这个 NodeJS 原生插件项目来举例说明我的错误:https://github.com/Psidium/node-opengl-context-error-example

这是在独立项目上执行时有效的代码,在 NodeJS 中运行时会出错:

//
//  main.cpp
//  test_cli
//
//  Created by Borges, Gabriel on 4/3/20.
//  Copyright © 2020 Psidium. All rights reserved.
//

#include <iostream>
#include <OpenGL/OpenGL.h>

int main(int argc, const char * argv[]) {
    std::cout << "Context before creating it: " << CGLGetCurrentContext() << "\n";
       CGLContextObj context;
    CGLPixelFormatAttribute attributes[2] = {
            kCGLPFAAccelerated,   // no software rendering
            (CGLPixelFormatAttribute) 0
    };
    CGLPixelFormatObj pix;
    CGLError errorCode;
    GLint num; // stores the number of possible pixel formats
    errorCode = CGLChoosePixelFormat( attributes, &pix, &num );
    if (errorCode > 0) {
      std::cout << ": Error returned by choosePixelFormat: " << errorCode << "\n";
        return 10;
    }

    errorCode = CGLCreateContext( pix, NULL, &context );
    if (errorCode > 0) {
      std::cout << ": Error returned by CGLCreateContext: " << errorCode << "\n";
      return 10 ;
    }

    CGLDestroyPixelFormat( pix );

    errorCode = CGLSetCurrentContext( context );
    if (errorCode > 0) {
      std::cout << "Error returned by CGLSetCurrentContext: " << errorCode << "\n";
      return 10;
    }
    std::cout << "Context after being created is: " << CGLGetCurrentContext() << "\n";
    return 0;
}

我已经试过了:

  • 使用fork() 在子进程中创建上下文(无效);
  • pixelformat 属性更改为可以创建我的上下文的内容(无效);

我有一种预感,它可能与 Node 原生插件是动态链接库这一事实有关,或者我的 OpenGL createContext 函数可能未在主线程上执行(但如果这是在这种情况下,fork() 会解决它,对吧?)。

【问题讨论】:

    标签: c++ node.js macos opengl node-native-addon


    【解决方案1】:

    访问图形硬件需要额外的权限 - Windows 和 macOS(其他人不知道)将硬件加速 OpenGL 上下文的创建限制为交互式用户会话(我可能对这里的术语有误) .来自网络上的一篇文章:

    如果用户未登录,CGLChosePixelFormat 将返回 kCGLBadConnection

    交互式会话理解更容易感受;例如当您以交互方式登录并启动应用程序时 - 它是交互式会话;当进程作为服务启动时 - 它是非交互式的。这实际上是如何由系统管理的需要更深入的阅读。据我所知,没有简单的方法“转义”非交互式进程标志。

    NodeJS 可以用作 Web 服务器的一部分,所以我可能认为它可能正是问题所在 - 它是由另一个非交互式用户作为服务启动的,或者有其他特殊条件使其非交互的。因此,关于如何使用/启动 NodeJS 本身的更多细节可能会阐明代码为什么不起作用。但我可能希望在服务器部分使用 OpenGL 可能不是一个好主意(如果这是一个目标);尽管软件 OpenGL 实现(没有kCGLPFAAccelerated 标志可能有效)可能是可行的。

    顺便说一下,NodeJS 至少有两个 OpenGL / WebGL 扩展 - 您是否尝试过他们的示例以查看它们在您的环境中的行为方式是否相同或不同? https://github.com/google/node-gles https://github.com/mikeseven/node-webgl

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多