我在 Ubuntu 中使用 opengl 制作了一个程序。现在,我想从中生成 .exe 文件。如果我在 Windows 中使用 g++ 编译器并编译它,是否有可能?
问问题
146 次
2 回答
1
取决于你的代码。通常设备上下文是在每个平台上以不同的方式创建的
视窗:
hRC_ = wglCreateContextAttribsARB(hDC_, 0, attributes); // Create and OpenGL 3.x context based on the given attributes
if(hRC_ == 0)
{
GLenum error = glGetError();
// ... handle error
}
wglMakeCurrent(nullptr, nullptr); // Remove the temporary context from being active
wglDeleteContext(tempContext); // Delete the temporary OpenGL 2.1 context
wglMakeCurrent(hDC_, hRC_); // Make our OpenGL 3.0 context current
X11/GLX:
const char *glxExts = glXQueryExtensionsString( display_, DefaultScreen( display_ ) );
if(isExtensionSupported( glxExts, "GLX_ARB_create_context" )) // If the OpenGL 3.x context creation extension is available
{
glXCreateContextAttribsARB = (GLXContext(*)(Display* dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list))glXGetProcAddressARB((GLubyte*)"glXCreateContextAttribsARB");
hRC_ = glXCreateContextAttribsARB(display_, fbConfigs_[0], 0, true, attributes); // Create and OpenGL 3.x context based on the given attributes
if(!glXMakeCurrent(display_, window_, hRC_))
{
throw BlueFramework::BlueCore::Exception("Making context current failed.");
}; // Make our OpenGL 3.0 context current
GLenum err = glewInit(); // Enable GLEW
if (GLEW_OK != err) // If GLEW fails
{
std::cerr << err << std::endl;
std::cerr << "Error: " << glewGetString(err) << std::endl;
throw BlueFramework::BlueCore::Exception("GLEW is not initialized!");
}
}
交给XXXCreateContextAttribs的属性也有区别:
视窗:
WGL_CONTEXT_MAJOR_VERSION_ARB, majorVersion_, // Set the MAJOR version of OpenGL
WGL_CONTEXT_MINOR_VERSION_ARB, miniorVersion_, // Set the MINOR version of OpenGL
WGL_CONTEXT_FLAGS_ARB,
WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB // Set our OpenGL context to be forward com
WGL_CONTEXT_PROFILE_MASK_ARB,
WGL_CONTEXT_CORE_PROFILE_BIT_ARB
...
X11/GLX:
GLX_CONTEXT_MAJOR_VERSION_ARB, majorVersion_, // Set the MAJOR version of OpenGL
GLX_CONTEXT_MINOR_VERSION_ARB, miniorVersion_, // Set the MINOR version of OpenGL
GLX_CONTEXT_FLAGS_ARB,
GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB // Set our OpenGL context to be forward compatible
//#ifdef _DEBUG
| GLX_CONTEXT_DEBUG_BIT_ARB
//#endif
,
GLX_CONTEXT_PROFILE_MASK_ARB,
GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
0
...
创建设备后,OpenGL 相关代码几乎相同。要在 Win 中创建渲染上下文,您需要创建一个窗口句柄。
看起来像这样:
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)RenderWindow::controlEventsProcessor;//WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = nullptr;//LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
if(fullscreen_)
{
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
}
else
{
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
}
wcex.lpszMenuName = NULL;
wcex.lpszClassName = wstrWindowClassName_.c_str(); //ss.str().c_str();
wcex.hIconSm = nullptr;//LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
if( !RegisterClassEx( &wcex ) )
{
BLUE_LOG_STREAM_EX(
"BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow",
buw::eLogSeverityLevel::Error)
<< "RegisterClassEx failed";
return E_FAIL;
}
// Create window
hInstance = hInstance;
RECT rc = { 0, 0, width, height };
if(!AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE ))
{
BLUE_LOG_STREAM_EX(
"BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow",
buw::eLogSeverityLevel::Error)
<< "AdjustWindowRect failed";
}
if(fullscreen_)
{
hWnd_ = CreateWindowEx(0,
wstrWindowClassName_.c_str(),//L"BlueRenderWindow",//ss.str().c_str(),
L"Render Window",
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
0, 0, // the starting x and y positions should be 0
rc.right - rc.left,
rc.bottom - rc.top,
nullptr,
nullptr,
hInstance,
nullptr);
}
else
{
hWnd_ = CreateWindowEx(0,
wstrWindowClassName_.c_str(),//L"BlueRenderWindow",//ss.str().c_str(),
L"Render Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rc.right - rc.left,
rc.bottom - rc.top,
nullptr,
nullptr,
hInstance,
nullptr);
}
X11/GLX 中的对应部分看起来像这样:
int attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_DOUBLEBUFFER, True,
GLX_RED_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
0L //= None
};
display_ = XOpenDisplay(nullptr);
if(display_ == nullptr)
{
BLUE_LOG_STREAM_EX(
"BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow",
buw::eLogSeverityLevel::Error)
<< "Cannot connect to X-Server";
return false;
}
int screenNr = XDefaultScreen (display_);
int numConfigs = 0;
// glXCreateContextAttribsARB = (GLXContext(*)(Display* dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list))glXGetProcAddressARB((GLubyte*)"glXCreateContextAttribsARB");
glXChooseFBConfig = (GLXFBConfig*(*)(Display *dpy, int screen, const int *attrib_list, int *nelements))glXGetProcAddressARB((GLubyte*)"glXChooseFBConfig");
glXGetVisualFromFBConfig = (XVisualInfo*(*)(Display *dpy, GLXFBConfig config))glXGetProcAddressARB((GLubyte*)"glXGetVisualFromFBConfig");
fbConfigs_ = glXChooseFBConfig(display_, XDefaultScreen(display_), attribs, &numConfigs);
if(fbConfigs_ == nullptr)
{
BLUE_LOG_STREAM_EX(
"BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow",
buw::eLogSeverityLevel::Error)
<< "Failed to get framebuffer config";
return false;
}
XVisualInfo* vi = glXGetVisualFromFBConfig(display_, fbConfigs_[0]);
if(vi == nullptr)
{
BLUE_LOG_STREAM_EX(
"BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow",
buw::eLogSeverityLevel::Error)
<< "No appropriate visual found (X-Server)";
return false;
}
bool doubleBuffered = true;
Colormap cmap = XCreateColormap (display_, RootWindow (display_, vi->screen),
vi->visual, AllocNone);
XSetWindowAttributes attributes;
attributes.background_pixmap = 0L;
attributes.colormap = cmap;
attributes.border_pixel = 0;
于 2013-11-05T11:35:03.000 回答
0
最简单的方法是使用cygwin
and freeglut
,以及,也许,glew
。
于 2013-11-05T11:43:41.327 回答