我正在尝试在 x64 平台上创建一个 OpenGL 窗口。 我的初始化代码适用于 x86/Win32,但在“wglMakeCurrent”处无法用于 x64。我想问题要么出在像素格式的设置上,要么出在获取 DC (getDC()) 中。我已经为我的 PIXELFORMATDESCRIPTOR 尝试了不同的设置(假设 OpenGL 的 x64 实现不支持我正在使用的那个),但没有成功。调试器指示 hdc 可能已损坏 -(类似于 0xfffffffff10102e0) - 但是 wglCreateContext 然后返回一个有效的 hglrc(即 0x0000000000010000)。但即使我将值从 hdc on-the-fly 更改为 0x0000000010102e0(使用调试器,在调用 wglCreateContext 之前)wglMakeCurrent 仍然失败。
我在装有 Visual Studio 12 RC 1 的 Windows 8 上。 知道我在这里做错了什么吗?或者 x64 OpenGL 实现是否存在一些限制?
#include <windows.h>
#pragma comment(lib, "OpenGL32.lib")
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.hInstance = nullptr;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClassTest";
RegisterClassEx(&wc);
DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)640;
WindowRect.top = (long)0;
WindowRect.bottom = (long)480;
AdjustWindowRect(&WindowRect, dwStyle, FALSE);
HWND hWnd = CreateWindowEx(0,
"WindowClassTest",
"WindowTitle",
dwStyle,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
nullptr,
nullptr,
wc.hInstance,
(LPVOID) nullptr);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
ShowWindow(hWnd, SW_SHOW);
SetFocus(hWnd);
HDC hdc = GetDC(hWnd);
int pf;
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pf = ChoosePixelFormat(hdc, &pfd);
if (pf == 0) {
MessageBox(NULL, "ChoosePixelFormat() failed: "
"Cannot find a suitable pixel format.", "Error", MB_OK);
}
if (SetPixelFormat(hdc, pf, &pfd) == FALSE) {
MessageBox(NULL, "SetPixelFormat() failed: "
"Cannot set format specified.", "Error", MB_OK);
}
DescribePixelFormat(hdc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
HGLRC hglrc = wglCreateContext(hdc);
if (!wglMakeCurrent(hdc, hglrc)){
MessageBox(NULL, "wglMakeCurrent() failed: "
"Cannot make context current.", "Error", MB_OK);
}
/* left out other unnecessary code here*/
return 0;
}
最佳答案
看起来这是一个非常具体的错误,在某些情况下会在 Windows 8.x 虚拟机中运行 64 位 OpenGL 应用程序时触发。根据可用的报告(见下文),它发生在
- 已使用 Visual Studio >= 2012 为 x64 编译应用程序
- 它在运行在 VM (Parallels/VMWare) 中的 Windows 8.x 中执行
我在用于编译我的 Windows 应用程序的 Parallels VM 中遇到了同样的问题。一个临时的解决方法是使用 32 位构建或使用以前版本的 Visual Studio(我使用 2010)编译 64 位。
重要的是,在非模拟硬件上运行的 Windows 8.x 似乎不会受到影响。
提及此问题的其他人的链接:
关于c++ - wglMakeCurrent 在 x64 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19236021/