SetPixelFormat возвращает 0, но только в релизе
Пытаюсь создать контекст для OpenGL, в debug работает отлично но в release функция SetPixelFormat возвращет 0, GetLastError тоже.
Может кто знает в чем проблема?
HRESULT engine::render::InitContext(HWND _hWnd, engine::math::vec2 _glVersion) {
WNDCLASSA temporary_class = { 0 };
temporary_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
temporary_class.lpfnWndProc = DefWindowProcA;
temporary_class.hInstance = GetModuleHandle(0);
temporary_class.lpszClassName = "kjadgflkashdglh22";
RegisterClassA(&temporary_class);
HWND temporary_window = CreateWindowExA(
0,
temporary_class.lpszClassName,
"openGLConetextTemporary",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
temporary_class.hInstance,
0);
HDC temporary_dc = GetDC(temporary_window);
PIXELFORMATDESCRIPTOR pfd = { 0 };
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cColorBits = 24;
pfd.cAlphaBits = 8;
int pixel_format = ChoosePixelFormat(temporary_dc, &pfd);
SetPixelFormat(temporary_dc, pixel_format, &pfd);
HGLRC temporary_context = wglCreateContext(temporary_dc);
if (!wglMakeCurrent(temporary_dc, temporary_context)) engine::FatalError(L"Error create temporary context");
wglCreateContextAttribsARB = (wglCreateContextAttribsARB_type*)wglGetProcAddress(
"wglCreateContextAttribsARB");
wglChoosePixelFormatARB = (wglChoosePixelFormatARB_type*)wglGetProcAddress(
"wglChoosePixelFormatARB");
wglMakeCurrent(temporary_dc, 0);
wglDeleteContext(temporary_context);
ReleaseDC(temporary_window, temporary_dc);
DestroyWindow(temporary_window);
UnregisterClassA("kjadgflkashdglh22", GetModuleHandle(0));
int pixel_format_attribs[] = {
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0
};
HDC real_dc = GetDC(_hWnd);
int pixel_format2;
UINT num_formats;
wglChoosePixelFormatARB(real_dc, pixel_format_attribs, 0, 1, &pixel_format2, &num_formats);
if (!num_formats) {
engine::FatalError(L"Error choose pixel format");
}
PIXELFORMATDESCRIPTOR pfd2 = { 0 };
DescribePixelFormat(real_dc, pixel_format2, sizeof(pfd2), &pfd2);
int err = SetPixelFormat(real_dc, pixel_format2, &pfd2);
if (!err) {
engine::Message(L"Error set pixel format");
engine::FatalError(std::to_string(GetLastError()).c_str());
}
int gl33_attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, _glVersion.x,
WGL_CONTEXT_MINOR_VERSION_ARB, _glVersion.y,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0,
};
HGLRC gl33_context = wglCreateContextAttribsARB(real_dc, 0, gl33_attribs);
if (!gl33_context) {
engine::FatalError(L"Error create context");
}
if (!wglMakeCurrent(real_dc, gl33_context)) {
engine::FatalError(L"Error make context");
}
UpdateWindow(_hWnd);
return (gladLoadGL()) ? S_OK : -1;
}
Ответы (1 шт):
Автор решения: Тимур Крамар
→ Ссылка
РЕШЕНИЕ: В релизе нельзя создать контекст в отдельном потоке, отдельном от того где был зарегистрирован класс, создано окно, и запущен цикл сообщений. А использовать функции ГЛ можно только из потока который создал контест.
ВОПРОС: Может можно как-то рисовать не в одном потоке с PeekMessage?