需要明确的是,我已经广泛测试了这段代码,发现问题出在 WGL 代码之前编写的代码的某个地方。正是在 WIN32 代码中。现在我认为这可能部分是由调用 gluOrth2D 引起的,但即便如此,据我所知,这不应该是主要原因。我可能会自己弄乱一些东西来解决这个问题,但考虑到这个问题(发生在一个更大的项目中)已经占用了我很多时间,我认为值得发布这个包裹,以防其他人遇到同样的问题。我希望得到解释以及修复/更正。
#include <GL/glew.h>
#include <glfw3.h>
#define GLFW_EXPOSE_NATIVE_WGL
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw3native.h>
constexpr int width = 1000, height = 1000;
bool running = true;
LRESULT CALLBACK WIN32_processMessage(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam)
{
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
int main()
{
HINSTANCE hinst = GetModuleHandleA(NULL);
HICON icon = LoadIcon(hinst, IDI_APPLICATION);
WNDCLASSA* wc = (WNDCLASSA*)memset(malloc(sizeof(WNDCLASSA)), 0, sizeof(WNDCLASSA));
if (wc == NULL)
{
return -1;
}
wc->style = CS_DBLCLKS;
wc->lpfnWndProc = WIN32_processMessage;
wc->cbClsExtra = 0;
wc->cbWndExtra = 0;
wc->hInstance = hinst;
wc->hIcon = icon;
wc->hCursor = LoadCursor(NULL, IDC_ARROW);
wc->hbrBackground = NULL;
wc->lpszClassName = "toast_window_class";
if (!RegisterClassA(wc))
{
return 1;
}
UINT32 windowX = 100;
UINT32 windowY = 100;
UINT32 windowWidth = width;
UINT32 windowHeight = height;
UINT32 windowStyle = WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION;
UINT32 windowExStyle = WS_EX_APPWINDOW;
windowStyle |= WS_MAXIMIZEBOX;
windowStyle |= WS_MINIMIZEBOX;
windowStyle |= WS_THICKFRAME;
RECT borderRect = { 0,0,0,0 };
AdjustWindowRectEx(&borderRect, windowStyle, 0, windowExStyle);
windowX += borderRect.left;
windowY += borderRect.top;
windowWidth += borderRect.right - borderRect.left;
windowWidth += borderRect.bottom - borderRect.top;
HWND window = CreateWindowExA(windowExStyle, "toast_window_class", (LPCSTR)"test",
windowStyle, windowX, windowY, windowWidth, windowHeight, 0, 0, hinst, 0);
if (window == 0)
{
return 2;
}
bool shouldActivate = true;
const int showWindowCommandFlags = shouldActivate ? SW_SHOW : SW_SHOWNOACTIVATE;
ShowWindow(window, showWindowCommandFlags);
// WGL -----------------------------------------------------------
HDC device = GetDC(window);
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cAlphaBits = 0;
pfd.cAccumBits = 0;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
SetPixelFormat(device, ChoosePixelFormat(device, &pfd), &pfd);
HGLRC render = wglCreateContext(device);
wglMakeCurrent(device, render);
// GLEW ---------------------------------------------------------
const GLint res = glewInit();
if (GLEW_OK != res)
{
return 1;
}
// setup OpenGL --------------------------------------------------
gluOrtho2D(0, width, 0, height);
// main loop -----------------------------------------------------
while (running)
{
glClear(GL_COLOR_BUFFER_BIT);
// rendering
glBegin(GL_POINTS);
for (int x = 10; x < width - 10; ++x)
{
for (int y = 10; y < height - 10; ++y)
{
glVertex2f(x, y);
}
}
glEnd();
SwapBuffers(device);
}
return 0;
}