this 的跟进问题。
我正在尝试创建一个使用 OpenGL 的 NodeJS native 插件。
我无法使用 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() 会解决它,对吧?)。
请您参考如下方法:
访问图形硬件需要额外的权限 - Windows 和 macOS(其他人不知道)将硬件加速 OpenGL 上下文的创建限制为 交互式用户 session (我可能对这里的条款有误)。来自网络上的一篇文章:
In case the user is not logged in, the CGLChoosePixelFormat will return kCGLBadConnection
互动环节感觉比理解更容易;例如当您以交互方式登录并启动应用程序时 - 它是交互式 session ;当进程作为服务启动时 - 它是非交互式的。这实际上是如何由系统管理的需要更深入的阅读。据我所知,没有简单的方法“转义”非交互式进程标志。
NodeJS 可以用作 Web 服务器的一部分,因此我可能认为它可能正是问题所在 - 它是由另一个非交互式用户作为服务启动的,或者具有其他使其非交互式的特殊条件。因此,关于如何使用/启动 NodeJS 本身的更多细节可能会阐明代码为什么不起作用。但我可能希望在服务器部分使用 OpenGL 可能不是一个好主意(如果这是一个目标);尽管软件 OpenGL 实现(没有
kCGLPFAAccelerated 标志可能有效)可能是可行的。
顺便说一句,NodeJS 至少有两个 OpenGL/WebGL 扩展 - 您是否尝试过他们的示例以查看它们在您的环境中的行为方式是否相同或不同?
https://github.com/google/node-gles
https://github.com/mikeseven/node-webgl




