gpt4 book ai didi

c++ - OpenGL 上下文创建从不选择像素格式

转载 作者:行者123 更新时间:2023-11-30 01:53:53 28 4
gpt4 key购买 nike

我正在尝试在 Win32 窗口内为我正在编写的库创建一个 OpenGL 上下文。我已将其归结为一个小示例,该示例将编译以帮助调试它。出于某种原因,它永远不会正确选择像素格式。这是代码:

#include <iostream>
#include <windows.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/wglew.h>

HWND handle;
HDC deviceContext;
HGLRC context;
int resolutionX = 1280;
int resolutionY = 720;
std::string title = "Test Window";

LRESULT CALLBACK eventCallback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int main()
{
// Register the window class
WNDCLASSA windowClass;
windowClass.style = 0;
windowClass.lpfnWndProc = &eventCallback;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = GetModuleHandle(nullptr);
windowClass.hIcon = nullptr;
windowClass.hCursor = nullptr;
windowClass.hbrBackground = nullptr;
windowClass.lpszMenuName = nullptr;
windowClass.lpszClassName = "Test_Window";
RegisterClassA(&windowClass);

// Compute position and size
HDC screenDC = GetDC(nullptr);
int left = (GetDeviceCaps(screenDC, HORZRES) - static_cast<int>(resolutionX)) / 2;
int top = (GetDeviceCaps(screenDC, VERTRES) - static_cast<int>(resolutionY)) / 2;
int width = resolutionX;
int height = resolutionY;
ReleaseDC(nullptr, screenDC);

// Set window style
DWORD win32Style = WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_SYSMENU;

// In windowed mode, adjust width and height so that window will have the requested client area
RECT rectangle = {0, 0, width, height};
AdjustWindowRect(&rectangle, win32Style, false);
width = rectangle.right - rectangle.left;
height = rectangle.bottom - rectangle.top;


// Create the window
handle = CreateWindowA("Test_Window", title.c_str(), win32Style, left, top, width, height, NULL, NULL, GetModuleHandle(nullptr), nullptr);
if(handle == NULL)
{
std::cout << "Failed to create window" << std::endl;
return 1;
}

// Setup a pixel format descriptor from the rendering settings
PIXELFORMATDESCRIPTOR descriptor;
ZeroMemory(&descriptor, sizeof(descriptor));
descriptor.nSize = sizeof(descriptor);
descriptor.nVersion = 1;
descriptor.iLayerType = PFD_MAIN_PLANE;
descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
descriptor.iPixelType = PFD_TYPE_RGBA;
descriptor.cColorBits = static_cast<BYTE>(32);
descriptor.cDepthBits = static_cast<BYTE>(24);
descriptor.cStencilBits = static_cast<BYTE>(8);
descriptor.cAlphaBits = 8;

// Get the pixel format that best matches our requirements
int bestFormat = ChoosePixelFormat(deviceContext, &descriptor);
if(bestFormat == 0)
{
std::cout << "Failed to find a suitable pixel format for device context -- cannot create OpenGL context" << std::endl;
return 1;
}

// Extract the depth and stencil bits from the chosen format
PIXELFORMATDESCRIPTOR actualFormat;
actualFormat.nSize = sizeof(actualFormat);
actualFormat.nVersion = 1;
DescribePixelFormat(deviceContext, bestFormat, sizeof(actualFormat), &actualFormat);

// Set the chosen pixel format
if(!SetPixelFormat(deviceContext, bestFormat, &actualFormat))
{
std::cout << "Failed to set pixel format for device context -- cannot create OpenGL context" << std::endl;
return 1;
}

PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));
if(wglCreateContextAttribsARB)
{
int attributes[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>(3),
WGL_CONTEXT_MINOR_VERSION_ARB, static_cast<int>(0),
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
0, 0
};

context = wglCreateContextAttribsARB(deviceContext, nullptr, attributes);
}

if(!context)
{
context = wglCreateContext(deviceContext);

if(!context)
{
std::cout << "Failed to create the OpenGL context" << std::endl;
return 1;
}
}

// Destroy the OpenGL context
wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(context);

// Destroy the device context
ReleaseDC(handle, deviceContext);

// Destroy the window handle
DestroyWindow(handle);

// Unregister the window
UnregisterClassA("Test_Window", GetModuleHandle(nullptr));

std::cout << "Window created" << std::endl;

return 0;
}

LRESULT CALLBACK eventCallback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Associate handle and Window instance when the creation message is received
if(message == WM_CREATE)
{
LONG_PTR window = (LONG_PTR)reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams;

// Set as the "user data" parameter of the window
SetWindowLongPtr(hwnd, GWLP_USERDATA, window);
}

return DefWindowProcA(hwnd, message, wParam, lParam);
}

最佳答案

您永远不会初始化 deviceContext,这意味着它将具有垃圾值。如果您在尝试 ChoosePixelFormat 后调用 GetLastError,我想您会发现它告诉您您有一个无效的句柄。

关于c++ - OpenGL 上下文创建从不选择像素格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22692632/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com